]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/mpi/test/gather_test.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / mpi / test / gather_test.cpp
1 // Copyright (C) 2005, 2006 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 gather() and gatherv() collectives.
8 #include <boost/mpi/collectives/gather.hpp>
9 #include <boost/mpi/collectives/gatherv.hpp>
10 #include <boost/mpi/communicator.hpp>
11 #include <boost/mpi/environment.hpp>
12 #include "gps_position.hpp"
13 #include <boost/serialization/string.hpp>
14 #include <boost/serialization/list.hpp>
15 #include <boost/iterator/counting_iterator.hpp>
16 #include <boost/lexical_cast.hpp>
17
18 #define BOOST_TEST_MODULE mpi_gather
19 #include <boost/test/included/unit_test.hpp>
20
21 using boost::mpi::communicator;
22
23 template<typename Generator>
24 void
25 gather_test(const communicator& comm, Generator generator,
26 const char* kind, int root = -1)
27 {
28 typedef typename Generator::result_type value_type;
29 value_type value = generator(comm.rank());
30
31 if (root == -1) {
32 for (root = 0; root < comm.size(); ++root)
33 gather_test(comm, generator, kind, root);
34 } else {
35 using boost::mpi::gather;
36
37 std::vector<value_type> values;
38 if (comm.rank() == root) {
39 std::cout << "Gathering " << kind << " from root "
40 << root << "..." << std::endl;
41 }
42
43 gather(comm, value, values, root);
44
45 if (comm.rank() == root) {
46 std::vector<value_type> expected_values;
47 for (int p = 0; p < comm.size(); ++p)
48 expected_values.push_back(generator(p));
49 BOOST_CHECK(values == expected_values);
50 } else {
51 BOOST_CHECK(values.empty());
52 }
53 }
54
55 (comm.barrier)();
56 }
57
58
59 template<typename Generator>
60 void
61 gatherv_test(const communicator& comm, Generator generator,
62 const char* kind, int root = -1)
63 {
64 typedef typename Generator::result_type value_type;
65
66 if (root == -1) {
67 for (root = 0; root < comm.size(); ++root)
68 gatherv_test(comm, generator, kind, root);
69 } else {
70 using boost::mpi::gatherv;
71
72 int mysize = comm.rank() + 1;
73 int nprocs = comm.size();
74
75 // process p will send p+1 identical generator(p) elements
76 std::vector<value_type> myvalues(mysize, generator(comm.rank()));
77
78 if (comm.rank() == root) {
79 std::vector<value_type> values((nprocs*(nprocs+1))/2);
80 std::vector<int> sizes(comm.size());
81 for (int p = 0; p < comm.size(); ++p)
82 sizes[p] = p + 1;
83
84 std::cout << "Gatheringv " << kind << " from root "
85 << root << "..." << std::endl;
86
87 gatherv(comm, myvalues, &values[0], sizes, root);
88
89 std::vector<value_type> expected_values;
90 for (int p = 0; p < comm.size(); ++p)
91 for (int i = 0; i < p+1; ++i)
92 expected_values.push_back(generator(p));
93
94 BOOST_CHECK(values == expected_values);
95 } else {
96 gatherv(comm, myvalues, root);
97 }
98 }
99
100 (comm.barrier)();
101 }
102
103 //
104 // Generators to test with gather/gatherv
105 //
106 struct int_generator
107 {
108 typedef int result_type;
109
110 int operator()(int p) const { return 17 + p; }
111 };
112
113 struct gps_generator
114 {
115 typedef gps_position result_type;
116
117 gps_position operator()(int p) const
118 {
119 return gps_position(39 + p, 16, 20.2799);
120 }
121 };
122
123 struct string_generator
124 {
125 typedef std::string result_type;
126
127 std::string operator()(int p) const
128 {
129 std::string result = boost::lexical_cast<std::string>(p);
130 result += " rosebud";
131 if (p != 1) result += 's';
132 return result;
133 }
134 };
135
136 struct string_list_generator
137 {
138 typedef std::list<std::string> result_type;
139
140 std::list<std::string> operator()(int p) const
141 {
142 std::list<std::string> result;
143 for (int i = 0; i <= p; ++i) {
144 std::string value = boost::lexical_cast<std::string>(i);
145 result.push_back(value);
146 }
147 return result;
148 }
149 };
150
151 BOOST_AUTO_TEST_CASE(gather_check)
152 {
153 boost::mpi::environment env;
154 communicator comm;
155
156 gather_test(comm, int_generator(), "integers");
157 gather_test(comm, gps_generator(), "GPS positions");
158 gather_test(comm, string_generator(), "string");
159 gather_test(comm, string_list_generator(), "list of strings");
160
161 gatherv_test(comm, int_generator(), "integers");
162 gatherv_test(comm, gps_generator(), "GPS positions");
163 gatherv_test(comm, string_generator(), "string");
164 gatherv_test(comm, string_list_generator(), "list of strings");
165 }