forked from h5py/h5py
/
h5f.pyx
511 lines (368 loc) · 14 KB
/
h5f.pyx
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# This file is part of h5py, a Python interface to the HDF5 library.
#
# http://www.h5py.org
#
# Copyright 2008-2013 Andrew Collette and contributors
#
# License: Standard 3-clause BSD; see "license.txt" for full license terms
# and contributor agreement.
"""
Low-level operations on HDF5 file objects.
"""
include "config.pxi"
# Compile-time imports
from _objects cimport pdefault
from h5p cimport propwrap, PropFAID, PropFCID
from h5t cimport typewrap
from h5i cimport wrap_identifier
from h5ac cimport CacheConfig
from utils cimport emalloc, efree
from h5py import _objects
from ._objects import phil, with_phil
import h5fd
from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AsString
# Initialization
# === Public constants and data structures ====================================
ACC_TRUNC = H5F_ACC_TRUNC
ACC_EXCL = H5F_ACC_EXCL
ACC_RDWR = H5F_ACC_RDWR
ACC_RDONLY = H5F_ACC_RDONLY
IF HDF5_VERSION >= SWMR_MIN_HDF5_VERSION:
ACC_SWMR_WRITE = H5F_ACC_SWMR_WRITE
ACC_SWMR_READ = H5F_ACC_SWMR_READ
SCOPE_LOCAL = H5F_SCOPE_LOCAL
SCOPE_GLOBAL = H5F_SCOPE_GLOBAL
CLOSE_WEAK = H5F_CLOSE_WEAK
CLOSE_SEMI = H5F_CLOSE_SEMI
CLOSE_STRONG = H5F_CLOSE_STRONG
CLOSE_DEFAULT = H5F_CLOSE_DEFAULT
OBJ_FILE = H5F_OBJ_FILE
OBJ_DATASET = H5F_OBJ_DATASET
OBJ_GROUP = H5F_OBJ_GROUP
OBJ_DATATYPE = H5F_OBJ_DATATYPE
OBJ_ATTR = H5F_OBJ_ATTR
OBJ_ALL = H5F_OBJ_ALL
OBJ_LOCAL = H5F_OBJ_LOCAL
LIBVER_EARLIEST = H5F_LIBVER_EARLIEST
LIBVER_LATEST = H5F_LIBVER_LATEST
# === File operations =========================================================
@with_phil
def open(char* name, unsigned int flags=H5F_ACC_RDWR, PropFAID fapl=None):
"""(STRING name, UINT flags=ACC_RDWR, PropFAID fapl=None) => FileID
Open an existing HDF5 file. Keyword "flags" may be:
ACC_RDWR
Open in read-write mode
ACC_RDONLY
Open in readonly mode
Keyword fapl may be a file access property list.
"""
return FileID(H5Fopen(name, flags, pdefault(fapl)))
@with_phil
def create(char* name, int flags=H5F_ACC_TRUNC, PropFCID fcpl=None,
PropFAID fapl=None):
"""(STRING name, INT flags=ACC_TRUNC, PropFCID fcpl=None,
PropFAID fapl=None) => FileID
Create a new HDF5 file. Keyword "flags" may be:
ACC_TRUNC
Truncate an existing file, discarding its data
ACC_EXCL
Fail if a conflicting file exists
To keep the behavior in line with that of Python's built-in functions,
the default is ACC_TRUNC. Be careful!
"""
return FileID(H5Fcreate(name, flags, pdefault(fcpl), pdefault(fapl)))
@with_phil
def flush(ObjectID obj not None, int scope=H5F_SCOPE_LOCAL):
"""(ObjectID obj, INT scope=SCOPE_LOCAL)
Tell the HDF5 library to flush file buffers to disk. "obj" may
be the file identifier, or the identifier of any object residing in
the file. Keyword "scope" may be:
SCOPE_LOCAL
Flush only the given file
SCOPE_GLOBAL
Flush the entire virtual file
"""
H5Fflush(obj.id, <H5F_scope_t>scope)
@with_phil
def is_hdf5(char* name):
"""(STRING name) => BOOL
Determine if a given file is an HDF5 file. Note this raises an
exception if the file doesn't exist.
"""
return <bint>(H5Fis_hdf5(name))
@with_phil
def mount(ObjectID loc not None, char* name, FileID fid not None):
"""(ObjectID loc, STRING name, FileID fid)
Mount an open file on the group "name" under group loc_id. Note that
"name" must already exist.
"""
H5Fmount(loc.id, name, fid.id, H5P_DEFAULT)
@with_phil
def unmount(ObjectID loc not None, char* name):
"""(ObjectID loc, STRING name)
Unmount a file, mounted at "name" under group loc_id.
"""
H5Funmount(loc.id, name)
@with_phil
def get_name(ObjectID obj not None):
"""(ObjectID obj) => STRING
Determine the name of the file in which the specified object resides.
"""
cdef ssize_t size
cdef char* name
name = NULL
size = H5Fget_name(obj.id, NULL, 0)
assert size >= 0
name = <char*>emalloc(sizeof(char)*(size+1))
try:
H5Fget_name(obj.id, name, size+1)
pname = name
return pname
finally:
efree(name)
@with_phil
def get_obj_count(object where=OBJ_ALL, int types=H5F_OBJ_ALL):
"""(OBJECT where=OBJ_ALL, types=OBJ_ALL) => INT
Get the number of open objects.
where
Either a FileID instance representing an HDF5 file, or the
special constant OBJ_ALL, to count objects in all files.
type
Specify what kinds of object to include. May be one of OBJ*,
or any bitwise combination (e.g. OBJ_FILE | OBJ_ATTR).
The special value OBJ_ALL matches all object types, and
OBJ_LOCAL will only match objects opened through a specific
identifier.
"""
cdef hid_t where_id
if isinstance(where, FileID):
where_id = where.id
elif isinstance(where, int) or isinstance(where, long):
where_id = where
else:
raise TypeError("Location must be a FileID or OBJ_ALL.")
return H5Fget_obj_count(where_id, types)
@with_phil
def get_obj_ids(object where=OBJ_ALL, int types=H5F_OBJ_ALL):
"""(OBJECT where=OBJ_ALL, types=OBJ_ALL) => LIST
Get a list of identifier instances for open objects.
where
Either a FileID instance representing an HDF5 file, or the
special constant OBJ_ALL, to list objects in all files.
type
Specify what kinds of object to include. May be one of OBJ*,
or any bitwise combination (e.g. OBJ_FILE | OBJ_ATTR).
The special value OBJ_ALL matches all object types, and
OBJ_LOCAL will only match objects opened through a specific
identifier.
"""
cdef int count
cdef int i
cdef hid_t where_id
cdef hid_t *obj_list = NULL
cdef list py_obj_list = []
if isinstance(where, FileID):
where_id = where.id
else:
try:
where_id = int(where)
except TypeError:
raise TypeError("Location must be a FileID or OBJ_ALL.")
try:
count = H5Fget_obj_count(where_id, types)
obj_list = <hid_t*>emalloc(sizeof(hid_t)*count)
if count > 0: # HDF5 complains that obj_list is NULL, even if count==0
H5Fget_obj_ids(where_id, types, count, obj_list)
for i from 0<=i<count:
py_obj_list.append(wrap_identifier(obj_list[i]))
# The HDF5 function returns a borrowed reference for each hid_t.
H5Iinc_ref(obj_list[i])
return py_obj_list
finally:
efree(obj_list)
# === FileID implementation ===================================================
cdef class FileID(GroupID):
"""
Represents an HDF5 file identifier.
These objects wrap a small portion of the H5F interface; all the
H5F functions which can take arbitrary objects in addition to
file identifiers are provided as functions in the h5f module.
Properties:
* name: File name on disk
Behavior:
* Hashable: Yes, unique to the file (but not the access mode)
* Equality: Hash comparison
"""
property name:
""" File name on disk (according to h5f.get_name()) """
def __get__(self):
with phil:
return get_name(self)
@with_phil
def close(self):
"""()
Terminate access through this identifier. Note that depending on
what property list settings were used to open the file, the
physical file might not be closed until all remaining open
identifiers are freed.
"""
self._close()
_objects.nonlocal_close()
@with_phil
def reopen(self):
"""() => FileID
Retrieve another identifier for a file (which must still be open).
The new identifier is guaranteed to neither be mounted nor contain
a mounted file.
"""
return FileID(H5Freopen(self.id))
@with_phil
def get_filesize(self):
"""() => LONG size
Determine the total size (in bytes) of the HDF5 file,
including any user block.
"""
cdef hsize_t size
H5Fget_filesize(self.id, &size)
return size
@with_phil
def get_create_plist(self):
"""() => PropFCID
Retrieve a copy of the file creation property list used to
create this file.
"""
return propwrap(H5Fget_create_plist(self.id))
@with_phil
def get_access_plist(self):
"""() => PropFAID
Retrieve a copy of the file access property list which manages access
to this file.
"""
return propwrap(H5Fget_access_plist(self.id))
@with_phil
def get_freespace(self):
"""() => LONG freespace
Determine the amount of free space in this file. Note that this
only tracks free space until the file is closed.
"""
return H5Fget_freespace(self.id)
@with_phil
def get_intent(self):
""" () => INT
Determine the file's write intent, either of:
- H5F_ACC_RDONLY
- H5F_ACC_RDWR
"""
cdef unsigned int mode
H5Fget_intent(self.id, &mode)
return mode
@with_phil
def get_vfd_handle(self):
""" () => INT
Retrieve the file handle used by the virtual file driver.
This method is only functional when the the SEC2 driver is used.
"""
if H5Pget_driver(H5Fget_access_plist(self.id)) != h5fd.SEC2:
raise NotImplementedError
cdef int *handle
H5Fget_vfd_handle(self.id, H5Fget_access_plist(self.id), <void**>&handle)
return handle[0]
IF HDF5_VERSION >= (1, 8, 9):
@with_phil
def get_file_image(self):
""" () => BYTES
Retrieves a copy of the image of an existing, open file.
Feature requries: 1.8.9
"""
cdef ssize_t size
size = H5Fget_file_image(self.id, NULL, 0)
image = PyBytes_FromStringAndSize(NULL, size)
H5Fget_file_image(self.id, PyBytes_AsString(image), size)
return image
IF MPI and HDF5_VERSION >= (1, 8, 9):
@with_phil
def set_mpi_atomicity(self, bint atomicity):
""" (BOOL atomicity)
For MPI-IO driver, set to atomic (True), which guarantees sequential
I/O semantics, or non-atomic (False), which improves performance.
Default is False.
Feature requires: 1.8.9 and Parallel HDF5
"""
H5Fset_mpi_atomicity(self.id, <hbool_t>atomicity)
@with_phil
def get_mpi_atomicity(self):
""" () => BOOL
Return atomicity setting for MPI-IO driver.
Feature requires: 1.8.9 and Parallel HDF5
"""
cdef hbool_t atom
H5Fget_mpi_atomicity(self.id, &atom)
return <bint>atom
@with_phil
def get_mdc_hit_rate(self):
"""() => DOUBLE
Retrieve the cache hit rate
"""
cdef double hit_rate
H5Fget_mdc_hit_rate(self.id, &hit_rate)
return hit_rate
@with_phil
def get_mdc_size(self):
"""() => (max_size, min_clean_size, cur_size, cur_num_entries) [SIZE_T, SIZE_T, SIZE_T, INT]
Obtain current metadata cache size data for specified file.
"""
cdef size_t max_size
cdef size_t min_clean_size
cdef size_t cur_size
cdef int cur_num_entries
H5Fget_mdc_size(self.id, &max_size, &min_clean_size, &cur_size, &cur_num_entries)
return (max_size, min_clean_size, cur_size, cur_num_entries)
@with_phil
def reset_mdc_hit_rate_stats(self):
"""no return
rests the hit-rate statistics
"""
H5Freset_mdc_hit_rate_stats(self.id)
@with_phil
def get_mdc_config(self):
"""() => CacheConfig
Returns an object that stores all the information about the meta-data cache
configuration
"""
cdef CacheConfig config = CacheConfig()
H5Fget_mdc_config(self.id, &config.cache_config)
return config
@with_phil
def set_mdc_config(self, CacheConfig config not None):
"""(CacheConfig) => None
Returns an object that stores all the information about the meta-data cache
configuration
"""
# I feel this should have some sanity checking to make sure that
H5Fset_mdc_config(self.id, &config.cache_config)
IF HDF5_VERSION >= SWMR_MIN_HDF5_VERSION:
@with_phil
def start_swmr_write(self):
""" no return
Enables SWMR writing mode for a file.
This function will activate SWMR writing mode for a file associated
with file_id. This routine will prepare and ensure the file is safe
for SWMR writing as follows:
* Check that the file is opened with write access (H5F_ACC_RDWR).
* Check that the file is opened with the latest library format
to ensure data structures with check-summed metadata are used.
* Check that the file is not already marked in SWMR writing mode.
* Enable reading retries for check-summed metadata to remedy
possible checksum failures from reading inconsistent metadata
on a system that is not atomic.
* Turn off usage of the library’s accumulator to avoid possible
ordering problem on a system that is not atomic.
* Perform a flush of the file’s data buffers and metadata to set
a consistent state for starting SWMR write operations.
Library objects are groups, datasets, and committed datatypes. For
the current implementation, groups and datasets can remain open when
activating SWMR writing mode, but not committed datatypes. Attributes
attached to objects cannot remain open.
Feature requires: 1.9.178 HDF5
"""
H5Fstart_swmr_write(self.id)