summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/mpi/test/wait_any_test.cpp
blob: e1f4c556de1aeab5e711ee0f0e8e9d2893c40633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2017 Alain Miniussi & Steffen Hirschmann 

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <set>

#include <boost/mpi.hpp>
#include <boost/mpi/nonblocking.hpp>
#include <boost/serialization/string.hpp>

#define BOOST_TEST_MODULE mpi_wait_any
#include <boost/test/included/unit_test.hpp>

namespace mpi = boost::mpi;

BOOST_AUTO_TEST_CASE(wait_any)
{
  mpi::environment  env;
  mpi::communicator world;
  
  std::vector<std::string> ss(world.size());
  typedef std::vector<mpi::request> requests;
  requests rreqs;
  
  std::set<int> pending_senders;
  for (int i = 0; i < world.size(); ++i) {
    rreqs.push_back(world.irecv(i, i, ss[i]));
    pending_senders.insert(i);
  }
  
  std::ostringstream fmt;
  std::string msg = "Hello, World! this is ";
  fmt << msg << world.rank();

  requests sreqs;
  for (int i = 0; i < world.size(); ++i) {
    sreqs.push_back(world.isend(i, world.rank(), fmt.str()));
  }
  
  for (int i = 0; i < world.size(); ++i) {
    std::pair<mpi::status, requests::iterator> completed = mpi::wait_any(rreqs.begin(), rreqs.end());
    std::ostringstream out;
    out << "Proc " << world.rank() << " got message from " << completed.first.source() << '\n';
    std::cout << out.str();
  }
  
  for (int i = 0; i < world.size(); ++i) {
    std::ostringstream fmt;
    fmt << msg << i;
    std::vector<std::string>::iterator found = std::find(ss.begin(), ss.end(), fmt.str());
    BOOST_CHECK(found != ss.end());
    fmt.str("");
    fmt << "Proc " << world.rank() << " Got msg from " << i << '\n';
    std::cout << fmt.str();
  }

  mpi::wait_all(sreqs.begin(), sreqs.end());
}