threading.qbk 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. [section:threading Threads]
  2. There are an increasing number of hybrid parallel applications that mix
  3. distributed and shared memory parallelism. To know how to support that model,
  4. one need to know what level of threading support is guaranteed by the MPI
  5. implementation. There are 4 ordered level of possible threading support described
  6. by [enumref boost::mpi::threading::level mpi::threading::level].
  7. At the lowest level, you should not use threads at all, at the highest level, any
  8. thread can perform MPI call.
  9. If you want to use multi-threading in your MPI application, you should indicate
  10. in the environment constructor your preferred threading support. Then probe the
  11. one the library did provide, and decide what you can do with it (it could be
  12. nothing, then aborting is a valid option):
  13. #include <boost/mpi/environment.hpp>
  14. #include <boost/mpi/communicator.hpp>
  15. #include <iostream>
  16. namespace mpi = boost::mpi;
  17. namespace mt = mpi::threading;
  18. int main()
  19. {
  20. mpi::environment env(mt::funneled);
  21. if (env.thread_level() < mt::funneled) {
  22. env.abort(-1);
  23. }
  24. mpi::communicator world;
  25. std::cout << "I am process " << world.rank() << " of " << world.size()
  26. << "." << std::endl;
  27. return 0;
  28. }
  29. [endsect:threading]