• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <vector>
2 #include <iostream>
3 #include <iterator>
4 #include <typeinfo>
5 
6 #include <boost/mpi.hpp>
7 #include <boost/serialization/vector.hpp>
8 #include <boost/core/demangle.hpp>
9 
10 //#include "debugger.cpp"
11 
12 #define BOOST_TEST_MODULE mpi_nonblocking
13 #include <boost/test/included/unit_test.hpp>
14 
15 namespace mpi = boost::mpi;
16 
17 template<typename T>
test(mpi::communicator const & comm,std::vector<T> const & ref,bool iswap,bool alloc)18 bool test(mpi::communicator const& comm, std::vector<T> const& ref, bool iswap, bool alloc)
19 {
20 
21   int rank = comm.rank();
22   if (rank == 0) {
23     std::cout << "Testing with type " << boost::core::demangle(typeid(T).name()) << '\n';
24     if (iswap) {
25       std::cout << "Blockin send, non blocking receive.\n";
26     } else {
27       std::cout << "Non blockin send, blocking receive.\n";
28     }
29     if (alloc) {
30       std::cout << "Explicitly allocate space for the receiver.\n";
31     } else {
32       std::cout << "Do not explicitly allocate space for the receiver.\n";
33     }
34   }
35   if (rank == 0) {
36     std::vector<T> data;
37     if (alloc) {
38       data.resize(ref.size());
39     }
40     if (iswap) {
41       mpi::request req = comm.irecv(1, 0, data);
42       req.wait();
43     } else {
44       comm.recv(1, 0, data);
45     }
46     std::cout << "Process 0 received " << data.size() << " elements :" << std::endl;
47     std::copy(data.begin(), data.end(), std::ostream_iterator<T>(std::cout, " "));
48     std::cout << std::endl;
49     std::cout << "While expecting " << ref.size() << " elements :" << std::endl;
50     std::copy(ref.begin(),  ref.end(),  std::ostream_iterator<T>(std::cout, " "));
51     std::cout << std::endl;
52     return (data == ref);
53   } else {
54     if (rank == 1) {
55       std::vector<T> vec = ref;
56       if (iswap) {
57         comm.send(0, 0, vec);
58       } else {
59         mpi::request req = comm.isend(0, 0, vec);
60         req.wait();
61       }
62     }
63     return true;
64   }
65 }
66 
BOOST_AUTO_TEST_CASE(non_blocking)67 BOOST_AUTO_TEST_CASE(non_blocking)
68 {
69   mpi::environment env;
70   mpi::communicator world;
71 
72   BOOST_TEST_REQUIRE(world.size() > 1);
73 
74   std::vector<int> integers(13); // don't assume we're lucky
75   for(int i = 0; i < int(integers.size()); ++i) {
76     integers[i] = i;
77   }
78 
79   std::vector<std::string> strings(13); // don't assume we're lucky
80   for(int i = 0; i < int(strings.size()); ++i) {
81     std::ostringstream fmt;
82     fmt << "S" << i;
83     strings[i] = fmt.str();
84   }
85 
86   std::vector<int> empty;
87 
88   BOOST_CHECK(test(world, empty, false, true));
89   BOOST_CHECK(test(world, empty, false, false));
90 
91   BOOST_CHECK(test(world, integers, true,  true));
92   BOOST_CHECK(test(world, integers, true,  false));
93   BOOST_CHECK(test(world, strings, true,  true));
94   BOOST_CHECK(test(world, strings, true,  false));
95 
96   BOOST_CHECK(test(world, integers, false,  true));
97   BOOST_CHECK(test(world, integers, false,  false));
98   BOOST_CHECK(test(world, strings, false,  true));
99   BOOST_CHECK(test(world, strings, false,  false));
100 }
101