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