Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
still not our
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / hindexed_block.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 #if !defined(USE_STRICT_MPI) && defined(MPICH)
15 #define TEST_HINDEXED_BLOCK 1
16 #endif
17
18 #if defined(TEST_HINDEXED_BLOCK)
19 static int verbose = 0;
20 #endif
21
22 /* tests */
23 int hindexed_block_contig_test(void);
24 int hindexed_block_vector_test(void);
25
26 /* helper functions */
27 int parse_args(int argc, char **argv);
28 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz);
29
30 int main(int argc, char **argv)
31 {
32 #if defined(TEST_HINDEXED_BLOCK)
33     int err;
34 #endif
35     int errs = 0;
36     int rank;
37
38     MPI_Init(&argc, &argv);     /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
39     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
40 #if defined(TEST_HINDEXED_BLOCK)
41     parse_args(argc, argv);
42
43     /* To improve reporting of problems about operations, we
44      * change the error handler to errors return */
45     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
46
47     /* perform some tests */
48     err = hindexed_block_contig_test();
49     if (err && verbose)
50         fprintf(stderr, "%d errors in hindexed_block test.\n", err);
51     errs += err;
52
53     err = hindexed_block_vector_test();
54     if (err && verbose)
55         fprintf(stderr, "%d errors in hindexed_block vector test.\n", err);
56     errs += err;
57 #endif /*defined(TEST_HINDEXED_BLOCK)*/
58
59     /* print message and exit */
60     if (rank == 0) {
61         if (errs) {
62             fprintf(stderr, "Found %d errors\n", errs);
63         }
64         else {
65             printf(" No Errors\n");
66         }
67     }
68     MPI_Finalize();
69     return 0;
70 }
71
72 #if defined(TEST_HINDEXED_BLOCK)
73
74 /* hindexed_block_contig_test()
75  *
76  * Tests behavior with a hindexed_block that can be converted to a
77  * contig easily.  This is specifically for coverage.
78  *
79  * Returns the number of errors encountered.
80  */
81 int hindexed_block_contig_test(void)
82 {
83     int buf[4] = { 7, -1, -2, -3 };
84     int err, errs = 0;
85
86     int i, count = 1;
87     MPI_Aint disp = 0;
88     MPI_Datatype newtype;
89
90     int size, int_size;
91     MPI_Aint extent;
92
93     err = MPI_Type_create_hindexed_block(count, 1, &disp, MPI_INT, &newtype);
94     if (err != MPI_SUCCESS) {
95         if (verbose) {
96             fprintf(stderr, "error creating struct type in hindexed_block_contig_test()\n");
97         }
98         errs++;
99     }
100
101     MPI_Type_size(MPI_INT, &int_size);
102
103     err = MPI_Type_size(newtype, &size);
104     if (err != MPI_SUCCESS) {
105         if (verbose) {
106             fprintf(stderr, "error obtaining type size in hindexed_block_contig_test()\n");
107         }
108         errs++;
109     }
110
111     if (size != int_size) {
112         if (verbose) {
113             fprintf(stderr, "error: size != int_size in hindexed_block_contig_test()\n");
114         }
115         errs++;
116     }
117
118     err = MPI_Type_extent(newtype, &extent);
119     if (err != MPI_SUCCESS) {
120         if (verbose) {
121             fprintf(stderr, "error obtaining type extent in hindexed_block_contig_test()\n");
122         }
123         errs++;
124     }
125
126     if (extent != int_size) {
127         if (verbose) {
128             fprintf(stderr, "error: extent != int_size in hindexed_block_contig_test()\n");
129         }
130         errs++;
131     }
132
133     MPI_Type_commit(&newtype);
134
135     err = pack_and_unpack((char *) buf, 1, newtype, 4 * sizeof(int));
136     if (err != 0) {
137         if (verbose) {
138             fprintf(stderr, "error packing/unpacking in hindexed_block_contig_test()\n");
139         }
140         errs += err;
141     }
142
143     for (i = 0; i < 4; i++) {
144         int goodval;
145
146         switch (i) {
147         case 0:
148             goodval = 7;
149             break;
150         default:
151             goodval = 0;        /* pack_and_unpack() zeros before unpack */
152             break;
153         }
154         if (buf[i] != goodval) {
155             errs++;
156             if (verbose)
157                 fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], goodval);
158         }
159     }
160
161     MPI_Type_free(&newtype);
162
163     return errs;
164 }
165
166 /* hindexed_block_vector_test()
167  *
168  * Tests behavior with a hindexed_block of some vector types;
169  * this shouldn't be easily convertable into anything else.
170  *
171  * Returns the number of errors encountered.
172  */
173 int hindexed_block_vector_test(void)
174 {
175 #define NELT (18)
176     int buf[NELT] = {
177         -1, -1, -1,
178          1, -2,  2,
179         -3, -3, -3,
180         -4, -4, -4,
181          3, -5,  4,
182          5, -6,  6
183     };
184     int expected[NELT] = {
185          0,  0,  0,
186          1,  0,  2,
187          0,  0,  0,
188          0,  0,  0,
189          3,  0,  4,
190          5,  0,  6
191     };
192     int err, errs = 0;
193
194     int i, count = 3;
195     MPI_Aint disp[] = { 1, 4, 5 };
196     MPI_Datatype vectype, newtype;
197
198     int size, int_size;
199     MPI_Aint extent;
200
201     /* create a vector type of 2 ints, skipping one in between */
202     err = MPI_Type_vector(2, 1, 2, MPI_INT, &vectype);
203     if (err != MPI_SUCCESS) {
204         if (verbose) {
205             fprintf(stderr, "error creating vector type in hindexed_block_contig_test()\n");
206         }
207         errs++;
208     }
209
210     MPI_Type_commit(&vectype);
211
212     MPI_Type_extent(vectype, &extent);
213     for (i = 0; i < count; i++)
214         disp[i] *= extent;
215
216     err = MPI_Type_create_hindexed_block(count, 1, disp, vectype, &newtype);
217     if (err != MPI_SUCCESS) {
218         if (verbose) {
219             fprintf(stderr,
220                     "error creating hindexed_block type in hindexed_block_contig_test()\n");
221         }
222         errs++;
223     }
224
225     MPI_Type_commit(&newtype);
226
227     err = MPI_Type_size(newtype, &size);
228     if (err != MPI_SUCCESS) {
229         if (verbose) {
230             fprintf(stderr, "error obtaining type size in hindexed_block_contig_test()\n");
231         }
232         errs++;
233     }
234
235     MPI_Type_size(MPI_INT, &int_size);
236
237     if (size != 6 * int_size) {
238         if (verbose) {
239             fprintf(stderr, "error: size != 6 * int_size in hindexed_block_contig_test()\n");
240         }
241         errs++;
242     }
243
244     MPI_Type_extent(newtype, &extent);
245
246     err = pack_and_unpack((char *) buf, 1, newtype, NELT * sizeof(int));
247     if (err != 0) {
248         if (verbose) {
249             fprintf(stderr, "error packing/unpacking in hindexed_block_vector_test()\n");
250         }
251         errs += err;
252     }
253
254     for (i = 0; i < NELT; i++) {
255         if (buf[i] != expected[i]) {
256             errs++;
257             if (verbose)
258                 fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], expected[i]);
259         }
260     }
261
262     MPI_Type_free(&vectype);
263     MPI_Type_free(&newtype);
264     return errs;
265 }
266
267
268 /* pack_and_unpack()
269  *
270  * Perform packing and unpacking of a buffer for the purposes of checking
271  * to see if we are processing a type correctly.  Zeros the buffer between
272  * these two operations, so the data described by the type should be in
273  * place upon return but all other regions of the buffer should be zero.
274  *
275  * Parameters:
276  * typebuf - pointer to buffer described by datatype and count that
277  *           will be packed and then unpacked into
278  * count, datatype - description of typebuf
279  * typebufsz - size of typebuf; used specifically to zero the buffer
280  *             between the pack and unpack steps
281  *
282  */
283 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz)
284 {
285     char *packbuf;
286     int err, errs = 0, pack_size, type_size, position;
287
288     err = MPI_Type_size(datatype, &type_size);
289     if (err != MPI_SUCCESS) {
290         errs++;
291         if (verbose) {
292             fprintf(stderr, "error in MPI_Type_size call; aborting after %d errors\n", errs);
293         }
294         return errs;
295     }
296
297     type_size *= count;
298
299     err = MPI_Pack_size(count, datatype, MPI_COMM_SELF, &pack_size);
300     if (err != MPI_SUCCESS) {
301         errs++;
302         if (verbose) {
303             fprintf(stderr, "error in MPI_Pack_size call; aborting after %d errors\n", errs);
304         }
305         return errs;
306     }
307     packbuf = (char *) malloc(pack_size);
308     if (packbuf == NULL) {
309         errs++;
310         if (verbose) {
311             fprintf(stderr, "error in malloc call; aborting after %d errors\n", errs);
312         }
313         return errs;
314     }
315
316     position = 0;
317     err = MPI_Pack(typebuf, count, datatype, packbuf, type_size, &position, MPI_COMM_SELF);
318
319     if (position != type_size) {
320         errs++;
321         if (verbose)
322             fprintf(stderr, "position = %d; should be %d (pack)\n", position, type_size);
323     }
324
325     memset(typebuf, 0, typebufsz);
326     position = 0;
327     err = MPI_Unpack(packbuf, type_size, &position, typebuf, count, datatype, MPI_COMM_SELF);
328     if (err != MPI_SUCCESS) {
329         errs++;
330         if (verbose) {
331             fprintf(stderr, "error in MPI_Unpack call; aborting after %d errors\n", errs);
332         }
333         return errs;
334     }
335     free(packbuf);
336
337     if (position != type_size) {
338         errs++;
339         if (verbose)
340             fprintf(stderr, "position = %d; should be %d (unpack)\n", position, type_size);
341     }
342
343     return errs;
344 }
345
346 int parse_args(int argc, char **argv)
347 {
348     if (argc > 1 && strcmp(argv[1], "-v") == 0)
349         verbose = 1;
350     return 0;
351 }
352 #endif /*defined(TEST_HINDEXED_BLOCK)*/