• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2013 Andreas Hehn <hehn@phys.ethz.ch>, ETH Zurich
2 // based on
3 // hellp-world_broadcast.cpp (C) 2006 Douglas Gregor <doug.gregor@gmail.com>
4 
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 // A simple Hello world! example
10 // using boost::mpi::group and boost::mpi::broadcast()
11 
12 #include <stdexcept>
13 #include <boost/mpi/environment.hpp>
14 #include <boost/mpi/communicator.hpp>
15 #include <boost/mpi/group.hpp>
16 #include <boost/mpi/collectives.hpp>
17 
18 #include <boost/serialization/string.hpp>
19 
20 namespace mpi = boost::mpi;
21 
main(int argc,char * argv[])22 int main(int argc, char* argv[])
23 {
24   mpi::environment  env(argc, argv);
25   mpi::communicator world;
26   if(world.size() < 2)
27     throw std::runtime_error("Please run with at least 2 MPI processes!");
28 
29   int group_a_ranks[2] = {0,1};
30 
31   mpi::group world_group = world.group();
32   mpi::group group_a     = world_group.include(group_a_ranks,group_a_ranks+2);
33 
34   mpi::communicator comm_a(world,group_a);
35 
36   std::string value("Hello world!");
37   if(comm_a)
38   {
39     if(comm_a.rank() == 0) {
40          value = "Hello group a!";
41     }
42     mpi::broadcast(comm_a, value, 0);
43   }
44   std::cout << "Process #" << world.rank() << " says " << value << std::endl;
45   return 0;
46 }
47