1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
12 static int verbose = 0;
14 int parse_args(int argc, char **argv);
15 int single_struct_test(void);
16 int array_of_structs_test(void);
17 int struct_of_structs_test(void);
19 struct test_struct_1 {
25 int main(int argc, char *argv[])
30 MPI_Init(&argc, &argv);
31 parse_args(argc, argv);
33 /* To improve reporting of problems about operations, we
34 change the error handler to errors return */
35 MPI_Comm_set_errhandler( MPI_COMM_WORLD, MPI_ERRORS_RETURN );
37 err = single_struct_test();
38 if (verbose && err) fprintf(stderr, "error in single_struct_test\n");
41 err = array_of_structs_test();
42 if (verbose && err) fprintf(stderr, "error in array_of_structs_test\n");
45 err = struct_of_structs_test();
46 if (verbose && err) fprintf(stderr, "error in struct_of_structs_test\n");
49 /* print message and exit */
51 fprintf(stderr, "Found %d errors\n", errs);
54 printf(" No Errors\n");
60 int single_struct_test(void)
63 int bufsize, position = 0;
64 struct test_struct_1 ts1, ts2;
65 MPI_Datatype mystruct;
68 MPI_Aint disps[3] = {0, 2*sizeof(int), 3*sizeof(int)}; /* guessing... */
69 int blks[3] = { 2, 2, 1 };
70 MPI_Datatype types[3] = { MPI_INT, MPI_CHAR, MPI_INT };
78 err = MPI_Type_struct(3, blks, disps, types, &mystruct);
79 if (err != MPI_SUCCESS) {
82 fprintf(stderr, "MPI_Type_struct returned error\n");
86 MPI_Type_commit(&mystruct);
88 MPI_Pack_size(1, mystruct, MPI_COMM_WORLD, &bufsize);
89 buffer = (char *) malloc(bufsize);
98 if (err != MPI_SUCCESS) {
101 fprintf(stderr, "MPI_Pack returned error\n");
106 err = MPI_Unpack(buffer,
113 if (err != MPI_SUCCESS) {
116 fprintf(stderr, "MPI_Unpack returned error\n");
120 MPI_Type_free(&mystruct);
123 if (ts1.a != ts2.a) {
126 fprintf(stderr, "ts2.a = %d; should be %d\n", ts2.a, ts1.a);
129 if (ts1.b != ts2.b) {
132 fprintf(stderr, "ts2.b = %d; should be %d\n", ts2.b, ts1.b);
135 if (ts1.c != ts2.c) {
138 fprintf(stderr, "ts2.c = %d; should be %d\n",
139 (int) ts2.c, (int) ts1.c);
142 if (ts1.d != ts2.d) {
145 fprintf(stderr, "ts2.d = %d; should be %d\n",
146 (int) ts2.d, (int) ts1.d);
149 if (ts1.e != ts2.e) {
152 fprintf(stderr, "ts2.e = %d; should be %d\n", ts2.e, ts1.e);
159 int array_of_structs_test(void)
161 int i, err, errs = 0;
162 int bufsize, position = 0;
163 struct test_struct_1 ts1[10], ts2[10];
164 MPI_Datatype mystruct;
167 MPI_Aint disps[3] = {0, 2*sizeof(int), 3*sizeof(int)}; /* guessing... */
168 int blks[3] = { 2, 2, 1 };
169 MPI_Datatype types[3] = { MPI_INT, MPI_CHAR, MPI_INT };
171 for (i=0; i < 10; i++) {
185 err = MPI_Type_struct(3, blks, disps, types, &mystruct);
186 if (err != MPI_SUCCESS) {
189 fprintf(stderr, "MPI_Type_struct returned error\n");
193 MPI_Type_commit(&mystruct);
195 MPI_Pack_size(10, mystruct, MPI_COMM_WORLD, &bufsize);
196 buffer = (char *) malloc(bufsize);
205 if (err != MPI_SUCCESS) {
208 fprintf(stderr, "MPI_Pack returned error\n");
213 err = MPI_Unpack(buffer,
220 if (err != MPI_SUCCESS) {
223 fprintf(stderr, "MPI_Unpack returned error\n");
227 MPI_Type_free(&mystruct);
230 for (i=0; i < 10; i++) {
231 if (ts1[i].a != ts2[i].a) {
234 fprintf(stderr, "ts2[%d].a = %d; should be %d\n",
235 i, ts2[i].a, ts1[i].a);
238 if (ts1[i].b != ts2[i].b) {
241 fprintf(stderr, "ts2[%d].b = %d; should be %d\n",
242 i, ts2[i].b, ts1[i].b);
245 if (ts1[i].c != ts2[i].c) {
248 fprintf(stderr, "ts2[%d].c = %d; should be %d\n",
249 i, (int) ts2[i].c, (int) ts1[i].c);
252 if (ts1[i].d != ts2[i].d) {
255 fprintf(stderr, "ts2[%d].d = %d; should be %d\n",
256 i, (int) ts2[i].d, (int) ts1[i].d);
259 if (ts1[i].e != ts2[i].e) {
262 fprintf(stderr, "ts2[%d].e = %d; should be %d\n",
263 i, ts2[i].e, ts1[i].e);
271 int struct_of_structs_test(void)
273 int i, j, err, errs = 0, bufsize, position;
275 char buf[50], buf2[50], *packbuf;
277 MPI_Aint disps[3] = {0, 3, 0};
278 int blks[3] = {2, 1, 0};
279 MPI_Datatype types[3], chartype, tiletype1, tiletype2, finaltype;
281 /* build a contig of one char to try to keep optimizations
282 * from being applied.
284 err = MPI_Type_contiguous(1, MPI_CHAR, &chartype);
285 if (err != MPI_SUCCESS) {
288 fprintf(stderr, "chartype create failed\n");
293 /* build a type that we can tile a few times */
297 err = MPI_Type_struct(2, blks, disps, types, &tiletype1);
298 if (err != MPI_SUCCESS) {
301 fprintf(stderr, "tiletype1 create failed\n");
306 /* build the same type again, again to avoid optimizations */
307 err = MPI_Type_struct(2, blks, disps, types, &tiletype2);
308 if (err != MPI_SUCCESS) {
311 fprintf(stderr, "tiletype2 create failed\n");
316 /* build a combination of those two tiletypes */
323 types[0] = tiletype1;
324 types[1] = tiletype2;
326 err = MPI_Type_struct(3, blks, disps, types, &finaltype);
327 if (err != MPI_SUCCESS) {
330 fprintf(stderr, "finaltype create failed\n");
335 MPI_Type_commit(&finaltype);
336 MPI_Type_free(&chartype);
337 MPI_Type_free(&tiletype1);
338 MPI_Type_free(&tiletype2);
340 MPI_Pack_size(5, finaltype, MPI_COMM_WORLD, &bufsize);
342 packbuf = malloc(bufsize);
343 if (packbuf == NULL) {
346 fprintf(stderr, "pack buffer allocation (%d bytes) failed\n", bufsize);
351 for (j=0; j < 10; j++) {
352 for (i=0; i < 5; i++) {
353 if (i == 2 || i == 4) buf[5*j + i] = 0;
354 else buf[5*j + i] = i;
359 err = MPI_Pack(buf, 5, finaltype, packbuf, bufsize, &position, MPI_COMM_WORLD);
360 if (err != MPI_SUCCESS) {
363 fprintf(stderr, "pack failed\n");
370 err = MPI_Unpack(packbuf, bufsize, &position, buf2, 5, finaltype, MPI_COMM_WORLD);
371 if (err != MPI_SUCCESS) {
374 fprintf(stderr, "unpack failed\n");
379 for (j=0; j < 10; j++) {
380 for (i=0; i < 5; i++) {
381 if (buf[5*j + i] != buf2[5*j + i]) {
385 "buf2[%d] = %d; should be %d\n",
395 MPI_Type_free(&finaltype);
399 int parse_args(int argc, char **argv)
404 while ((ret = getopt(argc, argv, "v")) >= 0)
413 if (argc > 1 && strcmp(argv[1], "-v") == 0)