wait_any_test.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2017 Alain Miniussi & Steffen Hirschmann
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <string>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <vector>
  9. #include <set>
  10. #include <boost/mpi.hpp>
  11. #include <boost/mpi/nonblocking.hpp>
  12. #include <boost/serialization/string.hpp>
  13. #define BOOST_TEST_MODULE mpi_wait_any
  14. #include <boost/test/included/unit_test.hpp>
  15. namespace mpi = boost::mpi;
  16. BOOST_AUTO_TEST_CASE(wait_any)
  17. {
  18. mpi::environment env;
  19. mpi::communicator world;
  20. std::vector<std::string> ss(world.size());
  21. typedef std::vector<mpi::request> requests;
  22. requests rreqs;
  23. std::set<int> pending_senders;
  24. for (int i = 0; i < world.size(); ++i) {
  25. rreqs.push_back(world.irecv(i, i, ss[i]));
  26. pending_senders.insert(i);
  27. }
  28. std::ostringstream fmt;
  29. std::string msg = "Hello, World! this is ";
  30. fmt << msg << world.rank();
  31. requests sreqs;
  32. for (int i = 0; i < world.size(); ++i) {
  33. sreqs.push_back(world.isend(i, world.rank(), fmt.str()));
  34. }
  35. for (int i = 0; i < world.size(); ++i) {
  36. std::pair<mpi::status, requests::iterator> completed = mpi::wait_any(rreqs.begin(), rreqs.end());
  37. std::ostringstream out;
  38. out << "Proc " << world.rank() << " got message from " << completed.first.source() << '\n';
  39. std::cout << out.str();
  40. }
  41. for (int i = 0; i < world.size(); ++i) {
  42. std::ostringstream fmt;
  43. fmt << msg << i;
  44. std::vector<std::string>::iterator found = std::find(ss.begin(), ss.end(), fmt.str());
  45. BOOST_CHECK(found != ss.end());
  46. fmt.str("");
  47. fmt << "Proc " << world.rank() << " Got msg from " << i << '\n';
  48. std::cout << fmt.str();
  49. }
  50. mpi::wait_all(sreqs.begin(), sreqs.end());
  51. }