sendrecv_test.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright Alain Miniussi 20014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // A test of the sendrecv() operation.
  6. #include <boost/mpi/communicator.hpp>
  7. #include <boost/mpi/environment.hpp>
  8. #include <vector>
  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_sendrecv
  15. #include <boost/test/included/unit_test.hpp>
  16. namespace mpi = boost::mpi;
  17. struct blob {
  18. blob(int i) : value(i) {}
  19. int value;
  20. template<class Archive>
  21. void serialize(Archive& s, const unsigned int version) {
  22. s & value;
  23. }
  24. };
  25. std::ostream& operator<<(std::ostream& out, blob const& b) {
  26. out << "blob(" << b.value << ")";
  27. return out;
  28. }
  29. bool operator==(blob const& b1, blob const& b2) {
  30. return b1.value == b2.value;
  31. }
  32. template<typename T>
  33. void test_sendrecv(mpi::communicator& com) {
  34. int const wrank = com.rank();
  35. int const wsize = com.size();
  36. int const wnext((wrank + 1) % wsize);
  37. int const wprev((wrank + wsize - 1) % wsize);
  38. T recv(-1);
  39. com.sendrecv(wnext, 1, T(wrank), wprev, 1, recv);
  40. for(int r = 0; r < wsize; ++r) {
  41. com.barrier();
  42. if (r == wrank) {
  43. std::cout << "rank " << wrank << " received " << recv << " from " << wprev << '\n';
  44. }
  45. }
  46. BOOST_CHECK(recv == T(wprev));
  47. }
  48. BOOST_AUTO_TEST_CASE(sendrecv)
  49. {
  50. mpi::environment env;
  51. mpi::communicator world;
  52. test_sendrecv<int>(world);
  53. test_sendrecv<blob>(world);
  54. }