6 #ifndef METALL_METALL_UTILITY_MPI_HPP
7 #define METALL_METALL_UTILITY_MPI_HPP
16 #include <metall/detail/file.hpp>
17 #include <metall/detail/mmap.hpp>
25 if (::MPI_Comm_rank(comm, &rank) != MPI_SUCCESS) {
27 "Failed MPI_Comm_rank");
35 if (::MPI_Comm_size(comm, &size) != MPI_SUCCESS) {
37 "Failed MPI_Comm_size");
43 inline bool barrier(
const MPI_Comm &comm) {
44 if (::MPI_Barrier(comm) != MPI_SUCCESS) {
57 const MPI_Comm &comm) {
58 char global_value = 0;
59 if (::MPI_Allreduce(&local_value, &global_value, 1, MPI_CHAR, MPI_LAND,
60 comm) != MPI_SUCCESS) {
61 return std::make_pair(
false, global_value);
63 return std::make_pair(
true, global_value);
72 const MPI_Comm &comm) {
73 char global_value = 0;
74 if (::MPI_Allreduce(&local_value, &global_value, 1, MPI_CHAR, MPI_LOR,
75 comm) != MPI_SUCCESS) {
76 return std::make_pair(
false,
false);
78 return std::make_pair(
true, global_value);
85 const char *shm_name =
"metall_local_root";
86 ::shm_unlink(shm_name);
94 if (::MPI_Recv(
nullptr, 0, MPI_BYTE, world_rank - 1, 1, MPI_COMM_WORLD,
95 MPI_STATUS_IGNORE) != MPI_SUCCESS) {
101 const int shm_size = 4096;
102 bool this_rank_created =
false;
104 int shm_fd = ::shm_open(shm_name, O_RDWR, 0666);
106 shm_fd = ::shm_open(shm_name, O_CREAT | O_RDWR, 0666);
109 "Failed to open & create a shm file");
112 this_rank_created =
true;
114 if (!metall::mtlldetail::extend_file_size(shm_fd, shm_size,
false)) {
116 "Failed to extend a shm file; however, continue work");
122 metall::mtlldetail::map_file_write_mode(shm_fd,
nullptr, shm_size, 0, 0);
125 "Failed to map a shm file");
129 auto *p_min_rank_size =
static_cast<std::pair<int, int> *
>(ptr);
130 if (this_rank_created) {
131 p_min_rank_size->first = world_rank;
132 p_min_rank_size->second = 1;
134 p_min_rank_size->first = std::min(p_min_rank_size->first, world_rank);
135 p_min_rank_size->second++;
139 if (world_rank < world_size - 1) {
140 if (MPI_Send(
nullptr, 0, MPI_BYTE, world_rank + 1, 1, MPI_COMM_WORLD) !=
149 const int local_root_rank = p_min_rank_size->first;
152 if (!metall::mtlldetail::munmap(shm_fd, ptr, shm_size,
false)) {
154 "Failed to unmap the shm file; however, continue work.");
157 if (this_rank_created && ::shm_unlink(shm_name) != 0) {
159 "Failed to remove the shm file; however, continue work.");
162 return local_root_rank;