random_gather.cpp 890 B

12345678910111213141516171819202122232425262728293031
  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. // An example using Boost.MPI's gather(): [main]
  6. #include <boost/mpi.hpp>
  7. #include <iostream>
  8. #include <cstdlib>
  9. namespace mpi = boost::mpi;
  10. int main(int argc, char* argv[])
  11. {
  12. mpi::environment env(argc, argv);
  13. mpi::communicator world;
  14. std::srand(time(0) + world.rank());
  15. int my_number = std::rand();
  16. if (world.rank() == 0) {
  17. std::vector<int> all_numbers;
  18. gather(world, my_number, all_numbers, 0);
  19. for (int proc = 0; proc < world.size(); ++proc)
  20. std::cout << "Process #" << proc << " thought of " << all_numbers[proc]
  21. << std::endl;
  22. } else {
  23. gather(world, my_number, 0);
  24. }
  25. return 0;
  26. }