Skip to content

Instantly share code, notes, and snippets.

@rkowalewski
Last active December 7, 2016 10:25
Show Gist options
  • Select an option

  • Save rkowalewski/0dab54d869470d60d3c063d0c52a8410 to your computer and use it in GitHub Desktop.

Select an option

Save rkowalewski/0dab54d869470d60d3c063d0c52a8410 to your computer and use it in GitHub Desktop.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define COUNT 10
int main(int argc, char** argv)
{
int myrank, nprocs, nprocs_per_node;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
if (nprocs != 2)
{
if (myrank == 0) fprintf(stderr, "usage: the number of processes must be 2!\n");
MPI_Abort(MPI_COMM_WORLD, 1);
return EXIT_FAILURE;
}
MPI_Win win;
int *baseptr;
MPI_Aint local_size;
local_size = (myrank == 1) ? COUNT : 0;
MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &win);
MPI_Win_lock_all(0, win);
int type_size;
MPI_Type_size(MPI_INT, &type_size);
size_t nbytes = COUNT * type_size;
assert(MPI_Alloc_mem(nbytes, MPI_INFO_NULL, &baseptr) == MPI_SUCCESS);
assert(MPI_Win_attach(win, baseptr, nbytes) == MPI_SUCCESS);
MPI_Aint ldisp;
MPI_Aint *disps = malloc(nprocs * sizeof(MPI_Aint));
assert(MPI_Get_address(baseptr, &ldisp) == MPI_SUCCESS);
assert(MPI_Allgather(&ldisp, 1, MPI_AINT, disps, 1, MPI_AINT, MPI_COMM_WORLD) == MPI_SUCCESS);
if (myrank == 0)
{
for (size_t idx = 0; idx < COUNT; ++idx) {
baseptr[idx] = idx * COUNT + 1;
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (myrank == 1) {
assert(MPI_Get(baseptr, 10, MPI_INT, 0, disps[0], 10, MPI_INT, win) == MPI_SUCCESS);
assert(MPI_Win_flush(0, win) == MPI_SUCCESS);
for (int idx = 0; idx < COUNT; ++idx) {
assert(baseptr[idx] == idx * 10 + 1);
}
}
MPI_Win_unlock_all(win);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Win_detach(win, baseptr);
MPI_Win_free(&win);
//BUG: If this statement is not executed a segfault will occur during MPI_Finalize
//MPI_Free_mem(baseptr);
printf("Test finished\n");
MPI_Finalize();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment