我在将c++中的线性数组保存到hdf5文件中的三维数据集时遇到了一个小问题。
内存中的布局如下所示:
|---+---+---+---+---+---+---+---+---+---+----+----|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---+---+---+---+---+---+---+---+---+---+----+----|当它被程序这样解释时:
x
|---+---+---|
| 0 | 1 | 2 |
z=0 |---+---+---| y
| 3 | 4 | 5 |
|---+---+---|
x
|---+----+----|
| 6 | 7 | 8 |
z=1 |---+----+----| y
| 9 | 10 | 11 |
|---+----+----|使用以下代码,将此数组保存到HDF5文件中的三维数据集
std::vector<int> buffer;
for(int i = 0; i < 12; ++i)
buffer.push_back(i);
hsize_t dims[3] = {2,3,2};
hisze_t mem_dim[1] = {12};
hid_t file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
hid_t space = H5Screate_simple (3, dims, NULL);
hid_t mem_space = H5Screate_simple(1,mem_dim,NULL);
hid_t dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT,
H5P_DEFAULT, H5P_DEFAULT);
herr_t status = H5Dwrite (dset, H5T_NATIVE_INT, mem_space, space, H5P_DEFAULT,
&buffer[0]);这将导致如下的布局:
x
|---+---+----|
| 0 | 2 | 4 |
z=0|---+---+----| y
| 6 | 8 | 10 |
|---+---+----|
x
|---+----+----|
| 1 | 3 | 5 |
z=1|---+----+----| y
| 7 | 9 | 11 |
|---+----+----|我猜这是因为行-主要格式(z是变化最快的索引)。不管怎样,有没有可能只调用一次H5DWrite就强制hdf5写出预期的格式呢?
我想出了这个想法,但它不起作用。我想我搞错了hyperslaps的功能。
... //Same as above, but before H5DWrite
hsize_t start[3];
hsize_t stride[3];
hsize_t count[3];
hsize_t block[3];
//Select two blocks from array. First for z=0, second for z=1
start[0] = 0;
stride[0] = 6;
count[0] = 2;
block[0] = 6;
status = H5Sselect_hyperslab (mem_space, H5S_SELECT_SET, start, stride, count,
block);
start[0] = 0;
start[1] = 0;
start[2] = 0;
stride[0] = 2;
stride[1] = 3;
stride[2] = 1;
count[0] = 1;
count[1] = 1;
count[2] = 2;
block[0] = 2;
block[1] = 3;
block[2] = 1;
status = H5Sselect_hyperslab (space, H5S_SELECT_SET, start, stride, count,
block);
status = H5Dwrite (dset, H5T_NATIVE_INT, mem_space, space, H5P_DEFAULT,
&buffer[0]);在我对H5Sselect_hyperslab的解释中,为mem_space定义的两个块映射到文件空间的两个块。但实际上结果与上面描述的是一样的。有没有可能在不重新格式化数组和不在循环中调用H5DWrite的情况下实现预期的行为?
我
发布于 2013-11-19 05:18:42
超级实验室只能跳过单元格,而不能对其重新排序。所以我觉得你不走运。
正如您所说,有两种解决方案:
hsize_t dims3 = {2,2,3};^^^z i j
然后,您可以在一次写入中转储数组。
在内存中重新排序数组将是浪费时间。
https://stackoverflow.com/questions/19907436
复制相似问题