global_min.cpp 841 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright (C) 2013 Alain Miniussi <alain.miniussi@oca.eu>
  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 all_reduce() that compute the minimum
  6. // of each process's value and broadcast the result to all the processes.
  7. #include <boost/mpi.hpp>
  8. #include <iostream>
  9. #include <cstdlib>
  10. namespace mpi = boost::mpi;
  11. int main(int argc, char* argv[])
  12. {
  13. mpi::environment env(argc, argv);
  14. mpi::communicator world;
  15. std::srand(world.rank());
  16. int my_number = std::rand();
  17. int minimum;
  18. all_reduce(world, my_number, minimum, mpi::minimum<int>());
  19. if (world.rank() == 0) {
  20. std::cout << "The minimum value is " << minimum << std::endl;
  21. }
  22. return 0;
  23. }