Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / teshsuite / smpi / mpich3-test / io / i_hindexed.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2014 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 /* Wei-keng Liao (wkliao@ece.northwestern.edu) September 8, 2008 */
8
9 /* Uses nonblocking collective I/O.*/
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <mpi.h>
15
16 #define YLEN 5
17 #define XLEN 10
18 #define SUB_XLEN 3
19
20 /* rjl: I was just too lazy to compute this at run-time */
21 char compare_buf[XLEN * 4][YLEN * 4] = {
22     {'0', '1', '2', 0, 0, '3', '4', '5', 0, 0, 'D', 'E', 'F', 0, 0, 'G', 'H', 'I'},
23     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
24     {'6', '7', '8', 0, 0, '9', ':', ';', 0, 0, 'J', 'K', 'L', 0, 0, 'M', 'N', 'O'},
25     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
26     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
27     {'X', 'Y', 'Z', 0, 0, '[', '\\', ']', 0, 0, 'l', 'm', 'n', 0, 0, 'o', 'p', 'q'},
28     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
29     {'^', '_', '`', 0, 0, 'a', 'b', 'c', 0, 0, 'r', 's', 't', 0, 0, 'u', 'v', 'w'},
30     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
31     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
32     {'0', '1', '2', 0, 0, '3', '4', '5', 0, 0, 'D', 'E', 'F', 0, 0, 'G', 'H', 'I'},
33     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
34     {'6', '7', '8', 0, 0, '9', ':', ';', 0, 0, 'J', 'K', 'L', 0, 0, 'M', 'N', 'O'},
35     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
36     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
37     {'X', 'Y', 'Z', 0, 0, '[', '\\', ']', 0, 0, 'l', 'm', 'n', 0, 0, 'o', 'p', 'q'},
38     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
39     {'^', '_', '`', 0, 0, 'a', 'b', 'c', 0, 0, 'r', 's', 't', 0, 0, 'u', 'v', 'w'},
40     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
41     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
42 };
43
44
45 /* set this if you want a dump of the global array
46 #define VERBOSE 1
47 */
48
49 /*----< main() >------------------------------------------------------------*/
50 int main(int argc, char **argv)
51 {
52     int i, j, err, rank, np, num_io;
53     char *buf, *filename;
54     int rank_dim[2], array_of_sizes[2];
55     int array_of_subsizes[2];
56     int count, *blocklengths, global_array_size;
57     MPI_Count ftype_size;
58     MPI_Aint *displacements;
59     MPI_File fh;
60     MPI_Datatype ftype;
61     MPI_Request *request;
62     MPI_Status *statuses;
63     MPI_Status status;
64     MPI_Offset offset = 0;
65     int nr_errors = 0;
66 #ifdef VERBOSE
67     int k;
68 #endif
69
70     MPI_Init(&argc, &argv);
71     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
72     MPI_Comm_size(MPI_COMM_WORLD, &np);
73
74     if (np != 4) {
75         if (!rank)
76             printf("Please run with 4 processes. Exiting ...\n\n");
77         MPI_Finalize();
78         return 1;
79     }
80
81     filename = (argc > 1) ? argv[1] : (char*)"testfile";
82
83     num_io = 2;
84
85     request = (MPI_Request *) malloc(num_io * sizeof(MPI_Request));
86     statuses = (MPI_Status *) malloc(num_io * sizeof(MPI_Status));
87
88     /*-----------------------------------------------------------------------*/
89     /* process rank in each dimension */
90     rank_dim[0] = rank / 2;
91     rank_dim[1] = rank % 2;
92
93     /* global 2D array size */
94     array_of_sizes[0] = YLEN * 2;
95     array_of_sizes[1] = XLEN * 2;
96
97     global_array_size = array_of_sizes[0] * array_of_sizes[1];
98
99     array_of_subsizes[0] = YLEN / 2;
100     array_of_subsizes[1] = XLEN * SUB_XLEN / 5;
101
102     offset = rank_dim[0] * YLEN * array_of_sizes[1] + rank_dim[1] * XLEN;
103
104     /* define data type for file view */
105     count = array_of_subsizes[0] * 2;   /* 2 is the no. blocks along X */
106     blocklengths = (int *) malloc(count * sizeof(int));
107     displacements = (MPI_Aint *) malloc(count * sizeof(MPI_Aint));
108     for (i = 0; i < count; i++)
109         blocklengths[i] = array_of_subsizes[1] / 2;
110     for (i = 0; i < array_of_subsizes[0]; i++)
111         for (j = 0; j < 2; j++)
112             displacements[i * 2 + j] = offset + i * 2 * array_of_sizes[1]
113                 + j * XLEN / 2;
114     MPI_Type_create_hindexed(count, blocklengths, displacements, MPI_CHAR, &ftype);
115     MPI_Type_commit(&ftype);
116     MPI_Type_size_x(ftype, &ftype_size);
117
118 /* subarray's layout in the global array
119
120    P0's 's layout                               P1's layout
121    [ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9] | [ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
122 [ 0] 0 1 2     3 4 5                          |                       D E F     G H I
123 [ 1]                                          |
124 [ 2] 6 7 8     9 : ;                          |                       J K L     M N O
125 [ 3]                                          |
126 [ 4]                                          |
127 [ 5]                                          |
128 [ 6]                                          |
129 [ 7]                                          |
130 [ 8]                                          |
131 [ 9]                                          |
132
133    P2's 's layout                               P3's layout
134    [ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9] | [ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
135 [ 0]                                          |
136 [ 1]                                          |
137 [ 2]                                          |
138 [ 3]                                          |
139 [ 4]                                          |
140 [ 5] X Y Z     [ \ ]                          |                       l m n     o p q
141 [ 6]                                          |
142 [ 7] ^ _ `     a b c                          |                       r s t     u v w
143 [ 8]                                          |
144 [ 9]                                          |
145 */
146
147     /* initialize the write buffer */
148     buf = (char *) malloc(array_of_subsizes[0] * array_of_subsizes[1]);
149     for (i = 0; i < array_of_subsizes[0] * array_of_subsizes[1]; i++)
150         buf[i] = '0' + rank * 20 + i % 79;
151
152     /* zero file contents --------------------------------------------------- */
153     if (rank == 0) {
154         char *wr_buf = (char *) calloc(num_io * global_array_size, 1);
155         MPI_File_open(MPI_COMM_SELF, filename,
156                       MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
157         MPI_File_write(fh, wr_buf, num_io * global_array_size, MPI_CHAR, &status);
158         MPI_File_close(&fh);
159         free(wr_buf);
160     }
161     /* open the file -------------------------------------------------------- */
162     err = MPI_File_open(MPI_COMM_WORLD, filename,
163                         MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
164     if (err != MPI_SUCCESS) {
165         printf("Error: MPI_File_open() filename %s\n", filename);
166         MPI_Abort(MPI_COMM_WORLD, -1);
167         exit(1);
168     }
169
170     /* MPI nonblocking collective write */
171     for (i = 0; i < num_io; i++) {
172         offset = i * global_array_size;
173         /* set the file view */
174         MPI_File_set_view(fh, offset, MPI_BYTE, ftype, "native", MPI_INFO_NULL);
175         MPI_File_iwrite_all(fh, buf, ftype_size, MPI_CHAR, &request[i]);
176     }
177     MPI_Waitall(num_io, request, statuses);
178     MPI_File_close(&fh);
179
180     /* read and print file contents ----------------------------------------- */
181     if (rank == 0) {
182         char *ptr;
183         char *rd_buf = (char *) calloc(num_io * global_array_size, 1);
184         MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
185         MPI_File_read(fh, rd_buf, num_io * global_array_size, MPI_CHAR, &status);
186         MPI_File_close(&fh);
187
188 #ifdef VERBOSE
189         printf("-------------------------------------------------------\n");
190         printf("   [");
191         for (i = 0; i < 2; i++) {
192             for (j = 0; j < XLEN; j++)
193                 printf(" %d", j);
194             printf(" ");
195         }
196         printf("]\n\n");
197
198
199         ptr = rd_buf;
200         for (k = 0; k < num_io; k++) {
201             for (i = 0; i < 2 * YLEN; i++) {
202                 printf("[%2d]", k * 2 * YLEN + i);
203                 for (j = 0; j < 2 * XLEN; j++) {
204                     if (j > 0 && j % XLEN == 0)
205                         printf(" ");
206                     if (*ptr != 0)
207                         printf(" %c", *ptr);
208                     else
209                         printf("  ");
210                     ptr++;
211                 }
212                 printf("\n");
213             }
214             printf("\n");
215         }
216 #endif
217         ptr = rd_buf;
218         for (i = 0; i < 2 * YLEN * num_io; i++) {
219             for (j = 0; j < 2 * XLEN; j++) {
220                 if (*ptr != compare_buf[i][j]) {
221                     fprintf(stderr, "expected %d got %d at [%d][%d]\n",
222                             *ptr, compare_buf[i][j], i, j);
223                     nr_errors++;
224                 }
225                 ptr++;
226             }
227         }
228         free(rd_buf);
229
230         if (nr_errors == 0)
231             fprintf(stdout, " No Errors\n");
232         else
233             fprintf(stderr, "Found %d errors\n", nr_errors);
234     }
235
236     free(blocklengths);
237     free(displacements);
238     free(buf);
239     free(request);
240     free(statuses);
241     MPI_Type_free(&ftype);
242     MPI_Finalize();
243     return 0;
244 }
245
246 /* command-line outputs are: (the global array is written twice)
247
248 % mpiexec -n 4 wkl_subarray
249 -------------------------------------------------------
250    [ 0 1 2 3 4 5 6 7 8 9  0 1 2 3 4 5 6 7 8 9 ]
251
252 [ 0] 0 1 2     3 4 5      D E F     G H I
253 [ 1]
254 [ 2] 6 7 8     9 : ;      J K L     M N O
255 [ 3]
256 [ 4]
257 [ 5] X Y Z     [ \ ]      l m n     o p q
258 [ 6]
259 [ 7] ^ _ `     a b c      r s t     u v w
260 [ 8]
261 [ 9]
262
263 [10] 0 1 2     3 4 5      D E F     G H I
264 [11]
265 [12] 6 7 8     9 : ;      J K L     M N O
266 [13]
267 [14]
268 [15] X Y Z     [ \ ]      l m n     o p q
269 [16]
270 [17] ^ _ `     a b c      r s t     u v w
271 [18]
272 [19]
273
274 */