Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / io / i_noncontig.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 /* tests noncontiguous reads/writes using nonblocking I/O */
13
14 /*
15 static char MTEST_Descrip[] = "Test nonblocking I/O";
16 */
17
18 #define SIZE 5000
19
20 #define VERBOSE 0
21
22 int main(int argc, char **argv)
23 {
24     int *buf, i, mynod, nprocs, len, b[3];
25     int errs = 0;
26     MPI_Aint d[3];
27     MPI_File fh;
28     MPI_Status status;
29     char *filename;
30     MPI_Datatype typevec, newtype, t[3];
31     MPI_Request req;
32
33     MTest_Init(&argc, &argv);
34     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
35     MPI_Comm_rank(MPI_COMM_WORLD, &mynod);
36
37     if (nprocs != 2) {
38         fprintf(stderr, "Run this program on two processes\n");
39         MPI_Abort(MPI_COMM_WORLD, 1);
40     }
41
42 /* process 0 takes the file name as a command-line argument and
43    broadcasts it to other processes */
44     if (!mynod) {
45         i = 1;
46         while ((i < argc) && strcmp("-fname", *argv)) {
47             i++;
48             argv++;
49         }
50         if (i >= argc) {
51             len = 8;
52             filename = (char *) malloc(len + 10);
53             strcpy(filename, "testfile");
54             /*
55              * fprintf(stderr, "\n*#  Usage: i_noncontig -fname filename\n\n");
56              * MPI_Abort(MPI_COMM_WORLD, 1);
57              */
58         }
59         else {
60             argv++;
61             len = (int) strlen(*argv);
62             filename = (char *) malloc(len + 1);
63             strcpy(filename, *argv);
64         }
65         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
66         MPI_Bcast(filename, len + 1, MPI_CHAR, 0, MPI_COMM_WORLD);
67     }
68     else {
69         MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD);
70         filename = (char *) malloc(len + 1);
71         MPI_Bcast(filename, len + 1, MPI_CHAR, 0, MPI_COMM_WORLD);
72     }
73
74     buf = (int *) malloc(SIZE * sizeof(int));
75
76     MPI_Type_vector(SIZE / 2, 1, 2, MPI_INT, &typevec);
77
78     b[0] = b[1] = b[2] = 1;
79     d[0] = 0;
80     d[1] = mynod * sizeof(int);
81     d[2] = SIZE * sizeof(int);
82     t[0] = MPI_LB;
83     t[1] = typevec;
84     t[2] = MPI_UB;
85
86     MPI_Type_struct(3, b, d, t, &newtype);
87     MPI_Type_commit(&newtype);
88     MPI_Type_free(&typevec);
89
90     if (!mynod) {
91 #if VERBOSE
92         fprintf(stderr,
93                 "\ntesting noncontiguous in memory, noncontiguous in file using nonblocking I/O\n");
94 #endif
95         MPI_File_delete(filename, MPI_INFO_NULL);
96     }
97     MPI_Barrier(MPI_COMM_WORLD);
98
99     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
100
101     MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
102
103     for (i = 0; i < SIZE; i++)
104         buf[i] = i + mynod * SIZE;
105     MPI_File_iwrite(fh, buf, 1, newtype, &req);
106     MPI_Wait(&req, &status);
107
108     MPI_Barrier(MPI_COMM_WORLD);
109
110     for (i = 0; i < SIZE; i++)
111         buf[i] = -1;
112
113     MPI_File_iread_at(fh, 0, buf, 1, newtype, &req);
114     MPI_Wait(&req, &status);
115
116     for (i = 0; i < SIZE; i++) {
117         if (!mynod) {
118             if ((i % 2) && (buf[i] != -1)) {
119                 errs++;
120                 fprintf(stderr, "Process %d: buf %d is %d, should be -1\n", mynod, i, buf[i]);
121             }
122             if (!(i % 2) && (buf[i] != i)) {
123                 errs++;
124                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", mynod, i, buf[i], i);
125             }
126         }
127         else {
128             if ((i % 2) && (buf[i] != i + mynod * SIZE)) {
129                 errs++;
130                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n",
131                         mynod, i, buf[i], i + mynod * SIZE);
132             }
133             if (!(i % 2) && (buf[i] != -1)) {
134                 errs++;
135                 fprintf(stderr, "Process %d: buf %d is %d, should be -1\n", mynod, i, buf[i]);
136             }
137         }
138     }
139
140     MPI_File_close(&fh);
141
142     MPI_Barrier(MPI_COMM_WORLD);
143
144     if (!mynod) {
145 #if VERBOSE
146         fprintf(stderr,
147                 "\ntesting noncontiguous in memory, contiguous in file using nonblocking I/O\n");
148 #endif
149         MPI_File_delete(filename, MPI_INFO_NULL);
150     }
151     MPI_Barrier(MPI_COMM_WORLD);
152
153     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
154
155     for (i = 0; i < SIZE; i++)
156         buf[i] = i + mynod * SIZE;
157     MPI_File_iwrite_at(fh, mynod * (SIZE / 2) * sizeof(int), buf, 1, newtype, &req);
158     MPI_Wait(&req, &status);
159
160     MPI_Barrier(MPI_COMM_WORLD);
161
162     for (i = 0; i < SIZE; i++)
163         buf[i] = -1;
164
165     MPI_File_iread_at(fh, mynod * (SIZE / 2) * sizeof(int), buf, 1, newtype, &req);
166     MPI_Wait(&req, &status);
167
168     for (i = 0; i < SIZE; i++) {
169         if (!mynod) {
170             if ((i % 2) && (buf[i] != -1)) {
171                 errs++;
172                 fprintf(stderr, "Process %d: buf %d is %d, should be -1\n", mynod, i, buf[i]);
173             }
174             if (!(i % 2) && (buf[i] != i)) {
175                 errs++;
176                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", mynod, i, buf[i], i);
177             }
178         }
179         else {
180             if ((i % 2) && (buf[i] != i + mynod * SIZE)) {
181                 errs++;
182                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n",
183                         mynod, i, buf[i], i + mynod * SIZE);
184             }
185             if (!(i % 2) && (buf[i] != -1)) {
186                 errs++;
187                 fprintf(stderr, "Process %d: buf %d is %d, should be -1\n", mynod, i, buf[i]);
188             }
189         }
190     }
191
192     MPI_File_close(&fh);
193
194     MPI_Barrier(MPI_COMM_WORLD);
195
196     if (!mynod) {
197 #if VERBOSE
198         fprintf(stderr,
199                 "\ntesting contiguous in memory, noncontiguous in file using nonblocking I/O\n");
200 #endif
201         MPI_File_delete(filename, MPI_INFO_NULL);
202     }
203     MPI_Barrier(MPI_COMM_WORLD);
204
205     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
206
207     MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
208
209     for (i = 0; i < SIZE; i++)
210         buf[i] = i + mynod * SIZE;
211     MPI_File_iwrite(fh, buf, SIZE, MPI_INT, &req);
212     MPI_Wait(&req, &status);
213
214     MPI_Barrier(MPI_COMM_WORLD);
215
216     for (i = 0; i < SIZE; i++)
217         buf[i] = -1;
218
219     MPI_File_iread_at(fh, 0, buf, SIZE, MPI_INT, &req);
220     MPI_Wait(&req, &status);
221
222     for (i = 0; i < SIZE; i++) {
223         if (!mynod) {
224             if (buf[i] != i) {
225                 errs++;
226                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n", mynod, i, buf[i], i);
227             }
228         }
229         else {
230             if (buf[i] != i + mynod * SIZE) {
231                 errs++;
232                 fprintf(stderr, "Process %d: buf %d is %d, should be %d\n",
233                         mynod, i, buf[i], i + mynod * SIZE);
234             }
235         }
236     }
237
238     MPI_File_close(&fh);
239
240     MPI_Type_free(&newtype);
241     free(buf);
242     free(filename);
243     MTest_Finalize(errs);
244     MPI_Finalize();
245     return 0;
246 }