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 gather(): [main]
8
9 #include <boost/mpi.hpp>
10 #include <boost/mpi/collectives.hpp>
11 #include <iostream>
12 #include <cstdlib>
13 #include <vector>
14
15 namespace mpi = boost::mpi;
16
main(int argc,char * argv[])17 int main(int argc, char* argv[])
18 {
19 mpi::environment env(argc, argv);
20 mpi::communicator world;
21
22 std::srand(time(0) + world.rank());
23 std::vector<int> all;
24 int mine = -1;
25 if (world.rank() == 0) {
26 all.resize(world.size());
27 std::generate(all.begin(), all.end(), std::rand);
28 }
29 mpi::scatter(world, all, mine, 0);
30 for (int r = 0; r < world.size(); ++r) {
31 world.barrier();
32 if (r == world.rank()) {
33 std::cout << "Rank " << r << " got " << mine << '\n';
34 }
35 }
36 return 0;
37 }
38