Blame test/mpi/rma/lockopts.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2012 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "stdio.h"
Packit 0848f5
#include "stdlib.h"
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/* tests passive target RMA on 2 processes. tests the lock-single_op-unlock
Packit 0848f5
   optimization for less common cases:
Packit 0848f5
Packit 0848f5
   origin datatype derived, target datatype predefined
Packit 0848f5
Packit 0848f5
*/
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int wrank, nprocs, *srcbuf, *rmabuf, i;
Packit 0848f5
    int memsize;
Packit 0848f5
    MPI_Datatype vectype;
Packit 0848f5
    MPI_Win win;
Packit 0848f5
    int errs = 0;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
Packit 0848f5
Packit 0848f5
    if (nprocs < 2) {
Packit 0848f5
        printf("Run this program with 2 or more processes\n");
Packit 0848f5
        MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    memsize = 10 * 4 * nprocs;
Packit 0848f5
    /* Create and initialize data areas */
Packit 0848f5
    srcbuf = (int *) malloc(sizeof(int) * memsize);
Packit 0848f5
    MPI_Alloc_mem(sizeof(int) * memsize, MPI_INFO_NULL, &rmabuf);
Packit 0848f5
    if (!srcbuf || !rmabuf) {
Packit 0848f5
        printf("Unable to allocate srcbuf and rmabuf of size %d\n", memsize);
Packit 0848f5
        MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
    }
Packit 0848f5
    for (i = 0; i < memsize; i++) {
Packit 0848f5
        rmabuf[i] = -i;
Packit 0848f5
        srcbuf[i] = i;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Win_create(rmabuf, memsize * sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win);
Packit 0848f5
Packit 0848f5
    /* Vector of 10 elements, separated by 4 */
Packit 0848f5
    MPI_Type_vector(10, 1, 4, MPI_INT, &vectype);
Packit 0848f5
    MPI_Type_commit(&vectype);
Packit 0848f5
Packit 0848f5
    /* Accumulate with a derived origin type and target predefined type */
Packit 0848f5
    if (wrank == 0) {
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
Packit 0848f5
        for (i = 0; i < 10; i++) {
Packit 0848f5
            if (rmabuf[i] != -i + 4 * i) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Acc: expected rmabuf[%d] = %d but saw %d\n", i, -i + 4 * i, rmabuf[i]);
Packit 0848f5
            }
Packit 0848f5
            rmabuf[i] = -i;
Packit 0848f5
        }
Packit 0848f5
        for (i = 10; i < memsize; i++) {
Packit 0848f5
            if (rmabuf[i] != -i) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Acc: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
Packit 0848f5
                rmabuf[i] = -i;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        MPI_Win_unlock(0, win);
Packit 0848f5
    }
Packit 0848f5
    else if (wrank == 1) {
Packit 0848f5
        MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
Packit 0848f5
        MPI_Accumulate(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, MPI_SUM, win);
Packit 0848f5
        MPI_Win_unlock(0, win);
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    /* Put with a derived origin type and target predefined type */
Packit 0848f5
    if (wrank == 0) {
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
Packit 0848f5
        for (i = 0; i < 10; i++) {
Packit 0848f5
            if (rmabuf[i] != 4 * i) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
Packit 0848f5
            }
Packit 0848f5
            rmabuf[i] = -i;
Packit 0848f5
        }
Packit 0848f5
        for (i = 10; i < memsize; i++) {
Packit 0848f5
            if (rmabuf[i] != -i) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
Packit 0848f5
                rmabuf[i] = -i;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        MPI_Win_unlock(0, win);
Packit 0848f5
    }
Packit 0848f5
    else if (wrank == 1) {
Packit 0848f5
        MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
Packit 0848f5
        MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
Packit 0848f5
        MPI_Win_unlock(0, win);
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    /* Put with a derived origin type and target predefined type, with
Packit 0848f5
     * a get (see the move-to-end optimization) */
Packit 0848f5
    if (wrank == 0) {
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
Packit 0848f5
        for (i = 0; i < 10; i++) {
Packit 0848f5
            if (rmabuf[i] != 4 * i) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
Packit 0848f5
            }
Packit 0848f5
            rmabuf[i] = -i;
Packit 0848f5
        }
Packit 0848f5
        for (i = 10; i < memsize; i++) {
Packit 0848f5
            if (rmabuf[i] != -i) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
Packit 0848f5
                rmabuf[i] = -i;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        MPI_Win_unlock(0, win);
Packit 0848f5
    }
Packit 0848f5
    else if (wrank == 1) {
Packit 0848f5
        int val;
Packit 0848f5
        MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
Packit 0848f5
        MPI_Get(&val, 1, MPI_INT, 0, 10, 1, MPI_INT, win);
Packit 0848f5
        MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
Packit 0848f5
        MPI_Win_unlock(0, win);
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
        if (val != -10) {
Packit 0848f5
            errs++;
Packit 0848f5
            printf("Get: Expected -10, got %d\n", val);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    /* Put with a derived origin type and target predefined type, with
Packit 0848f5
     * a get already at the end (see the move-to-end optimization) */
Packit 0848f5
    if (wrank == 0) {
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
Packit 0848f5
        for (i = 0; i < 10; i++) {
Packit 0848f5
            if (rmabuf[i] != 4 * i) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
Packit 0848f5
            }
Packit 0848f5
            rmabuf[i] = -i;
Packit 0848f5
        }
Packit 0848f5
        for (i = 10; i < memsize; i++) {
Packit 0848f5
            if (rmabuf[i] != -i) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
Packit 0848f5
                rmabuf[i] = -i;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        MPI_Win_unlock(0, win);
Packit 0848f5
    }
Packit 0848f5
    else if (wrank == 1) {
Packit 0848f5
        int val;
Packit 0848f5
        MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
Packit 0848f5
        MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
Packit 0848f5
        MPI_Get(&val, 1, MPI_INT, 0, 10, 1, MPI_INT, win);
Packit 0848f5
        MPI_Win_unlock(0, win);
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
        if (val != -10) {
Packit 0848f5
            errs++;
Packit 0848f5
            printf("Get: Expected -10, got %d\n", val);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Win_free(&win);
Packit 0848f5
    MPI_Free_mem(rmabuf);
Packit 0848f5
    free(srcbuf);
Packit 0848f5
    MPI_Type_free(&vectype);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}