block_nonblock_test.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <vector>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <typeinfo>
  5. #include <boost/mpi.hpp>
  6. #include <boost/serialization/vector.hpp>
  7. #include <boost/core/demangle.hpp>
  8. //#include "debugger.cpp"
  9. #define BOOST_TEST_MODULE mpi_nonblocking
  10. #include <boost/test/included/unit_test.hpp>
  11. namespace mpi = boost::mpi;
  12. template<typename T>
  13. bool test(mpi::communicator const& comm, std::vector<T> const& ref, bool iswap, bool alloc)
  14. {
  15. int rank = comm.rank();
  16. if (rank == 0) {
  17. std::cout << "Testing with type " << boost::core::demangle(typeid(T).name()) << '\n';
  18. if (iswap) {
  19. std::cout << "Blockin send, non blocking receive.\n";
  20. } else {
  21. std::cout << "Non blockin send, blocking receive.\n";
  22. }
  23. if (alloc) {
  24. std::cout << "Explicitly allocate space for the receiver.\n";
  25. } else {
  26. std::cout << "Do not explicitly allocate space for the receiver.\n";
  27. }
  28. }
  29. if (rank == 0) {
  30. std::vector<T> data;
  31. if (alloc) {
  32. data.resize(ref.size());
  33. }
  34. if (iswap) {
  35. mpi::request req = comm.irecv(1, 0, data);
  36. req.wait();
  37. } else {
  38. comm.recv(1, 0, data);
  39. }
  40. std::cout << "Process 0 received " << data.size() << " elements :" << std::endl;
  41. std::copy(data.begin(), data.end(), std::ostream_iterator<T>(std::cout, " "));
  42. std::cout << std::endl;
  43. std::cout << "While expecting " << ref.size() << " elements :" << std::endl;
  44. std::copy(ref.begin(), ref.end(), std::ostream_iterator<T>(std::cout, " "));
  45. std::cout << std::endl;
  46. return (data == ref);
  47. } else {
  48. if (rank == 1) {
  49. std::vector<T> vec = ref;
  50. if (iswap) {
  51. comm.send(0, 0, vec);
  52. } else {
  53. mpi::request req = comm.isend(0, 0, vec);
  54. req.wait();
  55. }
  56. }
  57. return true;
  58. }
  59. }
  60. BOOST_AUTO_TEST_CASE(non_blocking)
  61. {
  62. mpi::environment env;
  63. mpi::communicator world;
  64. BOOST_TEST_REQUIRE(world.size() > 1);
  65. std::vector<int> integers(13); // don't assume we're lucky
  66. for(int i = 0; i < int(integers.size()); ++i) {
  67. integers[i] = i;
  68. }
  69. std::vector<std::string> strings(13); // don't assume we're lucky
  70. for(int i = 0; i < int(strings.size()); ++i) {
  71. std::ostringstream fmt;
  72. fmt << "S" << i;
  73. strings[i] = fmt.str();
  74. }
  75. BOOST_CHECK(test(world, integers, true, true));
  76. BOOST_CHECK(test(world, integers, true, false));
  77. BOOST_CHECK(test(world, strings, true, true));
  78. BOOST_CHECK(test(world, strings, true, false));
  79. BOOST_CHECK(test(world, integers, false, true));
  80. BOOST_CHECK(test(world, integers, false, false));
  81. BOOST_CHECK(test(world, strings, false, true));
  82. BOOST_CHECK(test(world, strings, false, false));
  83. }