Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / simple-pack-external.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 #include "mpi.h"
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "mpitest.h"
12
13 static int verbose = 0;
14
15 /* tests */
16 int builtin_float_test(void);
17 int vector_of_vectors_test(void);
18 int optimizable_vector_of_basics_test(void);
19 int struct_of_basics_test(void);
20
21 /* helper functions */
22 int parse_args(int argc, char **argv);
23
24 int main(int argc, char **argv)
25 {
26     int err, errs = 0;
27
28     MTest_Init(&argc, &argv);
29     parse_args(argc, argv);
30
31     /* To improve reporting of problems about operations, we
32      * change the error handler to errors return */
33     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
34
35     /* perform some tests */
36     err = builtin_float_test();
37     if (err && verbose)
38         fprintf(stderr, "%d errors in builtin float test.\n", err);
39     errs += err;
40
41     err = vector_of_vectors_test();
42     if (err && verbose)
43         fprintf(stderr, "%d errors in vector of vectors test.\n", err);
44     errs += err;
45
46     err = optimizable_vector_of_basics_test();
47     if (err && verbose)
48         fprintf(stderr, "%d errors in vector of basics test.\n", err);
49     errs += err;
50
51     err = struct_of_basics_test();
52     if (err && verbose)
53         fprintf(stderr, "%d errors in struct of basics test.\n", err);
54     errs += err;
55
56     MTest_Finalize(errs);
57     MPI_Finalize();
58     return 0;
59 }
60
61 /* builtin_float_test()
62  *
63  * Tests functionality of get_envelope() and get_contents() on a MPI_FLOAT.
64  *
65  * Returns the number of errors encountered.
66  */
67 int builtin_float_test(void)
68 {
69     int nints, nadds, ntypes, combiner;
70
71     int err, errs = 0;
72
73     err = MPI_Type_get_envelope(MPI_FLOAT, &nints, &nadds, &ntypes, &combiner);
74
75     if (combiner != MPI_COMBINER_NAMED)
76         errs++;
77
78     /* Note: it is erroneous to call MPI_Type_get_contents() on a basic. */
79     return errs;
80 }
81
82 /* vector_of_vectors_test()
83  *
84  * Builds a vector of a vector of ints.  Assuming an int array of size 9
85  * integers, and treating the array as a 3x3 2D array, this will grab the
86  * corners.
87  *
88  * Returns the number of errors encountered.
89  */
90 int vector_of_vectors_test(void)
91 {
92     MPI_Datatype inner_vector;
93     MPI_Datatype outer_vector;
94     int array[9] = { 1, -1, 2,
95         -2, -3, -4,
96         3, -5, 4
97     };
98
99     char *buf;
100     int i, err, errs = 0;
101     MPI_Aint sizeoftype, position;
102
103     /* set up type */
104     err = MPI_Type_vector(2, 1, 2, MPI_INT, &inner_vector);
105     if (err != MPI_SUCCESS) {
106         errs++;
107         if (verbose)
108             fprintf(stderr, "error in MPI call; aborting after %d errors\n", errs + 1);
109         return errs;
110     }
111
112     err = MPI_Type_vector(2, 1, 2, inner_vector, &outer_vector);
113     if (err != MPI_SUCCESS) {
114         errs++;
115         if (verbose)
116             fprintf(stderr, "error in MPI call; aborting after %d errors\n", errs + 1);
117         return errs;
118     }
119
120     MPI_Type_commit(&outer_vector);
121
122     MPI_Pack_external_size((char *) "external32", 1, outer_vector, &sizeoftype);
123     if (sizeoftype != 4 * 4) {
124         errs++;
125         if (verbose)
126             fprintf(stderr, "size of type = %d; should be %d\n", (int) sizeoftype, 4 * 4);
127         return errs;
128     }
129
130     buf = (char *) malloc(sizeoftype);
131
132     position = 0;
133     err = MPI_Pack_external((char *) "external32",
134                             array, 1, outer_vector, buf, sizeoftype, &position);
135
136     if (position != sizeoftype) {
137         errs++;
138         if (verbose)
139             fprintf(stderr, "position = %d; should be %d (pack)\n",
140                     (int) position, (int) sizeoftype);
141     }
142
143     memset(array, 0, 9 * sizeof(int));
144     position = 0;
145     err = MPI_Unpack_external((char *) "external32",
146                               buf, sizeoftype, &position, array, 1, outer_vector);
147
148     if (position != sizeoftype) {
149         errs++;
150         if (verbose)
151             fprintf(stderr, "position = %d; should be %d (unpack)\n",
152                     (int) position, (int) sizeoftype);
153     }
154
155     for (i = 0; i < 9; i++) {
156         int goodval;
157         switch (i) {
158         case 0:
159             goodval = 1;
160             break;
161         case 2:
162             goodval = 2;
163             break;
164         case 6:
165             goodval = 3;
166             break;
167         case 8:
168             goodval = 4;
169             break;
170         default:
171             goodval = 0;
172             break;
173         }
174         if (array[i] != goodval) {
175             errs++;
176             if (verbose)
177                 fprintf(stderr, "array[%d] = %d; should be %d\n", i, array[i], goodval);
178         }
179     }
180
181     free(buf);
182     MPI_Type_free(&inner_vector);
183     MPI_Type_free(&outer_vector);
184     return errs;
185 }
186
187 /* optimizable_vector_of_basics_test()
188  *
189  * Builds a vector of ints.  Count is 10, blocksize is 2, stride is 2, so this
190  * is equivalent to a contig of 20.
191  *
192  * Returns the number of errors encountered.
193  */
194 int optimizable_vector_of_basics_test(void)
195 {
196     MPI_Datatype parent_type;
197     int array[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
198         16, 17, 18, 19
199     };
200     char *buf;
201     int i;
202     MPI_Aint sizeofint, sizeoftype, position;
203
204     int err, errs = 0;
205
206     MPI_Pack_external_size((char *) "external32", 1, MPI_INT, &sizeofint);
207
208     if (sizeofint != 4) {
209         errs++;
210         if (verbose)
211             fprintf(stderr, "size of external32 MPI_INT = %d; should be %d\n", (int) sizeofint, 4);
212     }
213
214     /* set up type */
215     err = MPI_Type_vector(10, 2, 2, MPI_INT, &parent_type);
216
217     MPI_Type_commit(&parent_type);
218
219     MPI_Pack_external_size((char *) "external32", 1, parent_type, &sizeoftype);
220
221
222     if (sizeoftype != 20 * sizeofint) {
223         errs++;
224         if (verbose)
225             fprintf(stderr, "size of vector = %d; should be %d\n",
226                     (int) sizeoftype, (int) (20 * sizeofint));
227     }
228
229     buf = (char *) malloc(sizeoftype);
230
231     position = 0;
232     err = MPI_Pack_external((char *) "external32",
233                             array, 1, parent_type, buf, sizeoftype, &position);
234
235     if (position != sizeoftype) {
236         errs++;
237         if (verbose)
238             fprintf(stderr, "position = %d; should be %d (pack)\n",
239                     (int) position, (int) sizeoftype);
240     }
241
242     memset(array, 0, 20 * sizeof(int));
243     position = 0;
244     err = MPI_Unpack_external((char *) "external32",
245                               buf, sizeoftype, &position, array, 1, parent_type);
246
247     if (position != sizeoftype) {
248         errs++;
249         if (verbose)
250             fprintf(stderr,
251                     "position = %ld; should be %ld (unpack)\n", (long) position, (long) sizeoftype);
252     }
253
254     for (i = 0; i < 20; i++) {
255         if (array[i] != i) {
256             errs++;
257             if (verbose)
258                 fprintf(stderr, "array[%d] = %d; should be %d\n", i, array[i], i);
259         }
260     }
261
262     free(buf);
263     MPI_Type_free(&parent_type);
264     return errs;
265 }
266
267 /* struct_of_basics_test()
268  *
269  * Builds a struct of ints.  Count is 10, all blocksizes are 2, all
270  * strides are 2*sizeofint, so this is equivalent to a contig of 20.
271  *
272  * Returns the number of errors encountered.
273  */
274 int struct_of_basics_test(void)
275 {
276     MPI_Datatype parent_type;
277     int array[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
278         16, 17, 18, 19
279     };
280     char *buf;
281     int i;
282     MPI_Aint sizeofint, sizeoftype, position;
283     int blocks[10];
284     MPI_Aint indices[10];
285     MPI_Datatype types[10];
286
287     int err, errs = 0;
288
289     MPI_Pack_external_size((char *) "external32", 1, MPI_INT, &sizeofint);
290
291     if (sizeofint != 4) {
292         errs++;
293         if (verbose)
294             fprintf(stderr, "size of external32 MPI_INT = %d; should be %d\n", (int) sizeofint, 4);
295     }
296
297     for (i = 0; i < 10; i++) {
298         blocks[i] = 2;
299         indices[i] = 2 * i * sizeofint;
300         /* This will cause MPICH to consider this as a blockindex. We
301          * need different types here. */
302         types[i] = MPI_INT;
303     }
304
305     /* set up type */
306     err = MPI_Type_struct(10, blocks, indices, types, &parent_type);
307
308     MPI_Type_commit(&parent_type);
309
310     MPI_Pack_external_size((char *) "external32", 1, parent_type, &sizeoftype);
311
312     if (sizeoftype != 20 * sizeofint) {
313         errs++;
314         if (verbose)
315             fprintf(stderr, "size of vector = %d; should be %d\n",
316                     (int) sizeoftype, (int) (20 * sizeofint));
317     }
318
319     buf = (char *) malloc(sizeoftype);
320
321     position = 0;
322     err = MPI_Pack_external((char *) "external32",
323                             array, 1, parent_type, buf, sizeoftype, &position);
324
325     if (position != sizeoftype) {
326         errs++;
327         if (verbose)
328             fprintf(stderr, "position = %d; should be %d (pack)\n",
329                     (int) position, (int) sizeoftype);
330     }
331
332     memset(array, 0, 20 * sizeof(int));
333     position = 0;
334     err = MPI_Unpack_external((char *) "external32",
335                               buf, sizeoftype, &position, array, 1, parent_type);
336
337     if (position != sizeoftype) {
338         errs++;
339         if (verbose)
340             fprintf(stderr,
341                     "position = %ld; should be %ld (unpack)\n", (long) position, (long) sizeoftype);
342     }
343
344     for (i = 0; i < 20; i++) {
345         if (array[i] != i) {
346             errs++;
347             if (verbose)
348                 fprintf(stderr, "array[%d] = %d; should be %d\n", i, array[i], i);
349         }
350     }
351
352     free(buf);
353     MPI_Type_free(&parent_type);
354     return errs;
355 }
356
357 int parse_args(int argc, char **argv)
358 {
359     int ret;
360
361     while ((ret = getopt(argc, argv, "v")) >= 0) {
362         switch (ret) {
363         case 'v':
364             verbose = 1;
365             break;
366         }
367     }
368     return 0;
369 }