32inline std::vector< MPI_Comm >
build_level_comms( MPI_Comm root_comm,
const std::vector< int >& factors )
34 std::vector< MPI_Comm > level_comms;
35 level_comms.reserve( factors.size() + 1 );
36 level_comms.push_back( root_comm );
38 MPI_Comm cur = root_comm;
40 for (
size_t i = 0; i < factors.size(); ++i )
42 const int factor = factors[i];
45 throw std::runtime_error(
"build_level_comms: agglomeration factor must be >= 1" );
48 if ( cur == MPI_COMM_NULL )
51 level_comms.push_back( MPI_COMM_NULL );
56 const int parent_rank =
rank( cur );
58 if ( parent_size % factor != 0 )
60 throw std::runtime_error(
61 "build_level_comms: parent comm size must be a multiple of the agglomeration factor" );
66 const int color = ( parent_rank % factor == 0 ) ? 0 : MPI_UNDEFINED;
67 const int key = parent_rank / factor;
69 MPI_Comm sub = MPI_COMM_NULL;
70 MPI_Comm_split( cur, color, key, &sub );
71 level_comms.push_back( sub );
103 for (
size_t i = 1; i < level_comms.size(); ++i )
105 if ( level_comms[i] != MPI_COMM_NULL )
107 MPI_Comm_free( &level_comms[i] );
108 level_comms[i] = MPI_COMM_NULL;