forked from NCAR/ParallelIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpio_file.c
More file actions
494 lines (436 loc) · 16 KB
/
pio_file.c
File metadata and controls
494 lines (436 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/**
* @file
* PIO File Handling
*/
#include <config.h>
#include <pio.h>
#include <pio_internal.h>
#include <uthash.h>
/**
* @defgroup PIO_open_file_c Open a File
* Open an existing netCDF file with PIO in C.
*
* @defgroup PIO_create_file_c Create a File
* Create a new netCDF file with PIO in C.
*
* @defgroup PIO_sync_file_c Sync a File
* Flush buffers and sync data to disk in C.
*
* @defgroup PIO_close_file_c Close a File
* Close a file in C.
*
*/
/* This is the next ncid that will be used when a file is opened or
created. We start at 128 so that it will be easy for us to notice
that it's not netcdf (starts at 4), pnetcdf (starts at 0) or
netCDF-4/HDF5 (starts at 65xxx). Also, when used with netCDF
intgration, this will allow the user to have 127 normal netCDF
files open, as well as many PIO ones. */
int pio_next_ncid = 128;
#ifdef USE_MPE
/* The event numbers for MPE logging. */
extern int event_num[2][NUM_EVENTS];
#endif /* USE_MPE */
/**
* Open an existing file using PIO library.
*
* If the open fails, try again as netCDF serial before giving
* up. Input parameters are read on comp task 0 and ignored elsewhere.
*
* Note that the file is opened with default fill mode, NOFILL for
* pnetcdf, and FILL for netCDF classic and netCDF-4 files.
*
* @param iosysid : A defined pio system descriptor (input)
* @param ncidp : A pio file descriptor (output)
* @param iotype : A pio output format (input)
* @param filename : The filename to open
* @param mode : The netcdf mode for the open operation
* @return 0 for success, error code otherwise.
* @ingroup PIO_open_file_c
* @author Jim Edwards, Ed Hartnett
*/
int
PIOc_openfile(int iosysid, int *ncidp, int *iotype, const char *filename,
int mode)
{
PLOG((1, "PIOc_openfile iosysid %d *iotype %d filename %s mode %d", iosysid,
iotype ? *iotype: 0, filename, mode));
return PIOc_openfile_retry(iosysid, ncidp, iotype, filename, mode, 1, 0);
}
/**
* Open an existing file using PIO library.
*
* This is like PIOc_openfile(), but if the open fails, this function
* will not try to open again as netCDF serial before giving
* up. Input parameters are read on comp task 0 and ignored elsewhere.
*
* Note that the file is opened with default fill mode, NOFILL for
* pnetcdf, and FILL for netCDF classic and netCDF-4 files.
*
* @param iosysid : A defined pio system descriptor (input)
* @param ncidp : A pio file descriptor (output)
* @param iotype : A pio output format (input)
* @param filename : The filename to open
* @param mode : The netcdf mode for the open operation
* @return 0 for success, error code otherwise.
* @ingroup PIO_open_file_c
* @author Ed Hartnett
*/
int
PIOc_openfile2(int iosysid, int *ncidp, int *iotype, const char *filename,
int mode)
{
PLOG((1, "PIOc_openfile2 iosysid %d *iotype %d filename %s mode %d", iosysid,
iotype ? *iotype : 0, filename, mode));
return PIOc_openfile_retry(iosysid, ncidp, iotype, filename, mode, 0, 0);
}
/**
* Open an existing file using PIO library.
*
* Input parameters are read on comp task 0 and ignored elsewhere.
*
* @param iosysid A defined pio system descriptor
* @param path The filename to open
* @param mode The netcdf mode for the open operation
* @param ncidp pointer to int where ncid will go
* @return 0 for success, error code otherwise.
* @ingroup PIO_open_file_c
* @author Ed Hartnett
*/
int
PIOc_open(int iosysid, const char *path, int mode, int *ncidp)
{
int iotype;
iosystem_desc_t *ios; /* Pointer to io system information. */
int ret;
PLOG((1, "PIOc_open iosysid = %d path = %s mode = %x", iosysid, path, mode));
/* Get the IO system info from the id. */
if (!(ios = pio_get_iosystem_from_id(iosysid)))
return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__);
/* Find the IOTYPE from the mode flag. */
if ((ret = find_iotype_from_omode(mode, &iotype)))
return pio_err(ios, NULL, ret, __FILE__, __LINE__);
/* Open the file. If the open fails, do not retry as serial
* netCDF. Just return the error code. */
return PIOc_openfile_retry(iosysid, ncidp, &iotype, path, mode, 0, 0);
}
/**
* Create a new file using pio. Input parameters are read on comp task
* 0 and ignored elsewhere. NOFILL mode will be turned on in all
* cases.
*
* @param iosysid A defined pio system ID, obtained from
* PIOc_InitIntercomm() or PIOc_InitAsync().
* @param ncidp A pointer that gets the ncid of the newly created
* file.
* @param iotype A pointer to a pio output format. Must be one of
* PIO_IOTYPE_PNETCDF, PIO_IOTYPE_NETCDF, PIO_IOTYPE_NETCDF4C, or
* PIO_IOTYPE_NETCDF4P.
* @param filename The filename to create.
* @param mode The netcdf mode for the create operation.
* @returns 0 for success, error code otherwise.
* @ingroup PIO_create_file_c
* @author Jim Edwards, Ed Hartnett
*/
int
PIOc_createfile(int iosysid, int *ncidp, int *iotype, const char *filename,
int mode)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
int ret; /* Return code from function calls. */
/* Get the IO system info from the id. */
if (!(ios = pio_get_iosystem_from_id(iosysid)))
return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__);
PLOG((1, "PIOc_createfile iosysid = %d iotype = %d filename = %s mode = %d",
iosysid, *iotype, filename, mode));
/* Create the file. */
if ((ret = PIOc_createfile_int(iosysid, ncidp, iotype, filename, mode, 0)))
return pio_err(ios, NULL, ret, __FILE__, __LINE__);
/* Set the fill mode to NOFILL. */
if ((ret = PIOc_set_fill(*ncidp, NC_NOFILL, NULL)))
return ret;
return ret;
}
/**
* Open a new file using pio. The default fill mode will be used (FILL
* for netCDF and netCDF-4 formats, NOFILL for pnetcdf.) Input
* parameters are read on comp task 0 and ignored elsewhere.
*
* @param iosysid : A defined pio system descriptor (input)
* @param path : The filename to create.
* @param cmode : The netcdf mode for the create operation.
* @param ncidp : A pio file descriptor (output)
*
* @return 0 for success, error code otherwise.
* @ingroup PIO_create_file_c
* @author Ed Hartnett
*/
int
PIOc_create(int iosysid, const char *path, int cmode, int *ncidp)
{
int iotype; /* The PIO IO type. */
iosystem_desc_t *ios; /* Pointer to io system information. */
int ret;
PLOG((1, "PIOc_create iosysid = %d path = %s cmode = %x", iosysid, path,
cmode));
/* Get the IO system info from the id. */
if (!(ios = pio_get_iosystem_from_id(iosysid)))
return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__);
/* Find the IOTYPE from the mode flag. */
if ((ret = find_iotype_from_cmode(cmode, &iotype)))
return pio_err(ios, NULL, ret, __FILE__, __LINE__);
return PIOc_createfile_int(iosysid, ncidp, &iotype, path, cmode, 0);
}
/**
* Close a file previously opened with PIO.
*
* @param ncid: the file pointer
* @returns PIO_NOERR for success, error code otherwise.
* @ingroup PIO_close_file_c
* @author Jim Edwards, Ed Hartnett
*/
int
PIOc_closefile(int ncid)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
int ierr = PIO_NOERR; /* Return code from function calls. */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function codes. */
#ifdef USE_MPE
pio_start_mpe_log(CLOSE);
#endif /* USE_MPE */
PLOG((1, "PIOc_closefile ncid = %d", ncid));
/* Find the info about this file. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, ierr, __FILE__, __LINE__);
ios = file->iosystem;
/* Sync changes before closing on all tasks if async is not in
* use, but only on non-IO tasks if async is in use. */
if (!ios->async || !ios->ioproc)
if (file->writable)
PIOc_sync(ncid);
/* If async is in use and this is a comp tasks, then the compmain
* sends a msg to the pio_msg_handler running on the IO main and
* waiting for a message. Then broadcast the ncid over the intercomm
* to the IO tasks. */
if (ios->async)
{
if (!ios->ioproc)
{
int msg = PIO_MSG_CLOSE_FILE;
if (ios->compmain == MPI_ROOT)
mpierr = MPI_Send(&msg, 1, MPI_INT, ios->ioroot, 1, ios->union_comm);
if (!mpierr)
mpierr = MPI_Bcast(&ncid, 1, MPI_INT, ios->compmain, ios->intercomm);
}
/* Handle MPI errors. */
if ((mpierr2 = MPI_Bcast(&mpierr, 1, MPI_INT, ios->comproot, ios->my_comm)))
return check_mpi(NULL, file, mpierr2, __FILE__, __LINE__);
if (mpierr)
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
}
/* If this is an IO task, then call the netCDF function. */
if (ios->ioproc)
{
switch (file->iotype)
{
#ifdef _NETCDF4
case PIO_IOTYPE_NETCDF4P:
ierr = nc_close(file->fh);
break;
case PIO_IOTYPE_NETCDF4C:
#endif
case PIO_IOTYPE_NETCDF:
if (ios->io_rank == 0)
ierr = nc_close(file->fh);
break;
#ifdef _PNETCDF
case PIO_IOTYPE_PNETCDF:
if (file->writable){
ierr = ncmpi_wait_all(file->fh, NC_REQ_ALL, NULL, NULL);
ierr = ncmpi_buffer_detach(file->fh);
}
ierr = ncmpi_close(file->fh);
break;
#endif
case PIO_IOTYPE_GDAL:
if (ios->io_rank == 0) {
ierr = GDALClose((void*)file->hDS);
printf("GDALClose ierr: %d\n",ierr);
}
break;
default:
return pio_err(ios, file, PIO_EBADIOTYPE, __FILE__, __LINE__);
}
}
/* Broadcast and check the return code. */
if ((mpierr = MPI_Bcast(&ierr, 1, MPI_INT, ios->ioroot, ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if (ierr)
return check_netcdf(file, ierr, __FILE__, __LINE__);
/* Delete file from our list of open files. */
if ((ierr = pio_delete_file_from_list(ncid)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
#ifdef USE_MPE
pio_stop_mpe_log(CLOSE, __func__);
#endif /* USE_MPE */
return ierr;
}
/**
* Delete a file.
*
* @param iosysid a pio system handle.
* @param filename a filename.
* @returns PIO_NOERR for success, error code otherwise.
* @author Jim Edwards, Ed Hartnett
*/
int
PIOc_deletefile(int iosysid, const char *filename)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
int ierr = PIO_NOERR; /* Return code from function calls. */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function codes. */
int msg = PIO_MSG_DELETE_FILE;
size_t len;
PLOG((1, "PIOc_deletefile iosysid = %d filename = %s", iosysid, filename));
/* Get the IO system info from the id. */
if (!(ios = pio_get_iosystem_from_id(iosysid)))
return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__);
/* If async is in use, send message to IO main task. */
if (ios->async)
{
if (!ios->ioproc)
{
if (ios->comp_rank==0)
mpierr = MPI_Send(&msg, 1,MPI_INT, ios->ioroot, 1, ios->union_comm);
len = strlen(filename);
if (!mpierr)
mpierr = MPI_Bcast(&len, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast((void *)filename, len + 1, MPI_CHAR, ios->compmain,
ios->intercomm);
PLOG((2, "Bcast len = %d filename = %s", len, filename));
}
/* Handle MPI errors. */
if ((mpierr2 = MPI_Bcast(&mpierr, 1, MPI_INT, ios->comproot, ios->my_comm)))
return check_mpi(ios, NULL, mpierr2, __FILE__, __LINE__);
if (mpierr)
return check_mpi(ios, NULL, mpierr, __FILE__, __LINE__);
PLOG((3, "done hanlding errors mpierr = %d", mpierr));
}
/* If this is an IO task, then call the netCDF function. The
* barriers are needed to assure that no task is trying to operate
* on the file while it is being deleted. IOTYPE is not known, but
* nc_delete() will delete any type of file. */
if (ios->ioproc)
{
mpierr = MPI_Barrier(ios->io_comm);
if (!mpierr && ios->io_rank == 0)
ierr = nc_delete(filename);
if (!mpierr)
mpierr = MPI_Barrier(ios->io_comm);
}
PLOG((2, "PIOc_deletefile ierr = %d", ierr));
/* Broadcast and check the return code. */
if ((mpierr = MPI_Bcast(&ierr, 1, MPI_INT, ios->ioroot, ios->my_comm)))
return check_mpi(ios, NULL, mpierr2, __FILE__, __LINE__);
if (ierr)
return check_netcdf2(ios, NULL, ierr, __FILE__, __LINE__);
return ierr;
}
/**
* PIO interface to nc_sync This routine is called collectively by all
* tasks in the communicator ios.union_comm.
*
* Refer to the <A
* HREF="http://www.unidata.ucar.edu/software/netcdf/docs/modules.html"
* target="_blank"> netcdf </A> documentation.
*
* @param ncid the ncid of the file to sync.
* @returns PIO_NOERR for success, error code otherwise.
* @ingroup PIO_sync_file_c
* @author Jim Edwards, Ed Hartnett
*/
int
PIOc_sync(int ncid)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function codes. */
int ierr = PIO_NOERR; /* Return code from function calls. */
PLOG((1, "PIOc_sync ncid = %d", ncid));
/* Get the file info from the ncid. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, ierr, __FILE__, __LINE__);
ios = file->iosystem;
/* Flush data buffers on computational tasks. */
if (!ios->async || !ios->ioproc)
{
if (file->writable)
{
wmulti_buffer *wmb, *twmb;
PLOG((3, "PIOc_sync checking buffers"));
HASH_ITER(hh, file->buffer, wmb, twmb)
{
/* If there are any data arrays waiting in the
* multibuffer, flush it. */
if (wmb->num_arrays > 0)
flush_buffer(ncid, wmb, true);
HASH_DEL(file->buffer, wmb);
free(wmb);
}
file->buffer = NULL;
}
}
/* If async is in use, send message to IO main tasks. */
if (ios->async)
{
if (!ios->ioproc)
{
int msg = PIO_MSG_SYNC;
if (ios->compmain == MPI_ROOT)
mpierr = MPI_Send(&msg, 1, MPI_INT, ios->ioroot, 1, ios->union_comm);
if (!mpierr)
mpierr = MPI_Bcast(&ncid, 1, MPI_INT, ios->compmain, ios->intercomm);
}
/* Handle MPI errors. */
if ((mpierr2 = MPI_Bcast(&mpierr, 1, MPI_INT, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr2, __FILE__, __LINE__);
if (mpierr)
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
}
/* Call the sync function on IO tasks. */
if (file->writable)
{
if (ios->ioproc)
{
switch(file->iotype)
{
#ifdef _NETCDF4
case PIO_IOTYPE_NETCDF4P:
ierr = nc_sync(file->fh);
break;
case PIO_IOTYPE_NETCDF4C:
#endif
case PIO_IOTYPE_NETCDF:
if (ios->io_rank == 0)
ierr = nc_sync(file->fh);
break;
#ifdef _PNETCDF
case PIO_IOTYPE_PNETCDF:
flush_output_buffer(file, true, 0);
break;
#endif
default:
return pio_err(ios, file, PIO_EBADIOTYPE, __FILE__, __LINE__);
}
}
PLOG((2, "PIOc_sync ierr = %d", ierr));
}
/* Broadcast and check the return code. */
// if ((mpierr = MPI_Bcast(&ierr, 1, MPI_INT, ios->ioroot, ios->my_comm)))
// return check_mpi(ios, NULL, mpierr, __FILE__, __LINE__);
if (ierr)
return check_netcdf2(ios, NULL, ierr, __FILE__, __LINE__);
return ierr;
}