Blame test/mpi/rma/acc-loc.c

Packit 0848f5
/*
Packit 0848f5
 *  (C) 2006 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 *
Packit 0848f5
 *  Portions of this code were written by Intel Corporation.
Packit 0848f5
 *  Copyright (C) 2011-2012 Intel Corporation.  Intel provides this material
Packit 0848f5
 *  to Argonne National Laboratory subject to Software Grant and Corporate
Packit 0848f5
 *  Contributor License Agreement dated February 8, 2012.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#include <mpi.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
typedef struct {
Packit 0848f5
    int val;
Packit 0848f5
    int loc;
Packit 0848f5
} twoint_t;
Packit 0848f5
Packit 0848f5
static int errors = 0;
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    int me, nproc;
Packit 0848f5
    twoint_t *data = NULL;
Packit 0848f5
    twoint_t mine;
Packit 0848f5
    MPI_Win win;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &me);
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
Packit 0848f5
Packit 0848f5
    if (me == 0) {
Packit 0848f5
        MPI_Alloc_mem(sizeof(twoint_t), MPI_INFO_NULL, &data);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Win_create(data, me == 0 ? sizeof(twoint_t) : 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
Packit 0848f5
    MPI_Win_fence(MPI_MODE_NOPRECEDE, win);
Packit 0848f5
Packit 0848f5
    /* All processes perform MAXLOC and MINLOC operations on a 2INT on rank 0.
Packit 0848f5
     * The loc is the origin process' rank, and the value is (nproc-me).  In
Packit 0848f5
     * the case of MAXLOC, rank 0 should win and in the case of MINLOC, rank
Packit 0848f5
     * nproc-1 should win.
Packit 0848f5
     */
Packit 0848f5
Packit 0848f5
    /** Test MAXLOC **/
Packit 0848f5
Packit 0848f5
    if (me == 0) {
Packit 0848f5
        data->val = 0;
Packit 0848f5
        data->loc = -1;
Packit 0848f5
    }
Packit 0848f5
    MPI_Win_fence(0, win);
Packit 0848f5
Packit 0848f5
    mine.loc = me;
Packit 0848f5
    mine.val = nproc - me;
Packit 0848f5
    MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MAXLOC, win);
Packit 0848f5
    MPI_Win_fence(0, win);
Packit 0848f5
Packit 0848f5
    if (me == 0 && (data->loc != 0 || data->val != nproc)) {
Packit 0848f5
        errors++;
Packit 0848f5
        printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
Packit 0848f5
               0, nproc, data->loc, data->val);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /** Test MINLOC **/
Packit 0848f5
Packit 0848f5
    if (me == 0) {
Packit 0848f5
        data->val = nproc;
Packit 0848f5
        data->loc = -1;
Packit 0848f5
    }
Packit 0848f5
    MPI_Win_fence(0, win);
Packit 0848f5
Packit 0848f5
    mine.loc = me;
Packit 0848f5
    mine.val = nproc - me;
Packit 0848f5
    MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MINLOC, win);
Packit 0848f5
    MPI_Win_fence(0, win);
Packit 0848f5
Packit 0848f5
    if (me == 0 && (data->loc != nproc - 1 || data->val != 1)) {
Packit 0848f5
        errors++;
Packit 0848f5
        printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
Packit 0848f5
               nproc - 1, 1, data->loc, data->val);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* All processes perform MAXLOC and MINLOC operations on a 2INT on rank 0.
Packit 0848f5
     * The loc is the origin process' rank, and the value is 1.  In both cases,
Packit 0848f5
     * rank 0 should win because the values are equal and it has the lowest
Packit 0848f5
     * loc.
Packit 0848f5
     */
Packit 0848f5
Packit 0848f5
    /** Test MAXLOC **/
Packit 0848f5
Packit 0848f5
    if (me == 0) {
Packit 0848f5
        data->val = 0;
Packit 0848f5
        data->loc = -1;
Packit 0848f5
    }
Packit 0848f5
    MPI_Win_fence(MPI_MODE_NOSUCCEED, win);
Packit 0848f5
Packit 0848f5
    mine.loc = me;
Packit 0848f5
    mine.val = 1;
Packit 0848f5
Packit 0848f5
    MPI_Win_lock(MPI_LOCK_SHARED, 0, MPI_MODE_NOCHECK, win);
Packit 0848f5
    MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MAXLOC, win);
Packit 0848f5
    MPI_Win_unlock(0, win);
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    if (me == 0 && (data->loc != 0 || data->val != 1)) {
Packit 0848f5
        errors++;
Packit 0848f5
        printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
Packit 0848f5
               0, 1, data->loc, data->val);
Packit 0848f5
    }
Packit 0848f5
    MPI_Win_fence(MPI_MODE_NOPRECEDE, win);
Packit 0848f5
Packit 0848f5
    /** Test MINLOC **/
Packit 0848f5
Packit 0848f5
    if (me == 0) {
Packit 0848f5
        data->val = nproc;
Packit 0848f5
        data->loc = -1;
Packit 0848f5
    }
Packit 0848f5
    MPI_Win_fence(MPI_MODE_NOSUCCEED, win);
Packit 0848f5
Packit 0848f5
    mine.loc = me;
Packit 0848f5
    mine.val = 1;
Packit 0848f5
Packit 0848f5
    MPI_Win_lock(MPI_LOCK_SHARED, 0, MPI_MODE_NOCHECK, win);
Packit 0848f5
    MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MINLOC, win);
Packit 0848f5
    MPI_Win_unlock(0, win);
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    if (me == 0 && (data->loc != 0 || data->val != 1)) {
Packit 0848f5
        errors++;
Packit 0848f5
        printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
Packit 0848f5
               0, 1, data->loc, data->val);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Win_free(&win);
Packit 0848f5
Packit 0848f5
    if (me == 0) {
Packit 0848f5
        MPI_Free_mem(data);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errors);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}