-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat.c
39 lines (31 loc) · 1.13 KB
/
concat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <boost/mpi.hpp>
#include <iostream>
#include <string>
#include <boost/serialization/string.hpp> // Important for sending strings!
namespace mpi = boost::mpi;
/* Defining STRING_CONCAT_COMMUTATIVE lies to Boost.MPI by forcing it
* to assume that string concatenation is commutative, which it is
* not. However, doing so illustrates how the results of a reduction
* can change when a non-commutative operator is assumed to be
* commutative.
*/
#ifdef STRING_CONCAT_COMMUTATIVE
namespace boost { namespace mpi {
template<>
struct is_commutative<std::plus<std::string>, std::string> : mpl::true_ { };
} } // end namespace boost::mpi
#endif
int main(int argc, char* argv[])
{
mpi::environment env(argc, argv);
mpi::communicator world;
std::string names[10] = { "zero ", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ", "nine " };
std::string result;
reduce(world,
world.rank() < 10? names[world.rank()] : std::string("many "),
result, std::plus<std::string>(), 0);
if (world.rank() == 0)
std::cout << "The result is " << result << std::endl;
return 0;
}