Blame test/mpi/rma/get-struct.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2014 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <string.h>
Packit 0848f5
Packit 0848f5
/* Communicating a datatype built out of structs
Packit 0848f5
 * This test was motivated by the failure of an example program for
Packit 0848f5
 * RMA involving simple operations on a struct that included a struct
Packit 0848f5
 *
Packit 0848f5
 * The observed failure was a SEGV in the MPI_Get
Packit 0848f5
 *
Packit 0848f5
 *
Packit 0848f5
 */
Packit 0848f5
#define MAX_KEY_SIZE 64
Packit 0848f5
#define MAX_VALUE_SIZE 256
Packit 0848f5
typedef struct {
Packit 0848f5
    MPI_Aint disp;
Packit 0848f5
    int rank;
Packit 0848f5
    void *lptr;
Packit 0848f5
} Rptr;
Packit 0848f5
typedef struct {
Packit 0848f5
    Rptr next;
Packit 0848f5
    char key[MAX_KEY_SIZE], value[MAX_VALUE_SIZE];
Packit 0848f5
} ListElm;
Packit 0848f5
Rptr nullDptr = { 0, -1, 0 };
Packit 0848f5
Packit 0848f5
int testCases = -1;
Packit 0848f5
#define BYTE_ONLY 0x1
Packit 0848f5
#define TWO_STRUCT 0x2
Packit 0848f5
int isOneLevel = 0;
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    int errors = 0;
Packit 0848f5
    Rptr headDptr;
Packit 0848f5
    ListElm *headLptr = 0;
Packit 0848f5
    int i, wrank;
Packit 0848f5
    MPI_Datatype dptrType, listelmType;
Packit 0848f5
    MPI_Win listwin;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
