Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / teshsuite / smpi / mpich3-test / io / async_any.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 "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test asynchronous I/O w/ multiple completion";
14 */
15
16 #define SIZE (65536)
17 #define NUMOPS 10
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 int main(int argc, char **argv)
24 {
25     int *buf, i, rank, nints, len;
26     char *filename, *tmp;
27     int errs = 0;
28     MPI_File fh;
29     MPI_Status statuses[NUMOPS];
30     MPI_Request requests[NUMOPS];
31
32     MTest_Init(&argc, &argv);
33     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
34
35 /* process 0 takes the file name as a command-line argument and
36    broadcasts it to other processes */
37     if (!rank) {
38         i = 1;
39         while ((i < argc) && strcmp("-fname", *argv)) {
40             i++;
41             argv++;
42         }
43         if (i >= argc) {
44             /* Use a default filename of testfile */
45             len = 8;
46             filename = (char *) malloc(len + 10);
47             memset(filename, 0, (len + 10) * sizeof(char));
48             strcpy(filename, "testfile");
49             /*
50              * fprintf(stderr, "\n*#  Usage: async_any -fname filename\n\n");
51              * MPI_Abort(MPI_COMM_WORLD, 1);
52              */
53         }
54         else {
55             argv++;
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);
60         }
61         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
62         MPI_Bcast(filename, len + 10, MPI_CHAR, 0, MPI_COMM_WORLD);
63     }
64     else {
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);
69     }
70
71
72     buf = (int *) malloc(SIZE);
73     nints = SIZE / sizeof(int);
74     for (i = 0; i < nints; i++)
75         buf[i] = rank * 100000 + i;
76
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);
81
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]));
86     }
87     MPI_Waitall(NUMOPS, requests, statuses);
88     MPI_File_close(&fh);
89
90     /* reopen the file and read the data back */
91
92     for (i = 0; i < nints; i++)
93         buf[i] = 0;
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]));
98     }
99     MPI_Waitall(NUMOPS, requests, statuses);
100     MPI_File_close(&fh);
101
102     /* check if the data read is correct */
103     for (i = 0; i < nints; i++) {
104         if (buf[i] != (rank * 100000 + i)) {
105             errs++;
106             fprintf(stderr, "Process %d: error, read %d, should be %d\n", rank, buf[i],
107                     rank * 100000 + i);
108         }
109     }
110
111     free(buf);
112     free(filename);
113     free(tmp);
114
115     MTest_Finalize(errs);
116     MPI_Finalize();
117     return 0;
118 }