1 // Copyright (C) 2005, 2006 Douglas Gregor.
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)
7 // A test of the all_reduce() collective.
8 #include <boost/mpi/collectives/all_reduce.hpp>
9 #include <boost/mpi/communicator.hpp>
10 #include <boost/mpi/environment.hpp>
13 #include <boost/serialization/string.hpp>
14 #include <boost/iterator/counting_iterator.hpp>
15 #include <boost/lexical_cast.hpp>
18 #define BOOST_TEST_MODULE mpi_all_reduce
19 #include <boost/test/included/unit_test.hpp>
21 using boost::mpi::communicator;
23 // A simple point class that we can build, add, compare, and
27 point() : x(0), y(0), z(0) { }
28 point(int x, int y, int z) : x(x), y(y), z(z) { }
35 template<typename Archiver>
36 void serialize(Archiver& ar, unsigned int /*version*/)
41 friend class boost::serialization::access;
44 std::ostream& operator<<(std::ostream& out, const point& p)
46 return out << p.x << ' ' << p.y << ' ' << p.z;
49 bool operator==(const point& p1, const point& p2)
51 return p1.x == p2.x && p1.y == p2.y && p1.z == p2.z;
54 bool operator!=(const point& p1, const point& p2)
59 point operator+(const point& p1, const point& p2)
61 return point(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z);
65 bool operator<(const point& p1, const point& p2)
74 namespace boost { namespace mpi {
77 struct is_mpi_datatype<point> : public mpl::true_ { };
79 } } // end namespace boost::mpi
81 template<typename Generator, typename Op>
83 all_reduce_one_test(const communicator& comm, Generator generator,
84 const char* type_kind, Op op, const char* op_kind,
85 typename Generator::result_type init, bool in_place)
87 typedef typename Generator::result_type value_type;
88 value_type value = generator(comm.rank());
90 using boost::mpi::all_reduce;
91 using boost::mpi::inplace;
93 if (comm.rank() == 0) {
94 std::cout << "Reducing to " << op_kind << " of " << type_kind << "...";
98 value_type result_value;
100 all_reduce(comm, inplace(value), op);
101 result_value = value;
103 result_value = all_reduce(comm, value, op);
106 // Compute expected result
107 std::vector<value_type> generated_values;
108 for (int p = 0; p < comm.size(); ++p)
109 generated_values.push_back(generator(p));
110 value_type expected_result = std::accumulate(generated_values.begin(),
111 generated_values.end(),
113 BOOST_CHECK(result_value == expected_result);
114 if (result_value == expected_result && comm.rank() == 0)
115 std::cout << "OK." << std::endl;
120 template<typename Generator, typename Op>
122 all_reduce_array_test(const communicator& comm, Generator generator,
123 const char* type_kind, Op op, const char* op_kind,
124 typename Generator::result_type init, bool in_place)
126 typedef typename Generator::result_type value_type;
127 value_type value = generator(comm.rank());
128 std::vector<value_type> send(10, value);
130 using boost::mpi::all_reduce;
131 using boost::mpi::inplace;
133 if (comm.rank() == 0) {
134 char const* place = in_place ? "in place" : "out of place";
135 std::cout << "Reducing (" << place << ") array to " << op_kind << " of " << type_kind << "...";
138 std::vector<value_type> result;
140 all_reduce(comm, inplace(&(send[0])), send.size(), op);
143 std::vector<value_type> recv(10, value_type());
144 all_reduce(comm, &(send[0]), send.size(), &(recv[0]), op);
148 // Compute expected result
149 std::vector<value_type> generated_values;
150 for (int p = 0; p < comm.size(); ++p)
151 generated_values.push_back(generator(p));
152 value_type expected_result = std::accumulate(generated_values.begin(),
153 generated_values.end(),
156 bool got_expected_result = (std::equal_range(result.begin(), result.end(),
158 == std::make_pair(result.begin(), result.end()));
159 BOOST_CHECK(got_expected_result);
160 if (got_expected_result && comm.rank() == 0)
161 std::cout << "OK." << std::endl;
166 // Test the 4 families of all reduce: (value, array) X (in place, out of place)
167 template<typename Generator, typename Op>
169 all_reduce_test(const communicator& comm, Generator generator,
170 const char* type_kind, Op op, const char* op_kind,
171 typename Generator::result_type init)
173 const bool in_place = true;
174 const bool out_of_place = false;
175 all_reduce_one_test(comm, generator, type_kind, op, op_kind, init, in_place);
176 all_reduce_one_test(comm, generator, type_kind, op, op_kind, init, out_of_place);
177 all_reduce_array_test(comm, generator, type_kind, op, op_kind,
179 all_reduce_array_test(comm, generator, type_kind, op, op_kind,
183 // Generates integers to test with all_reduce()
186 typedef int result_type;
188 int_generator(int base = 1) : base(base) { }
190 int operator()(int p) const { return base + p; }
196 // Generate points to test with all_reduce()
197 struct point_generator
199 typedef point result_type;
201 point_generator(point origin) : origin(origin) { }
203 point operator()(int p) const
205 return point(origin.x + 1, origin.y + 1, origin.z + 1);
212 struct string_generator
214 typedef std::string result_type;
216 std::string operator()(int p) const
218 std::string result = boost::lexical_cast<std::string>(p);
219 result += " rosebud";
220 if (p != 1) result += 's';
225 struct secret_int_bit_and
227 int operator()(int x, int y) const { return x & y; }
232 wrapped_int() : value(0) { }
233 explicit wrapped_int(int value) : value(value) { }
235 template<typename Archive>
236 void serialize(Archive& ar, unsigned int /* version */)
244 wrapped_int operator+(const wrapped_int& x, const wrapped_int& y)
246 return wrapped_int(x.value + y.value);
249 bool operator==(const wrapped_int& x, const wrapped_int& y)
251 return x.value == y.value;
254 bool operator<(const wrapped_int& x, const wrapped_int& y)
256 return x.value < y.value;
259 // Generates wrapped_its to test with all_reduce()
260 struct wrapped_int_generator
262 typedef wrapped_int result_type;
264 wrapped_int_generator(int base = 1) : base(base) { }
266 wrapped_int operator()(int p) const { return wrapped_int(base + p); }
272 namespace boost { namespace mpi {
274 // Make std::plus<wrapped_int> commutative.
276 struct is_commutative<std::plus<wrapped_int>, wrapped_int>
279 } } // end namespace boost::mpi
281 BOOST_AUTO_TEST_CASE(test_all_reduce)
283 using namespace boost::mpi;
287 // Built-in MPI datatypes with built-in MPI operations
288 all_reduce_test(comm, int_generator(), "integers", std::plus<int>(), "sum", 0);
289 all_reduce_test(comm, int_generator(), "integers", std::multiplies<int>(), "product", 1);
290 all_reduce_test(comm, int_generator(), "integers", maximum<int>(), "maximum", 0);
291 all_reduce_test(comm, int_generator(), "integers", minimum<int>(), "minimum", 2);
293 // User-defined MPI datatypes with operations that have the
294 // same name as built-in operations.
295 all_reduce_test(comm, point_generator(point(0,0,0)), "points", std::plus<point>(),
298 // Built-in MPI datatypes with user-defined operations
299 all_reduce_test(comm, int_generator(17), "integers", secret_int_bit_and(),
302 // Arbitrary types with user-defined, commutative operations.
303 all_reduce_test(comm, wrapped_int_generator(17), "wrapped integers",
304 std::plus<wrapped_int>(), "sum", wrapped_int(0));
306 // Arbitrary types with (non-commutative) user-defined operations
307 all_reduce_test(comm, string_generator(), "strings",
308 std::plus<std::string>(), "concatenation", std::string());