Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / mpi / test / non_blocking_any_source.cpp
1 // Copyright (C) 2018 Steffen Hirschmann
2
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // Test any_source on serialized data
8 #include <vector>
9 #include <iostream>
10 #include <iterator>
11 #include <boost/mpi.hpp>
12 #include <boost/serialization/vector.hpp>
13
14 #define BOOST_TEST_MODULE mpi_non_blockin_any_source
15 #include <boost/test/included/unit_test.hpp>
16
17 namespace mpi = boost::mpi;
18
19 std::string ok(bool b) {
20   return b ? "ok" : "ko";
21 }
22
23 BOOST_AUTO_TEST_CASE(non_blocking_any)
24 {
25   mpi::environment env;
26   mpi::communicator world;
27   int rank = world.rank();
28 #if BOOST_MPI_VERSION < 3
29   if (rank == 0) {
30     std::cout << "\nExpected failure with MPI standard < 3 ("
31               << BOOST_MPI_VERSION << "." << BOOST_MPI_SUBVERSION
32               << " detected)\n\n";
33   }
34   return;
35 #endif
36   if (rank == 0) {
37     std::vector<boost::mpi::request> req;
38     std::vector<std::vector<int> > data(world.size() - 1);
39     for (int i = 1; i < world.size(); ++i) {
40       req.push_back(world.irecv(mpi::any_source, 0, data[i - 1]));
41     }
42     boost::mpi::wait_all(req.begin(), req.end());
43     std::vector<bool> check(world.size()-1, false);
44     for (int i = 0; i < world.size() - 1; ++i) {
45       std::cout << "Process 0 received:" << std::endl;
46       std::copy(data[i].begin(), data[i].end(), std::ostream_iterator<int>(std::cout, " "));
47       std::cout << std::endl;
48       int idx = data[i].size();
49       BOOST_CHECK(std::equal_range(data[i].begin(), data[i].end(), idx)
50                   == std::make_pair(data[i].begin(), data[i].end()));
51       check[idx-1] = true;
52     }
53     for(int i = 0; i < world.size() - 1; ++i) {
54       std::cout << "Received from " << i+1 << " is " << ok(check[i]) << '\n';
55     }
56     BOOST_CHECK(std::equal_range(check.begin(), check.end(), true)
57                 == std::make_pair(check.begin(), check.end())); 
58   } else {
59     std::vector<int> vec(rank, rank);
60     mpi::request req = world.isend(0, 0, vec);
61     req.wait();
62   }
63 }