|
| 1 | +#include <mpi.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdbool.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +void test_shared_memory(MPI_Win win, MPI_Comm comm, int *local_mem, int shared_size, const char *test_name) { |
| 8 | + int ret = MPI_SUCCESS; |
| 9 | + int rank, size; |
| 10 | + MPI_Comm_rank(comm, &rank); |
| 11 | + MPI_Comm_size(comm, &size); |
| 12 | + |
| 13 | + // Configure the window to return errors instead of aborting |
| 14 | + MPI_Win_set_errhandler(win, MPI_ERRORS_RETURN); |
| 15 | + if (rank != 0) { |
| 16 | + do { |
| 17 | + // Query the shared memory of rank 0 |
| 18 | + int *baseptr; |
| 19 | + MPI_Aint queried_size; |
| 20 | + int disp_unit; |
| 21 | + ret = MPI_Win_shared_query(win, 0, &queried_size, &disp_unit, &baseptr); |
| 22 | + if (ret != MPI_SUCCESS) { |
| 23 | + printf("%s: rank %d: MPI_Win_shared_query failed with error code %d\n", test_name, rank, ret); |
| 24 | + ret = MPI_ERR_OTHER; |
| 25 | + break; |
| 26 | + } |
| 27 | + |
| 28 | + if (baseptr == NULL) { |
| 29 | + printf("%s: rank %d: MPI_Win_shared_query returned NULL\n", test_name, rank); |
| 30 | + if (queried_size != 0) { |
| 31 | + printf("%s: rank %d: Expected zero size but got %zu\n", test_name, rank, queried_size); |
| 32 | + } |
| 33 | + ret = MPI_ERR_NO_MEM; |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + if (queried_size != shared_size) { |
| 38 | + printf("%s: rank %d: Queried size mismatch: expected %d, got %zu\n", test_name, rank, shared_size, queried_size); |
| 39 | + ret = MPI_ERR_SIZE; |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + // Write data to the shared memory |
| 44 | + baseptr[rank] = 42; |
| 45 | + |
| 46 | + } while (false); |
| 47 | + } |
| 48 | + |
| 49 | + int *ret_vals = (int *)malloc(size * sizeof(int)); |
| 50 | + MPI_Gather(&ret, 1, MPI_INT, ret_vals, 1, MPI_INT, 0, comm); |
| 51 | + |
| 52 | + if (rank == 0) { |
| 53 | + int valid_procs = size - 1; // Exclude rank 0 |
| 54 | + bool all_success = true; |
| 55 | + for (int i = 1; i < size; ++i) { |
| 56 | + if (ret_vals[i] != MPI_SUCCESS) { |
| 57 | + valid_procs--; |
| 58 | + continue; // Skip ranks that had errors |
| 59 | + } |
| 60 | + // Check if the data was written correctly |
| 61 | + if (local_mem[i] != 42) { |
| 62 | + all_success = false; |
| 63 | + printf("%s: rank 0: Data verification failed at %d!\n", test_name, i); |
| 64 | + } |
| 65 | + } |
| 66 | + if (all_success && valid_procs > 0) { |
| 67 | + printf("%s: rank 0: all data written correctly!\n", test_name); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +int main(int argc, char *argv[]) { |
| 73 | + MPI_Init(&argc, &argv); |
| 74 | + |
| 75 | + int rank, size; |
| 76 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 77 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 78 | + |
| 79 | + int shared_size; |
| 80 | + int shared_rank; |
| 81 | + MPI_Comm comm_shared; |
| 82 | + MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &comm_shared); |
| 83 | + MPI_Comm_rank(comm_shared, &shared_rank); |
| 84 | + MPI_Comm_size(comm_shared, &shared_size); |
| 85 | + |
| 86 | + // Test with MPI_Win_allocate_shared |
| 87 | + MPI_Win win_shared; |
| 88 | + int *shared_mem_shared = NULL; |
| 89 | + int win_size = size*sizeof(int); |
| 90 | + MPI_Win_allocate_shared(shared_rank == 0 ? sizeof(int)*shared_size : 0, sizeof(int), MPI_INFO_NULL, comm_shared, &shared_mem_shared, &win_shared); |
| 91 | + test_shared_memory(win_shared, comm_shared, shared_mem_shared, sizeof(int)*shared_size, "MPI_Win_allocate_shared"); |
| 92 | + MPI_Win_free(&win_shared); |
| 93 | + |
| 94 | + // Test with MPI_Win_allocate |
| 95 | + MPI_Win win_allocate; |
| 96 | + int *shared_mem_allocate = NULL; |
| 97 | + MPI_Win_allocate(rank == 0 ? win_size : 0, sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &shared_mem_allocate, &win_allocate); |
| 98 | + test_shared_memory(win_allocate, MPI_COMM_WORLD, shared_mem_allocate, win_size, "MPI_Win_allocate"); |
| 99 | + MPI_Win_free(&win_allocate); |
| 100 | + |
| 101 | + // Test with MPI_Win_create |
| 102 | + MPI_Win win_create; |
| 103 | + int* local_mem = (int*)malloc(sizeof(int) * win_size); |
| 104 | + MPI_Win_create(rank == 0 ? local_mem : NULL, rank == 0 ? win_size : 0, sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win_create); |
| 105 | + test_shared_memory(win_create, MPI_COMM_WORLD, local_mem, win_size, "MPI_Win_create"); |
| 106 | + MPI_Win_free(&win_create); |
| 107 | + free(local_mem); |
| 108 | + |
| 109 | + MPI_Finalize(); |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
0 commit comments