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.
12 /* A 32^3 array. For other array sizes, change array_of_gsizes below. */
14 /* Uses nonblocking collective I/O. Writes a 3D block-distributed array to
15 a file corresponding to the global array in row-major (C) order, reads it
16 back, and checks that the data read is correct. */
18 /* Note that the file access pattern is noncontiguous. */
20 void handle_error(int errcode, const char *str);
22 void handle_error(int errcode, const char *str)
24 char msg[MPI_MAX_ERROR_STRING];
26 MPI_Error_string(errcode, msg, &resultlen);
27 fprintf(stderr, "%s: %s\n", str, msg);
28 MPI_Abort(MPI_COMM_WORLD, 1);
31 int main(int argc, char **argv)
34 int i, ndims, array_of_gsizes[3], array_of_distribs[3];
35 int order, nprocs, j, len;
36 int array_of_dargs[3], array_of_psizes[3];
37 int *readbuf, *writebuf, mynod, *tmpbuf, array_size;
40 int errs = 0, toterrs;
44 MPI_Info info = MPI_INFO_NULL;
47 MPI_Init(&argc, &argv);
48 MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
49 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
51 /* process 0 broadcasts the file name to other processes */
53 filename = (char*)"testfile";
54 len = strlen(filename);
55 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
56 MPI_Bcast(filename, len + 1, MPI_CHAR, 0, MPI_COMM_WORLD);
59 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
60 filename = (char *) malloc(len + 1);
61 MPI_Bcast(filename, len + 1, MPI_CHAR, 0, MPI_COMM_WORLD);
65 /* create the distributed array filetype */
69 array_of_gsizes[0] = 32;
70 array_of_gsizes[1] = 32;
71 array_of_gsizes[2] = 32;
73 array_of_distribs[0] = MPI_DISTRIBUTE_BLOCK;
74 array_of_distribs[1] = MPI_DISTRIBUTE_BLOCK;
75 array_of_distribs[2] = MPI_DISTRIBUTE_BLOCK;
77 array_of_dargs[0] = MPI_DISTRIBUTE_DFLT_DARG;
78 array_of_dargs[1] = MPI_DISTRIBUTE_DFLT_DARG;
79 array_of_dargs[2] = MPI_DISTRIBUTE_DFLT_DARG;
81 for (i = 0; i < ndims; i++)
82 array_of_psizes[i] = 0;
83 MPI_Dims_create(nprocs, ndims, array_of_psizes);
85 MPI_Type_create_darray(nprocs, mynod, ndims, array_of_gsizes,
86 array_of_distribs, array_of_dargs,
87 array_of_psizes, order, MPI_INT, &newtype);
88 MPI_Type_commit(&newtype);
90 /* initialize writebuf */
92 MPI_Type_size_x(newtype, &bufcount);
93 bufcount = bufcount / sizeof(int);
94 writebuf = (int *) malloc(bufcount * sizeof(int));
95 for (i = 0; i < bufcount; i++)
98 array_size = array_of_gsizes[0] * array_of_gsizes[1] * array_of_gsizes[2];
99 tmpbuf = (int *) calloc(array_size, sizeof(int));
100 MPI_Irecv(tmpbuf, 1, newtype, mynod, 10, MPI_COMM_WORLD, &request);
101 MPI_Send(writebuf, bufcount, MPI_INT, mynod, 10, MPI_COMM_WORLD);
102 MPI_Wait(&request, &status);
105 for (i = 0; i < array_size; i++)
113 fprintf(stderr, "Error in initializing writebuf on process %d\n", mynod);
114 MPI_Abort(MPI_COMM_WORLD, 1);
116 /* end of initialization */
118 /* write the array to the file */
119 errcode = MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh);
120 if (errcode != MPI_SUCCESS)
121 handle_error(errcode, "MPI_File_open");
123 errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
124 if (errcode != MPI_SUCCESS)
125 handle_error(errcode, "MPI_File_set_view");
127 errcode = MPI_File_iwrite_all(fh, writebuf, bufcount, MPI_INT, &request);
128 if (errcode != MPI_SUCCESS)
129 handle_error(errcode, "MPI_File_iwrite_all");
130 MPI_Wait(&request, &status);
132 errcode = MPI_File_close(&fh);
133 if (errcode != MPI_SUCCESS)
134 handle_error(errcode, "MPI_File_close");
137 /* wkl suggests potential for false " No Errors" if both read
138 * and write use the same file view */
139 /* solution: rank 0 reads entire file and checks write values */
140 errcode = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_RDONLY, info, &fh);
141 if (errcode != MPI_SUCCESS)
142 handle_error(errcode, "MPI_File_open");
144 readbuf = (int *) malloc(array_size * sizeof(int));
145 errcode = MPI_File_read(fh, readbuf, array_size, MPI_INT, &status);
146 if (errcode != MPI_SUCCESS)
147 handle_error(errcode, "MPI_File_read");
149 errcode = MPI_File_close(&fh);
150 if (errcode != MPI_SUCCESS)
151 handle_error(errcode, "MPI_File_close");
153 for (i = 0; i < array_size; i++)
154 if (readbuf[i] != i) {
156 fprintf(stderr, "Error: write integer %d but read %d\n", i, readbuf[i]);
161 MPI_Barrier(MPI_COMM_WORLD);
163 /* now read it back */
164 readbuf = (int *) malloc(bufcount * sizeof(int));
165 errcode = MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, info, &fh);
166 if (errcode != MPI_SUCCESS)
167 handle_error(errcode, "MPI_File_open");
169 errcode = MPI_File_set_view(fh, 0, MPI_INT, newtype, "native", info);
170 if (errcode != MPI_SUCCESS)
171 handle_error(errcode, "MPI_File_set_view");
172 errcode = MPI_File_iread_all(fh, readbuf, bufcount, MPI_INT, &request);
173 if (errcode != MPI_SUCCESS)
174 handle_error(errcode, "MPI_File_iread_all");
175 MPI_Wait(&request, &status);
176 errcode = MPI_File_close(&fh);
177 if (errcode != MPI_SUCCESS)
178 handle_error(errcode, "MPI_File_close");
180 /* check the data read */
181 for (i = 0; i < bufcount; i++) {
182 if (readbuf[i] != writebuf[i]) {
184 fprintf(stderr, "Process %d, readbuf %d, writebuf %d, i %d\n",
185 mynod, readbuf[i], writebuf[i], i);
189 MPI_Allreduce(&errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
192 fprintf(stderr, "Found %d errors\n", toterrs);
195 fprintf(stdout, " No Errors\n");
199 MPI_Type_free(&newtype);