scan_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright (C) 2005, 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // A test of the scan() collective.
  6. #include <boost/mpi/collectives/scan.hpp>
  7. #include <boost/mpi/communicator.hpp>
  8. #include <boost/mpi/environment.hpp>
  9. #include <algorithm>
  10. #include <boost/serialization/string.hpp>
  11. #include <boost/iterator/counting_iterator.hpp>
  12. #include <boost/lexical_cast.hpp>
  13. #include <numeric>
  14. #define BOOST_TEST_MODULE mpi_scan_test
  15. #include <boost/test/included/unit_test.hpp>
  16. using boost::mpi::communicator;
  17. // A simple point class that we can build, add, compare, and
  18. // serialize.
  19. struct point
  20. {
  21. point() : x(0), y(0), z(0) { }
  22. point(int x, int y, int z) : x(x), y(y), z(z) { }
  23. int x;
  24. int y;
  25. int z;
  26. private:
  27. template<typename Archiver>
  28. void serialize(Archiver& ar, unsigned int /*version*/)
  29. {
  30. ar & x & y & z;
  31. }
  32. friend class boost::serialization::access;
  33. };
  34. std::ostream& operator<<(std::ostream& out, const point& p)
  35. {
  36. return out << p.x << ' ' << p.y << ' ' << p.z;
  37. }
  38. bool operator==(const point& p1, const point& p2)
  39. {
  40. return p1.x == p2.x && p1.y == p2.y && p1.z == p2.z;
  41. }
  42. bool operator!=(const point& p1, const point& p2)
  43. {
  44. return !(p1 == p2);
  45. }
  46. point operator+(const point& p1, const point& p2)
  47. {
  48. return point(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z);
  49. }
  50. namespace boost { namespace mpi {
  51. template <>
  52. struct is_mpi_datatype<point> : public mpl::true_ { };
  53. } } // end namespace boost::mpi
  54. template<typename Generator, typename Op>
  55. void
  56. scan_test(const communicator& comm, Generator generator,
  57. const char* type_kind, Op op, const char* op_kind)
  58. {
  59. typedef typename Generator::result_type value_type;
  60. value_type value = generator(comm.rank());
  61. using boost::mpi::scan;
  62. if (comm.rank() == 0) {
  63. std::cout << "Prefix reducing to " << op_kind << " of " << type_kind
  64. << "...";
  65. std::cout.flush();
  66. }
  67. value_type result_value;
  68. scan(comm, value, result_value, op);
  69. value_type scan_result = scan(comm, value, op);
  70. BOOST_CHECK(scan_result == result_value);
  71. // Compute expected result
  72. std::vector<value_type> generated_values;
  73. for (int p = 0; p < comm.size(); ++p)
  74. generated_values.push_back(generator(p));
  75. std::vector<value_type> expected_results(comm.size());
  76. std::partial_sum(generated_values.begin(), generated_values.end(),
  77. expected_results.begin(), op);
  78. BOOST_CHECK(result_value == expected_results[comm.rank()]);
  79. if (comm.rank() == 0) std::cout << "Done." << std::endl;
  80. (comm.barrier)();
  81. }
  82. // Generates integers to test with scan()
  83. struct int_generator
  84. {
  85. typedef int result_type;
  86. int_generator(int base = 1) : base(base) { }
  87. int operator()(int p) const { return base + p; }
  88. private:
  89. int base;
  90. };
  91. // Generate points to test with scan()
  92. struct point_generator
  93. {
  94. typedef point result_type;
  95. point_generator(point origin) : origin(origin) { }
  96. point operator()(int p) const
  97. {
  98. return point(origin.x + 1, origin.y + 1, origin.z + 1);
  99. }
  100. private:
  101. point origin;
  102. };
  103. struct string_generator
  104. {
  105. typedef std::string result_type;
  106. std::string operator()(int p) const
  107. {
  108. std::string result = boost::lexical_cast<std::string>(p);
  109. result += " rosebud";
  110. if (p != 1) result += 's';
  111. return result;
  112. }
  113. };
  114. struct secret_int_bit_and
  115. {
  116. int operator()(int x, int y) const { return x & y; }
  117. };
  118. struct wrapped_int
  119. {
  120. wrapped_int() : value(0) { }
  121. explicit wrapped_int(int value) : value(value) { }
  122. template<typename Archive>
  123. void serialize(Archive& ar, unsigned int /* version */)
  124. {
  125. ar & value;
  126. }
  127. int value;
  128. };
  129. wrapped_int operator+(const wrapped_int& x, const wrapped_int& y)
  130. {
  131. return wrapped_int(x.value + y.value);
  132. }
  133. bool operator==(const wrapped_int& x, const wrapped_int& y)
  134. {
  135. return x.value == y.value;
  136. }
  137. // Generates wrapped_its to test with scan()
  138. struct wrapped_int_generator
  139. {
  140. typedef wrapped_int result_type;
  141. wrapped_int_generator(int base = 1) : base(base) { }
  142. wrapped_int operator()(int p) const { return wrapped_int(base + p); }
  143. private:
  144. int base;
  145. };
  146. namespace boost { namespace mpi {
  147. // Make std::plus<wrapped_int> commutative.
  148. template<>
  149. struct is_commutative<std::plus<wrapped_int>, wrapped_int>
  150. : mpl::true_ { };
  151. } } // end namespace boost::mpi
  152. BOOST_AUTO_TEST_CASE(scan_check)
  153. {
  154. using namespace boost::mpi;
  155. environment env;
  156. communicator comm;
  157. // Built-in MPI datatypes with built-in MPI operations
  158. scan_test(comm, int_generator(), "integers", std::plus<int>(), "sum");
  159. scan_test(comm, int_generator(), "integers", std::multiplies<int>(),
  160. "product");
  161. scan_test(comm, int_generator(), "integers", maximum<int>(),
  162. "maximum");
  163. scan_test(comm, int_generator(), "integers", minimum<int>(),
  164. "minimum");
  165. // User-defined MPI datatypes with operations that have the
  166. // same name as built-in operations.
  167. scan_test(comm, point_generator(point(0,0,0)), "points",
  168. std::plus<point>(), "sum");
  169. // Built-in MPI datatypes with user-defined operations
  170. scan_test(comm, int_generator(17), "integers", secret_int_bit_and(),
  171. "bitwise and");
  172. // Arbitrary types with user-defined, commutative operations.
  173. scan_test(comm, wrapped_int_generator(17), "wrapped integers",
  174. std::plus<wrapped_int>(), "sum");
  175. // Arbitrary types with (non-commutative) user-defined operations
  176. scan_test(comm, string_generator(), "strings",
  177. std::plus<std::string>(), "concatenation");
  178. }