| 338 | static int world_rank; |
| 339 | |
| 340 | void shm_initialize(int size, int rank, char* addr_string, char* port_string) |
| 341 | { |
| 342 | if (is_initialized) return; |
| 343 | is_initialized = 1; |
| 344 | |
| 345 | world_size = size; |
| 346 | world_rank = rank; |
| 347 | |
| 348 | char shm_name_prefix[NAME_BUF_SIZE]; |
| 349 | char shm_name[NAME_BUF_SIZE]; |
| 350 | snprintf(shm_name_prefix, |
| 351 | NAME_BUF_SIZE, |
| 352 | "%s_%d_%s_%s", |
| 353 | SHM_BUFFER_NAME, |
| 354 | getuid(), |
| 355 | addr_string, |
| 356 | port_string); |
| 357 | // create shared workspace for SHM based allreduce |
| 358 | SharedData allreduce_buffer; |
| 359 | // allocate workspace_buf for current rank |
| 360 | struct allreduce_workspace* workspace_buf; |
| 361 | struct allreduce_workspace* workspace_buf_other; |
| 362 | workspace_buf = (struct allreduce_workspace*)malloc(sizeof(struct allreduce_workspace)); |
| 363 | if (!workspace_buf) { |
| 364 | printf("shm_initialize: malloc failed to allocate workspace_buf\n"); |
| 365 | return; |
| 366 | } |
| 367 | snprintf(shm_name, NAME_BUF_SIZE, "%s_%d", shm_name_prefix, rank); |
| 368 | shared_create(&allreduce_buffer, shm_name, workspace_buf, sizeof(struct allreduce_workspace)); |
| 369 | // shared_create() only reads workspace_buf's contents (via write()) to seed the |
| 370 | // shared-memory segment; it doesn't take ownership of the pointer. Free it here, |
| 371 | // before workspace_buf is repointed at the shared mapping below, or the original |
| 372 | // heap allocation is leaked for the lifetime of the process. |
| 373 | free(workspace_buf); |
| 374 | workspace_buf = (struct allreduce_workspace*)allreduce_buffer.bytes; |
| 375 | workspace_buf->states[0] = coll_alt2_allreduce_naive__copy_in_done; |
| 376 | workspace_buf->states[1] = coll_begin; |
| 377 | |
| 378 | // calloc used for defensive zero-init; the loop below writes every element before any read |
| 379 | workspace = (struct allreduce_workspace**)calloc(size, sizeof(struct allreduce_workspace*)); |
| 380 | symmetric_buffer[0] = (char**)calloc(size, sizeof(char*)); |
| 381 | symmetric_buffer[1] = (char**)calloc(size, sizeof(char*)); |
| 382 | distributed_buffer[0] = (char**)calloc(size, sizeof(char*)); |
| 383 | distributed_buffer[1] = (char**)calloc(size, sizeof(char*)); |
| 384 | // all five must succeed before the loop below dereferences any of them -- |
| 385 | // calloc() can return NULL under memory pressure, and none of these were |
| 386 | // checked before this fix. |
| 387 | if (!workspace || !symmetric_buffer[0] || !symmetric_buffer[1] || !distributed_buffer[0] || |
| 388 | !distributed_buffer[1]) { |
| 389 | printf("shm_initialize: calloc failed to allocate rank workspace\n"); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | // map shm of all ranks |
| 394 | for (int i = 0; i < size; i++) { |
| 395 | if (i != rank) { |
| 396 | snprintf(shm_name, NAME_BUF_SIZE, "%s_%d", shm_name_prefix, i); |
| 397 | do { |
no test coverage detected