1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2014 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
11 #include <sys/types.h>
18 static void handle_error(int errcode, const char *str)
20 char msg[MPI_MAX_ERROR_STRING];
22 MPI_Error_string(errcode, msg, &resultlen);
23 fprintf(stderr, "%s: %s\n", str, msg);
24 MPI_Abort(MPI_COMM_WORLD, 1);
33 static int test_indexed_with_zeros(const char *filename, int testcase)
35 int i, rank, np, buflen, num, err, nr_errors = 0;
36 int nelms[MAXLEN], buf[MAXLEN], indices[MAXLEN], blocklen[MAXLEN];
40 MPI_Datatype filetype;
41 MPI_Datatype types[MAXLEN];
42 MPI_Aint addrs[MAXLEN];
44 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
45 MPI_Comm_size(MPI_COMM_WORLD, &np);
47 /* set up the number of integers to write in each iteration */
48 for (i = 0; i < MAXLEN; i++)
51 nelms[4] = nelms[5] = nelms[7] = 1;
53 nelms[0] = nelms[1] = nelms[2] = nelms[3] = nelms[6] = nelms[8] = 1;
55 /* pre-fill the file with integers -999 */
57 for (i = 0; i < MAXLEN; i++)
59 err = MPI_File_open(MPI_COMM_SELF, filename,
60 MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
61 if (err != MPI_SUCCESS)
62 handle_error(err, "MPI_File_open");
63 err = MPI_File_write(fh, buf, MAXLEN, MPI_INT, &status);
64 if (err != MPI_SUCCESS)
65 handle_error(err, "MPI_File_write");
66 err = MPI_File_close(&fh);
67 if (err != MPI_SUCCESS)
68 handle_error(err, "MPI_File_close");
70 MPI_Barrier(MPI_COMM_WORLD);
72 /* define a filetype with spurious leading zeros */
74 for (i = 0; i < MAXLEN; i++) {
77 addrs[num] = i * sizeof(int);
78 blocklen[num] = nelms[i];
84 MPI_Type_indexed(num, blocklen, indices, MPI_INT, &filetype);
87 MPI_Type_hindexed(num, blocklen, addrs, MPI_INT, &filetype);
90 MPI_Type_create_struct(num, blocklen, addrs, types, &filetype);
93 fprintf(stderr, "unknown testcase!\n");
97 MPI_Type_commit(&filetype);
99 /* initialize write buffer and write to file */
100 for (i = 0; i < MAXLEN; i++)
102 err = MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
103 if (err != MPI_SUCCESS)
104 handle_error(err, "MPI_File_open");
105 err = MPI_File_set_view(fh, 0, MPI_INT, filetype, "native", MPI_INFO_NULL);
106 if (err != MPI_SUCCESS)
107 handle_error(err, "MPI_File_set_view");
108 err = MPI_File_iwrite_all(fh, buf, buflen, MPI_INT, &request);
109 if (err != MPI_SUCCESS)
110 handle_error(err, "MPI_File_iwrite_all");
111 err = MPI_Wait(&request, &status);
112 if (err != MPI_SUCCESS)
113 handle_error(err, "MPI_Wait");
114 MPI_Type_free(&filetype);
115 err = MPI_File_close(&fh);
116 if (err != MPI_SUCCESS)
117 handle_error(err, "MPI_File_close");
119 /* read back and check */
121 err = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
122 if (err != MPI_SUCCESS)
123 handle_error(err, "MPI_File_open");
124 err = MPI_File_read(fh, buf, MAXLEN, MPI_INT, &status);
125 if (err != MPI_SUCCESS)
126 handle_error(err, "MPI_File_read");
127 err = MPI_File_close(&fh);
128 if (err != MPI_SUCCESS)
129 handle_error(err, "MPI_File_close");
130 for (i = 0; i < MAXLEN; i++) {
133 printf("Error: unexpected value for case %d at buf[%d] == %d\n",
134 testcase, i, buf[i]);
141 int main(int argc, char **argv)
143 int nr_errors, rank, np;
144 const char *filename;
146 filename = (argc > 1) ? argv[1] : "testfile";
148 MPI_Init(&argc, &argv);
149 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
150 MPI_Comm_size(MPI_COMM_WORLD, &np);
154 fprintf(stderr, "Must run on 2 MPI processes\n");
158 nr_errors = test_indexed_with_zeros(filename, INDEXED);
159 nr_errors += test_indexed_with_zeros(filename, HINDEXED);
160 nr_errors += test_indexed_with_zeros(filename, STRUCT);
162 if (rank == 0 && nr_errors == 0)
163 printf(" No Errors\n");