sendrecv_vector.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Author: K. Noel Belcourt <kbelco -at- sandia.gov>
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #if defined(__cplusplus) && (201103L <= __cplusplus)
  6. #include <array>
  7. #include <cassert>
  8. #include <vector>
  9. #include "boost/mpi/environment.hpp"
  10. #include "boost/mpi/communicator.hpp"
  11. using std::array;
  12. using std::vector;
  13. namespace mpi = boost::mpi;
  14. struct blob : array<int, 9>, array<double, 3>, array<char, 8> {
  15. };
  16. namespace boost {
  17. namespace mpi {
  18. template <>
  19. struct is_mpi_datatype<blob> : mpl::true_ {
  20. };
  21. template <>
  22. MPI_Datatype get_mpi_datatype<blob>(const blob& b) {
  23. array<unsigned long, 3> block_lengths{
  24. { 9, 3, 8 }
  25. };
  26. array<MPI_Aint, 3> displacements{
  27. { 0, 40, 64 }
  28. };
  29. array<MPI_Datatype, 3> datatypes{
  30. { MPI_INT, MPI_DOUBLE, MPI_CHAR }
  31. };
  32. MPI_Datatype blob_type;
  33. MPI_Type_create_struct(
  34. block_lengths.size()
  35. , reinterpret_cast<int*>(block_lengths.data())
  36. , displacements.data()
  37. , datatypes.data()
  38. , &blob_type);
  39. MPI_Type_commit(&blob_type);
  40. return blob_type;
  41. }
  42. } // namespace mpi
  43. } // namespace boost
  44. #endif // defined(__cplusplus)
  45. int main(int argc, char* argv[]) {
  46. #if defined(__cplusplus) && (201103L <= __cplusplus)
  47. mpi::environment env(argc, argv);
  48. mpi::communicator world;
  49. vector<blob> data;
  50. if (world.rank() == 0) {
  51. int size = 10000000;
  52. data.resize(size);
  53. // initialize data at vector ends
  54. blob& b1= data[0];
  55. array<int, 9>& i = b1;
  56. i[0] = -1;
  57. blob& b2= data[size-1];
  58. array<int, 9>& d = b2;
  59. d[2] = -17;
  60. world.send(1, 0, data);
  61. } else {
  62. world.recv(0, 0, data);
  63. // check data at vector ends
  64. blob& b1 = data[0];
  65. array<int, 9>& i = b1;
  66. assert(i[0] == -1);
  67. // blob& b2 = data[data.size()-1];
  68. // array<int, 9>& d = b2;
  69. // assert(d[2] == -17);
  70. }
  71. #endif // defined(__cplusplus)
  72. return 0;
  73. }