我正在尝试增加我的HDF5文件的缓存大小,但它似乎没有工作.这就是我所拥有的:
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()
Run Code Online (Sandbox Code Playgroud)
这是输出:
[0, 521, 1048576, 0.75]
[0, 521, 5242880, 0.75]
(0, 521, 1048576, 0.75)
Run Code Online (Sandbox Code Playgroud)
知道为什么阅读有效,但设置没有?
关闭并重新打开文件似乎也没有帮助.
根据文档,get_access_plist()返回文件访问属性列表的副本.因此,修改副本不会影响原始版本也就不足为奇了.
看来高级接口没有提供更改缓存设置的方法.
以下是使用低级接口的方法.
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)
Run Code Online (Sandbox Code Playgroud)
以上创建了PropFAID.然后我们可以打开文件并以这种方式获取FileID:
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]
Run Code Online (Sandbox Code Playgroud)
我们可以fid通过传递fid给h5py.File:使用高级接口打开文件:
f = h5py.File(fid)
print(f.id.get_access_plist().get_cache())
# (0, 521, 5242880, 0.75)
Run Code Online (Sandbox Code Playgroud)
因此,您仍然可以使用高级接口,但需要一些小小的功能才能实现.另一方面,如果你把它提炼成必需品,也许它不是那么糟糕:
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)
Run Code Online (Sandbox Code Playgroud)
从 h5py 2.9.0 版开始,现在可以直接通过主h5py.File界面使用此行为。有迹象表明,控制“原始数据块缓存”三个参数- rdcc_nbytes,rdcc_w0和rdcc_nslots-这是记录在这里。OP 正在尝试调整rdcc_nbytes设置,现在可以简单地完成
import h5py
with h5py.File("test.h5", "w", rdcc_nbytes=5242880) as f:
f.create_dataset(...)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我假设您知道实际需要多少空间,而不是像 OP 想要的那样乘以 5。当前默认值与找到的 OP 相同。当然,如果您真的想以编程方式执行此操作,您可以只打开一次,获取缓存,关闭它,然后使用所需的参数重新打开。