Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / mpi / test / all_to_all_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 all_to_all() collective.
8 #include <boost/mpi/collectives/all_to_all.hpp>
9 #include <boost/mpi/communicator.hpp>
10 #include <boost/mpi/environment.hpp>
11 #include <algorithm>
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>
17
18 #define BOOST_TEST_MODULE mpi_all_to_all
19 #include <boost/test/included/unit_test.hpp>
20
21 using boost::mpi::communicator;
22
23 using boost::mpi::packed_skeleton_iarchive;
24 using boost::mpi::packed_skeleton_oarchive;
25
26 template<typename Generator>
27 void
28 all_to_all_test(const communicator& comm, Generator generator,
29                 const char* kind)
30 {
31   typedef typename Generator::result_type value_type;
32
33   using boost::mpi::all_to_all;
34
35   std::vector<value_type> in_values;
36   for (int p = 0; p < comm.size(); ++p)
37     in_values.push_back(generator((p + 1) * (comm.rank() + 1)));
38
39   if (comm.rank() == 0) {
40     std::cout << "Performing all-to-all operation on " << kind << "...";
41     std::cout.flush();
42   }
43   std::vector<value_type> out_values;
44   all_to_all(comm, in_values, out_values);
45
46   for (int p = 0; p < comm.size(); ++p) {
47     BOOST_CHECK(out_values[p] == generator((p + 1) * (comm.rank() + 1)));
48   }
49
50   if (comm.rank() == 0) {
51     std::cout << " done." << std::endl;
52   }
53
54   (comm.barrier)();
55 }
56
57 // Generates integers to test with all_to_all()
58 struct int_generator
59 {
60   typedef int result_type;
61
62   int operator()(int p) const { return 17 + p; }
63 };
64
65 // Generates GPS positions to test with all_to_all()
66 struct gps_generator
67 {
68   typedef gps_position result_type;
69
70   gps_position operator()(int p) const
71   {
72     return gps_position(39 + p, 16, 20.2799);
73   }
74 };
75
76 struct string_generator
77 {
78   typedef std::string result_type;
79
80   std::string operator()(int p) const
81   {
82     std::string result = boost::lexical_cast<std::string>(p);
83     result += " rosebud";
84     if (p != 1) result += 's';
85     return result;
86   }
87 };
88
89 struct string_list_generator
90 {
91   typedef std::list<std::string> result_type;
92
93   std::list<std::string> operator()(int p) const
94   {
95     std::list<std::string> result;
96     for (int i = 0; i <= p; ++i) {
97       std::string value = boost::lexical_cast<std::string>(i);
98       result.push_back(value);
99     }
100     return result;
101   }
102 };
103
104 BOOST_AUTO_TEST_CASE(all_to_all_check)
105 {
106   boost::mpi::environment env;
107   communicator comm;
108
109   all_to_all_test(comm, int_generator(), "integers");
110   all_to_all_test(comm, gps_generator(), "GPS positions");
111   all_to_all_test(comm, string_generator(), "string");
112   all_to_all_test(comm, string_list_generator(), "list of strings");
113 }