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