random_min.cpp 838 B

123456789101112131415161718192021222324252627282930
  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 reduce() to compute a minimum value.
  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. int minimum;
  18. reduce(world, my_number, minimum, mpi::minimum<int>(), 0);
  19. std::cout << "The minimum value is " << minimum << std::endl;
  20. } else {
  21. reduce(world, my_number, mpi::minimum<int>(), 0);
  22. }
  23. return 0;
  24. }