]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/mpi/test/gather_test.cpp
faec09930a3e4ff6c8828d259f71a12d6f25ca23
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 <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>
19 using boost::mpi::communicator
;
21 template<typename Generator
>
23 gather_test(const communicator
& comm
, Generator generator
,
24 const char* kind
, int root
= -1)
26 typedef typename
Generator::result_type value_type
;
27 value_type value
= generator(comm
.rank());
30 for (root
= 0; root
< comm
.size(); ++root
)
31 gather_test(comm
, generator
, kind
, root
);
33 using boost::mpi::gather
;
35 std::vector
<value_type
> values
;
36 if (comm
.rank() == root
) {
37 std::cout
<< "Gathering " << kind
<< " from root "
38 << root
<< "..." << std::endl
;
41 gather(comm
, value
, values
, root
);
43 if (comm
.rank() == root
) {
44 std::vector
<value_type
> expected_values
;
45 for (int p
= 0; p
< comm
.size(); ++p
)
46 expected_values
.push_back(generator(p
));
47 BOOST_CHECK(values
== expected_values
);
49 BOOST_CHECK(values
.empty());
57 template<typename Generator
>
59 gatherv_test(const communicator
& comm
, Generator generator
,
60 const char* kind
, int root
= -1)
62 typedef typename
Generator::result_type value_type
;
65 for (root
= 0; root
< comm
.size(); ++root
)
66 gatherv_test(comm
, generator
, kind
, root
);
68 using boost::mpi::gatherv
;
70 int mysize
= comm
.rank() + 1;
71 int nprocs
= comm
.size();
73 // process p will send p+1 identical generator(p) elements
74 std::vector
<value_type
> myvalues(mysize
, generator(comm
.rank()));
76 if (comm
.rank() == root
) {
77 std::vector
<value_type
> values((nprocs
*(nprocs
+1))/2);
78 std::vector
<int> sizes(comm
.size());
79 for (int p
= 0; p
< comm
.size(); ++p
)
82 std::cout
<< "Gatheringv " << kind
<< " from root "
83 << root
<< "..." << std::endl
;
85 gatherv(comm
, myvalues
, &values
[0], sizes
, root
);
87 std::vector
<value_type
> expected_values
;
88 for (int p
= 0; p
< comm
.size(); ++p
)
89 for (int i
= 0; i
< p
+1; ++i
)
90 expected_values
.push_back(generator(p
));
92 BOOST_CHECK(values
== expected_values
);
94 gatherv(comm
, myvalues
, root
);
102 // Generators to test with gather/gatherv
106 typedef int result_type
;
108 int operator()(int p
) const { return 17 + p
; }
113 typedef gps_position result_type
;
115 gps_position
operator()(int p
) const
117 return gps_position(39 + p
, 16, 20.2799);
121 struct string_generator
123 typedef std::string result_type
;
125 std::string
operator()(int p
) const
127 std::string result
= boost::lexical_cast
<std::string
>(p
);
128 result
+= " rosebud";
129 if (p
!= 1) result
+= 's';
134 struct string_list_generator
136 typedef std::list
<std::string
> result_type
;
138 std::list
<std::string
> operator()(int p
) const
140 std::list
<std::string
> result
;
141 for (int i
= 0; i
<= p
; ++i
) {
142 std::string value
= boost::lexical_cast
<std::string
>(i
);
143 result
.push_back(value
);
149 int test_main(int argc
, char* argv
[])
151 boost::mpi::environment
env(argc
, argv
);
155 gather_test(comm
, int_generator(), "integers");
156 gather_test(comm
, gps_generator(), "GPS positions");
157 gather_test(comm
, string_generator(), "string");
158 gather_test(comm
, string_list_generator(), "list of strings");
160 gatherv_test(comm
, int_generator(), "integers");
161 gatherv_test(comm
, gps_generator(), "GPS positions");
162 gatherv_test(comm
, string_generator(), "string");
163 gatherv_test(comm
, string_list_generator(), "list of strings");