Improved the determination of MPI rank (#61)

* Improved the determination of MPI rank

* C-style cast of MPI_Comm
このコミットが含まれているのは:
Jonathan R. Madsen
2022-06-21 00:27:52 -05:00
committed by GitHub
コミット dfda902092
4個のファイルの変更319行の追加102行の削除
+150 -29
ファイルの表示
@@ -42,8 +42,9 @@ std::string _name = {};
template <typename Tp, size_t N>
void
all2all(int _rank)
all2all(int _rank, MPI_Comm _comm)
{
if(_comm == MPI_COMM_NULL) return;
static_assert(N > 0, "Error! N must be greater than zero!");
auto _mt = std::mt19937_64{ size_t(_rank + 100) };
@@ -74,7 +75,7 @@ all2all(int _rank)
printf("[%s][%i] values sent (# = %zu) :: %s.\n", _name.c_str(), _rank,
values_sent.size(), _get_values_str(values_sent).c_str());
auto _dtype = MPI_INT;
auto _dtype = MPI_INT; // NOLINT
if(std::is_same<Tp, long>::value)
_dtype = MPI_LONG;
else if(std::is_same<Tp, float>::value)
@@ -82,57 +83,177 @@ all2all(int _rank)
else if(std::is_same<Tp, double>::value)
_dtype = MPI_DOUBLE;
MPI_Alltoall(&values_sent[_rank], 1, _dtype, &values_recv[_rank], 1, _dtype,
MPI_COMM_WORLD);
MPI_Alltoall(&values_sent[_rank], 1, _dtype, &values_recv[_rank], 1, _dtype, _comm);
if(_rank == 0)
printf("[%s][%i] values recv (# = %zu) :: %s.\n", _name.c_str(), _rank,
values_sent.size(), _get_values_str(values_recv).c_str());
}
void
run(MPI_Comm _comm, int nitr)
{
if(_comm == MPI_COMM_NULL) return;
int _rank = 0;
int _size = 0;
MPI_Comm_rank(_comm, &_rank);
MPI_Comm_size(_comm, &_size);
printf("[%s][%i] running %i iterations on %i ranks...\n", _name.c_str(), _rank, nitr,
_size);
MPI_Barrier(_comm);
for(int i = 0; i < nitr; ++i)
{
all2all<int, 3>(_rank, _comm);
all2all<long, 4>(_rank, _comm);
MPI_Barrier(_comm);
all2all<float, 5>(_rank, _comm);
all2all<double, 6>(_rank, _comm);
}
MPI_Barrier(_comm);
}
void
print_info(MPI_Comm _comm, bool _verbose, std::string _msg = {})
{
if(_comm == MPI_COMM_NULL) return;
int _rank = 0;
int _size = 1;
MPI_Comm_rank(_comm, &_rank);
MPI_Comm_size(_comm, &_size);
if(!_msg.empty()) _msg = "[" + _msg + "] ";
if(_verbose)
{
auto _ppid = getppid();
std::ifstream _ifs{ "/proc/" + std::to_string(_ppid) + "/task/" +
std::to_string(_ppid) + "/children" };
std::stringstream _ss{};
while(_ifs)
{
std::string _s{};
_ifs >> _s;
_ss << _s << " ";
}
if(_rank == 0)
printf("[%s]%s RANK = %i (out of %i), PID = %i, PPID = %i :: %s\n",
_name.c_str(), _msg.c_str(), _rank, _size, getpid(), getppid(),
_ss.str().c_str());
}
else
{
if(_rank == 0)
printf("[%s]%s RANK = %i (out of %i), PID = %i, PPID = %i\n", _name.c_str(),
_msg.c_str(), _rank, _size, getpid(), getppid());
}
}
int
main(int argc, char** argv)
{
int _mpi_thread_provided;
MPI_Init_thread(&argc, &argv, MPI_THREAD_SINGLE, &_mpi_thread_provided);
int rank = 0;
int size = 1;
int nitr = 1;
if(argc > 1) nitr = atoi(argv[2]);
MPI_Comm_size(MPI_COMM_WORLD, &size);
_name = argv[0];
auto _pos = _name.find_last_of('/');
if(_pos < _name.length()) _name = _name.substr(_pos + 1);
printf("[%s] Number of iterations: %i\n", _name.c_str(), nitr);
int _mpi_thread_provided;
MPI_Init_thread(&argc, &argv, MPI_THREAD_SINGLE, &_mpi_thread_provided);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("[%s][%i] running with MPI_COMM_WORLD...\n", _name.c_str(), getpid());
run(MPI_COMM_WORLD, nitr);
auto _ppid = getppid();
std::ifstream _ifs{ "/proc/" + std::to_string(_ppid) + "/task/" +
std::to_string(_ppid) + "/children" };
std::stringstream _ss{};
while(_ifs)
{
std::string _s{};
_ifs >> _s;
_ss << _s << " ";
}
printf("[%s] RANK = %i, PID = %i, PPID = %i :: %s\n", _name.c_str(), rank, getpid(),
getppid(), _ss.str().c_str());
print_info(MPI_COMM_WORLD, true, "MPI_COMM_WORLD");
MPI_Barrier(MPI_COMM_WORLD);
for(int i = 0; i < nitr; ++i)
printf("[%s]\n", _name.c_str());
if(size > 1)
{
all2all<int, 3>(rank);
all2all<long, 4>(rank);
MPI_Barrier(MPI_COMM_WORLD);
all2all<float, 5>(rank);
all2all<double, 6>(rank);
MPI_Comm dup;
printf("[%s][%i] Duplicating MPI_COMM_WORLD...\n", _name.c_str(), getpid());
MPI_Comm_dup(MPI_COMM_WORLD, &dup);
printf("[%s][%i] running with duplicated comm of MPI_COMM_WORLD...\n",
_name.c_str(), getpid());
run(dup, nitr);
MPI_Comm_rank(dup, &rank);
if(rank == 0) printf("[%s]\n", _name.c_str());
printf("[%s][%i] RANK = %i on duplicated MPI_COMM_WORLD...\n", _name.c_str(),
getpid(), rank);
if(size > 3)
{
std::vector<MPI_Comm> comms(3);
for(int i = 0; i < size; ++i)
{
auto _idx = i % 3;
printf("[%s][%i] Splitting duplicated MPI_COMM_WORLD %i (rank = %i)...\n",
_name.c_str(), getpid(), _idx, rank);
MPI_Comm* comm = &comms.at(_idx);
MPI_Comm_split(dup, _idx, rank, comm);
}
for(auto itr : comms) // NOLINT
MPI_Barrier(itr);
for(int i = 0; i < size; ++i)
{
auto _idx = i % 3;
int _rank = 0;
MPI_Comm_rank(comms.at(_idx), &_rank);
printf("[%s][%i] Running on split communicator %i (rank = %i)...\n",
_name.c_str(), getpid(), _idx, _rank);
run(comms.at(_idx), nitr);
}
// Get the group of processes in MPI_COMM_WORLD
MPI_Group world_group;
MPI_Comm_group(MPI_COMM_WORLD, &world_group);
int n = 0;
const int ranks[7] = { 1, 2, 3, 5, 7, 11, 13 };
for(int rank : ranks)
if(rank < size) ++n;
// Construct a group containing all of the prime ranks in world_group
MPI_Group prime_group;
MPI_Group_incl(world_group, n, ranks, &prime_group);
// Create a new communicator based on the group
MPI_Comm prime_comm;
MPI_Comm_create_group(MPI_COMM_WORLD, prime_group, 0, &prime_comm);
MPI_Group nonprime_group;
MPI_Group_difference(world_group, prime_group, &nonprime_group);
MPI_Comm nonprime_comm;
MPI_Comm_create_group(MPI_COMM_WORLD, nonprime_group, 1, &nonprime_comm);
print_info(prime_comm, false, "Prime comm");
print_info(nonprime_comm, false, "Non-prime comm");
run(prime_comm, nitr);
run(nonprime_comm, nitr);
MPI_Group_free(&world_group);
MPI_Group_free(&prime_group);
MPI_Group_free(&nonprime_group);
}
print_info(dup, false);
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}