Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix HAVE_FOOBAR flags handling
[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) fprintf(stderr, "%d errors in builtin float test.\n",
38                                 err);
39     errs += err;
40
41     err = vector_of_vectors_test();
42     if (err && verbose) fprintf(stderr,
43                                 "%d errors in vector of vectors test.\n", err);
44     errs += err;
45
46     err = optimizable_vector_of_basics_test();
47     if (err && verbose) fprintf(stderr,
48                                 "%d errors in vector of basics test.\n", err);
49     errs += err;
50
51     err = struct_of_basics_test();
52     if (err && verbose) fprintf(stderr, 
53                                 "%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,
74                                 &nints,
75                                 &nadds,
76                                 &ntypes,
77                                 &combiner);
78     
79     if (combiner != MPI_COMBINER_NAMED) errs++;
80
81     /* Note: it is erroneous to call MPI_Type_get_contents() on a basic. */
82     return errs;
83 }
84
85 /* vector_of_vectors_test()
86  *
87  * Builds a vector of a vector of ints.  Assuming an int array of size 9 
88  * integers, and treating the array as a 3x3 2D array, this will grab the
89  * corners.
90  *
91  * Returns the number of errors encountered.
92  */
93 int vector_of_vectors_test(void)
94 {
95     MPI_Datatype inner_vector;
96     MPI_Datatype outer_vector;
97     int array[9] = {  1, -1,  2,
98                      -2, -3, -4,
99                       3, -5,  4 };
100
101     char *buf;
102     int i, err, errs = 0;
103     MPI_Aint sizeoftype, position;
104
105     /* set up type */
106     err = MPI_Type_vector(2,
107                           1,
108                           2,
109                           MPI_INT,
110                           &inner_vector);
111     if (err != MPI_SUCCESS) {
112         errs++;
113         if (verbose) fprintf(stderr, 
114                              "error in MPI call; aborting after %d errors\n",
115                              errs+1);
116         return errs;
117     }
118
119     err = MPI_Type_vector(2,
120                           1,
121                           2,
122                           inner_vector,
123                           &outer_vector);
124     if (err != MPI_SUCCESS) {
125         errs++;
126         if (verbose) fprintf(stderr, 
127                              "error in MPI call; aborting after %d errors\n",
128                              errs+1);
129         return errs;
130     }
131
132     MPI_Type_commit(&outer_vector);
133
134     MPI_Pack_external_size((char*)"external32", 1, outer_vector, &sizeoftype);
135     if (sizeoftype != 4*4) {
136         errs++;
137         if (verbose) fprintf(stderr, "size of type = %d; should be %d\n",
138                              (int) sizeoftype, 4*4);
139         return errs;
140     }
141
142     buf = (char *) malloc(sizeoftype);
143
144     position = 0;
145     err = MPI_Pack_external((char*)"external32",
146                             array,
147                             1,
148                             outer_vector,
149                             buf,
150                             sizeoftype,
151                             &position);
152
153     if (position != sizeoftype) {
154         errs++;
155         if (verbose) fprintf(stderr, "position = %d; should be %d (pack)\n",
156                              (int) position, (int) sizeoftype);
157     }
158
159     memset(array, 0, 9*sizeof(int));
160     position = 0;
161     err = MPI_Unpack_external((char*)"external32",
162                               buf,
163                               sizeoftype,
164                               &position,
165                               array,
166                               1,
167                               outer_vector);
168
169     if (position != sizeoftype) {
170         errs++;
171         if (verbose) fprintf(stderr, "position = %d; should be %d (unpack)\n",
172                              (int) position, (int) sizeoftype);
173     }
174
175     for (i=0; i < 9; i++) {
176         int goodval;
177         switch (i) {
178             case 0:
179                 goodval = 1;
180                 break;
181             case 2:
182                 goodval = 2;
183                 break;
184             case 6:
185                 goodval = 3;
186                 break;
187             case 8:
188                 goodval = 4;
189                 break;
190             default:
191                 goodval = 0;
192                 break;
193         }
194         if (array[i] != goodval) {
195             errs++;
196             if (verbose) fprintf(stderr, "array[%d] = %d; should be %d\n",
197                                  i, array[i], goodval);
198         }
199     }
200
201     MPI_Type_free(&inner_vector);
202     MPI_Type_free(&outer_vector);
203     return errs;
204 }
205
206 /* optimizable_vector_of_basics_test()
207  *
208  * Builds a vector of ints.  Count is 10, blocksize is 2, stride is 2, so this
209  * is equivalent to a contig of 20.
210  *
211  * Returns the number of errors encountered.
212  */
213 int optimizable_vector_of_basics_test(void)
214 {
215     MPI_Datatype parent_type;
216     int array[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
217                       16, 17, 18, 19 };
218     char *buf;
219     int i;
220     MPI_Aint sizeofint, sizeoftype, position;
221
222     int /* err, */ errs = 0;
223
224     MPI_Pack_external_size((char*)"external32", 1, MPI_INT, &sizeofint);
225
226     if (sizeofint != 4) {
227         errs++;
228         if (verbose) fprintf(stderr, 
229                              "size of external32 MPI_INT = %d; should be %d\n",
230                              (int) sizeofint, 4);
231     }
232
233     /* set up type */
234     /* err = */ MPI_Type_vector(10,
235                           2,
236                           2,
237                           MPI_INT,
238                           &parent_type);
239
240     MPI_Type_commit(&parent_type);
241
242     MPI_Pack_external_size((char*)"external32", 1, parent_type, &sizeoftype);
243
244
245     if (sizeoftype != 20 * sizeofint) {
246         errs++;
247         if (verbose) fprintf(stderr, "size of vector = %d; should be %d\n",
248                              (int) sizeoftype, (int) (20 * sizeofint));
249     }
250
251     buf = (char *) malloc(sizeoftype);
252
253     position = 0;
254     /* err = */ MPI_Pack_external((char*)"external32",
255                             array,
256                             1,
257                             parent_type,
258                             buf,
259                             sizeoftype,
260                             &position);
261
262     if (position != sizeoftype) {
263         errs++;
264         if (verbose) fprintf(stderr, "position = %d; should be %d (pack)\n",
265                              (int) position, (int) sizeoftype);
266     }
267
268     memset(array, 0, 20 * sizeof(int));
269     position = 0;
270     /* err = */ MPI_Unpack_external((char*)"external32",
271                               buf,
272                               sizeoftype,
273                               &position,
274                               array,
275                               1,
276                               parent_type);
277
278     if (position != sizeoftype) {
279         errs++;
280         if (verbose) fprintf(stderr, 
281                              "position = %ld; should be %ld (unpack)\n",
282                              (long) position, (long) sizeoftype);
283     }
284
285     for (i=0; i < 20; i++) {
286         if (array[i] != i) {
287             errs++;
288             if (verbose) fprintf(stderr, "array[%d] = %d; should be %d\n",
289                                  i, array[i], i);
290         }
291     }
292
293     MPI_Type_free(&parent_type);
294     return errs;
295 }
296
297 /* struct_of_basics_test()
298  *
299  * Builds a struct of ints.  Count is 10, all blocksizes are 2, all
300  * strides are 2*sizeofint, so this is equivalent to a contig of 20.
301  *
302  * Returns the number of errors encountered.
303  */
304 int struct_of_basics_test(void)
305 {
306     MPI_Datatype parent_type;
307     int array[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
308                       16, 17, 18, 19 };
309     char *buf;
310     int i;
311     MPI_Aint sizeofint, sizeoftype, position;
312     int blocks[10];
313     MPI_Aint indices[10];
314     MPI_Datatype types[10];
315
316     int /* err, */ errs = 0;
317
318     MPI_Pack_external_size((char*)"external32", 1, MPI_INT, &sizeofint);
319
320     if (sizeofint != 4) {
321         errs++;
322         if (verbose) fprintf(stderr, 
323                              "size of external32 MPI_INT = %d; should be %d\n",
324                              (int) sizeofint, 4);
325     }
326
327     for (i = 0; i < 10; i++) {
328         blocks[i] = 2;
329         indices[i] = 2 * i * sizeofint;
330         /* This will cause MPICH to consider this as a blockindex. We
331          * need different types here. */
332         types[i] = MPI_INT;
333     }
334
335     /* set up type */
336     /* err = */ MPI_Type_struct(10,
337                           blocks,
338                           indices,
339                           types,
340                           &parent_type);
341
342     MPI_Type_commit(&parent_type);
343
344     MPI_Pack_external_size((char*)"external32", 1, parent_type, &sizeoftype);
345
346     if (sizeoftype != 20 * sizeofint) {
347         errs++;
348         if (verbose) fprintf(stderr, "size of vector = %d; should be %d\n",
349                              (int) sizeoftype, (int) (20 * sizeofint));
350     }
351
352     buf = (char *) malloc(sizeoftype);
353
354     position = 0;
355     /* err = */ MPI_Pack_external((char*)"external32",
356                             array,
357                             1,
358                             parent_type,
359                             buf,
360                             sizeoftype,
361                             &position);
362
363     if (position != sizeoftype) {
364         errs++;
365         if (verbose) fprintf(stderr, "position = %d; should be %d (pack)\n",
366                              (int) position, (int) sizeoftype);
367     }
368
369     memset(array, 0, 20 * sizeof(int));
370     position = 0;
371     /* err = */ MPI_Unpack_external((char*)"external32",
372                               buf,
373                               sizeoftype,
374                               &position,
375                               array,
376                               1,
377                               parent_type);
378
379     if (position != sizeoftype) {
380         errs++;
381         if (verbose) fprintf(stderr, 
382                              "position = %ld; should be %ld (unpack)\n",
383                              (long) position, (long) sizeoftype);
384     }
385
386     for (i=0; i < 20; i++) {
387         if (array[i] != i) {
388             errs++;
389             if (verbose) fprintf(stderr, "array[%d] = %d; should be %d\n",
390                                  i, array[i], i);
391         }
392     }
393
394     MPI_Type_free(&parent_type);
395     return errs;
396 }
397
398 int parse_args(int argc, char **argv)
399 {
400     int ret;
401
402     while ((ret = getopt(argc, argv, "v")) >= 0)
403     {
404         switch (ret) {
405             case 'v':
406                 verbose = 1;
407                 break;
408         }
409     }
410     return 0;
411 }
412