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.
9 #include "mpitestconf.h"
14 #if !defined(USE_STRICT_MPI) && defined(MPICH)
15 #define TEST_HINDEXED_BLOCK 1
18 static int verbose = 0;
21 int hindexed_block_contig_test(void);
22 int hindexed_block_vector_test(void);
24 /* helper functions */
25 int parse_args(int argc, char **argv);
26 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz);
28 int main(int argc, char **argv)
33 MPI_Init(&argc, &argv); /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
34 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
35 #if defined(TEST_HINDEXED_BLOCK)
36 parse_args(argc, argv);
38 /* To improve reporting of problems about operations, we
39 * change the error handler to errors return */
40 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
42 /* perform some tests */
43 err = hindexed_block_contig_test();
45 fprintf(stderr, "%d errors in hindexed_block test.\n", err);
48 err = hindexed_block_vector_test();
50 fprintf(stderr, "%d errors in hindexed_block vector test.\n", err);
52 #endif /*defined(TEST_HINDEXED_BLOCK) */
54 /* print message and exit */
57 fprintf(stderr, "Found %d errors\n", errs);
60 printf(" No Errors\n");
67 #if defined(TEST_HINDEXED_BLOCK)
69 /* hindexed_block_contig_test()
71 * Tests behavior with a hindexed_block that can be converted to a
72 * contig easily. This is specifically for coverage.
74 * Returns the number of errors encountered.
76 int hindexed_block_contig_test(void)
78 int buf[4] = { 7, -1, -2, -3 };
88 err = MPI_Type_create_hindexed_block(count, 1, &disp, MPI_INT, &newtype);
89 if (err != MPI_SUCCESS) {
91 fprintf(stderr, "error creating struct type in hindexed_block_contig_test()\n");
96 MPI_Type_size(MPI_INT, &int_size);
98 err = MPI_Type_size(newtype, &size);
99 if (err != MPI_SUCCESS) {
101 fprintf(stderr, "error obtaining type size in hindexed_block_contig_test()\n");
106 if (size != int_size) {
108 fprintf(stderr, "error: size != int_size in hindexed_block_contig_test()\n");
113 err = MPI_Type_extent(newtype, &extent);
114 if (err != MPI_SUCCESS) {
116 fprintf(stderr, "error obtaining type extent in hindexed_block_contig_test()\n");
121 if (extent != int_size) {
123 fprintf(stderr, "error: extent != int_size in hindexed_block_contig_test()\n");
128 MPI_Type_commit(&newtype);
130 err = pack_and_unpack((char *) buf, 1, newtype, 4 * sizeof(int));
133 fprintf(stderr, "error packing/unpacking in hindexed_block_contig_test()\n");
138 for (i = 0; i < 4; i++) {
146 goodval = 0; /* pack_and_unpack() zeros before unpack */
149 if (buf[i] != goodval) {
152 fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], goodval);
156 MPI_Type_free(&newtype);
161 /* hindexed_block_vector_test()
163 * Tests behavior with a hindexed_block of some vector types;
164 * this shouldn't be easily convertible into anything else.
166 * Returns the number of errors encountered.
168 int hindexed_block_vector_test(void)
179 int expected[NELT] = {
190 MPI_Aint disp[] = { 1, 4, 5 };
191 MPI_Datatype vectype, newtype;
196 /* create a vector type of 2 ints, skipping one in between */
197 err = MPI_Type_vector(2, 1, 2, MPI_INT, &vectype);
198 if (err != MPI_SUCCESS) {
200 fprintf(stderr, "error creating vector type in hindexed_block_contig_test()\n");
205 MPI_Type_commit(&vectype);
207 MPI_Type_extent(vectype, &extent);
208 for (i = 0; i < count; i++)
211 err = MPI_Type_create_hindexed_block(count, 1, disp, vectype, &newtype);
212 if (err != MPI_SUCCESS) {
214 fprintf(stderr, "error creating hindexed_block type in hindexed_block_contig_test()\n");
219 MPI_Type_commit(&newtype);
221 err = MPI_Type_size(newtype, &size);
222 if (err != MPI_SUCCESS) {
224 fprintf(stderr, "error obtaining type size in hindexed_block_contig_test()\n");
229 MPI_Type_size(MPI_INT, &int_size);
231 if (size != 6 * int_size) {
233 fprintf(stderr, "error: size != 6 * int_size in hindexed_block_contig_test()\n");
238 MPI_Type_extent(newtype, &extent);
240 err = pack_and_unpack((char *) buf, 1, newtype, NELT * sizeof(int));
243 fprintf(stderr, "error packing/unpacking in hindexed_block_vector_test()\n");
248 for (i = 0; i < NELT; i++) {
249 if (buf[i] != expected[i]) {
252 fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], expected[i]);
256 MPI_Type_free(&vectype);
257 MPI_Type_free(&newtype);
264 * Perform packing and unpacking of a buffer for the purposes of checking
265 * to see if we are processing a type correctly. Zeros the buffer between
266 * these two operations, so the data described by the type should be in
267 * place upon return but all other regions of the buffer should be zero.
270 * typebuf - pointer to buffer described by datatype and count that
271 * will be packed and then unpacked into
272 * count, datatype - description of typebuf
273 * typebufsz - size of typebuf; used specifically to zero the buffer
274 * between the pack and unpack steps
277 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz)
280 int err, errs = 0, pack_size, type_size, position;
282 err = MPI_Type_size(datatype, &type_size);
283 if (err != MPI_SUCCESS) {
286 fprintf(stderr, "error in MPI_Type_size call; aborting after %d errors\n", errs);
293 err = MPI_Pack_size(count, datatype, MPI_COMM_SELF, &pack_size);
294 if (err != MPI_SUCCESS) {
297 fprintf(stderr, "error in MPI_Pack_size call; aborting after %d errors\n", errs);
301 packbuf = (char *) malloc(pack_size);
302 if (packbuf == NULL) {
305 fprintf(stderr, "error in malloc call; aborting after %d errors\n", errs);
311 err = MPI_Pack(typebuf, count, datatype, packbuf, type_size, &position, MPI_COMM_SELF);
313 if (position != type_size) {
316 fprintf(stderr, "position = %d; should be %d (pack)\n", position, type_size);
319 memset(typebuf, 0, typebufsz);
321 err = MPI_Unpack(packbuf, type_size, &position, typebuf, count, datatype, MPI_COMM_SELF);
322 if (err != MPI_SUCCESS) {
325 fprintf(stderr, "error in MPI_Unpack call; aborting after %d errors\n", errs);
331 if (position != type_size) {
334 fprintf(stderr, "position = %d; should be %d (unpack)\n", position, type_size);
340 int parse_args(int argc, char **argv)
342 if (argc > 1 && strcmp(argv[1], "-v") == 0)
346 #endif /*defined(TEST_HINDEXED_BLOCK) */