groups_test.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2013 Andreas Hehn <hehn@phys.ethz.ch>, ETH Zurich
  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. // A test of communicators created from groups.
  6. #include <boost/mpi/environment.hpp>
  7. #include <boost/mpi/communicator.hpp>
  8. #include <boost/mpi/group.hpp>
  9. #include <vector>
  10. #include <algorithm>
  11. #define BOOST_TEST_MODULE mpi_group_test
  12. #include <boost/test/included/unit_test.hpp>
  13. namespace mpi = boost::mpi;
  14. template <typename T>
  15. struct iota
  16. {
  17. iota() : state(0){};
  18. T operator()()
  19. {
  20. return state++;
  21. }
  22. T state;
  23. };
  24. void group_test(const mpi::communicator& comm)
  25. {
  26. std::vector<int> grp_a_ranks(comm.size() / 2);
  27. std::generate(grp_a_ranks.begin(),grp_a_ranks.end(),iota<int>());
  28. mpi::group grp_a = comm.group().include(grp_a_ranks.begin(),grp_a_ranks.end());
  29. mpi::group grp_b = comm.group().exclude(grp_a_ranks.begin(),grp_a_ranks.end());
  30. mpi::communicator part_a(comm,grp_a);
  31. mpi::communicator part_b(comm,grp_b);
  32. if(part_a)
  33. {
  34. std::cout << "comm rank: " << comm.rank() << " -> part_a rank:" << part_a.rank() << std::endl;
  35. BOOST_CHECK(part_a.rank() == comm.rank());
  36. }
  37. if(part_b)
  38. {
  39. std::cout << "comm rank: " << comm.rank() << " -> part_b rank:" << part_b.rank() << std::endl;
  40. BOOST_CHECK(part_b.rank() == comm.rank() - comm.size()/2);
  41. }
  42. }
  43. BOOST_AUTO_TEST_CASE(group)
  44. {
  45. mpi::environment env;
  46. mpi::communicator comm;
  47. group_test(comm);
  48. }