• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 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 // An example using Boost.MPI's reduce() to concatenate strings.
8 #include <boost/mpi.hpp>
9 #include <iostream>
10 #include <string>
11 #include <boost/serialization/string.hpp> // Important for sending strings!
12 namespace mpi = boost::mpi;
13 
14 /* Defining STRING_CONCAT_COMMUTATIVE lies to Boost.MPI by forcing it
15  * to assume that string concatenation is commutative, which it is
16  * not. However, doing so illustrates how the results of a reduction
17  * can change when a non-commutative operator is assumed to be
18  * commutative.
19  */
20 #ifdef STRING_CONCAT_COMMUTATIVE
21 namespace boost { namespace mpi {
22 
23 template<>
24 struct is_commutative<std::plus<std::string>, std::string> : mpl::true_ { };
25 
26 } } // end namespace boost::mpi
27 #endif
28 
main(int argc,char * argv[])29 int main(int argc, char* argv[])
30 {
31   mpi::environment env(argc, argv);
32   mpi::communicator world;
33 
34   std::string names[10] = { "zero ", "one ", "two ", "three ", "four ",
35                             "five ", "six ", "seven ", "eight ", "nine " };
36 
37   std::string result;
38   reduce(world,
39          world.rank() < 10? names[world.rank()] : std::string("many "),
40          result, std::plus<std::string>(), 0);
41 
42   if (world.rank() == 0)
43     std::cout << "The result is " << result << std::endl;
44 
45   return 0;
46 }
47