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