random_scatter.cpp 958 B

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