1 // Copyright (C) 2017 Alain Miniussi & 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 #include <string>
8 #include <iostream>
9 #include <sstream>
10 #include <vector>
11 #include <set>
12
13 #include <boost/mpi.hpp>
14 #include <boost/mpi/nonblocking.hpp>
15 #include <boost/serialization/string.hpp>
16
17 #define BOOST_TEST_MODULE mpi_wait_any
18 #include <boost/test/included/unit_test.hpp>
19
20 namespace mpi = boost::mpi;
21
BOOST_AUTO_TEST_CASE(wait_any)22 BOOST_AUTO_TEST_CASE(wait_any)
23 {
24 mpi::environment env;
25 mpi::communicator world;
26
27 std::vector<std::string> ss(world.size());
28 typedef std::vector<mpi::request> requests;
29 requests rreqs;
30
31 std::set<int> pending_senders;
32 for (int i = 0; i < world.size(); ++i) {
33 rreqs.push_back(world.irecv(i, i, ss[i]));
34 pending_senders.insert(i);
35 }
36
37 std::ostringstream fmt;
38 std::string msg = "Hello, World! this is ";
39 fmt << msg << world.rank();
40
41 requests sreqs;
42 for (int i = 0; i < world.size(); ++i) {
43 sreqs.push_back(world.isend(i, world.rank(), fmt.str()));
44 }
45
46 for (int i = 0; i < world.size(); ++i) {
47 std::pair<mpi::status, requests::iterator> completed = mpi::wait_any(rreqs.begin(), rreqs.end());
48 std::ostringstream out;
49 out << "Proc " << world.rank() << " got message from " << completed.first.source() << '\n';
50 std::cout << out.str();
51 }
52
53 for (int i = 0; i < world.size(); ++i) {
54 std::ostringstream fmt;
55 fmt << msg << i;
56 std::vector<std::string>::iterator found = std::find(ss.begin(), ss.end(), fmt.str());
57 BOOST_CHECK(found != ss.end());
58 fmt.str("");
59 fmt << "Proc " << world.rank() << " Got msg from " << i << '\n';
60 std::cout << fmt.str();
61 }
62
63 mpi::wait_all(sreqs.begin(), sreqs.end());
64 }
65