all_to_all_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 all_to_all() collective.
  6. #include <boost/mpi/collectives/all_to_all.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/iterator/counting_iterator.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #define BOOST_TEST_MODULE mpi_all_to_all
  16. #include <boost/test/included/unit_test.hpp>
  17. using boost::mpi::communicator;
  18. using boost::mpi::packed_skeleton_iarchive;
  19. using boost::mpi::packed_skeleton_oarchive;
  20. template<typename Generator>
  21. void
  22. all_to_all_test(const communicator& comm, Generator generator,
  23. const char* kind)
  24. {
  25. typedef typename Generator::result_type value_type;
  26. using boost::mpi::all_to_all;
  27. std::vector<value_type> in_values;
  28. for (int p = 0; p < comm.size(); ++p)
  29. in_values.push_back(generator((p + 1) * (comm.rank() + 1)));
  30. if (comm.rank() == 0) {
  31. std::cout << "Performing all-to-all operation on " << kind << "...";
  32. std::cout.flush();
  33. }
  34. std::vector<value_type> out_values;
  35. all_to_all(comm, in_values, out_values);
  36. for (int p = 0; p < comm.size(); ++p) {
  37. BOOST_CHECK(out_values[p] == generator((p + 1) * (comm.rank() + 1)));
  38. }
  39. if (comm.rank() == 0) {
  40. std::cout << " done." << std::endl;
  41. }
  42. (comm.barrier)();
  43. }
  44. // Generates integers to test with all_to_all()
  45. struct int_generator
  46. {
  47. typedef int result_type;
  48. int operator()(int p) const { return 17 + p; }
  49. };
  50. // Generates GPS positions to test with all_to_all()
  51. struct gps_generator
  52. {
  53. typedef gps_position result_type;
  54. gps_position operator()(int p) const
  55. {
  56. return gps_position(39 + p, 16, 20.2799);
  57. }
  58. };
  59. struct string_generator
  60. {
  61. typedef std::string result_type;
  62. std::string operator()(int p) const
  63. {
  64. std::string result = boost::lexical_cast<std::string>(p);
  65. result += " rosebud";
  66. if (p != 1) result += 's';
  67. return result;
  68. }
  69. };
  70. struct string_list_generator
  71. {
  72. typedef std::list<std::string> result_type;
  73. std::list<std::string> operator()(int p) const
  74. {
  75. std::list<std::string> result;
  76. for (int i = 0; i <= p; ++i) {
  77. std::string value = boost::lexical_cast<std::string>(i);
  78. result.push_back(value);
  79. }
  80. return result;
  81. }
  82. };
  83. BOOST_AUTO_TEST_CASE(all_to_all_check)
  84. {
  85. boost::mpi::environment env;
  86. communicator comm;
  87. all_to_all_test(comm, int_generator(), "integers");
  88. all_to_all_test(comm, gps_generator(), "GPS positions");
  89. all_to_all_test(comm, string_generator(), "string");
  90. all_to_all_test(comm, string_list_generator(), "list of strings");
  91. }