在使用h5py高级接口时如何设置缓存设置?

2024-10-01 22:40:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图增加我的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()

输出如下:

^{pr2}$

你知道为什么读书有用,但是环境不行吗?
关闭和重新打开文件似乎也没有帮助。在


Tags: 文件importidcachegetsettingsaccesswith
3条回答

从h5py版本2.9.0开始,这种行为现在可以直接通过主h5py.File接口获得。有三个参数控制“原始数据块缓存”rdcc_nbytesrdcc_w0、和{},这些参数被记录为here。OP试图调整rdcc_nbytes设置,现在可以简单地按照

import h5py

with h5py.File("test.h5", "w", rdcc_nbytes=5242880) as fid:
    # Use fid for something here

唯一的区别是,你必须知道你实际需要多少空间,而不是仅仅按操作需要乘以5。当前默认值与找到的OP相同。当然,如果你真的想用编程的方式来实现,你只需打开它一次,获取缓存,关闭它,然后用所需的参数重新打开。在

如果您使用的是h5py版本2.9.0或更新版本,请参见Mike's answer。在


根据the docsget_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)

上面创建了一个PropFAID。然后我们可以打开该文件并通过以下方式获得FileID

^{pr2}$

我们可以使用fid通过向h5py.File传递fid来打开具有高级接口的文件:

    f = h5py.File(fid)
    print(f.id.get_access_plist().get_cache())
    # (0, 521, 5242880, 0.75)

因此,您仍然可以使用高级接口,但它需要一些 费尽心思去那里。另一方面,如果你把它提炼成最基本的东西,也许也没那么糟糕:

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-cache项目可能会有帮助,尽管我没有使用它:

import h5py_cache
with h5py_cache.File('test.h5', chunk_cache_mem_size=1024**3, 'a') as f:
f.create_dataset(...)

相关问题 更多 >

    热门问题