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