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 #if defined(TEST_HINDEXED_BLOCK)
19 static int verbose = 0;
23 int hindexed_block_contig_test(void);
24 int hindexed_block_vector_test(void);
26 /* helper functions */
27 int parse_args(int argc, char **argv);
28 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz);
30 int main(int argc, char **argv)
32 #if defined(TEST_HINDEXED_BLOCK)
38 MPI_Init(&argc, &argv); /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
39 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
40 #if defined(TEST_HINDEXED_BLOCK)
41 parse_args(argc, argv);
43 /* To improve reporting of problems about operations, we
44 * change the error handler to errors return */
45 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
47 /* perform some tests */
48 err = hindexed_block_contig_test();
50 fprintf(stderr, "%d errors in hindexed_block test.\n", err);
53 err = hindexed_block_vector_test();
55 fprintf(stderr, "%d errors in hindexed_block vector test.\n", err);
57 #endif /*defined(TEST_HINDEXED_BLOCK)*/
59 /* print message and exit */
62 fprintf(stderr, "Found %d errors\n", errs);
65 printf(" No Errors\n");
72 #if defined(TEST_HINDEXED_BLOCK)
74 /* hindexed_block_contig_test()
76 * Tests behavior with a hindexed_block that can be converted to a
77 * contig easily. This is specifically for coverage.
79 * Returns the number of errors encountered.
81 int hindexed_block_contig_test(void)
83 int buf[4] = { 7, -1, -2, -3 };
93 err = MPI_Type_create_hindexed_block(count, 1, &disp, MPI_INT, &newtype);
94 if (err != MPI_SUCCESS) {
96 fprintf(stderr, "error creating struct type in hindexed_block_contig_test()\n");
101 MPI_Type_size(MPI_INT, &int_size);
103 err = MPI_Type_size(newtype, &size);
104 if (err != MPI_SUCCESS) {
106 fprintf(stderr, "error obtaining type size in hindexed_block_contig_test()\n");
111 if (size != int_size) {
113 fprintf(stderr, "error: size != int_size in hindexed_block_contig_test()\n");
118 err = MPI_Type_extent(newtype, &extent);
119 if (err != MPI_SUCCESS) {
121 fprintf(stderr, "error obtaining type extent in hindexed_block_contig_test()\n");
126 if (extent != int_size) {
128 fprintf(stderr, "error: extent != int_size in hindexed_block_contig_test()\n");
133 MPI_Type_commit(&newtype);
135 err = pack_and_unpack((char *) buf, 1, newtype, 4 * sizeof(int));
138 fprintf(stderr, "error packing/unpacking in hindexed_block_contig_test()\n");
143 for (i = 0; i < 4; i++) {
151 goodval = 0; /* pack_and_unpack() zeros before unpack */
154 if (buf[i] != goodval) {
157 fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], goodval);
161 MPI_Type_free(&newtype);
166 /* hindexed_block_vector_test()
168 * Tests behavior with a hindexed_block of some vector types;
169 * this shouldn't be easily convertable into anything else.
171 * Returns the number of errors encountered.
173 int hindexed_block_vector_test(void)
184 int expected[NELT] = {
195 MPI_Aint disp[] = { 1, 4, 5 };
196 MPI_Datatype vectype, newtype;
201 /* create a vector type of 2 ints, skipping one in between */
202 err = MPI_Type_vector(2, 1, 2, MPI_INT, &vectype);
203 if (err != MPI_SUCCESS) {
205 fprintf(stderr, "error creating vector type in hindexed_block_contig_test()\n");
210 MPI_Type_commit(&vectype);
212 MPI_Type_extent(vectype, &extent);
213 for (i = 0; i < count; i++)
216 err = MPI_Type_create_hindexed_block(count, 1, disp, vectype, &newtype);
217 if (err != MPI_SUCCESS) {
220 "error creating hindexed_block type in hindexed_block_contig_test()\n");
225 MPI_Type_commit(&newtype);
227 err = MPI_Type_size(newtype, &size);
228 if (err != MPI_SUCCESS) {
230 fprintf(stderr, "error obtaining type size in hindexed_block_contig_test()\n");
235 MPI_Type_size(MPI_INT, &int_size);
237 if (size != 6 * int_size) {
239 fprintf(stderr, "error: size != 6 * int_size in hindexed_block_contig_test()\n");
244 MPI_Type_extent(newtype, &extent);
246 err = pack_and_unpack((char *) buf, 1, newtype, NELT * sizeof(int));
249 fprintf(stderr, "error packing/unpacking in hindexed_block_vector_test()\n");
254 for (i = 0; i < NELT; i++) {
255 if (buf[i] != expected[i]) {
258 fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], expected[i]);
262 MPI_Type_free(&vectype);
263 MPI_Type_free(&newtype);
270 * Perform packing and unpacking of a buffer for the purposes of checking
271 * to see if we are processing a type correctly. Zeros the buffer between
272 * these two operations, so the data described by the type should be in
273 * place upon return but all other regions of the buffer should be zero.
276 * typebuf - pointer to buffer described by datatype and count that
277 * will be packed and then unpacked into
278 * count, datatype - description of typebuf
279 * typebufsz - size of typebuf; used specifically to zero the buffer
280 * between the pack and unpack steps
283 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz)
286 int err, errs = 0, pack_size, type_size, position;
288 err = MPI_Type_size(datatype, &type_size);
289 if (err != MPI_SUCCESS) {
292 fprintf(stderr, "error in MPI_Type_size call; aborting after %d errors\n", errs);
299 err = MPI_Pack_size(count, datatype, MPI_COMM_SELF, &pack_size);
300 if (err != MPI_SUCCESS) {
303 fprintf(stderr, "error in MPI_Pack_size call; aborting after %d errors\n", errs);
307 packbuf = (char *) malloc(pack_size);
308 if (packbuf == NULL) {
311 fprintf(stderr, "error in malloc call; aborting after %d errors\n", errs);
317 err = MPI_Pack(typebuf, count, datatype, packbuf, type_size, &position, MPI_COMM_SELF);
319 if (position != type_size) {
322 fprintf(stderr, "position = %d; should be %d (pack)\n", position, type_size);
325 memset(typebuf, 0, typebufsz);
327 err = MPI_Unpack(packbuf, type_size, &position, typebuf, count, datatype, MPI_COMM_SELF);
328 if (err != MPI_SUCCESS) {
331 fprintf(stderr, "error in MPI_Unpack call; aborting after %d errors\n", errs);
337 if (position != type_size) {
340 fprintf(stderr, "position = %d; should be %d (unpack)\n", position, type_size);
346 int parse_args(int argc, char **argv)
348 if (argc > 1 && strcmp(argv[1], "-v") == 0)
352 #endif /*defined(TEST_HINDEXED_BLOCK)*/