From 1ad1972a5e741460eabe425ecae57e16e54190ab Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Fri, 30 Apr 2021 14:53:17 -0600 Subject: [PATCH] add one-sided --- one_sided.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 one_sided.cpp diff --git a/one_sided.cpp b/one_sided.cpp new file mode 100644 index 0000000..5cf47ef --- /dev/null +++ b/one_sided.cpp @@ -0,0 +1,55 @@ +#include +#include + +int main(int argc, char **argv) { + + MPI_Init(&argc, &argv); + + + MPI_Win win; + int *a; + { + /* Make rank accessible in all processes */ + + MPI_Aint size = sizeof(int); + int disp_unit = sizeof(int); + MPI_Win_allocate(size, disp_unit, MPI_INFO_NULL, MPI_COMM_WORLD, &a, &win); + } + + int rank, size; + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + // expect our a to be set by the left + int source; + if (0 == rank) { + source = size - 1; + } else { + source = rank - 1; + } + + int target; // set the right + if (rank == size - 1) { + target = 0; + } else { + target = rank + 1; + } + + // send our rank to the target window + std::cout << "rank " << rank << " put to " << target << std::endl << std::flush; + MPI_Put(&rank, 1, MPI_INT, target, 0, 1, MPI_INT, win); + + MPI_Barrier(MPI_COMM_WORLD); + + int err = 0; + + if (*a != source) { + std::cerr << "ERR: rank " << rank << " got " << *a << " expected " << source << std::endl; + err = 1; + } + + MPI_Win_free(&win); + MPI_Finalize(); + std::cout << "rank " << rank << "completed" << std::endl << std::flush; + return err; +}