gpt4 book ai didi

c++ - 隐藏复合数据类型的某些字段以防止写入(或读回)hdf5 文件

转载 作者:太空宇宙 更新时间:2023-11-04 03:35:30 25 4
gpt4 key购买 nike

我期望通过不向 memory_type 中插入字段,我可以避免将该字段输出到磁盘。然而,下面的测试程序表明并非如此。即使我没有插入字段 c,所有内容都会被写入,所有内容也会被读回。 (虽然示例代码是用 C++ 给出的,但任何使用 hdf5 的 C API 的解决方案同样值得赞赏!)

#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
#include "H5Cpp.h"
using namespace H5;


const H5std_string FILE_NAME( "test_compound2.hdf5" );
const H5std_string DATASET_NAME( "data" );
const int LENGTH = 5;
const int RANK = 1;

#define ShowField(s,f){\
cout << endl<<"Field "<<#f<<" : " << endl; \
for(int i = 0; i < LENGTH; i++)\
cout<<s[i].f<<" ";\
cout<<endl;\
}

int main(void)
{

struct s_t
{
int a;
float b;
int c;
};
CompType mtype( sizeof(s_t) );
/*only insert a,b, do not insert c*/
mtype.insertMember( "a", HOFFSET(s_t, a), PredType::NATIVE_INT);
mtype.insertMember( "b", HOFFSET(s_t, b), PredType::NATIVE_FLOAT);
/*note field c is not inserted!*/

hsize_t dim[] = {LENGTH};
vector <s_t> datain(LENGTH);
for(int i=0; i<LENGTH; i++)/* init data*/
{
datain[i].a=i;
datain[i].b=i*i;
datain[i].c=-i;
}
cout<<"==========Data initialized=============\n";
ShowField(datain, a);
ShowField(datain, b);
ShowField(datain, c);

/*write to file*/
{
DataSpace space( RANK, dim );
H5File file( FILE_NAME, H5F_ACC_TRUNC );
DataSet dset(file.createDataSet(DATASET_NAME, mtype, space));
dset.write( datain.data(), mtype );
}

/*read back*/
H5File file( FILE_NAME, H5F_ACC_RDONLY );
DataSet dset(file.openDataSet( DATASET_NAME ));
vector <s_t> dataout(LENGTH);

dset.read( dataout.data(), mtype );

cout<<"\n===========Data Read==========\n";
ShowField(dataout,a);
ShowField(dataout,b);
ShowField(dataout,c);

return 0;
}

输出如下。注意字段 c 被正确读回,虽然没有插入到 memtype 中!

  ==========Data initialized=============

Field a :
0 1 2 3 4

Field b :
0 1 4 9 16

Field c :
0 -1 -2 -3 -4

===========Data Read==========

Field a :
0 1 2 3 4

Field b :
0 1 4 9 16

Field c :
0 -1 -2 -3 -4

我想这与 memtype 的大小有关。我尝试使用 pack() 来减小大小,但随后数据解释出错了。

定义一个仅包含这些所需字段的新结构并不是最佳选择,因为它需要将数据复制到新结构或复制回来,而我的应用程序涉及大量数据。我试图隐藏的实际上是一个 vector 场,我将其作为可变长度数组的数组单独写出。目前,虽然我在 memtype 中省略了 vector 字段,但它仍然被写入然后也被读回,这会破坏内存(读取自动用它们的写入值填充 vector 的大小和内存指针,它们不再是有效指针).

那么有没有一种方法可以真正隐藏特定字段,使其不被写入和被读回,而无需定义新的临时类?

最佳答案

在挖掘文档和测试之后,我找到了以下解决方案。关键是保存的时候要指定和内存模型不同的存储模型。此存储类型是 mtype 的紧凑版本,它删除了未使用的字段。 pack() 函数可以实现这一点:

CompType mtype_disk;
mtype_disk.copy(mtype);
mtype_disk.pack()

现在 mtype_disk 的大小应该小于 mtype,因为未使用的字段(可能还有一些填充)已被删除。使用 mtype_disk 而不是 mtype 创建数据集,将确保只有明确插入到 mtype 的字段(因此 mtype_disk 也会被写入。

DataSet dset(file.createDataSet(DATASET_NAME, mtype_disk, space));
dset.write(datain.data(), mtype);

数据集创建步骤是唯一需要 mtype_disk 的地方。读回数据时,应该像以前一样使用mtype来描述内存布局。

关于c++ - 隐藏复合数据类型的某些字段以防止写入(或读回)hdf5 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33085684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com