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.
13 static char MTEST_Descrip[] = "Test asynchronous I/O w/ multiple completion";
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 int main(int argc, char **argv)
25 int *buf, i, rank, nints, len;
29 MPI_Status statuses[NUMOPS];
30 MPI_Request requests[NUMOPS];
32 MTest_Init(&argc, &argv);
33 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
35 /* process 0 takes the file name as a command-line argument and
36 broadcasts it to other processes */
39 while ((i < argc) && strcmp("-fname", *argv)) {
44 /* Use a default filename of testfile */
46 filename = (char *) malloc(len + 10);
47 memset(filename, 0, (len + 10) * sizeof(char));
48 strcpy(filename, "testfile");
50 * fprintf(stderr, "\n*# Usage: async_any -fname filename\n\n");
51 * MPI_Abort(MPI_COMM_WORLD, 1);
56 len = (int) strlen(*argv);
57 filename = (char *) malloc(len + 10);
58 MTEST_VG_MEM_INIT(filename, (len + 10) * sizeof(char));
59 strcpy(filename, *argv);
61 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
62 MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
65 MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
66 filename = (char *) malloc(len + 10);
67 MTEST_VG_MEM_INIT(filename, (len + 10) * sizeof(char));
68 MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
72 buf = (int *) malloc(SIZE);
73 nints = SIZE / sizeof(int);
74 for (i = 0; i < nints; i++)
75 buf[i] = rank * 100000 + i;
77 /* each process opens a separate file called filename.'myrank' */
78 tmp = (char *) malloc(len + 10);
79 strcpy(tmp, filename);
80 sprintf(filename, "%s.%d", tmp, rank);
82 MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
83 MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *) "native", MPI_INFO_NULL);
84 for (i = 0; i < NUMOPS; i++) {
85 MPI_File_iwrite(fh, buf, nints, MPI_INT, &(requests[i]));
87 MPI_Waitall(NUMOPS, requests, statuses);
90 /* reopen the file and read the data back */
92 for (i = 0; i < nints; i++)
94 MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
95 MPI_File_set_view(fh, 0, MPI_INT, MPI_INT, (char *) "native", MPI_INFO_NULL);
96 for (i = 0; i < NUMOPS; i++) {
97 MPI_File_iread(fh, buf, nints, MPI_INT, &(requests[i]));
99 MPI_Waitall(NUMOPS, requests, statuses);
102 /* check if the data read is correct */
103 for (i = 0; i < nints; i++) {
104 if (buf[i] != (rank * 100000 + i)) {
106 fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i],
115 MTest_Finalize(errs);