]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/mpi/test/gather_test.cpp
faec09930a3e4ff6c8828d259f71a12d6f25ca23
[ceph.git] / ceph / src / boost / libs / mpi / test / gather_test.cpp
1 // Copyright (C) 2005, 2006 Douglas Gregor.
2
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // A test of the gather() and gatherv() collectives.
8 #include <boost/mpi/collectives/gather.hpp>
9 #include <boost/mpi/collectives/gatherv.hpp>
10 #include <boost/mpi/communicator.hpp>
11 #include <boost/mpi/environment.hpp>
12 #include <boost/test/minimal.hpp>
13 #include "gps_position.hpp"
14 #include <boost/serialization/string.hpp>
15 #include <boost/serialization/list.hpp>
16 #include <boost/iterator/counting_iterator.hpp>
17 #include <boost/lexical_cast.hpp>
18
19 using boost::mpi::communicator;
20
21 template<typename Generator>
22 void
23 gather_test(const communicator& comm, Generator generator,
24 const char* kind, int root = -1)
25 {
26 typedef typename Generator::result_type value_type;
27 value_type value = generator(comm.rank());
28
29 if (root == -1) {
30 for (root = 0; root < comm.size(); ++root)
31 gather_test(comm, generator, kind, root);
32 } else {
33 using boost::mpi::gather;
34
35 std::vector<value_type> values;
36 if (comm.rank() == root) {
37 std::cout << "Gathering " << kind << " from root "
38 << root << "..." << std::endl;
39 }
40
41 gather(comm, value, values, root);
42
43 if (comm.rank() == root) {
44 std::vector<value_type> expected_values;
45 for (int p = 0; p < comm.size(); ++p)
46 expected_values.push_back(generator(p));
47 BOOST_CHECK(values == expected_values);
48 } else {
49 BOOST_CHECK(values.empty());
50 }
51 }
52
53 (comm.barrier)();
54 }
55
56
57 template<typename Generator>
58 void
59 gatherv_test(const communicator& comm, Generator generator,
60 const char* kind, int root = -1)
61 {
62 typedef typename Generator::result_type value_type;
63
64 if (root == -1) {
65 for (root = 0; root < comm.size(); ++root)
66 gatherv_test(comm, generator, kind, root);
67 } else {
68 using boost::mpi::gatherv;
69
70 int mysize = comm.rank() + 1;
71 int nprocs = comm.size();
72
73 // process p will send p+1 identical generator(p) elements
74 std::vector<value_type> myvalues(mysize, generator(comm.rank()));
75
76 if (comm.rank() == root) {
77 std::vector<value_type> values((nprocs*(nprocs+1))/2);
78 std::vector<int> sizes(comm.size());
79 for (int p = 0; p < comm.size(); ++p)
80 sizes[p] = p + 1;
81
82 std::cout << "Gatheringv " << kind << " from root "
83 << root << "..." << std::endl;
84
85 gatherv(comm, myvalues, &values[0], sizes, root);
86
87 std::vector<value_type> expected_values;
88 for (int p = 0; p < comm.size(); ++p)
89 for (int i = 0; i < p+1; ++i)
90 expected_values.push_back(generator(p));
91
92 BOOST_CHECK(values == expected_values);
93 } else {
94 gatherv(comm, myvalues, root);
95 }
96 }
97
98 (comm.barrier)();
99 }
100
101 //
102 // Generators to test with gather/gatherv
103 //
104 struct int_generator
105 {
106 typedef int result_type;
107
108 int operator()(int p) const { return 17 + p; }
109 };
110
111 struct gps_generator
112 {
113 typedef gps_position result_type;
114
115 gps_position operator()(int p) const
116 {
117 return gps_position(39 + p, 16, 20.2799);
118 }
119 };
120
121 struct string_generator
122 {
123 typedef std::string result_type;
124
125 std::string operator()(int p) const
126 {
127 std::string result = boost::lexical_cast<std::string>(p);
128 result += " rosebud";
129 if (p != 1) result += 's';
130 return result;
131 }
132 };
133
134 struct string_list_generator
135 {
136 typedef std::list<std::string> result_type;
137
138 std::list<std::string> operator()(int p) const
139 {
140 std::list<std::string> result;
141 for (int i = 0; i <= p; ++i) {
142 std::string value = boost::lexical_cast<std::string>(i);
143 result.push_back(value);
144 }
145 return result;
146 }
147 };
148
149 int test_main(int argc, char* argv[])
150 {
151 boost::mpi::environment env(argc, argv);
152
153 communicator comm;
154
155 gather_test(comm, int_generator(), "integers");
156 gather_test(comm, gps_generator(), "GPS positions");
157 gather_test(comm, string_generator(), "string");
158 gather_test(comm, string_list_generator(), "list of strings");
159
160 gatherv_test(comm, int_generator(), "integers");
161 gatherv_test(comm, gps_generator(), "GPS positions");
162 gatherv_test(comm, string_generator(), "string");
163 gatherv_test(comm, string_list_generator(), "list of strings");
164
165 return 0;
166 }