I have been facing the following issue: I have to loop over num_objects = 897 objects, for every single one of which I have to use num_files = 2120 h5 files. The files are very large, each being 1.48 GB and the content of interest to me is 3 arrays of floats of size 256 x 256 x 256 contained in every file (v1,v2 and v3). This is to say, the loop looks like:
for i in range(num_objects):
...
for j in range(num_files):
some operation with the three 256 x 256 x 256 arrays in each file
and my current way of loading them is by doing the following in the innermost loop:
f = h5py.File('output_'+str(q)+'.h5','r')
key1 = np.array(f['key1'])
v1=key1[:,:,:,0]
v2=key2[:,:,:,1]
v3=key3[:,:,:,2]
The above option of loading the files every time for every object is obviously very slow. On the other hand, loading all files at once and importing them in a dictionary results in excessive use of memory and my job gets killed. Some diagnostics:
- The above way takes 0.48 seconds per file, per object, resulting in a total of 10.5 days (!) spent only on this operation.
- I tried exporting
key1to npz files, but it was actually slower by 0.7 seconds per file. - I exported
v1,v2andv3individually for every file to npz files (i.e. 3 npz files for each h5 file), but that saved me only 1.5 day in total.
Does anyone have some other idea/suggestion I could try to be fast and at the same time not limited by excessive memory usage?