Debian Codegraph @

src/mpi/romio/adio/ad_daos/ad_daos_io_str.c

raw
1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 #include "ad_daos.h"
7 #include "adio_extern.h"
8 #include <assert.h>
9
10 #include "../../mpi-io/mpioimpl.h"
11 #ifdef MPIO_BUILD_PROFILING
12 #include "../../mpi-io/mpioprof.h"
13 #endif
14 #include "mpiu_greq.h"
15
16 enum {
17 DAOS_WRITE,
18 DAOS_READ
19 };
20
21 static void
22 ADIOI_DAOS_StridedListIO(ADIO_File fd, const void *buf, MPI_Aint count,
23 MPI_Datatype datatype, int file_ptr_type,
24 ADIO_Offset offset, ADIO_Status * status,
25 MPI_Request * request, int rw_type, int *error_code);
26
27 static MPIX_Grequest_class ADIOI_DAOS_greq_class = 0;
28
29 void ADIOI_DAOS_ReadStrided(ADIO_File fd, void *buf, MPI_Aint count,
30 MPI_Datatype datatype, int file_ptr_type,
31 ADIO_Offset offset, ADIO_Status * status, int *error_code)
32 {
33 ADIOI_DAOS_StridedListIO(fd, buf, count, datatype, file_ptr_type,
34 offset, status, NULL, DAOS_READ, error_code);
35 return;
36 }
37
38 void ADIOI_DAOS_WriteStrided(ADIO_File fd, const void *buf, MPI_Aint count,
39 MPI_Datatype datatype, int file_ptr_type,
40 ADIO_Offset offset, ADIO_Status * status, int *error_code)
41 {
42 ADIOI_DAOS_StridedListIO(fd, (void *) buf, count, datatype, file_ptr_type,
43 offset, status, NULL, DAOS_WRITE, error_code);
44 return;
45 }
46
47 void ADIOI_DAOS_IreadStrided(ADIO_File fd, void *buf, MPI_Aint count,
48 MPI_Datatype datatype, int file_ptr_type,
49 ADIO_Offset offset, ADIO_Request * request, int *error_code)
50 {
51 ADIOI_DAOS_StridedListIO(fd, buf, count, datatype, file_ptr_type,
52 offset, NULL, request, DAOS_READ, error_code);
53 return;
54 }
55
56 void ADIOI_DAOS_IwriteStrided(ADIO_File fd, const void *buf, MPI_Aint count,
57 MPI_Datatype datatype, int file_ptr_type,
58 ADIO_Offset offset, MPI_Request * request, int *error_code)
59 {
60 ADIOI_DAOS_StridedListIO(fd, (void *) buf, count, datatype, file_ptr_type,
61 offset, NULL, request, DAOS_WRITE, error_code);
62 return;
63 }
64
65
66 static void
67 ADIOI_DAOS_StridedListIO(ADIO_File fd, const void *buf, MPI_Aint count,
68 MPI_Datatype datatype, int file_ptr_type,
69 ADIO_Offset offset, ADIO_Status * status,
70 MPI_Request * request, int rw_type, int *error_code)
71 {
72 ADIOI_Flatlist_node *flat_buf, *flat_file;
73 MPI_Count fwr_size = 0, st_index = 0;
74 MPI_Count sum, n_etypes_in_filetype, size_in_filetype;
75 MPI_Count bufsize;
76 MPI_Count n_filetypes, etype_in_filetype;
77 ADIO_Offset abs_off_in_filetype = 0;
78 MPI_Count filetype_size, etype_size, buftype_size;
79 MPI_Aint lb, filetype_extent, buftype_extent;
80 int buftype_is_contig, filetype_is_contig;
81 ADIO_Offset off, disp, start_off;
82 int flag;
83 MPI_Count st_fwr_size, st_n_filetypes;
84 MPI_Count mem_list_count;
85 int64_t file_length;
86 int total_blks_to_write;
87 MPI_Count f_data_wrote;
88 int n_write_lists;
89 struct ADIO_DAOS_cont *cont = fd->fs_ptr;
90 struct ADIO_DAOS_req *aio_req = NULL;
91 static char myname[] = "ADIOI_DAOS_StridedListIO";
92 int err_flag = 0, ret;
93 int mpi_rank;
94
95 MPI_Comm_rank(fd->comm, &mpi_rank);
96 *error_code = MPI_SUCCESS;
97
98 ADIOI_Datatype_iscontig(datatype, &buftype_is_contig);
99 ADIOI_Datatype_iscontig(fd->filetype, &filetype_is_contig);
100
101 MPI_Type_size_x(fd->filetype, &filetype_size);
102
103 MPI_Type_get_extent(fd->filetype, &lb, &filetype_extent);
104 MPI_Type_size_x(datatype, &buftype_size);
105 MPI_Type_get_extent(datatype, &lb, &buftype_extent);
106 etype_size = fd->etype_size;
107
108 bufsize = buftype_size * count;
109
110
111 d_sg_list_t *sgl, loc_sgl;
112 d_iov_t *iovs;
113 dfs_iod_t *iod, loc_iod;
114 daos_range_t *rgs;
115 daos_size_t *nbytes, loc_nbytes;
116
117 if (request) {
118 aio_req = (struct ADIO_DAOS_req *) ADIOI_Calloc(sizeof(struct ADIO_DAOS_req), 1);
119 daos_event_init(&aio_req->daos_event, DAOS_HDL_INVAL, NULL);
120 iod = &aio_req->iod;
121 sgl = &aio_req->sgl;
122 nbytes = &aio_req->nbytes;
123
124 if (ADIOI_DAOS_greq_class == 0) {
125 MPIX_Grequest_class_create(ADIOI_GEN_aio_query_fn,
126 ADIOI_DAOS_aio_free_fn, MPIU_Greq_cancel_fn,
127 ADIOI_DAOS_aio_poll_fn, ADIOI_DAOS_aio_wait_fn,
128 &ADIOI_DAOS_greq_class);
129 }
130 MPIX_Grequest_class_allocate(ADIOI_DAOS_greq_class, aio_req, request);
131 memcpy(&(aio_req->req), request, sizeof(MPI_Request));
132 } else {
133 iod = &loc_iod;
134 sgl = &loc_sgl;
135 nbytes = &loc_nbytes;
136 }
137
138 if (filetype_size == 0) {
139 #ifdef HAVE_STATUS_SET_BYTES
140 if (status)
141 MPIR_Status_set_bytes(status, datatype, 0);
142 #endif
143 *error_code = MPI_SUCCESS;
144 return;
145 }
146
147 if (bufsize == 0) {
148 #ifdef HAVE_STATUS_SET_BYTES
149 if (status)
150 MPIR_Status_set_bytes(status, datatype, 0);
151 #endif
152 *error_code = MPI_SUCCESS;
153 return;
154 }
155
156 /* Create Memory SGL */
157 file_length = 0;
158 MPI_Count k; // how many entries in scatter-gather list
159 if (!buftype_is_contig) {
160 flat_buf = ADIOI_Flatten_and_find(datatype);
161 mem_list_count = count * flat_buf->count;
162
163 iovs = (d_iov_t *) ADIOI_Malloc(mem_list_count * sizeof(d_iov_t));
164
165 k = 0;
166 for (MPI_Count j = 0; j < count; j++) {
167 for (MPI_Count i = 0; i < flat_buf->count; i++) {
168 ADIO_Offset tmp_off;
169
170 if (flat_buf->blocklens[i] == 0) {
171 continue;
172 }
173 if (file_length + flat_buf->blocklens[i] > bufsize)
174 break;
175
176 tmp_off = ((size_t) buf + j * buftype_extent + flat_buf->indices[i]);
177 file_length += flat_buf->blocklens[i];
178 d_iov_set(&iovs[k++], (char *) tmp_off, flat_buf->blocklens[i]);
179
180 #ifdef D_PRINT_IO_MEM
181 printf("(MEM %d) %d: off %lld len %zu\n", mpi_rank, k,
182 tmp_off, flat_buf->blocklens[i]);
183 #endif
184 }
185 }
186 } else {
187 k = 1;
188 iovs = (d_iov_t *) ADIOI_Malloc(sizeof(d_iov_t));
189 file_length = bufsize;
190 d_iov_set(iovs, (void *) buf, bufsize);
191 #ifdef D_PRINT_IO_MEM
192 printf("(MEM SINGLE) off %lld len %zu\n", buf, bufsize);
193 #endif
194 }
195 ADIOI_Assert(k < UINT_MAX);
196 sgl->sg_nr = (uint32_t) k;
197 sgl->sg_nr_out = 0;
198 sgl->sg_iovs = iovs;
199 if (request)
200 aio_req->iovs = iovs;
201
202 if (filetype_is_contig) {
203 n_write_lists = 1;
204
205 if (file_ptr_type == ADIO_EXPLICIT_OFFSET)
206 off = fd->disp + etype_size * offset;
207 else
208 off = fd->fp_ind;
209
210 rgs = (daos_range_t *) ADIOI_Malloc(sizeof(daos_range_t));
211 rgs->rg_idx = off;
212 rgs->rg_len = bufsize;
213 #ifdef D_PRINT_IO
214 printf("(%d) Single: idx %lld len %zu\n", mpi_rank, rgs->rg_idx, rgs->rg_len);
215 #endif
216 } else {
217 flat_file = ADIOI_Flatten_and_find(fd->filetype);
218 disp = fd->disp;
219
220 /* for each case - ADIO_Individual pointer or explicit, find offset
221 * (file offset in bytes), n_filetypes (how many filetypes into file to
222 * start), fwr_size (remaining amount of data in present file block),
223 * and st_index (start point in terms of blocks in starting filetype) */
224 if (file_ptr_type == ADIO_INDIVIDUAL) {
225 start_off = fd->fp_ind; /* in bytes */
226 n_filetypes = -1;
227 flag = 0;
228 while (!flag) {
229 n_filetypes++;
230 for (MPI_Count i = 0; i < flat_file->count; i++) {
231 if (disp + flat_file->indices[i] +
232 ((ADIO_Offset) n_filetypes) * filetype_extent +
233 flat_file->blocklens[i] >= start_off) {
234 st_index = i;
235 fwr_size = disp + flat_file->indices[i] +
236 ((ADIO_Offset) n_filetypes) * filetype_extent
237 + flat_file->blocklens[i] - start_off;
238 flag = 1;
239 break;
240 }
241 }
242 } /* while (!flag) */
243 } /* if (file_ptr_type == ADIO_INDIVIDUAL) */
244 else {
245 n_etypes_in_filetype = filetype_size / etype_size;
246 n_filetypes = offset / n_etypes_in_filetype;
247 etype_in_filetype = offset % n_etypes_in_filetype;
248 size_in_filetype = etype_in_filetype * etype_size;
249
250 sum = 0;
251 for (MPI_Count i = 0; i < flat_file->count; i++) {
252 sum += flat_file->blocklens[i];
253 if (sum > size_in_filetype) {
254 st_index = i;
255 fwr_size = sum - size_in_filetype;
256 abs_off_in_filetype = flat_file->indices[i] +
257 size_in_filetype - (sum - flat_file->blocklens[i]);
258 break;
259 }
260 }
261 /* abs. offset in bytes in the file */
262 start_off = disp + ((ADIO_Offset) n_filetypes) * filetype_extent + abs_off_in_filetype;
263 } /* else [file_ptr_type != ADIO_INDIVIDUAL] */
264
265 st_fwr_size = fwr_size;
266 st_n_filetypes = n_filetypes;
267
268 MPI_Count j = st_index;
269 f_data_wrote = MPL_MIN(st_fwr_size, bufsize);
270 n_filetypes = st_n_filetypes;
271
272 /* determine how many blocks in file to write */
273 total_blks_to_write = 1;
274 if (j < (flat_file->count - 1))
275 j++;
276 else {
277 j = 0;
278 n_filetypes++;
279 }
280
281 while (f_data_wrote < bufsize) {
282 f_data_wrote += flat_file->blocklens[j];
283 if (flat_file->blocklens[j])
284 total_blks_to_write++;
285 if (j < (flat_file->count - 1))
286 j++;
287 else {
288 j = 0;
289 n_filetypes++;
290 }
291 }
292
293 j = st_index;
294 n_filetypes = st_n_filetypes;
295 n_write_lists = total_blks_to_write;
296
297 rgs = (daos_range_t *) ADIOI_Malloc(sizeof(daos_range_t) * n_write_lists);
298
299 #if 0
300 for (i = 0; i < flat_file->count; i++)
301 fprintf(stderr, "(%d) FF: %d: off %lld, len %zu\n", mpi_rank, i,
302 flat_file->indices[i], flat_file->blocklens[i]);
303 fprintf(stderr, "NUM IO lists = %d\n", n_write_lists);
304 #endif
305
306 for (MPI_Count i = 0; i < n_write_lists; i++) {
307 if (!i) {
308 rgs[i].rg_idx = start_off;
309 rgs[i].rg_len = MPL_MIN(f_data_wrote, st_fwr_size);
310 #ifdef D_PRINT_IO
311 printf("(%d) %d: idx %lld len %zu\n", mpi_rank, i, rgs[i].rg_idx, rgs[i].rg_len);
312 #endif
313 } else {
314 if (flat_file->blocklens[j]) {
315 rgs[i].rg_idx = disp +
316 ((ADIO_Offset) n_filetypes) * filetype_extent + flat_file->indices[j];
317 rgs[i].rg_len = flat_file->blocklens[j];
318 #ifdef D_PRINT_IO
319 printf("(%d) %d: idx %lld len %zu\n",
320 mpi_rank, i, rgs[i].rg_idx, rgs[i].rg_len);
321 #endif
322 } else
323 i--;
324 }
325
326 if (j < (flat_file->count - 1))
327 j++;
328 else {
329 j = 0;
330 n_filetypes++;
331 }
332 }
333 }
334
335 /** set array location */
336 iod->iod_nr = n_write_lists;
337 iod->iod_rgs = rgs;
338 if (request)
339 aio_req->rgs = rgs;
340
341 if (rw_type == DAOS_WRITE) {
342 ret = dfs_writex(cont->dfs, cont->obj, iod, sgl, (request ? &aio_req->daos_event : NULL));
343 if (ret != 0) {
344 PRINT_MSG(stderr, "dfs_writex() failed with %d\n", ret);
345 *error_code = ADIOI_DAOS_err(myname, cont->obj_name, __LINE__, ret);
346 return;
347 }
348 *nbytes = bufsize;
349 } else if (rw_type == DAOS_READ) {
350 ret = dfs_readx(cont->dfs, cont->obj, iod, sgl, nbytes,
351 (request ? &aio_req->daos_event : NULL));
352 if (ret != 0) {
353 PRINT_MSG(stderr, "dfs_readx() failed with %d\n", ret);
354 *error_code = ADIOI_DAOS_err(myname, cont->obj_name, __LINE__, ret);
355 return;
356 }
357 }
358
359 if (file_ptr_type == ADIO_INDIVIDUAL) {
360 if (filetype_is_contig)
361 fd->fp_ind += bufsize;
362 else
363 fd->fp_ind = rgs[n_write_lists - 1].rg_idx + rgs[n_write_lists - 1].rg_len;
364 }
365
366 if (!err_flag)
367 *error_code = MPI_SUCCESS;
368
369 fd->fp_sys_posn = -1; /* clear this. */
370
371 #ifdef HAVE_STATUS_SET_BYTES
372 if (request == NULL && status) {
373 MPIR_Status_set_bytes(status, datatype, *nbytes);
374 }
375 #endif
376
377 if (!request) {
378 ADIOI_Free(iovs);
379 ADIOI_Free(rgs);
380 }
381
382 return;
383 }