簡體   English   中英

優化HDF5數據集的讀寫速度

[英]Optimising HDF5 dataset for Read/Write speed

我目前正在運行一個實驗,該實驗在空間上掃描目標並在每個離散像素處捕獲示波器軌跡。 通常我的走線長度為200Kpts。 掃描完整個目標之后,我會在空間上組合這些時域信號,並從本質上播放已掃描內容的電影。 我的掃描區域大小為330x220像素,因此整個數據集大於我必須使用的計算機上的RAM。

首先,我只是將每個示波器軌跡保存為一個numpy數組,然后在我的掃描完成下采樣/過濾等之后,然后以不會遇到內存問題的方式將影片拼接在一起。 但是,我現在無法進行下采樣,因為會發生混疊,因此需要訪問原始數據。

我已經開始考慮使用H5py將大型3D數據塊存儲在HDF5數據集中。 我的主要問題是我的塊大小分配。 我輸入的數據與要在其中讀取數據的平面正交。(據我所知)寫入數據的主要選擇是:

    #Fast write Slow read
    with h5py.File("test_h5py.hdf5","a") as f:
        dset = f.create_dataset("uncompchunk",(height,width,dataLen),chunks = (1,1,dataLen), dtype = 'f')
        for i in range(height):
            for j in range(width):
                dset[i,j,:] = np.random.random(200000)

要么

    #Slow write Fast read
    with h5py.File("test_h5py.hdf5","a") as f:
        dset = f.create_dataset("uncompchunk",(height,width,dataLen),chunks = (height,width,1), dtype = 'f')
        for i in range(height):
            for j in range(width):
                dset[i,j,:] = np.random.random(200000)     

有什么方法可以優化這兩種情況,從而使兩種方法都無法高效運行?

如果要通過分塊優化I / O性能,則應閱讀unidata中的這兩篇文章:

通用塊

優化訪問模式

如果只想獲得原始I / O性能,請考慮@titusjan建議

您的代碼中存在一些性能陷阱。

  1. 您在該行中使用某種形式的花式索引(在讀取/寫入HDF5-Dataset時,請勿更改數組變暗的數量。
  2. 如果您不讀取或寫入整個塊,請設置適當的塊緩存大小。 https://stackoverflow.com/a/42966070/4045774

  3. 減少對HDF5-Api的讀寫調用量。

  4. 選擇適當的塊大小(塊只能完全讀取/寫入,因此,如果您只需要塊的一部分,其余部分應保留在緩存中)

以下示例使用HDF5-API進行緩存。 要設置適當的緩存大小,我將使用h5py_cache。 https://pypi.python.org/pypi/h5py-cache/1.0.1

如果您自己進行緩存,則可以進一步提高性能。 (讀取和寫入整個塊)

寫作

# minimal chache size for reasonable performance would be 20*20*dataLen*4= 320 MB, lets take a bit more
with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=500*1024**2) as f:
    dset = f.create_dataset("uncompchunk",(height,width,dataLen),chunks = (20,20,20), dtype = 'f')
    for i in range(height):
        for j in range(width):
            # avoid fancy slicing
            dset[i:i+1,j:j+1,:] = expand_dims(expand_dims(np.random.random(200000),axis=0),axis=0)

# minimal chache size for reasonable performance would be height*width*500*4= 145 MB, lets take a bit more
with h5py_cache.File(h5pyfile, 'r+',chunk_cache_mem_size=200*1024**2) as f:
     dset=f["uncompchunk"]
     for i in xrange(0,dataLen):
         Image=np.squeeze(dset[:,:,i:i+1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM