non_blocking_any_source.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (C) 2018 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. // Test any_source on serialized data
  6. #include <vector>
  7. #include <iostream>
  8. #include <iterator>
  9. #include <boost/mpi.hpp>
  10. #include <boost/serialization/vector.hpp>
  11. #define BOOST_TEST_MODULE mpi_non_blockin_any_source
  12. #include <boost/test/included/unit_test.hpp>
  13. namespace mpi = boost::mpi;
  14. std::string ok(bool b) {
  15. return b ? "ok" : "ko";
  16. }
  17. BOOST_AUTO_TEST_CASE(non_blocking_any)
  18. {
  19. mpi::environment env;
  20. mpi::communicator world;
  21. int rank = world.rank();
  22. #if BOOST_MPI_VERSION < 3
  23. if (rank == 0) {
  24. std::cout << "\nExpected failure with MPI standard < 3 ("
  25. << BOOST_MPI_VERSION << "." << BOOST_MPI_SUBVERSION
  26. << " detected)\n\n";
  27. }
  28. return;
  29. #endif
  30. if (rank == 0) {
  31. std::vector<boost::mpi::request> req;
  32. std::vector<std::vector<int> > data(world.size() - 1);
  33. for (int i = 1; i < world.size(); ++i) {
  34. req.push_back(world.irecv(mpi::any_source, 0, data[i - 1]));
  35. }
  36. boost::mpi::wait_all(req.begin(), req.end());
  37. std::vector<bool> check(world.size()-1, false);
  38. for (int i = 0; i < world.size() - 1; ++i) {
  39. std::cout << "Process 0 received:" << std::endl;
  40. std::copy(data[i].begin(), data[i].end(), std::ostream_iterator<int>(std::cout, " "));
  41. std::cout << std::endl;
  42. int idx = data[i].size();
  43. BOOST_CHECK(std::equal_range(data[i].begin(), data[i].end(), idx)
  44. == std::make_pair(data[i].begin(), data[i].end()));
  45. check[idx-1] = true;
  46. }
  47. for(int i = 0; i < world.size() - 1; ++i) {
  48. std::cout << "Received from " << i+1 << " is " << ok(check[i]) << '\n';
  49. }
  50. BOOST_CHECK(std::equal_range(check.begin(), check.end(), true)
  51. == std::make_pair(check.begin(), check.end()));
  52. } else {
  53. std::vector<int> vec(rank, rank);
  54. mpi::request req = world.isend(0, 0, vec);
  55. req.wait();
  56. }
  57. }