broadcast_test.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (C) 2005, 2006 Douglas Gregor.
  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 broadcast() collective.
  6. #include <boost/mpi/collectives/broadcast.hpp>
  7. #include <boost/mpi/communicator.hpp>
  8. #include <boost/mpi/environment.hpp>
  9. #include <algorithm>
  10. #include "gps_position.hpp"
  11. #include <boost/serialization/string.hpp>
  12. #include <boost/serialization/list.hpp>
  13. #include <boost/mpi/skeleton_and_content.hpp>
  14. #include <boost/iterator/counting_iterator.hpp>
  15. //#include "debugger.hpp"
  16. #define BOOST_TEST_MODULE mpi_broadcast
  17. #include <boost/test/included/unit_test.hpp>
  18. using boost::mpi::communicator;
  19. using boost::mpi::packed_skeleton_iarchive;
  20. using boost::mpi::packed_skeleton_oarchive;
  21. template<typename T>
  22. void
  23. broadcast_test(const communicator& comm, const T& bc_value,
  24. const char* kind, int root = -1)
  25. {
  26. if (root == -1) {
  27. for (root = 0; root < comm.size(); ++root)
  28. broadcast_test(comm, bc_value, kind, root);
  29. } else {
  30. using boost::mpi::broadcast;
  31. T value;
  32. if (comm.rank() == root) {
  33. value = bc_value;
  34. std::cout << "Broadcasting " << kind << " from root " << root << "...";
  35. std::cout.flush();
  36. }
  37. broadcast(comm, value, root);
  38. BOOST_CHECK(value == bc_value);
  39. if (comm.rank() == root && value == bc_value)
  40. std::cout << "OK." << std::endl;
  41. }
  42. (comm.barrier)();
  43. }
  44. void
  45. test_skeleton_and_content(const communicator& comm, int root = 0)
  46. {
  47. using boost::mpi::content;
  48. using boost::mpi::get_content;
  49. using boost::make_counting_iterator;
  50. using boost::mpi::broadcast;
  51. int list_size = comm.size() + 7;
  52. if (comm.rank() == root) {
  53. // Fill in the seed data
  54. std::list<int> original_list;
  55. for (int i = 0; i < list_size; ++i)
  56. original_list.push_back(i);
  57. // Build up the skeleton
  58. packed_skeleton_oarchive oa(comm);
  59. oa << original_list;
  60. // Broadcast the skeleton
  61. std::cout << "Broadcasting integer list skeleton from root " << root
  62. << "..." << std::flush;
  63. broadcast(comm, oa, root);
  64. std::cout << "OK." << std::endl;
  65. // Broadcast the content
  66. std::cout << "Broadcasting integer list content from root " << root
  67. << "..." << std::flush;
  68. {
  69. content c = get_content(original_list);
  70. broadcast(comm, c, root);
  71. }
  72. std::cout << "OK." << std::endl;
  73. // Reverse the list, broadcast the content again
  74. std::reverse(original_list.begin(), original_list.end());
  75. std::cout << "Broadcasting reversed integer list content from root "
  76. << root << "..." << std::flush;
  77. {
  78. content c = get_content(original_list);
  79. broadcast(comm, c, root);
  80. }
  81. std::cout << "OK." << std::endl;
  82. } else {
  83. // Allocate some useless data, to try to get the addresses of the
  84. // list<int>'s used later to be different across processes.
  85. std::list<int> junk_list(comm.rank() * 3 + 1, 17);
  86. // Receive the skeleton
  87. packed_skeleton_iarchive ia(comm);
  88. broadcast(comm, ia, root);
  89. // Build up a list to match the skeleton, and make sure it has the
  90. // right structure (we have no idea what the data will be).
  91. std::list<int> transferred_list;
  92. ia >> transferred_list;
  93. BOOST_CHECK((int)transferred_list.size() == list_size);
  94. // Receive the content and check it
  95. broadcast(comm, get_content(transferred_list), root);
  96. bool list_content_ok = std::equal(make_counting_iterator(0),
  97. make_counting_iterator(list_size),
  98. transferred_list.begin());
  99. BOOST_CHECK(list_content_ok);
  100. // Receive the reversed content and check it
  101. broadcast(comm, get_content(transferred_list), root);
  102. bool rlist_content_ok = std::equal(make_counting_iterator(0),
  103. make_counting_iterator(list_size),
  104. transferred_list.rbegin());
  105. BOOST_CHECK(rlist_content_ok);
  106. if (!(list_content_ok && rlist_content_ok)) {
  107. if (comm.rank() == 1) {
  108. std::cout
  109. << "\n##### You might want to check for BOOST_MPI_BCAST_BOTTOM_WORKS_FINE "
  110. << "in boost/mpi/config.hpp.\n\n";
  111. }
  112. }
  113. }
  114. (comm.barrier)();
  115. }
  116. BOOST_AUTO_TEST_CASE(broadcast_check)
  117. {
  118. boost::mpi::environment env;
  119. communicator comm;
  120. BOOST_TEST_REQUIRE(comm.size() > 1);
  121. // Check transfer of individual objects
  122. broadcast_test(comm, 17, "integers");
  123. broadcast_test(comm, gps_position(39,16,20.2799), "GPS positions");
  124. broadcast_test(comm, gps_position(26,25,30.0), "GPS positions");
  125. broadcast_test(comm, std::string("Rosie"), "string");
  126. std::list<std::string> strings;
  127. strings.push_back("Hello");
  128. strings.push_back("MPI");
  129. strings.push_back("World");
  130. broadcast_test(comm, strings, "list of strings");
  131. test_skeleton_and_content(comm, 0);
  132. test_skeleton_and_content(comm, 1);
  133. }