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.
14 The default behavior of the test routines should be to briefly indicate
15 the cause of any errors - in this test, that means that verbose needs
16 to be set. Verbose should turn on output that is independent of error
19 static int verbose = 1;
21 int main(int argc, char *argv[]);
22 int parse_args(int argc, char **argv);
23 int struct_negdisp_test(void);
24 int vector_negstride_test(void);
25 int indexed_negdisp_test(void);
26 int struct_struct_test(void);
27 int flatten_test(void);
29 int build_array_section_type(MPI_Aint aext, MPI_Aint astart, MPI_Aint aend,
30 MPI_Datatype * datatype);
32 int main(int argc, char *argv[])
37 MPI_Init(&argc, &argv);
38 parse_args(argc, argv);
40 /* To improve reporting of problems about operations, we
41 * change the error handler to errors return */
42 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
44 err = struct_negdisp_test();
46 fprintf(stderr, "error in struct_negdisp_test\n");
49 err = vector_negstride_test();
51 fprintf(stderr, "error in vector_negstride_test\n");
54 err = indexed_negdisp_test();
56 fprintf(stderr, "error in indexed_negdisp_test\n");
59 err = struct_struct_test();
61 fprintf(stderr, "error in struct_struct_test\n");
66 fprintf(stderr, "error in flatten_test\n");
69 /* print message and exit */
71 fprintf(stderr, "Found %d errors\n", errs);
74 printf(" No Errors\n");
80 /* test uses a struct type that describes data that is contiguous,
81 * but processed in a noncontiguous way.
83 int struct_negdisp_test(void)
86 int sendbuf[6] = { 1, 2, 3, 4, 5, 6 };
87 int recvbuf[6] = { -1, -2, -3, -4, -5, -6 };
88 MPI_Datatype mystruct;
92 MPI_Aint disps[2] = { 0, -1 * ((int) sizeof(int)) };
93 int blks[2] = { 1, 1, };
94 MPI_Datatype types[2] = { MPI_INT, MPI_INT };
96 err = MPI_Type_struct(2, blks, disps, types, &mystruct);
97 if (err != MPI_SUCCESS) {
100 fprintf(stderr, "MPI_Type_struct returned error\n");
104 MPI_Type_commit(&mystruct);
106 err = MPI_Irecv(recvbuf + 1, 4, MPI_INT, 0, 0, MPI_COMM_SELF, &request);
107 if (err != MPI_SUCCESS) {
110 fprintf(stderr, "MPI_Irecv returned error\n");
114 err = MPI_Send(sendbuf + 2, 2, mystruct, 0, 0, MPI_COMM_SELF);
115 if (err != MPI_SUCCESS) {
118 fprintf(stderr, "MPI_Send returned error\n");
122 err = MPI_Wait(&request, &status);
123 if (err != MPI_SUCCESS) {
126 fprintf(stderr, "MPI_Wait returned error\n");
131 if (recvbuf[0] != -1) {
134 fprintf(stderr, "recvbuf[0] = %d; should be %d\n", recvbuf[0], -1);
137 if (recvbuf[1] != 3) {
140 fprintf(stderr, "recvbuf[1] = %d; should be %d\n", recvbuf[1], 3);
143 if (recvbuf[2] != 2) {
146 fprintf(stderr, "recvbuf[2] = %d; should be %d\n", recvbuf[2], 2);
149 if (recvbuf[3] != 5) {
152 fprintf(stderr, "recvbuf[3] = %d; should be %d\n", recvbuf[3], 5);
155 if (recvbuf[4] != 4) {
158 fprintf(stderr, "recvbuf[4] = %d; should be %d\n", recvbuf[4], 4);
161 if (recvbuf[5] != -6) {
164 fprintf(stderr, "recvbuf[5] = %d; should be %d\n", recvbuf[5], -6);
168 MPI_Type_free(&mystruct);
173 /* test uses a vector type that describes data that is contiguous,
174 * but processed in a noncontiguous way. this is effectively the
175 * same type as in the struct_negdisp_test above.
177 int vector_negstride_test(void)
180 int sendbuf[6] = { 1, 2, 3, 4, 5, 6 };
181 int recvbuf[6] = { -1, -2, -3, -4, -5, -6 };
182 MPI_Datatype myvector;
186 err = MPI_Type_vector(2, 1, -1, MPI_INT, &myvector);
187 if (err != MPI_SUCCESS) {
190 fprintf(stderr, "MPI_Type_vector returned error\n");
194 MPI_Type_commit(&myvector);
196 err = MPI_Irecv(recvbuf + 1, 4, MPI_INT, 0, 0, MPI_COMM_SELF, &request);
197 if (err != MPI_SUCCESS) {
200 fprintf(stderr, "MPI_Irecv returned error\n");
204 err = MPI_Send(sendbuf + 2, 2, myvector, 0, 0, MPI_COMM_SELF);
205 if (err != MPI_SUCCESS) {
208 fprintf(stderr, "MPI_Send returned error\n");
212 err = MPI_Wait(&request, &status);
213 if (err != MPI_SUCCESS) {
216 fprintf(stderr, "MPI_Wait returned error\n");
221 if (recvbuf[0] != -1) {
224 fprintf(stderr, "recvbuf[0] = %d; should be %d\n", recvbuf[0], -1);
227 if (recvbuf[1] != 3) {
230 fprintf(stderr, "recvbuf[1] = %d; should be %d\n", recvbuf[1], 3);
233 if (recvbuf[2] != 2) {
236 fprintf(stderr, "recvbuf[2] = %d; should be %d\n", recvbuf[2], 2);
239 if (recvbuf[3] != 5) {
242 fprintf(stderr, "recvbuf[3] = %d; should be %d\n", recvbuf[3], 5);
245 if (recvbuf[4] != 4) {
248 fprintf(stderr, "recvbuf[4] = %d; should be %d\n", recvbuf[4], 4);
251 if (recvbuf[5] != -6) {
254 fprintf(stderr, "recvbuf[5] = %d; should be %d\n", recvbuf[5], -6);
258 MPI_Type_free(&myvector);
263 /* test uses a indexed type that describes data that is contiguous,
264 * but processed in a noncontiguous way. this is effectively the same
265 * type as in the two tests above.
267 int indexed_negdisp_test(void)
270 int sendbuf[6] = { 1, 2, 3, 4, 5, 6 };
271 int recvbuf[6] = { -1, -2, -3, -4, -5, -6 };
272 MPI_Datatype myindexed;
276 int disps[2] = { 0, -1 };
277 int blks[2] = { 1, 1 };
279 err = MPI_Type_indexed(2, blks, disps, MPI_INT, &myindexed);
280 if (err != MPI_SUCCESS) {
283 fprintf(stderr, "MPI_Type_indexed returned error\n");
287 MPI_Type_commit(&myindexed);
289 err = MPI_Irecv(recvbuf + 1, 4, MPI_INT, 0, 0, MPI_COMM_SELF, &request);
290 if (err != MPI_SUCCESS) {
293 fprintf(stderr, "MPI_Irecv returned error\n");
297 err = MPI_Send(sendbuf + 2, 2, myindexed, 0, 0, MPI_COMM_SELF);
298 if (err != MPI_SUCCESS) {
301 fprintf(stderr, "MPI_Send returned error\n");
305 err = MPI_Wait(&request, &status);
306 if (err != MPI_SUCCESS) {
309 fprintf(stderr, "MPI_Wait returned error\n");
314 if (recvbuf[0] != -1) {
317 fprintf(stderr, "recvbuf[0] = %d; should be %d\n", recvbuf[0], -1);
320 if (recvbuf[1] != 3) {
323 fprintf(stderr, "recvbuf[1] = %d; should be %d\n", recvbuf[1], 3);
326 if (recvbuf[2] != 2) {
329 fprintf(stderr, "recvbuf[2] = %d; should be %d\n", recvbuf[2], 2);
332 if (recvbuf[3] != 5) {
335 fprintf(stderr, "recvbuf[3] = %d; should be %d\n", recvbuf[3], 5);
338 if (recvbuf[4] != 4) {
341 fprintf(stderr, "recvbuf[4] = %d; should be %d\n", recvbuf[4], 4);
344 if (recvbuf[5] != -6) {
347 fprintf(stderr, "recvbuf[5] = %d; should be %d\n", recvbuf[5], -6);
351 MPI_Type_free(&myindexed);
356 #define check_err(fn_name_) \
358 if (err != MPI_SUCCESS) { \
362 char err_str_[MPI_MAX_ERROR_STRING]; \
363 MPI_Error_string(err, err_str_, &len_); \
364 fprintf(stderr, #fn_name_ " failed at line %d, err=%d: %s\n", \
365 __LINE__, err, err_str_); \
369 /* test case from tt#1030 ported to C
371 * Thanks to Matthias Lieber for reporting the bug and providing a good test
373 int struct_struct_test(void)
376 int i, j, dt_size = 0;
381 MPI_Aint displ[COUNT];
383 MPI_Datatype types[COUNT];
384 MPI_Datatype datatype;
386 /* A slight difference from the F90 test: F90 arrays are column-major, C
387 * arrays are row-major. So we invert the order of dimensions. */
390 int array[N][M] = { {-1, -1, -1, -1}, {-1, -1, -1, -1} };
391 int expected[N][M] = { {-1, 1, 2, 5}, {-1, 3, 4, 6} };
392 int seq_array[N * M];
393 MPI_Aint astart, aend;
394 MPI_Aint size_exp = 0;
396 /* 1st section selects elements 1 and 2 out of 2nd dimension, complete 1st dim.
397 * should receive the values 1, 2, 3, 4 */
400 err = build_array_section_type(M, astart, aend, &types[0]);
404 fprintf(stderr, "build_array_section_type failed\n");
409 size_exp = size_exp + N * (aend - astart + 1) * sizeof(int);
411 /* 2nd section selects last element of 2nd dimension, complete 1st dim.
412 * should receive the values 5, 6 */
415 err = build_array_section_type(M, astart, aend, &types[1]);
419 fprintf(stderr, "build_array_section_type failed\n");
424 size_exp = size_exp + N * (aend - astart + 1) * sizeof(int);
427 err = MPI_Type_create_struct(COUNT, blens, displ, types, &datatype);
428 check_err(MPI_Type_create_struct);
429 err = MPI_Type_commit(&datatype);
430 check_err(MPI_Type_commit);
432 err = MPI_Type_size(datatype, &dt_size);
433 check_err(MPI_Type_size);
434 if (dt_size != size_exp) {
437 fprintf(stderr, "unexpected type size\n");
441 /* send the type to ourselves to make sure that the type describes data correctly */
442 for (i = 0; i < (N * M); ++i)
443 seq_array[i] = i + 1; /* source values 1..(N*M) */
444 err = MPI_Isend(&seq_array[0], dt_size / sizeof(int), MPI_INT, 0, 42, MPI_COMM_SELF, &req[0]);
445 check_err(MPI_Isend);
446 err = MPI_Irecv(&array[0][0], 1, datatype, 0, 42, MPI_COMM_SELF, &req[1]);
447 check_err(MPI_Irecv);
448 err = MPI_Waitall(2, req, MPI_STATUSES_IGNORE);
449 check_err(MPI_Waitall);
451 /* check against expected */
452 for (i = 0; i < N; ++i) {
453 for (j = 0; j < M; ++j) {
454 if (array[i][j] != expected[i][j]) {
457 fprintf(stderr, "array[%d][%d]=%d, should be %d\n", i, j, array[i][j],
463 err = MPI_Type_free(&datatype);
464 check_err(MPI_Type_free);
465 err = MPI_Type_free(&types[0]);
466 check_err(MPI_Type_free);
467 err = MPI_Type_free(&types[1]);
468 check_err(MPI_Type_free);
476 /* create a datatype for a 1D int array subsection
478 - a subsection of the first dimension is defined via astart, aend
479 - indexes are assumed to start with 0, that means:
480 - 0 <= astart <= aend < aext
481 - astart and aend are inclusive
485 aext = 8, astart=2, aend=4 would produce:
487 index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
488 1D array ###############################
489 datatype LB ########### UB
491 int build_array_section_type(MPI_Aint aext, MPI_Aint astart, MPI_Aint aend, MPI_Datatype * datatype)
495 MPI_Aint displ[COUNT];
497 MPI_Datatype types[COUNT];
499 *datatype = MPI_DATATYPE_NULL;
501 /* lower bound marker */
506 /* subsection starting at astart */
507 displ[1] = astart * sizeof(int);
509 blens[1] = aend - astart + 1;
511 /* upper bound marker */
513 displ[2] = aext * sizeof(int);
516 err = MPI_Type_create_struct(COUNT, blens, displ, types, datatype);
517 if (err != MPI_SUCCESS) {
520 fprintf(stderr, "MPI_Type_create_struct failed, err=%d\n", err);
528 /* start_idx is the "zero" point for the unpack */
529 static int pack_and_check_expected(MPI_Datatype type, const char *name,
530 int start_idx, int size, int *array, int *expected)
535 int *pack_buf = NULL;
538 int sendbuf[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
540 err = MPI_Type_size(type, &type_size);
541 check_err(MPI_Type_size);
542 assert(sizeof(sendbuf) >= type_size);
544 err = MPI_Pack_size(type_size / sizeof(int), MPI_INT, MPI_COMM_SELF, &pack_size);
545 check_err(MPI_Pack_size);
546 pack_buf = malloc(pack_size);
551 MPI_Pack(&sendbuf[0], type_size / sizeof(int), MPI_INT, pack_buf, pack_size, &pos,
555 err = MPI_Unpack(pack_buf, pack_size, &pos, &array[start_idx], 1, type, MPI_COMM_SELF);
556 check_err(MPI_Unpack);
559 /* check against expected */
560 for (i = 0; i < size; ++i) {
561 if (array[i] != expected[i]) {
564 fprintf(stderr, "%s: array[%d]=%d, should be %d\n", name, i, array[i], expected[i]);
571 /* regression for tt#1030, checks for bad offset math in the
572 * blockindexed and indexed dataloop flattening code */
573 int flatten_test(void)
577 /* real indices 0 1 2 3 4 5 6 7 8
578 * indices w/ &array[3] -3 -2 -1 0 1 2 3 4 5 */
579 int array[ARR_SIZE] = { -1, -1, -1, -1, -1, -1, -1, -1, -1 };
580 int expected[ARR_SIZE] = { -1, 0, 1, -1, 2, -1, 3, -1, 4 };
581 MPI_Datatype idx_type = MPI_DATATYPE_NULL;
582 MPI_Datatype blkidx_type = MPI_DATATYPE_NULL;
583 MPI_Datatype combo = MPI_DATATYPE_NULL;
586 MPI_Aint adispl[COUNT];
588 MPI_Datatype types[COUNT];
590 /* indexed type layout:
592 * 2101 <-- pos (left of 0 is neg)
594 * different blens to prevent optimization into a blockindexed
597 displ[0] = -2; /* elements, puts byte after block end at 0 */
599 displ[1] = 1; /*elements */
601 err = MPI_Type_indexed(COUNT, blens, displ, MPI_INT, &idx_type);
602 check_err(MPI_Type_indexed);
603 err = MPI_Type_commit(&idx_type);
604 check_err(MPI_Type_commit);
606 /* indexed type layout:
608 * 2101 <-- pos (left of 0 is neg)
612 err = MPI_Type_create_indexed_block(COUNT, 1, displ, MPI_INT, &blkidx_type);
613 check_err(MPI_Type_indexed_block);
614 err = MPI_Type_commit(&blkidx_type);
615 check_err(MPI_Type_commit);
617 /* struct type layout:
618 * II_I_B_B (I=idx_type, B=blkidx_type)
619 * 21012345 <-- pos (left of 0 is neg)
622 adispl[0] = 0; /*bytes */
626 adispl[1] = 4 * sizeof(int); /* bytes */
627 types[1] = blkidx_type;
629 /* must be a struct in order to trigger flattening code */
630 err = MPI_Type_create_struct(COUNT, blens, adispl, types, &combo);
631 check_err(MPI_Type_indexed);
632 err = MPI_Type_commit(&combo);
633 check_err(MPI_Type_commit);
635 /* pack/unpack with &array[3] */
636 errs += pack_and_check_expected(combo, "combo", 3, ARR_SIZE, array, expected);
638 MPI_Type_free(&combo);
639 MPI_Type_free(&idx_type);
640 MPI_Type_free(&blkidx_type);
648 int parse_args(int argc, char **argv)
653 * while ((ret = getopt(argc, argv, "v")) >= 0)
662 if (argc > 1 && strcmp(argv[1], "-v") == 0)