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 static int verbose = 0;
17 int blockindexed_contig_test(void);
18 int blockindexed_vector_test(void);
20 /* helper functions */
21 int parse_args(int argc, char **argv);
22 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz);
24 int main(int argc, char **argv)
28 MPI_Init(&argc, &argv); /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
29 parse_args(argc, argv);
31 /* To improve reporting of problems about operations, we
32 * change the error handler to errors return */
33 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
35 /* perform some tests */
36 err = blockindexed_contig_test();
38 fprintf(stderr, "%d errors in blockindexed test.\n", err);
41 err = blockindexed_vector_test();
43 fprintf(stderr, "%d errors in blockindexed vector test.\n", err);
46 /* print message and exit */
48 fprintf(stderr, "Found %d errors\n", errs);
51 printf(" No Errors\n");
57 /* blockindexed_contig_test()
59 * Tests behavior with a blockindexed that can be converted to a
60 * contig easily. This is specifically for coverage.
62 * Returns the number of errors encountered.
64 int blockindexed_contig_test(void)
66 int buf[4] = { 7, -1, -2, -3 };
76 err = MPI_Type_create_indexed_block(count, 1, &disp, MPI_INT, &newtype);
77 if (err != MPI_SUCCESS) {
79 fprintf(stderr, "error creating struct type in blockindexed_contig_test()\n");
84 MPI_Type_size(MPI_INT, &int_size);
86 err = MPI_Type_size(newtype, &size);
87 if (err != MPI_SUCCESS) {
89 fprintf(stderr, "error obtaining type size in blockindexed_contig_test()\n");
94 if (size != int_size) {
96 fprintf(stderr, "error: size != int_size in blockindexed_contig_test()\n");
101 err = MPI_Type_extent(newtype, &extent);
102 if (err != MPI_SUCCESS) {
104 fprintf(stderr, "error obtaining type extent in blockindexed_contig_test()\n");
109 if (extent != int_size) {
111 fprintf(stderr, "error: extent != int_size in blockindexed_contig_test()\n");
116 MPI_Type_commit(&newtype);
118 err = pack_and_unpack((char *) buf, 1, newtype, 4 * sizeof(int));
121 fprintf(stderr, "error packing/unpacking in blockindexed_contig_test()\n");
126 for (i = 0; i < 4; i++) {
134 goodval = 0; /* pack_and_unpack() zeros before unpack */
137 if (buf[i] != goodval) {
140 fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], goodval);
144 MPI_Type_free(&newtype);
149 /* blockindexed_vector_test()
151 * Tests behavior with a blockindexed of some vector types;
152 * this shouldn't be easily convertable into anything else.
154 * Returns the number of errors encountered.
156 int blockindexed_vector_test(void)
159 int buf[NELT] = { -1, -1, -1,
166 int expected[NELT] = {
177 int disp[] = { 1, 4, 5 };
178 MPI_Datatype vectype, newtype;
182 /* create a vector type of 2 ints, skipping one in between */
183 err = MPI_Type_vector(2, 1, 2, MPI_INT, &vectype);
184 if (err != MPI_SUCCESS) {
186 fprintf(stderr, "error creating vector type in blockindexed_contig_test()\n");
191 err = MPI_Type_create_indexed_block(count, 1, disp, vectype, &newtype);
192 if (err != MPI_SUCCESS) {
194 fprintf(stderr, "error creating blockindexed type in blockindexed_contig_test()\n");
199 MPI_Type_size(MPI_INT, &int_size);
201 err = MPI_Type_size(newtype, &size);
202 if (err != MPI_SUCCESS) {
204 fprintf(stderr, "error obtaining type size in blockindexed_contig_test()\n");
209 if (size != 6 * int_size) {
211 fprintf(stderr, "error: size != 6 * int_size in blockindexed_contig_test()\n");
216 MPI_Type_commit(&newtype);
218 err = pack_and_unpack((char *) buf, 1, newtype, NELT * sizeof(int));
221 fprintf(stderr, "error packing/unpacking in blockindexed_vector_test()\n");
226 for (i = 0; i < NELT; i++) {
227 if (buf[i] != expected[i]) {
230 fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], expected[i]);
234 MPI_Type_free(&vectype);
235 MPI_Type_free(&newtype);
242 * Perform packing and unpacking of a buffer for the purposes of checking
243 * to see if we are processing a type correctly. Zeros the buffer between
244 * these two operations, so the data described by the type should be in
245 * place upon return but all other regions of the buffer should be zero.
248 * typebuf - pointer to buffer described by datatype and count that
249 * will be packed and then unpacked into
250 * count, datatype - description of typebuf
251 * typebufsz - size of typebuf; used specifically to zero the buffer
252 * between the pack and unpack steps
255 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz)
258 int err, errs = 0, pack_size, type_size, position;
260 err = MPI_Type_size(datatype, &type_size);
261 if (err != MPI_SUCCESS) {
264 fprintf(stderr, "error in MPI_Type_size call; aborting after %d errors\n", errs);
271 err = MPI_Pack_size(count, datatype, MPI_COMM_SELF, &pack_size);
272 if (err != MPI_SUCCESS) {
275 fprintf(stderr, "error in MPI_Pack_size call; aborting after %d errors\n", errs);
279 packbuf = (char *) malloc(pack_size);
280 if (packbuf == NULL) {
283 fprintf(stderr, "error in malloc call; aborting after %d errors\n", errs);
289 err = MPI_Pack(typebuf, count, datatype, packbuf, type_size, &position, MPI_COMM_SELF);
291 if (position != type_size) {
294 fprintf(stderr, "position = %d; should be %d (pack)\n", position, type_size);
297 memset(typebuf, 0, typebufsz);
299 err = MPI_Unpack(packbuf, type_size, &position, typebuf, count, datatype, MPI_COMM_SELF);
300 if (err != MPI_SUCCESS) {
303 fprintf(stderr, "error in MPI_Unpack call; aborting after %d errors\n", errs);
309 if (position != type_size) {
312 fprintf(stderr, "position = %d; should be %d (unpack)\n", position, type_size);
318 int parse_args(int argc, char **argv)
323 * while ((ret = getopt(argc, argv, "v")) >= 0)
332 if (argc > 1 && strcmp(argv[1], "-v") == 0)