hello_world_groups.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // A simple Hello world! example
  8. // using boost::mpi::group and boost::mpi::broadcast()
  9. #include <stdexcept>
  10. #include <boost/mpi/environment.hpp>
  11. #include <boost/mpi/communicator.hpp>
  12. #include <boost/mpi/group.hpp>
  13. #include <boost/mpi/collectives.hpp>
  14. #include <boost/serialization/string.hpp>
  15. namespace mpi = boost::mpi;
  16. int main(int argc, char* argv[])
  17. {
  18. mpi::environment env(argc, argv);
  19. mpi::communicator world;
  20. if(world.size() < 2)
  21. throw std::runtime_error("Please run with at least 2 MPI processes!");
  22. int group_a_ranks[2] = {0,1};
  23. mpi::group world_group = world.group();
  24. mpi::group group_a = world_group.include(group_a_ranks,group_a_ranks+2);
  25. mpi::communicator comm_a(world,group_a);
  26. std::string value("Hello world!");
  27. if(comm_a)
  28. {
  29. if(comm_a.rank() == 0) {
  30. value = "Hello group a!";
  31. }
  32. mpi::broadcast(comm_a, value, 0);
  33. }
  34. std::cout << "Process #" << world.rank() << " says " << value << std::endl;
  35. return 0;
  36. }