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 broadcast() collective.
8 #include <algorithm>
9 #include <vector>
10 #include <map>
11 
12 #include <boost/mpi/collectives/broadcast.hpp>
13 #include <boost/mpi/communicator.hpp>
14 #include <boost/mpi/environment.hpp>
15 
16 #include <boost/serialization/string.hpp>
17 #include <boost/serialization/vector.hpp>
18 #include <boost/serialization/map.hpp>
19 
20 #define BOOST_TEST_MODULE mpi_broadcast_stl
21 #include <boost/test/included/unit_test.hpp>
22 
23 namespace mpi = boost::mpi;
24 
25 typedef std::vector<std::map<int, double> > sparse;
26 
27 template<typename T>
28 void
broadcast_test(const mpi::communicator & comm,const T & bc_value,std::string const & kind,int root)29 broadcast_test(const mpi::communicator& comm, const T& bc_value,
30                std::string const& kind, int root) {
31   using boost::mpi::broadcast;
32 
33   T value;
34   if (comm.rank() == root) {
35     value = bc_value;
36     std::cout << "Broadcasting " << kind << " from root " << root << "...";
37     std::cout.flush();
38   }
39 
40 
41   broadcast(comm, value, root);
42   BOOST_CHECK(value == bc_value);
43   if (comm.rank() == root) {
44     if (value == bc_value) {
45       std::cout << "OK." << std::endl;
46     } else {
47       std::cout << "FAIL." << std::endl;
48     }
49   }
50   comm.barrier();
51 }
52 
53 template<typename T>
54 void
broadcast_test(const mpi::communicator & comm,const T & bc_value,std::string const & kind)55 broadcast_test(const mpi::communicator& comm, const T& bc_value,
56                std::string const& kind)
57 {
58   for (int root = 0; root < comm.size(); ++root) {
59     broadcast_test(comm, bc_value, kind, root);
60   }
61 }
62 
BOOST_AUTO_TEST_CASE(broadcast_stl)63 BOOST_AUTO_TEST_CASE(broadcast_stl)
64 {
65   boost::mpi::environment env;
66 
67   mpi::communicator comm;
68   BOOST_TEST_REQUIRE(comm.size() > 1);
69 
70   sparse s;
71   s.resize(2);
72   s[0][12] = 0.12;
73   s[1][13] = 1.13;
74   broadcast_test(comm, s, "sparse");
75 }
76