Packit 0848f5
Packit 0848f5
    for (i = 1; i < argc; i++) {
Packit 0848f5
        if (strcmp(argv[i], "-byteonly") == 0) {
Packit 0848f5
            testCases = BYTE_ONLY;
Packit 0848f5
        }
Packit 0848f5
        else if (strcmp(argv[i], "-twostruct") == 0) {
Packit 0848f5
            testCases = TWO_STRUCT;
Packit 0848f5
        }
Packit 0848f5
        else if (strcmp(argv[i], "-onelevel") == 0) {
Packit 0848f5
            isOneLevel = 1;
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            printf("Unrecognized argument %s\n", argv[i]);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* Create the datatypes that we will use to move the data */
Packit 0848f5
    {
Packit 0848f5
        int blens[3];
Packit 0848f5
        MPI_Aint displ[3];
Packit 0848f5
        MPI_Datatype dtypes[3];
Packit 0848f5
        ListElm sampleElm;
Packit 0848f5
Packit 0848f5
        blens[0] = 1;
Packit 0848f5
        blens[1] = 1;
Packit 0848f5
        MPI_Get_address(&nullDptr.disp, &displ[0]);
Packit 0848f5
        MPI_Get_address(&nullDptr.rank, &displ[1]);
Packit 0848f5
        displ[1] = displ[1] - displ[0];
Packit 0848f5
        displ[0] = 0;
Packit 0848f5
        dtypes[0] = MPI_AINT;
Packit 0848f5
        dtypes[1] = MPI_INT;
Packit 0848f5
        MPI_Type_create_struct(2, blens, displ, dtypes, &dptrType);
Packit 0848f5
        MPI_Type_commit(&dptrType);
Packit 0848f5
Packit 0848f5
        if (isOneLevel) {
Packit 0848f5
            blens[0] = sizeof(nullDptr);
Packit 0848f5
            dtypes[0] = MPI_BYTE;
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            blens[0] = 1;
Packit 0848f5
            dtypes[0] = dptrType;
Packit 0848f5
        }
Packit 0848f5
        blens[1] = MAX_KEY_SIZE;
Packit 0848f5
        dtypes[1] = MPI_CHAR;
Packit 0848f5
        blens[2] = MAX_VALUE_SIZE;
Packit 0848f5
        dtypes[2] = MPI_CHAR;
Packit 0848f5
        MPI_Get_address(&sampleElm.next, &displ[0]);
Packit 0848f5
        MPI_Get_address(&sampleElm.key[0], &displ[1]);
Packit 0848f5
        MPI_Get_address(&sampleElm.value[0], &displ[2]);
Packit 0848f5
        displ[2] -= displ[0];
Packit 0848f5
        displ[1] -= displ[0];
Packit 0848f5
        displ[0] = 0;
Packit 0848f5
        for (i = 0; i < 3; i++) {
Packit 0848f5
            MTestPrintfMsg(0, "%d:count=%d,disp=%ld\n", i, blens[i], displ[i]);
Packit 0848f5
        }
Packit 0848f5
        MPI_Type_create_struct(3, blens, displ, dtypes, &listelmType);
Packit 0848f5
        MPI_Type_commit(&listelmType);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &listwin);
Packit 0848f5
Packit 0848f5
    headDptr.rank = 0;
Packit 0848f5
    if (wrank == 0) {
Packit 0848f5
        /* Create 1 list element (the head) and initialize it */
Packit 0848f5
        MPI_Alloc_mem(sizeof(ListElm), MPI_INFO_NULL, &headLptr);
Packit 0848f5
        MPI_Get_address(headLptr, &headDptr.disp);
Packit 0848f5
        headLptr->next.rank = -1;
Packit 0848f5
        headLptr->next.disp = (MPI_Aint) MPI_BOTTOM;
Packit 0848f5
        headLptr->next.lptr = 0;
Packit 0848f5
        strncpy(headLptr->key, "key1", MAX_KEY_SIZE);
Packit 0848f5
        strncpy(headLptr->value, "value1", MAX_VALUE_SIZE);
Packit 0848f5
        MPI_Win_attach(listwin, headLptr, sizeof(ListElm));
Packit 0848f5
    }
Packit 0848f5
    MPI_Bcast(&headDptr.disp, 1, MPI_AINT, 0, MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    if (wrank == 1) {
Packit 0848f5
        ListElm headcopy;
Packit 0848f5
Packit 0848f5
        MPI_Win_lock_all(0, listwin);
Packit 0848f5
        /* Get head element with simple get of BYTES */
Packit 0848f5
        if (testCases & BYTE_ONLY) {
Packit 0848f5
            headcopy.next.rank = 100;
Packit 0848f5
            headcopy.next.disp = 0xefefefef;
Packit 0848f5
            MPI_Get(&headcopy, sizeof(ListElm), MPI_BYTE,
Packit 0848f5
                    headDptr.rank, headDptr.disp, sizeof(ListElm), MPI_BYTE, listwin);
Packit 0848f5
            MPI_Win_flush(headDptr.rank, listwin);
Packit 0848f5
            if (headcopy.next.rank != -1 && headcopy.next.disp != (MPI_Aint) MPI_BOTTOM) {
Packit 0848f5
                errors++;
Packit 0848f5
                printf("MPI_BYTE: headcopy contains incorrect next:<%d,%ld>\n",
Packit 0848f5
                       headcopy.next.rank, (long) headcopy.next.disp);
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        if (testCases & TWO_STRUCT) {
Packit 0848f5
            headcopy.next.rank = 100;
Packit 0848f5
            headcopy.next.disp = 0xefefefef;
Packit 0848f5
            /* Get head element using struct of struct type.  This is
Packit 0848f5
             * not an identical get to the simple BYTE one above but should
Packit 0848f5
             * work */
Packit 0848f5
            MPI_Get(&headcopy, 1, listelmType, headDptr.rank, headDptr.disp,
Packit 0848f5
                    1, listelmType, listwin);
Packit 0848f5
            MPI_Win_flush(headDptr.rank, listwin);
Packit 0848f5
            if (headcopy.next.rank != -1 && headcopy.next.disp != (MPI_Aint) MPI_BOTTOM) {
Packit 0848f5
                errors++;
Packit 0848f5
                printf("ListelmType: headcopy contains incorrect next:<%d,%ld>\n",
Packit 0848f5
                       headcopy.next.rank, (long) headcopy.next.disp);
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        MPI_Win_unlock_all(listwin);
Packit 0848f5
    }
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    if (wrank == 0) {
Packit 0848f5
        MPI_Win_detach(listwin, headLptr);
Packit 0848f5
        MPI_Free_mem(headLptr);
Packit 0848f5
    }
Packit 0848f5
    MPI_Win_free(&listwin);
Packit 0848f5
    MPI_Type_free(&dptrType);
Packit 0848f5
    MPI_Type_free(&listelmType);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errors);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return MTestReturnValue(errors);
Packit 0848f5
}