Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix build and dist, add missing folder
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / get-elements.c
1 /* -*- Mode: c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2011 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stddef.h>
10 #include <assert.h>
11
12 /* Tests MPI_Get_elements with a contiguous datatype that triggered a bug in
13  * past versions of MPICH.  See ticket #1467 for more info. */
14
15 struct test_struct {
16     char a;
17     short b;
18     int c;
19 };
20
21 int main(int argc, char **argv)
22 {
23     int rank, count;
24     struct test_struct sendbuf, recvbuf;
25     int blens[3];
26     MPI_Aint displs[3];
27     MPI_Datatype types[3];
28     MPI_Datatype struct_type, contig;
29     MPI_Status status;
30
31     MPI_Init(&argc, &argv);
32     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
33
34     /* We use a contig of a struct in order to satisfy two properties:
35      * (A) a type that contains more than one element type (the struct portion)
36      * (B) a type that has an odd number of ints in its "type contents" (1 in
37      *     this case)
38      * This triggers a specific bug in some versions of MPICH. */
39     blens[0]  = 1;
40     displs[0] = offsetof(struct test_struct, a);
41     types[0]  = MPI_CHAR;
42     blens[1]  = 1;
43     displs[1] = offsetof(struct test_struct, b);
44     types[1]  = MPI_SHORT;
45     blens[2]  = 1;
46     displs[2] = offsetof(struct test_struct, c);
47     types[2]  = MPI_INT;
48     MPI_Type_create_struct(3, blens, displs, types, &struct_type);
49     MPI_Type_contiguous(1, struct_type, &contig);
50     MPI_Type_commit(&struct_type);
51     MPI_Type_commit(&contig);
52
53     sendbuf.a = 20;
54     sendbuf.b = 30;
55     sendbuf.c = 40;
56     recvbuf.a = -1;
57     recvbuf.b = -1;
58     recvbuf.c = -1;
59
60     /* send to ourself */
61     MPI_Sendrecv(&sendbuf, 1, contig, 0, 0,
62                  &recvbuf, 1, contig, 0, 0,
63                  MPI_COMM_SELF, &status);
64
65     /* sanity */
66     assert(sendbuf.a == recvbuf.a);
67     assert(sendbuf.b == recvbuf.b);
68     assert(sendbuf.c == recvbuf.c);
69
70     /* now check that MPI_Get_elements returns the correct answer and that the
71      * library doesn't explode in the process */
72     count = 0xdeadbeef;
73     MPI_Get_elements(&status, contig, &count);
74     MPI_Type_free(&struct_type);
75     MPI_Type_free(&contig);
76
77     if (count != 3) {
78         printf("unexpected value for count, expected 3, got %d\n", count);
79     }
80     else {
81         if (rank == 0) {
82             printf(" No Errors\n");
83         }
84     }
85
86     MPI_Finalize();
87     return 0;
88 }
89