Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / teshsuite / smpi / mpich3-test / io / async.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include "mpitest.h"
12
13 /*
14 static char MTEST_Descrip[] = "Test contig asynchronous I/O";
15 */
16
17 #define DEFAULT_SIZE (65536)
18
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.*/
22
23 static void handle_error(int errcode, const char *str)
24 {
25     char msg[MPI_MAX_ERROR_STRING];
26     int resultlen;
27     MPI_Error_string(errcode, msg, &resultlen);
28     fprintf(stderr, "%s: %s\n", str, msg);
29     MPI_Abort(MPI_COMM_WORLD, 1);
30 }
31
32 int main(int argc, char **argv)
33 {
34     int *buf, i, rank, nints, len, err;
35     char *filename = 0, *tmp;
36     int errs = 0;
37     int SIZE = DEFAULT_SIZE;
38     MPI_File fh;
39     MPI_Status status;
40 #ifdef MPIO_USES_MPI_REQUEST
41     MPI_Request request;
42 #else
43     MPIO_Request request;
44 #endif
45
46     MTest_Init(&argc, &argv);
47     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
48
49
50 /* process 0 takes the file name as a command-line argument and
51    broadcasts it to other processes */
52     if (!rank) {
53         i = 1;
54         argv++;
55         /* Skip unrecognized arguments */
56         while (i < argc) {
57             if (strcmp(*argv, "-fname") == 0) {
58                 argv++;
59                 i++;
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);
64             }
65             else if (strcmp(*argv, "-size") == 0) {
66                 argv++;
67                 i++;
68                 SIZE = strtol(*argv, 0, 10);
69                 if (errno) {
70                     fprintf(stderr, "-size requires a numeric argument\n");
71                     MPI_Abort(MPI_COMM_WORLD, 1);
72                 }
73                 else if (SIZE <= 0) {
74                     fprintf(stderr, "-size requires a positive value\n");
75                 }
76             }
77             else {
78                 i++;
79                 argv++;
80             }
81         }
82
83         if (!filename) {
84             /* Use a default filename of testfile */
85             len = 8;
86             filename = (char *) malloc(len + 10);
87             MTEST_VG_MEM_INIT(filename, (len + 10) * sizeof(char));
88             strcpy(filename, "/scratch/testfile");
89         }
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);
93     }
94     else {
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);
99     }
100
101
102     /*     printf("Starting (size=%d, file=%s)...\n", SIZE, filename); fflush(stdout); */
103
104     buf = (int *) malloc(SIZE);
105     nints = SIZE / sizeof(int);
106     for (i = 0; i < nints; i++)
107         buf[i] = rank * 100000 + i;
108
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);
113     free(tmp);
114
115     err = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
116                         MPI_INFO_NULL, &fh);
117     if (err != MPI_SUCCESS)
118         handle_error(err, "MPI_File_open");
119
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);
128 #else
129     MPIO_Wait(&request, &status);
130 #endif
131     MPI_File_close(&fh);
132
133     /* reopen the file and read the data back */
134
135     for (i = 0; i < nints; i++)
136         buf[i] = 0;
137     err = MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE | MPI_MODE_RDWR,
138                         MPI_INFO_NULL, &fh);
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);
149 #else
150     MPIO_Wait(&request, &status);
151 #endif
152     MPI_File_close(&fh);
153
154     /* check if the data read is correct */
155 /*    for (i = 0; i < nints; i++) {*/
156 /*        if (buf[i] != (rank * 100000 + i)) {*/
157 /*            errs++;*/
158 /*            if (errs < 25) {*/
159 /*                fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i],*/
160 /*                        rank * 100000 + i);*/
161 /*            }*/
162 /*            else if (errs == 25) {*/
163 /*                fprintf(stderr, "Reached maximum number of errors to report\n");*/
164 /*            }*/
165 /*        }*/
166 /*    }*/
167
168     free(buf);
169     free(filename);
170
171     MTest_Finalize(errs);
172     MPI_Finalize();
173     return 0;
174 }