Blame test/mpi/rma/wrma_flush_get.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2015 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
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
#define ITER 10000
Packit 0848f5
#define BUF_CNT 1
Packit 0848f5
int local_buf[BUF_CNT], result_addr[BUF_CNT], check_addr[BUF_CNT];
Packit 0848f5
#ifdef TEST_CAS
Packit 0848f5
int compare_buf[BUF_CNT];
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
const int verbose = 0;
Packit 0848f5
Packit 0848f5
/* This test checks the remote completion of flush with RMA write-like operations
Packit 0848f5
 * (PUT, ACC, GET_ACC, FOP, CAS), and confirms result by GET issued from another
Packit 0848f5
 * process.
Packit 0848f5
 * 1. Three processes create window by Win_allocate.
Packit 0848f5
 * 2. P(origin) issues RMA operations and flush to P(target), then call
Packit 0848f5
 *    send-recv to synchronize with P(checker).
Packit 0848f5
 * 3. P(checker) then issues get to P(target) to check the results.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
int rank = -1, nproc = 0;
Packit 0848f5
int origin = -1, target = -1, checker = -1;
Packit 0848f5
MPI_Win win = MPI_WIN_NULL;
Packit 0848f5
int *my_base = NULL;
Packit 0848f5
Packit 0848f5
/* Define operation name for error message */
Packit 0848f5
#ifdef TEST_PUT
Packit 0848f5
const char *rma_name = "Put";
Packit 0848f5
#elif defined(TEST_ACC)
Packit 0848f5
const char *rma_name = "Accumulate";
Packit 0848f5
#elif defined(TEST_GACC)
Packit 0848f5
const char *rma_name = "Get_accumulate";
Packit 0848f5
#elif defined(TEST_FOP)
Packit 0848f5
const char *rma_name = "Fetch_and_op";
Packit 0848f5
#elif defined(TEST_CAS)
Packit 0848f5
const char *rma_name = "Compare_and_swap";
Packit 0848f5
#else
Packit 0848f5
const char *rma_name = "None";
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
/* Issue functions for different RMA operations */
Packit 0848f5
#ifdef TEST_PUT
Packit 0848f5
static inline void issue_rma_op(int i)
Packit 0848f5
{
Packit 0848f5
    MPI_Put(&local_buf[i], 1, MPI_INT, target, i, 1, MPI_INT, win);
Packit 0848f5
}
Packit 0848f5
#elif defined(TEST_ACC)
Packit 0848f5
static inline void issue_rma_op(int i)
Packit 0848f5
{
Packit 0848f5
    MPI_Accumulate(&local_buf[i], 1, MPI_INT, target, i, 1, MPI_INT, MPI_REPLACE, win);
Packit 0848f5
}
Packit 0848f5
#elif defined(TEST_GACC)
Packit 0848f5
static inline void issue_rma_op(int i)
Packit 0848f5
{
Packit 0848f5
    MPI_Get_accumulate(&local_buf[i], 1, MPI_INT, &result_addr[i], 1, MPI_INT, target, i,
Packit 0848f5
                       1, MPI_INT, MPI_REPLACE, win);
Packit 0848f5
}
Packit 0848f5
#elif defined(TEST_FOP)
Packit 0848f5
static inline void issue_rma_op(int i)
Packit 0848f5
{
Packit 0848f5
    MPI_Fetch_and_op(&local_buf[i], &result_addr[i], MPI_INT, target, i, MPI_REPLACE, win);
Packit 0848f5
}
Packit 0848f5
#elif defined(TEST_CAS)
Packit 0848f5
static inline void issue_rma_op(int i)
Packit 0848f5
{
Packit 0848f5
    compare_buf[i] = i;        /* always equal to window value, thus swap happens */
Packit 0848f5
    MPI_Compare_and_swap(&local_buf[i], &compare_buf[i], &result_addr[i], MPI_INT, target, i, win);
Packit 0848f5
}
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
Packit 0848f5
/* Local check function for GET-like operations */
Packit 0848f5
#if defined(TEST_GACC) || defined(TEST_FOP) || defined(TEST_CAS)
Packit 0848f5
Packit 0848f5
/* Check local result buffer for GET-like operations */
Packit 0848f5
static int check_local_result(int iter)
Packit 0848f5
{
Packit 0848f5
    int i = 0;
Packit 0848f5
    int errors = 0;
Packit 0848f5
Packit 0848f5
    for (i = 0; i < BUF_CNT; i++) {
Packit 0848f5
        if (result_addr[i] != i) {
Packit 0848f5
            printf("rank %d (iter %d) - check %s, got result_addr[%d] = %d, expected %d\n",
Packit 0848f5
                   rank, iter, rma_name, i, result_addr[i], i);
Packit 0848f5
            errors++;
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    return errors;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
#else
Packit 0848f5
#define check_local_result(iter) (0)
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
static int run_test()
Packit 0848f5
{
Packit 0848f5
    int i = 0, x = 0;
Packit 0848f5
    int errors = 0;
Packit 0848f5
    int sbuf = 0, rbuf = 0;
Packit 0848f5
    MPI_Status stat;
Packit 0848f5
Packit 0848f5
    for (x = 0; x < ITER; x++) {
Packit 0848f5
        /* 1. Target resets window data */
Packit 0848f5
        if (rank == target) {
Packit 0848f5
            for (i = 0; i < BUF_CNT; i++)
Packit 0848f5
                my_base[i] = i;
Packit 0848f5
            MPI_Win_sync(win);  /* write is done on window */
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
        /* 2. Every one resets local data */
Packit 0848f5
        for (i = 0; i < BUF_CNT; i++) {
Packit 0848f5
            local_buf[i] = BUF_CNT + x * BUF_CNT + i;
Packit 0848f5
            result_addr[i] = 0;
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* 3. Origin issues RMA operation to target */
Packit 0848f5
        if (rank == origin) {
Packit 0848f5
            /* 3-1. Issue RMA. */
Packit 0848f5
            for (i = 0; i < BUF_CNT; i++) {
Packit 0848f5
                issue_rma_op(i);
Packit 0848f5
            }
Packit 0848f5
            MPI_Win_flush(target, win);
Packit 0848f5
Packit 0848f5
            /* 3-2. Sync with checker */
Packit 0848f5
            MPI_Send(&sbuf, 1, MPI_INT, checker, 999, MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
            /* 3-3. Check local result buffer */
Packit 0848f5
            errors += check_local_result(x);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* 4. Checker issues GET to target and checks result */
Packit 0848f5
        if (rank == checker) {
Packit 0848f5
            /* 4-1. Sync with origin */
Packit 0848f5
            MPI_Recv(&rbuf, 1, MPI_INT, origin, 999, MPI_COMM_WORLD, &stat;;
Packit 0848f5
Packit 0848f5
            /* 4-2. Get result and check */
Packit 0848f5
            MPI_Get(check_addr, BUF_CNT, MPI_INT, target, 0, BUF_CNT, MPI_INT, win);
Packit 0848f5
            MPI_Win_flush(target, win);
Packit 0848f5
Packit 0848f5
            for (i = 0; i < BUF_CNT; i++) {
Packit 0848f5
                if (check_addr[i] != local_buf[i]) {
Packit 0848f5
                    printf("rank %d (iter %d) - check %s, got check_addr[%d] = %d, expected %d\n",
Packit 0848f5
                           rank, x, rma_name, i, check_addr[i], local_buf[i]);
Packit 0848f5
                    errors++;
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    return errors;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int errors = 0, all_errors = 0;
Packit 0848f5
    int win_size = sizeof(int) * BUF_CNT;
Packit 0848f5
    int win_unit = sizeof(int);
Packit 0848f5
Packit 0848f5
    MPI_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
Packit 0848f5
Packit 0848f5
    if (nproc != 3) {
Packit 0848f5
        if (rank == 0)
Packit 0848f5
            printf("Error: must be run with three processes\n");
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
        MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
#if !defined(TEST_PUT) && !defined(TEST_ACC) && !defined(TEST_GACC) && !defined(TEST_FOP) && !defined(TEST_CAS)
Packit 0848f5
    if (rank == 0)
Packit 0848f5
        printf("Error: must specify operation type at compile time\n");
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
    /* Identify origin, target and checker ranks. */
Packit 0848f5
    target = 0;
Packit 0848f5
    checker = 2;
Packit 0848f5
    origin = 1;
Packit 0848f5
Packit 0848f5
    MPI_Win_allocate(win_size, win_unit, MPI_INFO_NULL, MPI_COMM_WORLD, &my_base, &win);
Packit 0848f5
Packit 0848f5
    /* Start checking. */
Packit 0848f5
    MPI_Win_lock_all(0, win);
Packit 0848f5
Packit 0848f5
    errors = run_test();
Packit 0848f5
Packit 0848f5
    MPI_Win_unlock_all(win);
Packit 0848f5
Packit 0848f5
    MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
Packit 0848f5
    if (rank == 0 && all_errors == 0)
Packit 0848f5
        printf(" No Errors\n");
Packit 0848f5
Packit 0848f5
    if (win != MPI_WIN_NULL)
Packit 0848f5
        MPI_Win_free(&win);
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}