Blame test/mpi/coll/allgather_struct.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 <stdlib.h>
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/* Allgather a two-field struct datatype. This test
Packit 0848f5
   may trigger bugs such as when the implementation
Packit 0848f5
   does not handle well misaligned types.*/
Packit 0848f5
Packit 0848f5
typedef struct {
Packit 0848f5
    int first;
Packit 0848f5
    long second;
Packit 0848f5
} int_long_t;
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    MPI_Comm comm;
Packit 0848f5
    int minsize = 2;
Packit 0848f5
    int i, errs = 0;
Packit 0848f5
    int rank, size;
Packit 0848f5
    int_long_t object;
Packit 0848f5
    MPI_Datatype type;
Packit 0848f5
    MPI_Aint begin;
Packit 0848f5
    MPI_Aint displacements[2];
Packit 0848f5
    MPI_Datatype types[] = { MPI_INT, MPI_LONG };
Packit 0848f5
    int blocklength[2] = { 1, 1 };
Packit 0848f5
    int_long_t* gathered_objects;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
Packit 0848f5
Packit 0848f5
        if (comm == MPI_COMM_NULL)
Packit 0848f5
            continue;
Packit 0848f5
        /* Determine the sender and receiver */
Packit 0848f5
        MPI_Comm_rank(comm, &rank;;
Packit 0848f5
        MPI_Comm_size(comm, &size);
Packit 0848f5
Packit 0848f5
        gathered_objects = (int_long_t*) malloc (size*sizeof(int_long_t));
Packit 0848f5
Packit 0848f5
        /* Local object */
Packit 0848f5
        object.first = rank;
Packit 0848f5
        object.second = rank * 10;
Packit 0848f5
Packit 0848f5
        /* Datatype creation */
Packit 0848f5
        MPI_Get_address(&object, &begin);
Packit 0848f5
        MPI_Get_address(&object.first, &displacements[0]);
Packit 0848f5
        MPI_Get_address(&object.second, &displacements[1]);
Packit 0848f5
Packit 0848f5
        for (i = 0; i != 2; ++i)
Packit 0848f5
            displacements[i] -= begin;
Packit 0848f5
Packit 0848f5
        MPI_Type_create_struct(2, &blocklength[0], &displacements[0], &types[0], &type);
Packit 0848f5
        MPI_Type_commit(&type);
Packit 0848f5
Packit 0848f5
        MPI_Allgather(&object, 1, type, gathered_objects, 1, type, comm);
Packit 0848f5
Packit 0848f5
        for (i = 0; i < size; i++) {
Packit 0848f5
            if (gathered_objects[i].first != i || gathered_objects[i].second != i * 10)
Packit 0848f5
                errs++;
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        MPI_Type_free(&type);
Packit 0848f5
        MTestFreeComm(&comm);
Packit 0848f5
        free(gathered_objects);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}