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;