HDF5 是否支持并发读取或写入不同的文件?

Does HDF5 support concurrent reads, or writes to different files?

我正在尝试了解 HDF5 并发的限制。

HDF5 有两个版本:并行 HDF5默认。并行版本目前在 Ubuntu 中提供,Anaconda 中默认提供(由 --enable-parallel 标志判断)。

我知道并行写入同一个文件是不可能的。但是,我不完全理解以下操作在默认或并行构建下的可能扩展范围:

此外,anaconda 没有默认启用 --enable-parallel 标志的原因是什么? (https://github.com/conda/conda-recipes/blob/master/hdf5/build.sh)

AFAICT,可以通过三种方式构建libhdf5:

  • 既不支持 thread-safety 也不支持 MPI(如您发布的 conda 配方)
  • 有 MPI 支持但没有线程安全
  • 具有线程安全性但不支持 MPI

也就是说,--enable-threadsafe--enable-parallel标志是互斥的(https://www.hdfgroup.org/hdf5-quest.html#p5thread)。

对于一个甚至多个文件的并发读取,答案是需要线程安全(https://www.hdfgroup.org/hdf5-quest.html#tsafe):

Concurrent access to one or more HDF5 file(s) from multiple threads in the same process will not work with a non-thread-safe build of the HDF5 library. The pre-built binaries that are available for download are not thread-safe.

Users are often surprised to learn that (1) concurrent access to different datasets in a single HDF5 file and (2) concurrent access to different HDF5 files both require a thread-safe version of the HDF5 library. Although each thread in these examples is accessing different data, the HDF5 library modifies global data structures that are independent of a particular HDF5 dataset or HDF5 file. HDF5 relies on a semaphore around the library API calls in the thread-safe version of the library to protect the data structure from corruption by simultaneous manipulation from different threads. Examples of HDF5 library global data structures that must be protected are the freespace manager and open file lists.

编辑: 上面的链接不再有效,因为 HDF Group 重组了他们的网站。有一个页面 Questions about thread-safety and concurrent access in the HDF5 Knowledge Base 包含一些有用的信息。

虽然文章中只提到了单个进程上的并发线程,但它似乎同样适用于分叉的子进程:请参阅此 h5py multiprocessing example

现在,对于 并行 访问,您可能想要使用 "Parallel HDF5" 但这些功能需要使用 MPI。 h5py 支持此模式,但更复杂和深奥,并且可能比 thread-safe 模式更不便携。更重要的是,尝试天真地使用并行构建的 libhdf5 进行并发读取将导致意外结果,因为 该库不是 thread-safe.

除了效率之外,thread-safe 构建标志的一个限制是缺少 Windows 支持 (https://www.hdfgroup.org/hdf5-quest.html#gconc):

The thread-safe version of HDF5 is currently not tested or supported on MS Windows platforms. A user was able to get this working on Windows 64-bit and contributed his Windows 64-bit Pthreads patches.

从 Python 读取(不同!)文件时出现奇怪的损坏结果绝对是出乎意料和令人沮丧的,因为并发读取访问是 HDF5 吹捧的 "features" 之一。也许 conda 更好的默认方法是在支持它的平台上包含 --enable-threadsafe,但我想你最终会得到 platform-specific 行为。也许应该为三种构建模式提供单独的包?

补充一下: 我认为进行读取访问的独立并发进程(即python)应该是fine

HDF5 1.10 将支持 Single Writer Multiple Reader,more infos and also h5py 2.5.0 will have support