1 // Copyright 2005 Douglas Gregor.
2 
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 // A test of the communicator that transmits skeletons and
8 // content for data types.
9 #include <boost/mpi/communicator.hpp>
10 #include <boost/mpi/environment.hpp>
11 #include <boost/serialization/list.hpp>
12 #include <boost/mpi/skeleton_and_content.hpp>
13 #include <boost/mpi/nonblocking.hpp>
14 #include <algorithm>
15 #include <boost/iterator/counting_iterator.hpp>
16 #include <boost/mpi/collectives/broadcast.hpp>
17 
18 #define BOOST_TEST_MODULE mpi_skeleton_content
19 #include <boost/test/included/unit_test.hpp>
20 
21 using boost::mpi::communicator;
22 
23 using boost::mpi::packed_skeleton_iarchive;
24 using boost::mpi::packed_skeleton_oarchive;
25 
26 void
test_skeleton_and_content(const communicator & comm,int root,bool manual_broadcast)27 test_skeleton_and_content(const communicator& comm, int root,
28                           bool manual_broadcast)
29 {
30   using boost::mpi::skeleton;
31   using boost::mpi::content;
32   using boost::mpi::get_content;
33   using boost::make_counting_iterator;
34   using boost::mpi::broadcast;
35 
36   int list_size = comm.size() + 7;
37   if (comm.rank() == root) {
38     // Fill in the seed data
39     std::list<int> original_list;
40     for (int i = 0; i < list_size; ++i)
41       original_list.push_back(i);
42 
43     std::cout << "Broadcasting integer list skeleton from root " << root
44               << "...";
45     if (manual_broadcast) {
46       // Broadcast the skeleton (manually)
47       for (int p = 0; p < comm.size(); ++p)
48         if (p != root) comm.send(p, 0, skeleton(original_list));
49     } else {
50       broadcast(comm, skeleton(original_list), root);
51     }
52     std::cout << "OK." << std::endl;
53 
54     // Broadcast the content (manually)
55     std::cout << "Broadcasting integer list content from root " << root
56               << "...";
57     {
58       content c = get_content(original_list);
59       for (int p = 0; p < comm.size(); ++p)
60         if (p != root) comm.send(p, 1, c);
61     }
62     std::cout << "OK." << std::endl;
63 
64     // Reverse the list, broadcast the content again
65     std::reverse(original_list.begin(), original_list.end());
66     std::cout << "Broadcasting reversed integer list content from root "
67               << root << "...";
68     {
69       content c = get_content(original_list);
70       for (int p = 0; p < comm.size(); ++p)
71         if (p != root) comm.send(p, 2, c);
72     }
73     std::cout << "OK." << std::endl;
74   } else {
75     // Allocate some useless data, to try to get the addresses of the
76     // list<int>'s used later to be different across processes.
77     std::list<int> junk_list(comm.rank() * 3 + 1, 17);
78 
79     // Receive the skeleton to build up the transferred list
80     std::list<int> transferred_list;
81     if (manual_broadcast) {
82       comm.recv(root, 0, skeleton(transferred_list));
83     } else {
84       broadcast(comm, skeleton(transferred_list), root);
85     }
86     BOOST_CHECK((int)transferred_list.size() == list_size);
87 
88     // Receive the content and check it
89     comm.recv(root, 1, get_content(transferred_list));
90     BOOST_CHECK(std::equal(make_counting_iterator(0),
91                            make_counting_iterator(list_size),
92                            transferred_list.begin()));
93 
94     // Receive the reversed content and check it
95     comm.recv(root, 2, get_content(transferred_list));
96     BOOST_CHECK(std::equal(make_counting_iterator(0),
97                            make_counting_iterator(list_size),
98                            transferred_list.rbegin()));
99   }
100 
101   (comm.barrier)();
102 }
103 
104 void
test_skeleton_and_content_nonblocking(const communicator & comm,int root)105 test_skeleton_and_content_nonblocking(const communicator& comm, int root)
106 {
107   using boost::mpi::skeleton;
108   using boost::mpi::content;
109   using boost::mpi::get_content;
110   using boost::make_counting_iterator;
111   using boost::mpi::broadcast;
112   using boost::mpi::request;
113   using boost::mpi::wait_all;
114 
115   int list_size = comm.size() + 7;
116   if (comm.rank() == root) {
117     // Fill in the seed data
118     std::list<int> original_list;
119     for (int i = 0; i < list_size; ++i)
120       original_list.push_back(i);
121 
122     std::cout << "Non-blocking broadcast of integer list skeleton from root " << root
123               << "...";
124 
125     // Broadcast the skeleton (manually)
126     {
127       std::vector<request> reqs;
128       for (int p = 0; p < comm.size(); ++p)
129         if (p != root)
130           reqs.push_back(comm.isend(p, 0, skeleton(original_list)));
131       wait_all(reqs.begin(), reqs.end());
132     }
133     std::cout << "OK." << std::endl;
134 
135     // Broadcast the content (manually)
136     std::cout << "Non-blocking broadcast of integer list content from root " << root
137               << "...";
138     {
139       content c = get_content(original_list);
140       std::vector<request> reqs;
141       for (int p = 0; p < comm.size(); ++p)
142         if (p != root) reqs.push_back(comm.isend(p, 1, c));
143       wait_all(reqs.begin(), reqs.end());
144     }
145     std::cout << "OK." << std::endl;
146 
147     // Reverse the list, broadcast the content again
148     std::reverse(original_list.begin(), original_list.end());
149     std::cout << "Non-blocking broadcast of reversed integer list content from root "
150               << root << "...";
151     {
152       std::vector<request> reqs;
153       content c = get_content(original_list);
154       for (int p = 0; p < comm.size(); ++p)
155         if (p != root) reqs.push_back(comm.isend(p, 2, c));
156       wait_all(reqs.begin(), reqs.end());
157     }
158     std::cout << "OK." << std::endl;
159   } else {
160     // Allocate some useless data, to try to get the addresses of the
161     // list<int>'s used later to be different across processes.
162     std::list<int> junk_list(comm.rank() * 3 + 1, 17);
163 
164     // Receive the skeleton to build up the transferred list
165     std::list<int> transferred_list;
166     request req = comm.irecv(root, 0, skeleton(transferred_list));
167     req.wait();
168     BOOST_CHECK((int)transferred_list.size() == list_size);
169 
170     // Receive the content and check it
171     req = comm.irecv(root, 1, get_content(transferred_list));
172     req.wait();
173     BOOST_CHECK(std::equal(make_counting_iterator(0),
174                            make_counting_iterator(list_size),
175                            transferred_list.begin()));
176 
177     // Receive the reversed content and check it
178     req = comm.irecv(root, 2, get_content(transferred_list));
179     req.wait();
180     BOOST_CHECK(std::equal(make_counting_iterator(0),
181                            make_counting_iterator(list_size),
182                            transferred_list.rbegin()));
183   }
184 
185   (comm.barrier)();
186 }
187 
BOOST_AUTO_TEST_CASE(sendrecv)188 BOOST_AUTO_TEST_CASE(sendrecv)
189 {
190   boost::mpi::environment env;
191   communicator comm;
192   BOOST_TEST_REQUIRE(comm.size() > 1);
193 
194   test_skeleton_and_content(comm, 0, true);
195   test_skeleton_and_content(comm, 0, false);
196   test_skeleton_and_content(comm, 1, true);
197   test_skeleton_and_content(comm, 1, false);
198   test_skeleton_and_content_nonblocking(comm, 0);
199   test_skeleton_and_content_nonblocking(comm, 1);
200 }
201