]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/mpi/test/gather_test.cpp
1 // Copyright (C) 2005, 2006 Douglas Gregor.
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)
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>
18 #define BOOST_TEST_MODULE mpi_gather
19 #include <boost/test/included/unit_test.hpp>
21 using boost::mpi::communicator
;
23 template<typename Generator
>
25 gather_test(const communicator
& comm
, Generator generator
,
26 const char* kind
, int root
= -1)
28 typedef typename
Generator::result_type value_type
;
29 value_type value
= generator(comm
.rank());
32 for (root
= 0; root
< comm
.size(); ++root
)
33 gather_test(comm
, generator
, kind
, root
);
35 using boost::mpi::gather
;
37 std::vector
<value_type
> values
;
38 if (comm
.rank() == root
) {
39 std::cout
<< "Gathering " << kind
<< " from root "
40 << root
<< "..." << std::endl
;
43 gather(comm
, value
, values
, root
);
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
);
51 BOOST_CHECK(values
.empty());
59 template<typename Generator
>
61 gatherv_test(const communicator
& comm
, Generator generator
,
62 const char* kind
, int root
= -1)
64 typedef typename
Generator::result_type value_type
;
67 for (root
= 0; root
< comm
.size(); ++root
)
68 gatherv_test(comm
, generator
, kind
, root
);
70 using boost::mpi::gatherv
;
72 int mysize
= comm
.rank() + 1;
73 int nprocs
= comm
.size();
75 // process p will send p+1 identical generator(p) elements
76 std::vector
<value_type
> myvalues(mysize
, generator(comm
.rank()));
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
)
84 std::cout
<< "Gatheringv " << kind
<< " from root "
85 << root
<< "..." << std::endl
;
87 gatherv(comm
, myvalues
, &values
[0], sizes
, root
);
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
));
94 BOOST_CHECK(values
== expected_values
);
96 gatherv(comm
, myvalues
, root
);
104 // Generators to test with gather/gatherv
108 typedef int result_type
;
110 int operator()(int p
) const { return 17 + p
; }
115 typedef gps_position result_type
;
117 gps_position
operator()(int p
) const
119 return gps_position(39 + p
, 16, 20.2799);
123 struct string_generator
125 typedef std::string result_type
;
127 std::string
operator()(int p
) const
129 std::string result
= boost::lexical_cast
<std::string
>(p
);
130 result
+= " rosebud";
131 if (p
!= 1) result
+= 's';
136 struct string_list_generator
138 typedef std::list
<std::string
> result_type
;
140 std::list
<std::string
> operator()(int p
) const
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
);
151 BOOST_AUTO_TEST_CASE(gather_check
)
153 boost::mpi::environment env
;
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");
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");