• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //          Copyright Alain Miniussi 2014.
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 
6 // A test of the sendrecv() operation.
7 #include <boost/mpi/communicator.hpp>
8 #include <boost/mpi/environment.hpp>
9 #include <vector>
10 #include <algorithm>
11 #include <boost/serialization/string.hpp>
12 #include <boost/iterator/counting_iterator.hpp>
13 #include <boost/lexical_cast.hpp>
14 #include <numeric>
15 
16 #define BOOST_TEST_MODULE mpi_sendrecv
17 #include <boost/test/included/unit_test.hpp>
18 
19 namespace mpi = boost::mpi;
20 
21 struct blob {
blobblob22   blob(int i) : value(i) {}
23   int value;
24   template<class Archive>
serializeblob25   void serialize(Archive& s, const unsigned int version) {
26     s & value;
27   }
28 };
29 
operator <<(std::ostream & out,blob const & b)30 std::ostream& operator<<(std::ostream& out, blob const& b) {
31   out << "blob(" << b.value << ")";
32   return out;
33 }
34 
operator ==(blob const & b1,blob const & b2)35 bool operator==(blob const& b1, blob const& b2) {
36   return b1.value == b2.value;
37 }
38 
39 template<typename T>
test_sendrecv(mpi::communicator & com)40 void test_sendrecv(mpi::communicator& com) {
41   int const wrank = com.rank();
42   int const wsize = com.size();
43   int const wnext((wrank + 1) % wsize);
44   int const wprev((wrank + wsize - 1) % wsize);
45   T recv(-1);
46   com.sendrecv(wnext, 1, T(wrank), wprev, 1, recv);
47   for(int r = 0; r < wsize; ++r) {
48     com.barrier();
49     if (r == wrank) {
50       std::cout << "rank " << wrank << " received " << recv << " from " << wprev << '\n';
51     }
52   }
53   BOOST_CHECK(recv == T(wprev));
54 }
55 
BOOST_AUTO_TEST_CASE(sendrecv)56 BOOST_AUTO_TEST_CASE(sendrecv)
57 {
58   mpi::environment env;
59   mpi::communicator world;
60   test_sendrecv<int>(world);
61   test_sendrecv<blob>(world);
62 }
63