• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2013 Andreas Hehn <hehn@phys.ethz.ch>, ETH Zurich
2 
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 // A test of communicators created from groups.
8 
9 #include <boost/mpi/environment.hpp>
10 #include <boost/mpi/communicator.hpp>
11 #include <boost/mpi/group.hpp>
12 #include <vector>
13 #include <algorithm>
14 
15 #define BOOST_TEST_MODULE mpi_group_test
16 #include <boost/test/included/unit_test.hpp>
17 
18 namespace mpi = boost::mpi;
19 
20 template <typename T>
21 struct iota
22 {
iotaiota23     iota() : state(0){};
operator ()iota24     T operator()()
25     {
26         return state++;
27     }
28     T state;
29 };
30 
group_test(const mpi::communicator & comm)31 void group_test(const mpi::communicator& comm)
32 {
33     std::vector<int> grp_a_ranks(comm.size() / 2);
34     std::generate(grp_a_ranks.begin(),grp_a_ranks.end(),iota<int>());
35 
36     mpi::group grp_a = comm.group().include(grp_a_ranks.begin(),grp_a_ranks.end());
37     mpi::group grp_b = comm.group().exclude(grp_a_ranks.begin(),grp_a_ranks.end());
38 
39     mpi::communicator part_a(comm,grp_a);
40     mpi::communicator part_b(comm,grp_b);
41 
42     if(part_a)
43     {
44         std::cout << "comm rank: " << comm.rank() << " -> part_a rank:" << part_a.rank() << std::endl;
45         BOOST_CHECK(part_a.rank() == comm.rank());
46     }
47     if(part_b)
48     {
49         std::cout << "comm rank: " << comm.rank() << " -> part_b rank:" << part_b.rank() << std::endl;
50         BOOST_CHECK(part_b.rank() == comm.rank() - comm.size()/2);
51     }
52 }
53 
BOOST_AUTO_TEST_CASE(group)54 BOOST_AUTO_TEST_CASE(group)
55 {
56     mpi::environment env;
57     mpi::communicator comm;
58     group_test(comm);
59 }
60