hello_world.cpp 904 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright (C) 2006 Douglas Gregor <doug.gregor@gmail.com>
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // A simple Hello, world! example using Boost.MPI message passing.
  6. #include <boost/mpi.hpp>
  7. #include <iostream>
  8. #include <boost/serialization/string.hpp> // Needed to send/receive strings!
  9. namespace mpi = boost::mpi;
  10. int main(int argc, char* argv[])
  11. {
  12. mpi::environment env(argc, argv);
  13. mpi::communicator world;
  14. if (world.rank() == 0) {
  15. world.send(1, 0, std::string("Hello"));
  16. std::string msg;
  17. world.recv(1, 1, msg);
  18. std::cout << msg << "!" << std::endl;
  19. } else {
  20. std::string msg;
  21. world.recv(0, 0, msg);
  22. std::cout << msg << ", ";
  23. std::cout.flush();
  24. world.send(0, 1, std::string("world"));
  25. }
  26. return 0;
  27. }