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