Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Upgrade coll mpich testlist to new mpich
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / allgather_struct.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2015 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 #include <stdlib.h>
9 #include "mpi.h"
10 #include "mpitest.h"
11
12 /* Allgather a two-field struct datatype. This test
13    may trigger bugs such as when the implementation
14    does not handle well misaligned types.*/
15
16 typedef struct {
17     int first;
18     long second;
19 } int_long_t;
20
21 int main(int argc, char **argv)
22 {
23     MPI_Comm comm;
24     int minsize = 2;
25     int i, errs = 0;
26     int rank, size;
27     int_long_t object;
28     MPI_Datatype type;
29     MPI_Aint begin;
30     MPI_Aint displacements[2];
31     MPI_Datatype types[] = { MPI_INT, MPI_LONG };
32     int blocklength[2] = { 1, 1 };
33     int_long_t* gathered_objects;
34
35     MTest_Init(&argc, &argv);
36
37     while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
38
39         if (comm == MPI_COMM_NULL)
40             continue;
41         /* Determine the sender and receiver */
42         MPI_Comm_rank(comm, &rank);
43         MPI_Comm_size(comm, &size);
44
45         gathered_objects = (int_long_t*) malloc (size*sizeof(int_long_t));
46
47         /* Local object */
48         object.first = rank;
49         object.second = rank * 10;
50
51         /* Datatype creation */
52         MPI_Get_address(&object, &begin);
53         MPI_Get_address(&object.first, &displacements[0]);
54         MPI_Get_address(&object.second, &displacements[1]);
55
56         for (i = 0; i != 2; ++i)
57             displacements[i] -= begin;
58
59         MPI_Type_create_struct(2, &blocklength[0], &displacements[0], &types[0], &type);
60         MPI_Type_commit(&type);
61
62         MPI_Allgather(&object, 1, type, gathered_objects, 1, type, comm);
63
64         for (i = 0; i < size; i++) {
65             if (gathered_objects[i].first != i || gathered_objects[i].second != i * 10)
66                 errs++;
67         }
68
69         MPI_Type_free(&type);
70         MTestFreeComm(&comm);
71         free(gathered_objects);
72     }
73
74     MTest_Finalize(errs);
75     MPI_Finalize();
76     return 0;
77 }