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 main(int argc, char *argv[]);
15 int parse_args(int argc, char **argv);
16 int single_struct_test(void);
17 int array_of_structs_test(void);
18 int struct_of_structs_test(void);
20 struct test_struct_1 {
26 int main(int argc, char *argv[])
31 MPI_Init(&argc, &argv);
32 parse_args(argc, argv);
34 /* To improve reporting of problems about operations, we
35 * change the error handler to errors return */
36 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
38 err = single_struct_test();
40 fprintf(stderr, "error in single_struct_test\n");
43 err = array_of_structs_test();
45 fprintf(stderr, "error in array_of_structs_test\n");
48 err = struct_of_structs_test();
50 fprintf(stderr, "error in struct_of_structs_test\n");
53 /* print message and exit */
55 fprintf(stderr, "Found %d errors\n", errs);
58 printf(" No Errors\n");
64 int single_struct_test(void)
67 int bufsize, position = 0;
68 struct test_struct_1 ts1, ts2;
69 MPI_Datatype mystruct;
72 MPI_Aint disps[3] = { 0, 2 * sizeof(int), 3 * sizeof(int) }; /* guessing... */
73 int blks[3] = { 2, 2, 1 };
74 MPI_Datatype types[3] = { MPI_INT, MPI_CHAR, MPI_INT };
82 err = MPI_Type_struct(3, blks, disps, types, &mystruct);
83 if (err != MPI_SUCCESS) {
86 fprintf(stderr, "MPI_Type_struct returned error\n");
90 MPI_Type_commit(&mystruct);
92 MPI_Pack_size(1, mystruct, MPI_COMM_WORLD, &bufsize);
93 buffer = (char *) malloc(bufsize);
95 err = MPI_Pack(&ts1, 1, mystruct, buffer, bufsize, &position, MPI_COMM_WORLD);
96 if (err != MPI_SUCCESS) {
99 fprintf(stderr, "MPI_Pack returned error\n");
104 err = MPI_Unpack(buffer, bufsize, &position, &ts2, 1, mystruct, MPI_COMM_WORLD);
105 if (err != MPI_SUCCESS) {
108 fprintf(stderr, "MPI_Unpack returned error\n");
112 MPI_Type_free(&mystruct);
115 if (ts1.a != ts2.a) {
118 fprintf(stderr, "ts2.a = %d; should be %d\n", ts2.a, ts1.a);
121 if (ts1.b != ts2.b) {
124 fprintf(stderr, "ts2.b = %d; should be %d\n", ts2.b, ts1.b);
127 if (ts1.c != ts2.c) {
130 fprintf(stderr, "ts2.c = %d; should be %d\n", (int) ts2.c, (int) ts1.c);
133 if (ts1.d != ts2.d) {
136 fprintf(stderr, "ts2.d = %d; should be %d\n", (int) ts2.d, (int) ts1.d);
139 if (ts1.e != ts2.e) {
142 fprintf(stderr, "ts2.e = %d; should be %d\n", ts2.e, ts1.e);
149 int array_of_structs_test(void)
151 int i, err, errs = 0;
152 int bufsize, position = 0;
153 struct test_struct_1 ts1[10], ts2[10];
154 MPI_Datatype mystruct;
157 MPI_Aint disps[3] = { 0, 2 * sizeof(int), 3 * sizeof(int) }; /* guessing... */
158 int blks[3] = { 2, 2, 1 };
159 MPI_Datatype types[3] = { MPI_INT, MPI_CHAR, MPI_INT };
161 for (i = 0; i < 10; i++) {
162 ts1[i].a = 10 * i + 1;
163 ts1[i].b = 10 * i + 2;
164 ts1[i].c = 10 * i + 3;
165 ts1[i].d = 10 * i + 4;
166 ts1[i].e = 10 * i + 5;
175 err = MPI_Type_struct(3, blks, disps, types, &mystruct);
176 if (err != MPI_SUCCESS) {
179 fprintf(stderr, "MPI_Type_struct returned error\n");
183 MPI_Type_commit(&mystruct);
185 MPI_Pack_size(10, mystruct, MPI_COMM_WORLD, &bufsize);
186 buffer = (char *) malloc(bufsize);
188 err = MPI_Pack(ts1, 10, mystruct, buffer, bufsize, &position, MPI_COMM_WORLD);
189 if (err != MPI_SUCCESS) {
192 fprintf(stderr, "MPI_Pack returned error\n");
197 err = MPI_Unpack(buffer, bufsize, &position, ts2, 10, mystruct, MPI_COMM_WORLD);
198 if (err != MPI_SUCCESS) {
201 fprintf(stderr, "MPI_Unpack returned error\n");
205 MPI_Type_free(&mystruct);
208 for (i = 0; i < 10; i++) {
209 if (ts1[i].a != ts2[i].a) {
212 fprintf(stderr, "ts2[%d].a = %d; should be %d\n", i, ts2[i].a, ts1[i].a);
215 if (ts1[i].b != ts2[i].b) {
218 fprintf(stderr, "ts2[%d].b = %d; should be %d\n", i, ts2[i].b, ts1[i].b);
221 if (ts1[i].c != ts2[i].c) {
224 fprintf(stderr, "ts2[%d].c = %d; should be %d\n",
225 i, (int) ts2[i].c, (int) ts1[i].c);
228 if (ts1[i].d != ts2[i].d) {
231 fprintf(stderr, "ts2[%d].d = %d; should be %d\n",
232 i, (int) ts2[i].d, (int) ts1[i].d);
235 if (ts1[i].e != ts2[i].e) {
238 fprintf(stderr, "ts2[%d].e = %d; should be %d\n", i, ts2[i].e, ts1[i].e);
246 int struct_of_structs_test(void)
248 int i, j, err, errs = 0, bufsize, position;
250 char buf[50], buf2[50], *packbuf;
252 MPI_Aint disps[3] = { 0, 3, 0 };
253 int blks[3] = { 2, 1, 0 };
254 MPI_Datatype types[3], chartype, tiletype1, tiletype2, finaltype;
256 /* build a contig of one char to try to keep optimizations
257 * from being applied.
259 err = MPI_Type_contiguous(1, MPI_CHAR, &chartype);
260 if (err != MPI_SUCCESS) {
263 fprintf(stderr, "chartype create failed\n");
268 /* build a type that we can tile a few times */
272 err = MPI_Type_struct(2, blks, disps, types, &tiletype1);
273 if (err != MPI_SUCCESS) {
276 fprintf(stderr, "tiletype1 create failed\n");
281 /* build the same type again, again to avoid optimizations */
282 err = MPI_Type_struct(2, blks, disps, types, &tiletype2);
283 if (err != MPI_SUCCESS) {
286 fprintf(stderr, "tiletype2 create failed\n");
291 /* build a combination of those two tiletypes */
298 types[0] = tiletype1;
299 types[1] = tiletype2;
301 err = MPI_Type_struct(3, blks, disps, types, &finaltype);
302 if (err != MPI_SUCCESS) {
305 fprintf(stderr, "finaltype create failed\n");
310 MPI_Type_commit(&finaltype);
311 MPI_Type_free(&chartype);
312 MPI_Type_free(&tiletype1);
313 MPI_Type_free(&tiletype2);
315 MPI_Pack_size(5, finaltype, MPI_COMM_WORLD, &bufsize);
317 packbuf = malloc(bufsize);
318 if (packbuf == NULL) {
321 fprintf(stderr, "pack buffer allocation (%d bytes) failed\n", bufsize);
326 for (j = 0; j < 10; j++) {
327 for (i = 0; i < 5; i++) {
328 if (i == 2 || i == 4)
336 err = MPI_Pack(buf, 5, finaltype, packbuf, bufsize, &position, MPI_COMM_WORLD);
337 if (err != MPI_SUCCESS) {
340 fprintf(stderr, "pack failed\n");
347 err = MPI_Unpack(packbuf, bufsize, &position, buf2, 5, finaltype, MPI_COMM_WORLD);
348 if (err != MPI_SUCCESS) {
351 fprintf(stderr, "unpack failed\n");
356 for (j = 0; j < 10; j++) {
357 for (i = 0; i < 5; i++) {
358 if (buf[5 * j + i] != buf2[5 * j + i]) {
362 "buf2[%d] = %d; should be %d\n",
363 5 * j + i, (int) buf2[5 * j + i], (int) buf[5 * j + i]);
370 MPI_Type_free(&finaltype);
374 int parse_args(int argc, char **argv)
379 * while ((ret = getopt(argc, argv, "v")) >= 0)
388 if (argc > 1 && strcmp(argv[1], "-v") == 0)