Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
still not our
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / pairtype-size-extent.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 #include "mpi.h"
8 #include <stdio.h>
9 #include "mpitestconf.h"
10 #ifdef HAVE_STRING_H
11 #include <string.h>
12 #endif
13
14 static int verbose = 1;
15
16
17
18 int parse_args(int argc, char **argv);
19
20 MPI_Aint pairtype_displacement(MPI_Datatype type, int *out_size_p);
21
22 MPI_Aint pairtype_displacement(MPI_Datatype type, int *out_size_p)
23 {
24     MPI_Aint disp;
25
26     /* Note that a portable test may not use a switch statement for 
27        datatypes, as they are not required to be compile-time constants */
28     if (type == MPI_FLOAT_INT) {
29         struct { float a; int b; } foo;
30         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
31         *out_size_p = sizeof(foo);
32     }
33     else if (type == MPI_DOUBLE_INT) {
34         struct { double a; int b; } foo;
35         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
36         *out_size_p = sizeof(foo);
37     }
38     else if (type == MPI_LONG_INT) {
39         struct { long a; int b; } foo;
40         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
41         *out_size_p = sizeof(foo);
42     }
43     else if (type == MPI_SHORT_INT) {
44         struct { short a; int b; } foo;
45         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
46         *out_size_p = sizeof(foo);
47     }
48     else if (type == MPI_LONG_DOUBLE_INT && type != MPI_DATATYPE_NULL) {
49         struct { long double a; int b; } foo;
50         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
51         *out_size_p = sizeof(foo);
52     }
53     else {
54         disp = -1;
55     }
56     return disp;
57 }
58
59 int main(int argc, char *argv[])
60 {
61
62 struct { MPI_Datatype atype, ptype; char name[32]; }
63 pairtypes[] =
64     { {MPI_FLOAT, MPI_FLOAT_INT, "MPI_FLOAT_INT"},
65       {MPI_DOUBLE, MPI_DOUBLE_INT, "MPI_DOUBLE_INT"},
66       {MPI_LONG, MPI_LONG_INT, "MPI_LONG_INT"},
67       {MPI_SHORT, MPI_SHORT_INT, "MPI_SHORT_INT"},
68       {MPI_LONG_DOUBLE, MPI_LONG_DOUBLE_INT, "MPI_LONG_DOUBLE_INT"},
69       {(MPI_Datatype) -1, (MPI_Datatype) -1, "end"}
70     };
71     int errs = 0;
72
73     int i;
74     int blks[2] = {1, 1};
75     MPI_Aint disps[2] = {0, 0};
76     MPI_Datatype types[2] = {MPI_INT, MPI_INT};
77     MPI_Datatype stype;
78     
79     MPI_Init(&argc, &argv);
80     parse_args(argc, argv);
81
82     for (i=0; pairtypes[i].atype != (MPI_Datatype) -1; i++) {
83         int atype_size, ptype_size, stype_size, handbuilt_extent;
84         MPI_Aint ptype_extent, stype_extent, dummy_lb;
85
86         types[0] = pairtypes[i].atype;
87
88         /* Check for undefined optional types, such as
89            LONG_DOUBLE_INT (if, for example, long double or
90            long long are not supported) */
91         if (types[0] == MPI_DATATYPE_NULL) continue;
92
93         MPI_Type_size(types[0], &atype_size);
94         disps[1] = pairtype_displacement(pairtypes[i].ptype,
95                                          &handbuilt_extent);
96
97         MPI_Type_create_struct(2, blks, disps, types, &stype);
98
99         MPI_Type_size(stype, &stype_size);
100         MPI_Type_size(pairtypes[i].ptype, &ptype_size);
101         if (stype_size != ptype_size) {
102             errs++;
103
104             if (verbose) fprintf(stderr,
105                                  "size of %s (%d) does not match size of hand-built MPI struct (%d)\n",
106                                  pairtypes[i].name, ptype_size, stype_size);
107         }
108
109         MPI_Type_get_extent(stype, &dummy_lb, &stype_extent);
110         MPI_Type_get_extent(pairtypes[i].ptype, &dummy_lb, &ptype_extent);
111         if (stype_extent != ptype_extent || stype_extent != handbuilt_extent) {
112             errs++;
113
114             if (verbose) fprintf(stderr,
115                                  "extent of %s (%d) does not match extent of either hand-built MPI struct (%d) or equivalent C struct (%d)\n",
116                                  pairtypes[i].name, (int) ptype_extent,
117                                  (int) stype_extent,
118                                  handbuilt_extent);
119         }
120         MPI_Type_free( &stype );
121     }
122     
123
124     /* print message and exit */
125     if (errs) {
126         fprintf(stderr, "Found %d errors\n", errs);
127     }
128     else {
129         printf(" No Errors\n");
130     }
131     MPI_Finalize();
132     return 0;
133 }
134
135 int parse_args(int argc, char **argv)
136 {
137     /* We use a simple test because getopt isn't universally available */
138     if (argc > 1 && strcmp(argv[1], "-v") == 0)
139         verbose = 1;
140     if (argc > 1 && strcmp(argv[1], "-nov") == 0)
141         verbose = 0;
142     return 0;
143 }