1 // Copyright (C) 2005, 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
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 scan() collective.
8 #include <boost/mpi/collectives/scan.hpp>
9 #include <boost/mpi/communicator.hpp>
10 #include <boost/mpi/environment.hpp>
12 #include <boost/serialization/string.hpp>
13 #include <boost/iterator/counting_iterator.hpp>
14 #include <boost/lexical_cast.hpp>
17 #define BOOST_TEST_MODULE mpi_scan_test
18 #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);
64 namespace boost { namespace mpi {
67 struct is_mpi_datatype<point> : public mpl::true_ { };
69 } } // end namespace boost::mpi
71 template<typename Generator, typename Op>
73 scan_test(const communicator& comm, Generator generator,
74 const char* type_kind, Op op, const char* op_kind)
76 typedef typename Generator::result_type value_type;
77 value_type value = generator(comm.rank());
78 using boost::mpi::scan;
80 if (comm.rank() == 0) {
81 std::cout << "Prefix reducing to " << op_kind << " of " << type_kind
86 value_type result_value;
87 scan(comm, value, result_value, op);
88 value_type scan_result = scan(comm, value, op);
89 BOOST_CHECK(scan_result == result_value);
91 // Compute expected result
92 std::vector<value_type> generated_values;
93 for (int p = 0; p < comm.size(); ++p)
94 generated_values.push_back(generator(p));
95 std::vector<value_type> expected_results(comm.size());
96 std::partial_sum(generated_values.begin(), generated_values.end(),
97 expected_results.begin(), op);
98 BOOST_CHECK(result_value == expected_results[comm.rank()]);
99 if (comm.rank() == 0) std::cout << "Done." << std::endl;
104 // Generates integers to test with scan()
107 typedef int result_type;
109 int_generator(int base = 1) : base(base) { }
111 int operator()(int p) const { return base + p; }
117 // Generate points to test with scan()
118 struct point_generator
120 typedef point result_type;
122 point_generator(point origin) : origin(origin) { }
124 point operator()(int p) const
126 return point(origin.x + 1, origin.y + 1, origin.z + 1);
133 struct string_generator
135 typedef std::string result_type;
137 std::string operator()(int p) const
139 std::string result = boost::lexical_cast<std::string>(p);
140 result += " rosebud";
141 if (p != 1) result += 's';
146 struct secret_int_bit_and
148 int operator()(int x, int y) const { return x & y; }
153 wrapped_int() : value(0) { }
154 explicit wrapped_int(int value) : value(value) { }
156 template<typename Archive>
157 void serialize(Archive& ar, unsigned int /* version */)
165 wrapped_int operator+(const wrapped_int& x, const wrapped_int& y)
167 return wrapped_int(x.value + y.value);
170 bool operator==(const wrapped_int& x, const wrapped_int& y)
172 return x.value == y.value;
175 // Generates wrapped_its to test with scan()
176 struct wrapped_int_generator
178 typedef wrapped_int result_type;
180 wrapped_int_generator(int base = 1) : base(base) { }
182 wrapped_int operator()(int p) const { return wrapped_int(base + p); }
188 namespace boost { namespace mpi {
190 // Make std::plus<wrapped_int> commutative.
192 struct is_commutative<std::plus<wrapped_int>, wrapped_int>
195 } } // end namespace boost::mpi
197 BOOST_AUTO_TEST_CASE(scan_check)
199 using namespace boost::mpi;
203 // Built-in MPI datatypes with built-in MPI operations
204 scan_test(comm, int_generator(), "integers", std::plus<int>(), "sum");
205 scan_test(comm, int_generator(), "integers", std::multiplies<int>(),
207 scan_test(comm, int_generator(), "integers", maximum<int>(),
209 scan_test(comm, int_generator(), "integers", minimum<int>(),
212 // User-defined MPI datatypes with operations that have the
213 // same name as built-in operations.
214 scan_test(comm, point_generator(point(0,0,0)), "points",
215 std::plus<point>(), "sum");
217 // Built-in MPI datatypes with user-defined operations
218 scan_test(comm, int_generator(17), "integers", secret_int_bit_and(),
221 // Arbitrary types with user-defined, commutative operations.
222 scan_test(comm, wrapped_int_generator(17), "wrapped integers",
223 std::plus<wrapped_int>(), "sum");
225 // Arbitrary types with (non-commutative) user-defined operations
226 scan_test(comm, string_generator(), "strings",
227 std::plus<std::string>(), "concatenation");