Blame test/mpi/rma/aint.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2014 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
 * This program tests MPI_Aint_add/diff in MPI-3.1.
Packit 0848f5
 * The two functions are often used in RMA code.
Packit 0848f5
 * See https://svn.mpi-forum.org/trac/mpi-forum-web/ticket/349
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <mpi.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    int rank, nproc;
Packit 0848f5
    int errs = 0;
Packit 0848f5
    int array[1024];
Packit 0848f5
    int val = 0;
Packit 0848f5
    int target_rank;
Packit 0848f5
    MPI_Aint bases[2];
Packit 0848f5
    MPI_Aint disp, offset;
Packit 0848f5
    MPI_Win win;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
Packit 0848f5
Packit 0848f5
    if (rank == 0 && nproc != 2) {
Packit 0848f5
        MTestError("Must run with 2 ranks\n");
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* Get the base address in the middle of the array */
Packit 0848f5
    if (rank == 0) {
Packit 0848f5
        target_rank = 1;
Packit 0848f5
        array[0] = 1234;
Packit 0848f5
        MPI_Get_address(&array[512], &bases[0]);
Packit 0848f5
    }
Packit 0848f5
    else if (rank == 1) {
Packit 0848f5
        target_rank = 0;
Packit 0848f5
        array[1023] = 1234;
Packit 0848f5
        MPI_Get_address(&array[512], &bases[1]);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* Exchange bases */
Packit 0848f5
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, bases, 1, MPI_AINT, MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &win);
Packit 0848f5
    MPI_Win_attach(win, array, sizeof(int) * 1024);
Packit 0848f5
Packit 0848f5
    /* Do MPI_Aint addressing arithmetic */
Packit 0848f5
    if (rank == 0) {
Packit 0848f5
        disp = sizeof(int) * 511;
Packit 0848f5
        offset = MPI_Aint_add(bases[1], disp);  /* offset points to array[1023] */
Packit 0848f5
    }
Packit 0848f5
    else if (rank == 1) {
Packit 0848f5
        disp = sizeof(int) * 512;
Packit 0848f5
        offset = MPI_Aint_diff(bases[0], disp); /* offset points to array[0] */
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* Get val and verify it */
Packit 0848f5
    MPI_Win_fence(MPI_MODE_NOPRECEDE, win);
Packit 0848f5
    MPI_Get(&val, 1, MPI_INT, target_rank, offset, 1, MPI_INT, win);
Packit 0848f5
    MPI_Win_fence(MPI_MODE_NOSUCCEED, win);
Packit 0848f5
Packit 0848f5
    if (val != 1234) {
Packit 0848f5
        errs++;
Packit 0848f5
        printf("%d -- Got %d, expected 1234\n", rank, val);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Win_detach(win, array);
Packit 0848f5
    MPI_Win_free(&win);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}