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.
14 static char MTEST_Descrip[] = "Test contig asynchronous I/O";
17 #define DEFAULT_SIZE (65536)
19 /* Uses asynchronous I/O. Each process writes to separate files and
20 reads them back. The file name is taken as a command-line argument,
21 and the process rank is appended to it.*/
23 static void handle_error(int errcode, const char *str)
25 char msg[MPI_MAX_ERROR_STRING];
27 MPI_Error_string(errcode, msg, &resultlen);
28 fprintf(stderr, "%s: %s\n", str, msg);
29 MPI_Abort(MPI_COMM_WORLD, 1);
32 int main(int argc, char **argv)
34 int *buf, i, rank, nints, len, err;
35 char *filename = 0, *tmp;
37 int SIZE = DEFAULT_SIZE;
40 #ifdef MPIO_USES_MPI_REQUEST
46 MTest_Init(&argc, &argv);
47 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
50 /* process 0 takes the file name as a command-line argument and
51 broadcasts it to other processes */
55 /* Skip unrecognized arguments */
57 if (strcmp(*argv, "-fname") == 0) {
60 len = (int) strlen(*argv);
61 filename = (char *) malloc(len + 10);
62 MTEST_VG_MEM_INIT(filename, (len + 10) * sizeof(char));
63 strcpy(filename, *argv);
65 else if (strcmp(*argv, "-size") == 0) {
68 SIZE = strtol(*argv, 0, 10);
70 fprintf(stderr, "-size requires a numeric argument\n");
71 MPI_Abort(MPI_COMM_WORLD, 1);
74 fprintf(stderr, "-size requires a positive value\n");
84 /* Use a default filename of testfile */
86 filename = (char *) malloc(len + 10);
87 MTEST_VG_MEM_INIT(filename, (len + 10) * sizeof(char));
88 strcpy(filename, "/scratch/testfile");
90 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
91 MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
92 MPI_Bcast(&SIZE, 1, MPI_INT, 0, MPI_COMM_WORLD);
95 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
96 filename = (char *) malloc(len + 10);
97 MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
98 MPI_Bcast(&SIZE, 1, MPI_INT, 0, MPI_COMM_WORLD);
102 /* printf("Starting (size=%d, file=%s)...\n", SIZE, filename); fflush(stdout); */
104 buf = (int *) malloc(SIZE);
105 nints = SIZE / sizeof(int);
106 for (i = 0; i < nints; i++)
107 buf[i] = rank * 100000 + i;
109 /* each process opens a separate file called filename.'myrank' */
110 tmp = (char *) malloc(len + 10);
111 strcpy(tmp, filename);
112 sprintf(filename, "%s.%d", tmp, rank);
115 err = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
117 if (err != MPI_SUCCESS)
118 handle_error(err, "MPI_File_open");
120 err = MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *) "native", MPI_INFO_NULL);
121 if (err != MPI_SUCCESS)
122 handle_error(err, "MPI_File_set_view");
123 err = MPI_File_iwrite(fh, buf, nints, MPI_INT, &request);
124 if (err != MPI_SUCCESS)
125 handle_error(err, "MPI_File_iwrtie");
126 #ifdef MPIO_USES_MPI_REQUEST
127 MPI_Wait(&request, &status);
129 MPIO_Wait(&request, &status);
133 /* reopen the file and read the data back */
135 for (i = 0; i < nints; i++)
137 err = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
139 if (err != MPI_SUCCESS)
140 handle_error(err, "MPI_File_open (read)");
141 err = MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *) "native", MPI_INFO_NULL);
142 if (err != MPI_SUCCESS)
143 handle_error(err, "MPI_File_set_view (read)");
144 err = MPI_File_iread(fh, buf, nints, MPI_INT, &request);
145 if (err != MPI_SUCCESS)
146 handle_error(err, "MPI_File_iread");
147 #ifdef MPIO_USES_MPI_REQUEST
148 MPI_Wait(&request, &status);
150 MPIO_Wait(&request, &status);
154 /* check if the data read is correct */
155 /* for (i = 0; i < nints; i++) {*/
156 /* if (buf[i] != (rank * 100000 + i)) {*/
158 /* if (errs < 25) {*/
159 /* fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i],*/
160 /* rank * 100000 + i);*/
162 /* else if (errs == 25) {*/
163 /* fprintf(stderr, "Reached maximum number of errors to report\n");*/
171 MTest_Finalize(errs);