all_gather_test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor@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 all_gather() collective.
  6. #include <algorithm>
  7. #include <boost/mpi/collectives/all_gather.hpp>
  8. #include <boost/mpi/collectives/all_gatherv.hpp>
  9. #include <boost/mpi/environment.hpp>
  10. #include <boost/mpi/communicator.hpp>
  11. #include <boost/serialization/string.hpp>
  12. #include <boost/serialization/list.hpp>
  13. #include <boost/iterator/counting_iterator.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #define BOOST_TEST_MODULE mpi_all_gather
  16. #include <boost/test/included/unit_test.hpp>
  17. #include "gps_position.hpp"
  18. namespace mpi = boost::mpi;
  19. template<typename Generator>
  20. void
  21. all_gather_test(const mpi::communicator& comm, Generator generator,
  22. std::string kind)
  23. {
  24. typedef typename Generator::result_type value_type;
  25. value_type value = generator(comm.rank());
  26. std::vector<value_type> values;
  27. if (comm.rank() == 0) {
  28. std::cout << "Gathering " << kind << "...";
  29. std::cout.flush();
  30. }
  31. mpi::all_gather(comm, value, values);
  32. std::vector<value_type> expected_values;
  33. for (int p = 0; p < comm.size(); ++p)
  34. expected_values.push_back(generator(p));
  35. BOOST_CHECK(values == expected_values);
  36. if (comm.rank() == 0 && values == expected_values)
  37. std::cout << "OK." << std::endl;
  38. (comm.barrier)();
  39. }
  40. template<typename Generator>
  41. void
  42. all_gatherv_test(const mpi::communicator& comm, Generator generator,
  43. std::string kind)
  44. {
  45. typedef typename Generator::result_type value_type;
  46. using boost::mpi::all_gatherv;
  47. std::vector<value_type> myvalues, expected, values;
  48. std::vector<int> sizes;
  49. for(int r = 0; r < comm.size(); ++r) {
  50. value_type value = generator(r);
  51. sizes.push_back(r+1);
  52. for (int k=0; k < r+1; ++k) {
  53. expected.push_back(value);
  54. if(comm.rank() == r) {
  55. myvalues.push_back(value);
  56. }
  57. }
  58. }
  59. if (comm.rank() == 0) {
  60. std::cout << "Gathering " << kind << "...";
  61. std::cout.flush();
  62. }
  63. mpi::all_gatherv(comm, myvalues, values, sizes);
  64. BOOST_CHECK(values == expected);
  65. if (comm.rank() == 0 && values == expected)
  66. std::cout << "OK." << std::endl;
  67. (comm.barrier)();
  68. }
  69. // Generates integers to test with gather()
  70. struct int_generator
  71. {
  72. typedef int result_type;
  73. int operator()(int p) const { return 17 + p; }
  74. };
  75. // Generates GPS positions to test with gather()
  76. struct gps_generator
  77. {
  78. typedef gps_position result_type;
  79. gps_position operator()(int p) const
  80. {
  81. return gps_position(39 + p, 16, 20.2799);
  82. }
  83. };
  84. struct string_generator
  85. {
  86. typedef std::string result_type;
  87. std::string operator()(int p) const
  88. {
  89. std::string result = boost::lexical_cast<std::string>(p);
  90. result += " rosebud";
  91. if (p != 1) result += 's';
  92. return result;
  93. }
  94. };
  95. struct string_list_generator
  96. {
  97. typedef std::list<std::string> result_type;
  98. std::list<std::string> operator()(int p) const
  99. {
  100. std::list<std::string> result;
  101. for (int i = 0; i <= p; ++i) {
  102. std::string value = boost::lexical_cast<std::string>(i);
  103. result.push_back(value);
  104. }
  105. return result;
  106. }
  107. };
  108. BOOST_AUTO_TEST_CASE(all_gather)
  109. {
  110. boost::mpi::environment env;
  111. mpi::communicator comm;
  112. all_gather_test(comm, int_generator(), "integers");
  113. all_gather_test(comm, gps_generator(), "GPS positions");
  114. all_gather_test(comm, string_generator(), "string");
  115. all_gather_test(comm, string_list_generator(), "list of strings");
  116. all_gatherv_test(comm, int_generator(), "integers");
  117. all_gatherv_test(comm, gps_generator(), "GPS positions");
  118. all_gatherv_test(comm, string_generator(), "string");
  119. all_gatherv_test(comm, string_list_generator(), "list of strings");
  120. }