Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[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 static struct {
17     MPI_Datatype atype, ptype;
18     char name[32];
19 } pairtypes[] = { {
20 MPI_FLOAT, MPI_FLOAT_INT, "MPI_FLOAT_INT"}, {
21 MPI_DOUBLE, MPI_DOUBLE_INT, "MPI_DOUBLE_INT"}, {
22 MPI_LONG, MPI_LONG_INT, "MPI_LONG_INT"}, {
23 MPI_SHORT, MPI_SHORT_INT, "MPI_SHORT_INT"}, {
24 MPI_LONG_DOUBLE, MPI_LONG_DOUBLE_INT, "MPI_LONG_DOUBLE_INT"}, {
25 (MPI_Datatype) - 1, (MPI_Datatype) - 1, "end"}
26 };
27
28 int parse_args(int argc, char **argv);
29
30 MPI_Aint pairtype_displacement(MPI_Datatype type, int *out_size_p);
31
32 MPI_Aint pairtype_displacement(MPI_Datatype type, int *out_size_p)
33 {
34     MPI_Aint disp;
35
36     /* Note that a portable test may not use a switch statement for
37      * datatypes, as they are not required to be compile-time constants */
38     if (type == MPI_FLOAT_INT) {
39         struct {
40             float a;
41             int b;
42         } foo;
43         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
44         *out_size_p = sizeof(foo);
45     }
46     else if (type == MPI_DOUBLE_INT) {
47         struct {
48             double a;
49             int b;
50         } foo;
51         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
52         *out_size_p = sizeof(foo);
53     }
54     else if (type == MPI_LONG_INT) {
55         struct {
56             long a;
57             int b;
58         } foo;
59         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
60         *out_size_p = sizeof(foo);
61     }
62     else if (type == MPI_SHORT_INT) {
63         struct {
64             short a;
65             int b;
66         } foo;
67         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
68         *out_size_p = sizeof(foo);
69     }
70     else if (type == MPI_LONG_DOUBLE_INT && type != MPI_DATATYPE_NULL) {
71         struct {
72             long double a;
73             int b;
74         } foo;
75         disp = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
76         *out_size_p = sizeof(foo);
77     }
78     else {
79         disp = -1;
80     }
81     return disp;
82 }
83
84 int main(int argc, char *argv[])
85 {
86     int errs = 0;
87
88     int i;
89     int blks[2] = { 1, 1 };
90     MPI_Aint disps[2] = { 0, 0 };
91     MPI_Datatype types[2] = { MPI_INT, MPI_INT };
92     MPI_Datatype stype;
93
94     MPI_Init(&argc, &argv);
95     parse_args(argc, argv);
96
97     for (i = 0; pairtypes[i].atype != (MPI_Datatype) - 1; i++) {
98         int atype_size, ptype_size, stype_size, handbuilt_extent;
99         MPI_Aint ptype_extent, stype_extent, dummy_lb;
100
101         types[0] = pairtypes[i].atype;
102
103         /* Check for undefined optional types, such as
104          * LONG_DOUBLE_INT (if, for example, long double or
105          * long long are not supported) */
106         if (types[0] == MPI_DATATYPE_NULL)
107             continue;
108
109         MPI_Type_size(types[0], &atype_size);
110         disps[1] = pairtype_displacement(pairtypes[i].ptype, &handbuilt_extent);
111
112         MPI_Type_create_struct(2, blks, disps, types, &stype);
113
114         MPI_Type_size(stype, &stype_size);
115         MPI_Type_size(pairtypes[i].ptype, &ptype_size);
116         if (stype_size != ptype_size) {
117             errs++;
118
119             if (verbose)
120                 fprintf(stderr,
121                         "size of %s (%d) does not match size of hand-built MPI struct (%d)\n",
122                         pairtypes[i].name, ptype_size, stype_size);
123         }
124
125         MPI_Type_get_extent(stype, &dummy_lb, &stype_extent);
126         MPI_Type_get_extent(pairtypes[i].ptype, &dummy_lb, &ptype_extent);
127         if (stype_extent != ptype_extent || stype_extent != handbuilt_extent) {
128             errs++;
129
130             if (verbose)
131                 fprintf(stderr,
132                         "extent of %s (%d) does not match extent of either hand-built MPI struct (%d) or equivalent C struct (%d)\n",
133                         pairtypes[i].name, (int) ptype_extent,
134                         (int) stype_extent, handbuilt_extent);
135         }
136         MPI_Type_free(&stype);
137     }
138
139
140     /* print message and exit */
141     if (errs) {
142         fprintf(stderr, "Found %d errors\n", errs);
143     }
144     else {
145         printf(" No Errors\n");
146     }
147     MPI_Finalize();
148     return 0;
149 }
150
151 int parse_args(int argc, char **argv)
152 {
153     /* We use a simple test because getopt isn't universally available */
154     if (argc > 1 && strcmp(argv[1], "-v") == 0)
155         verbose = 1;
156     if (argc > 1 && strcmp(argv[1], "-nov") == 0)
157         verbose = 0;
158     return 0;
159 }