]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor@gmail.com> |
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 all_gather() collective. | |
11fdf7f2 TL |
8 | |
9 | #include <algorithm> | |
10 | ||
7c673cae | 11 | #include <boost/mpi/collectives/all_gather.hpp> |
11fdf7f2 | 12 | #include <boost/mpi/collectives/all_gatherv.hpp> |
7c673cae FG |
13 | #include <boost/mpi/environment.hpp> |
14 | #include <boost/mpi/communicator.hpp> | |
15 | #include <boost/test/minimal.hpp> | |
7c673cae FG |
16 | #include <boost/serialization/string.hpp> |
17 | #include <boost/serialization/list.hpp> | |
18 | #include <boost/iterator/counting_iterator.hpp> | |
19 | #include <boost/lexical_cast.hpp> | |
20 | ||
11fdf7f2 | 21 | #include "gps_position.hpp" |
7c673cae | 22 | |
11fdf7f2 | 23 | namespace mpi = boost::mpi; |
7c673cae FG |
24 | |
25 | template<typename Generator> | |
26 | void | |
11fdf7f2 TL |
27 | all_gather_test(const mpi::communicator& comm, Generator generator, |
28 | std::string kind) | |
7c673cae FG |
29 | { |
30 | typedef typename Generator::result_type value_type; | |
31 | value_type value = generator(comm.rank()); | |
32 | ||
7c673cae FG |
33 | std::vector<value_type> values; |
34 | if (comm.rank() == 0) { | |
35 | std::cout << "Gathering " << kind << "..."; | |
36 | std::cout.flush(); | |
37 | } | |
38 | ||
11fdf7f2 | 39 | mpi::all_gather(comm, value, values); |
7c673cae FG |
40 | |
41 | std::vector<value_type> expected_values; | |
42 | for (int p = 0; p < comm.size(); ++p) | |
43 | expected_values.push_back(generator(p)); | |
44 | BOOST_CHECK(values == expected_values); | |
45 | if (comm.rank() == 0 && values == expected_values) | |
46 | std::cout << "OK." << std::endl; | |
47 | ||
48 | (comm.barrier)(); | |
49 | } | |
50 | ||
11fdf7f2 TL |
51 | template<typename Generator> |
52 | void | |
53 | all_gatherv_test(const mpi::communicator& comm, Generator generator, | |
54 | std::string kind) | |
55 | { | |
56 | typedef typename Generator::result_type value_type; | |
57 | using boost::mpi::all_gatherv; | |
58 | ||
59 | std::vector<value_type> myvalues, expected, values; | |
60 | std::vector<int> sizes; | |
61 | for(int r = 0; r < comm.size(); ++r) { | |
62 | value_type value = generator(r); | |
63 | sizes.push_back(r+1); | |
64 | for (int k=0; k < r+1; ++k) { | |
65 | expected.push_back(value); | |
66 | if(comm.rank() == r) { | |
67 | myvalues.push_back(value); | |
68 | } | |
69 | } | |
70 | } | |
71 | if (comm.rank() == 0) { | |
72 | std::cout << "Gathering " << kind << "..."; | |
73 | std::cout.flush(); | |
74 | } | |
75 | ||
76 | mpi::all_gatherv(comm, myvalues, values, sizes); | |
77 | ||
78 | BOOST_CHECK(values == expected); | |
79 | ||
80 | if (comm.rank() == 0 && values == expected) | |
81 | std::cout << "OK." << std::endl; | |
82 | ||
83 | (comm.barrier)(); | |
84 | } | |
85 | ||
7c673cae FG |
86 | // Generates integers to test with gather() |
87 | struct int_generator | |
88 | { | |
89 | typedef int result_type; | |
90 | ||
91 | int operator()(int p) const { return 17 + p; } | |
92 | }; | |
93 | ||
94 | // Generates GPS positions to test with gather() | |
95 | struct gps_generator | |
96 | { | |
97 | typedef gps_position result_type; | |
98 | ||
99 | gps_position operator()(int p) const | |
100 | { | |
101 | return gps_position(39 + p, 16, 20.2799); | |
102 | } | |
103 | }; | |
104 | ||
105 | struct string_generator | |
106 | { | |
107 | typedef std::string result_type; | |
108 | ||
109 | std::string operator()(int p) const | |
110 | { | |
111 | std::string result = boost::lexical_cast<std::string>(p); | |
112 | result += " rosebud"; | |
113 | if (p != 1) result += 's'; | |
114 | return result; | |
115 | } | |
116 | }; | |
117 | ||
118 | struct string_list_generator | |
119 | { | |
120 | typedef std::list<std::string> result_type; | |
121 | ||
122 | std::list<std::string> operator()(int p) const | |
123 | { | |
124 | std::list<std::string> result; | |
125 | for (int i = 0; i <= p; ++i) { | |
126 | std::string value = boost::lexical_cast<std::string>(i); | |
127 | result.push_back(value); | |
128 | } | |
129 | return result; | |
130 | } | |
131 | }; | |
132 | ||
133 | int test_main(int argc, char* argv[]) | |
134 | { | |
135 | boost::mpi::environment env(argc, argv); | |
11fdf7f2 | 136 | mpi::communicator comm; |
7c673cae FG |
137 | all_gather_test(comm, int_generator(), "integers"); |
138 | all_gather_test(comm, gps_generator(), "GPS positions"); | |
139 | all_gather_test(comm, string_generator(), "string"); | |
140 | all_gather_test(comm, string_list_generator(), "list of strings"); | |
11fdf7f2 TL |
141 | |
142 | all_gatherv_test(comm, int_generator(), "integers"); | |
143 | all_gatherv_test(comm, gps_generator(), "GPS positions"); | |
144 | all_gatherv_test(comm, string_generator(), "string"); | |
145 | all_gatherv_test(comm, string_list_generator(), "list of strings"); | |
7c673cae FG |
146 | return 0; |
147 | } |