broadcast_stl_test.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <algorithm>
  7. #include <vector>
  8. #include <map>
  9. #include <boost/mpi/collectives/broadcast.hpp>
  10. #include <boost/mpi/communicator.hpp>
  11. #include <boost/mpi/environment.hpp>
  12. #include <boost/serialization/string.hpp>
  13. #include <boost/serialization/vector.hpp>
  14. #include <boost/serialization/map.hpp>
  15. #define BOOST_TEST_MODULE mpi_broadcast_stl
  16. #include <boost/test/included/unit_test.hpp>
  17. namespace mpi = boost::mpi;
  18. typedef std::vector<std::map<int, double> > sparse;
  19. template<typename T>
  20. void
  21. broadcast_test(const mpi::communicator& comm, const T& bc_value,
  22. std::string const& kind, int root) {
  23. using boost::mpi::broadcast;
  24. T value;
  25. if (comm.rank() == root) {
  26. value = bc_value;
  27. std::cout << "Broadcasting " << kind << " from root " << root << "...";
  28. std::cout.flush();
  29. }
  30. broadcast(comm, value, root);
  31. BOOST_CHECK(value == bc_value);
  32. if (comm.rank() == root) {
  33. if (value == bc_value) {
  34. std::cout << "OK." << std::endl;
  35. } else {
  36. std::cout << "FAIL." << std::endl;
  37. }
  38. }
  39. comm.barrier();
  40. }
  41. template<typename T>
  42. void
  43. broadcast_test(const mpi::communicator& comm, const T& bc_value,
  44. std::string const& kind)
  45. {
  46. for (int root = 0; root < comm.size(); ++root) {
  47. broadcast_test(comm, bc_value, kind, root);
  48. }
  49. }
  50. BOOST_AUTO_TEST_CASE(broadcast_stl)
  51. {
  52. boost::mpi::environment env;
  53. mpi::communicator comm;
  54. BOOST_TEST_REQUIRE(comm.size() > 1);
  55. sparse s;
  56. s.resize(2);
  57. s[0][12] = 0.12;
  58. s[1][13] = 1.13;
  59. broadcast_test(comm, s, "sparse");
  60. }