使用h5py高级界面时如何设置缓存设置? [英] How to set cache settings while using h5py high level interface?
问题描述
我正在尝试增加HDF5文件的缓存大小,但似乎无法正常工作. 这就是我所拥有的:
I'm trying to increase cache size for my HDF5 files, but it doesn't seem to be working. This is what I have:
import h5py
with h5py.File("test.h5", 'w') as fid:
# cache settings of file
cacheSettings = list(fid.id.get_access_plist().get_cache())
print cacheSettings
# increase cache
cacheSettings[2] = int(5 * cacheSettings[2])
print cacheSettings
# read cache settings from file
fid.id.get_access_plist().set_cache(*cacheSettings)
print fid.id.get_access_plist().get_cache()
以下是输出:
[0, 521, 1048576, 0.75]
[0, 521, 5242880, 0.75]
(0, 521, 1048576, 0.75)
有人知道为什么阅读有效但设置无效吗?
关闭并重新打开文件似乎也无济于事.
Any idea why reading works, but setting doesn't?
Closing and reopening the file doesn't seem to help either.
推荐答案
如果您使用的是h5py 2.9.0或更高版本,请参见迈克的答案.
If you are using h5py version 2.9.0 or newer, see Mike's answer.
根据文档, get_access_plist()
返回文件访问属性列表的副本.因此,修改副本不会影响原始副本也就不足为奇了.
According to the docs, get_access_plist()
returns a copy of the file access property list. So it is not surprising that modifying the copy does not affect the original.
看来高级界面没有提供更改缓存设置的方法.
It appears the high-level interface does not provide a way to change the cache settings.
这里是使用低级界面的方法.
Here is how you could do it using the low-level interface.
propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS)
settings = list(propfaid.get_cache())
print(settings)
# [0, 521, 1048576, 0.75]
settings[2] *= 5
propfaid.set_cache(*settings)
settings = propfaid.get_cache()
print(settings)
# (0, 521, 5242880, 0.75)
上面的代码创建了 PropFAID .然后,我们可以打开文件并以这种方式获取 FileID :
The above creates a PropFAID. We can then open the file and get a FileID this way:
import contextlib
with contextlib.closing(h5py.h5f.open(
filename, flags=h5py.h5f.ACC_RDWR, fapl=propfaid)) as fid:
# <h5py.h5f.FileID object at 0x9abc694>
settings = list(fid.get_access_plist().get_cache())
print(settings)
# [0, 521, 5242880, 0.75]
然后我们可以使用fid
通过将fid
传递给h5py.File
来通过高级界面打开文件:
And we can use the fid
to open the file with the high-level interface by passing fid
to h5py.File
:
f = h5py.File(fid)
print(f.id.get_access_plist().get_cache())
# (0, 521, 5242880, 0.75)
因此,您仍然可以使用高级界面,但这需要一些 努力到达那里.另一方面,如果仅将其提炼成必需品,也许还不错:
Thus, you can still use the high-level interface, but it takes some fiddling to get there. On the other hand, if you distill it to just the essentials, perhaps it isn't so bad:
import h5py
import contextlib
filename = '/tmp/foo.hdf5'
propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS)
settings = list(propfaid.get_cache())
settings[2] *= 5
propfaid.set_cache(*settings)
with contextlib.closing(h5py.h5f.open(filename, fapl=propfaid)) as fid:
f = h5py.File(fid)
这篇关于使用h5py高级界面时如何设置缓存设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!