【发布时间】:2016-06-02 01:10:36
【问题描述】:
编辑:原来 HDF5 C++ 库 1.8.15 中存在(已知?)关于 DataSet.getSpace() 的内存泄漏。
我编写了一个简单的变通方法,通过修复 C 库来避免泄漏代码。因此,我将标记问题已解决
H5::DataSpace getSpace( H5::DataSet& ds )
{
hid_t id2 = ds.getId();
hid_t myspace = H5Dget_space(id2);
H5::DataSpace origspace( myspace );
H5Sclose( myspace );
return origspace;
}
这可能与https://github.com/h5py/h5py/issues/480有关,它与python有关。
HDF5 (C++?) API 中存在与内存空间相关的大量内存泄漏。
编辑:找到http://lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2015-August/008854.html
在软件的典型运行期间,我多次读取/写入 HDF5 数据集。每次读取行子集时,我必须创建一个内存空间和一个文件空间(以使 hyperslab 选择要读取的数据子集)。这些显然消耗了 HDF5 幕后的“资源 ID”。由于我多次执行此操作(因为我将行添加到矩阵并读取它们),最终这些用完了。即使我正确“关闭”数据空间也会发生这种情况。哎呀,如果我不创建新的数据空间,而是通过 setExtentSimple() 重用数据空间,它似乎甚至会发生。
这里是重现问题的最小玩具代码!我什至根本不需要添加/读/写!只需创建一个空间,就好像我要阅读它然后 BAM。哎呀,我只在数据空间中创建了 1 行,并假装我会读一堆。请注意,这会消耗大量内存(内存泄漏!)它不是每次都释放 DataSpace,似乎......这是当函数中的 H5::DataSpace memspace 超出范围时所期望的!它退出并出现如下错误:
HDF5-DIAG: Error detected in HDF5 (1.8.15-patch1) thread 0:
#000: H5S.c line 1393 in H5Screate_simple(): unable to register dataspace ID
major: Object atom
minor: Unable to register new atom
#001: H5I.c line 902 in H5I_register(): no IDs available in type
major: Object atom
minor: Out of IDs for group
terminate called after throwing an instance of 'H5::DataSpaceIException'
编译:
h5c++ -std=c++11 -O2 -g testhdf5id.cpp -o testhdf5id.exe
文件 testhdf5id.cpp:
#include <H5Cpp.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <cstdint>
//Open a dataset. Write 1 row to it. Read every row one at a time. Do this a gazillion times.
//Because what the heck? Hopefully it won't cache it.
hsize_t currid;
void addrow( H5::DataSet& ds, const std::vector<double>& rowtowrite )
{
//Get the space (since it may have grown in length since last time of course )
H5::DataSpace origspace = ds.getSpace();
//get the rank, even though I know it is 2
int rank = origspace.getSimpleExtentNdims();
//Get the actual dimensions of the ranks.
hsize_t dims[rank];
int ndims = origspace.getSimpleExtentDims( dims, NULL);
//Want to ADD a row, so need to offset at row = nrows, and col = 0;
hsize_t offset[rank] = { dims[0], 0 };
hsize_t dims_toadd[rank] = { 1, rowtowrite.size() }; //will write 1 row, ncols columns.
//Compute "new" size (extended by 1 row).
hsize_t size[rank] = { dims[0]+dims_toadd[0], rowtowrite.size() };
//Do the extension.
ds.extend( size );
//Get the new (extended) space, and select the hyperslab to write the row to.
origspace = ds.getSpace();
origspace.selectHyperslab( H5S_SELECT_SET, dims_toadd, offset );
//Make the "memory" data space?
H5::DataSpace toaddspace(rank, dims_toadd);
ds.write( rowtowrite.data(), H5::PredType::NATIVE_DOUBLE, toaddspace, origspace );
//Can close toaddspace/origspace with no effect.
//Can also close/open data set at the beginning of each time with no effect.
}
hsize_t getnrows( H5::DataSet& ds )
{
H5::DataSpace origspace = ds.getSpace();
int rank = origspace.getSimpleExtentNdims();
hsize_t dims[rank];
int ndims = origspace.getSimpleExtentDims( dims, NULL);
hsize_t nrows=dims[0];
hsize_t ncols=dims[1];
return nrows;
}
std::vector<double> read1row( H5::DataSet& ds, const hsize_t& row )
{
H5::DataSpace origspace = ds.getSpace();
int rank = origspace.getSimpleExtentNdims();
hsize_t dims[rank];
int ndims = origspace.getSimpleExtentDims( dims, NULL);
hsize_t nrows=dims[0];
hsize_t ncols=dims[1];
std::vector<double> returnvect( ncols );
if(row >= nrows )
{
fprintf(stderr, "REV: ERROR, trying to read a row outside of matrix ([%lld] vs mat size [%lld])\n", row, nrows);
exit(1);
}
hsize_t targrowoffset = row;
hsize_t targcoloffset = 0;
hsize_t dimsmem[rank] = {1, ncols};
H5::DataSpace memspace(rank, dimsmem);
int id=memspace.getId();
if(id==INT_MAX)
{
fprintf(stdout, "WOW, ID == INT_MAX, errortime?\n");
}
if( (row+1) % 100000 == 0 )
{
fprintf(stdout, "MEMSPACE ID [%d]\n", memspace.getId() );
}
hsize_t offset[rank] = { targrowoffset, targcoloffset };
origspace.selectHyperslab( H5S_SELECT_SET, dimsmem, offset );
ds.read( returnvect.data(), H5::PredType::NATIVE_DOUBLE, memspace, origspace );
return returnvect;
}
void readallrows( H5::DataSet& ds )
{
hsize_t nrows = getnrows( ds );
//fprintf(stdout, "Read [%lld] rows\n", nrows );
for(hsize_t r=0; r<nrows; ++r)
{
std::vector<double> gotvect = read1row( ds, r );
if( gotvect[0] != (double)r )
{
fprintf(stderr, "ERROR IN READING...\n");
exit(1);
}
}
return;
}
std::vector<double> readlastrow( H5::DataSet& ds )
{
H5::DataSpace origspace = ds.getSpace();
int rank = origspace.getSimpleExtentNdims();
hsize_t dims[rank];
int ndims = origspace.getSimpleExtentDims( dims, NULL);
hsize_t nrows=dims[0];
hsize_t ncols=dims[1];
std::vector<double> returnvect( ncols );
hsize_t targrowoffset = nrows-1;
hsize_t targcoloffset = 0;
hsize_t dimsmem[rank] = {1, ncols};
H5::DataSpace memspace(rank, dimsmem);
hsize_t offset[rank] = { targrowoffset, targcoloffset };
origspace.selectHyperslab( H5S_SELECT_SET, dimsmem, offset );
ds.read( returnvect.data(), H5::PredType::NATIVE_DOUBLE, memspace, origspace );
return returnvect;
}
int fakereadlastrow( H5::DataSet& ds, const int& previd )
{
H5::DataSpace origspace = ds.getSpace();
int rank = origspace.getSimpleExtentNdims();
hsize_t dims[rank];
int ndims = origspace.getSimpleExtentDims( dims, NULL);
hsize_t nrows=dims[0];
hsize_t ncols=dims[1];
std::vector<double> returnvect( ncols );
hsize_t targrowoffset = nrows-1;
hsize_t targcoloffset = 0;
hsize_t dimsmem[rank] = {1, ncols};
H5::DataSpace memspace(rank, dimsmem);
hsize_t offset[rank] = { targrowoffset, targcoloffset };
origspace.selectHyperslab( H5S_SELECT_SET, dimsmem, offset );
//REV: Would read here, but I don't for speed.
//ds.read( returnvect.data(), H5::PredType::NATIVE_DOUBLE, memspace, origspace );
int id = memspace.getId();
if(id % 1000000 == 0 )
{
fprintf(stdout, "PREV ID: [%d] now ID: [%d]\n", previd, id);
}
return id;
//return returnvect;
}
int main()
{
std::string fname = "testhdf5file.h5";
H5::H5File f( fname, H5F_ACC_TRUNC );
std::string dsetname = "dset1";
const hsize_t nranks = 2;
const hsize_t ncols = 20;
const hsize_t nrows = 0; //start with zero rows.
hsize_t dims[nranks] = {nrows, ncols};
hsize_t max_dims[nranks] = {H5S_UNLIMITED, ncols};
H5::DataSpace dataspace( nranks, dims, max_dims );
H5::DSetCreatPropList prop; //could set properties, but whatever.
const hsize_t nrows_chunk = 100; //Need to mess with CACHE size too!
hsize_t chunk_dims[nranks] = { nrows_chunk, ncols};
prop.setChunk(nranks, chunk_dims);
//Create the dataset
H5::DataSet ds = f.createDataSet( dsetname, H5::PredType::NATIVE_DOUBLE,
dataspace, prop);
size_t nrowstoadd=1;
for(size_t t=0; t<nrowstoadd; ++t)
{
std::vector<double> rowtowrite( ncols, (double)t );
addrow( ds, rowtowrite );
}
int id=0;
for(size_t t=0; t<10000000; ++t)
{
//readallrows( ds );
//readlastrow();
id = fakereadlastrow(ds, id);
//f.flush(H5F_SCOPE_GLOBAL);
}
return 0;
}
【问题讨论】:
-
无论在每次 fakereadlastrow() 调用结束时调用 memspace.close()、memspace.~H5::DataSpace() 等,请注意相同的行为。
-
如果我打印出每个调用,你可以看到 memspace ID 每次递增 1,而 origspace 保持不变(我每次“获取”相同的空间,所以它不是创建一个新的id 似乎) PREV ID: [67113598] 现在 ID: [67113599] (origspace 是: [67108867]) PREV ID: [67113599] 现在 ID: [67113600] (origspace 是: [67108867]) PREV ID: [67113600]现在 ID: [67113601] (origspace 是: [67108867]) 上一个 ID: [67113601] 现在 ID: [67113602] (origspace 是: [67108867])