Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #193 from Takishipp/signals
[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, &recvbuf, 1, contig, 0, 0, MPI_COMM_SELF, &status);
62
63     /* sanity */
64     assert(sendbuf.a == recvbuf.a);
65     assert(sendbuf.b == recvbuf.b);
66     assert(sendbuf.c == recvbuf.c);
67
68     /* now check that MPI_Get_elements returns the correct answer and that the
69      * library doesn't explode in the process */
70     count = 0xdeadbeef;
71     MPI_Get_elements(&status, contig, &count);
72     MPI_Type_free(&struct_type);
73     MPI_Type_free(&contig);
74
75     if (count != 3) {
76         printf("unexpected value for count, expected 3, got %d\n", count);
77     }
78     else {
79         if (rank == 0) {
80             printf(" No Errors\n");
81         }
82     }
83
84     MPI_Finalize();
85     return 0;
86 }