Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix HAVE_FOOBAR flags handling
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / simple-pack.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 <stdio.h>
8 #include <stdlib.h>
9 #include "mpitestconf.h"
10 #ifdef HAVE_STRING_H
11 #include <string.h>
12 #endif
13
14 static int verbose = 0;
15
16 /* tests */
17 int builtin_float_test(void);
18 int vector_of_vectors_test(void);
19 int optimizable_vector_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     MPI_Init(&argc, &argv); /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
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     /* print message and exit */
52     if (errs) {
53         fprintf(stderr, "Found %d errors\n", errs);
54     }
55     else {
56         printf(" No Errors\n");
57     }
58     MPI_Finalize();
59     return 0;
60 }
61
62 /* builtin_float_test()
63  *
64  * Tests functionality of get_envelope() and get_contents() on a MPI_FLOAT.
65  *
66  * Returns the number of errors encountered.
67  */
68 int builtin_float_test(void)
69 {
70     int nints, nadds, ntypes, combiner;
71
72     int /* err, */ errs = 0;
73
74     /* err = */ MPI_Type_get_envelope(MPI_FLOAT,
75                                 &nints,
76                                 &nadds,
77                                 &ntypes,
78                                 &combiner);
79     
80     if (combiner != MPI_COMBINER_NAMED) errs++;
81
82     /* Note: it is erroneous to call MPI_Type_get_contents() on a basic. */
83     return errs;
84 }
85
86 /* vector_of_vectors_test()
87  *
88  * Builds a vector of a vector of ints.  Assuming an int array of size 9 
89  * integers, and treating the array as a 3x3 2D array, this will grab the 
90  * corners.
91  *
92  * Returns the number of errors encountered.
93  */
94 int vector_of_vectors_test(void)
95 {
96     MPI_Datatype inner_vector;
97     MPI_Datatype outer_vector;
98     int array[9] = {  1, -1,  2,
99                      -2, -3, -4,
100                       3, -5,  4 };
101
102     char *buf;
103     int i, err, errs = 0, 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     MPI_Type_size(outer_vector, &sizeoftype);
134     if (sizeoftype != 4*sizeof(int)) {
135         errs++;
136         if (verbose) fprintf(stderr, "size of type = %d; should be %d\n",
137                              (int) sizeoftype, (int) (4*sizeof(int)));
138         return errs;
139     }
140
141     buf = (char *) malloc(sizeoftype);
142
143     position = 0;
144     err = MPI_Pack(array,
145                    1,
146                    outer_vector,
147                    buf,
148                    sizeoftype,
149                    &position,
150                    MPI_COMM_WORLD);
151
152     if (position != sizeoftype) {
153         errs++;
154         if (verbose) fprintf(stderr, "position = %d; should be %d (pack)\n",
155                              position, sizeoftype);
156     }
157
158     memset(array, 0, 9*sizeof(int));
159     position = 0;
160     err = MPI_Unpack(buf,
161                      sizeoftype,
162                      &position,
163                      array,
164                      1,
165                      outer_vector,
166                      MPI_COMM_WORLD);
167
168     if (position != sizeoftype) {
169         errs++;
170         if (verbose) fprintf(stderr, "position = %d; should be %d (unpack)\n",
171                              position, sizeoftype);
172     }
173
174     for (i=0; i < 9; i++) {
175         int goodval;
176         switch (i) {
177             case 0:
178                 goodval = 1;
179                 break;
180             case 2:
181                 goodval = 2;
182                 break;
183             case 6:
184                 goodval = 3;
185                 break;
186             case 8:
187                 goodval = 4;
188                 break;
189             default:
190                 goodval = 0;
191                 break;
192         }
193         if (array[i] != goodval) {
194             errs++;
195             if (verbose) fprintf(stderr, "array[%d] = %d; should be %d\n",
196                                  i, array[i], goodval);
197         }
198     }
199
200     MPI_Type_free(&inner_vector);
201     MPI_Type_free(&outer_vector);
202     return errs;
203 }
204
205 /* optimizable_vector_of_basics_test()
206  *
207  * Builds a vector of ints.  Count is 10, blocksize is 2, stride is 2, so this
208  * is equivalent to a contig of 20.
209  *
210  * Returns the number of errors encountered.
211  */
212 int optimizable_vector_of_basics_test(void)
213 {
214     MPI_Datatype parent_type;
215     int array[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
216                       16, 17, 18, 19 };
217     char *buf;
218     int i, sizeofint, sizeoftype, position;
219
220     int /* err, */ errs = 0;
221
222     MPI_Type_size(MPI_INT, &sizeofint);
223
224     if (sizeofint != sizeof(int)) {
225         errs++;
226         if (verbose) fprintf(stderr, "size of MPI_Int = %d; should be %d\n",
227                              sizeofint, (int) sizeof(int));
228     }
229
230     /* set up type */
231     /* err = */ MPI_Type_vector(10,
232                           2,
233                           2,
234                           MPI_INT,
235                           &parent_type);
236
237     MPI_Type_commit(&parent_type);
238
239     MPI_Type_size(parent_type, &sizeoftype);
240
241     if (sizeoftype != 20 * sizeof(int)) {
242         errs++;
243         if (verbose) fprintf(stderr, "size of vector = %d; should be %d\n",
244                              (int) sizeoftype, (int) (20 * sizeof(int)));
245     }
246
247     buf = (char *) malloc(sizeoftype);
248
249     position = 0;
250     /* err = */ MPI_Pack(array,
251                    1,
252                    parent_type,
253                    buf,
254                    sizeoftype,
255                    &position,
256                    MPI_COMM_WORLD);
257
258     if (position != sizeoftype) {
259         errs++;
260         if (verbose) fprintf(stderr, "position = %d; should be %d (pack)\n",
261                              position, sizeoftype);
262     }
263
264     memset(array, 0, 20 * sizeof(int));
265     position = 0;
266     /* err = */ MPI_Unpack(buf,
267                      sizeoftype,
268                      &position,
269                      array,
270                      1,
271                      parent_type,
272                      MPI_COMM_WORLD);
273
274     if (position != sizeoftype) {
275         errs++;
276         if (verbose) fprintf(stderr, "position = %d; should be %d (unpack)\n",
277                              position, sizeoftype);
278     }
279
280     for (i=0; i < 20; i++) {
281         if (array[i] != i) {
282             errs++;
283             if (verbose) fprintf(stderr, "array[%d] = %d; should be %d\n",
284                                  i, array[i], i);
285         }
286     }
287
288     MPI_Type_free(&parent_type);
289     return errs;
290 }
291
292
293 int parse_args(int argc, char **argv)
294 {
295     /*
296     int ret;
297
298     while ((ret = getopt(argc, argv, "v")) >= 0)
299     {
300         switch (ret) {
301             case 'v':
302                 verbose = 1;
303                 break;
304         }
305     }
306     */
307     if (argc > 1 && strcmp(argv[1], "-v") == 0)
308         verbose = 1;
309     return 0;
310 }
311