30
#ifndef OPENCV_FLANN_HDF5_H_
31
#define OPENCV_FLANN_HDF5_H_
49
throw
FLANNException(
"Unsupported type for IO operations");
53hid_t get_hdf5_type<char>() {
return
H5T_NATIVE_CHAR; }
55hid_t get_hdf5_type<unsigned char>() {
return
H5T_NATIVE_UCHAR; }
57hid_t get_hdf5_type<short int>() {
return
H5T_NATIVE_SHORT; }
59hid_t get_hdf5_type<unsigned short int>() {
return
H5T_NATIVE_USHORT; }
61hid_t get_hdf5_type<int>() {
return
H5T_NATIVE_INT; }
63hid_t get_hdf5_type<unsigned int>() {
return
H5T_NATIVE_UINT; }
65hid_t get_hdf5_type<long>() {
return
H5T_NATIVE_LONG; }
67hid_t get_hdf5_type<unsigned long>() {
return
H5T_NATIVE_ULONG; }
69hid_t get_hdf5_type<float>() {
return
H5T_NATIVE_FLOAT; }
71hid_t get_hdf5_type<double>() {
return
H5T_NATIVE_DOUBLE; }
75
#define CHECK_ERROR(x,y) if ((x)<0) throw FLANNException((y));
78
void
save_to_file(
const
cvflann::Matrix<T>& dataset,
const
String& filename,
const
String& name)
81
#if H5Eset_auto_vers == 2
82
H5Eset_auto( H5E_DEFAULT, NULL, NULL );
84
H5Eset_auto( NULL, NULL );
89
file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
91
file_id = H5Fcreate(filename.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
93
CHECK_ERROR(file_id,
"Error creating hdf5 file.");
96
dimsf[0] = dataset.rows;
97
dimsf[1] = dataset.cols;
99
hid_t space_id = H5Screate_simple(2, dimsf, NULL);
100
hid_t memspace_id = H5Screate_simple(2, dimsf, NULL);
103
#if H5Dcreate_vers == 2
104
dataset_id = H5Dcreate2(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
106
dataset_id = H5Dcreate(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT);
110
#if H5Dopen_vers == 2
111
dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
113
dataset_id = H5Dopen(file_id, name.c_str());
116
CHECK_ERROR(dataset_id,
"Error creating or opening dataset in file.");
118
status = H5Dwrite(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, H5P_DEFAULT, dataset.data );
119
CHECK_ERROR(status,
"Error writing to dataset");
121
H5Sclose(memspace_id);
123
H5Dclose(dataset_id);
130
void
load_from_file(cvflann::Matrix<T>& dataset,
const
String& filename,
const
String& name)
133
hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
134
CHECK_ERROR(file_id,
"Error opening hdf5 file.");
137
#if H5Dopen_vers == 2
138
dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
140
dataset_id = H5Dopen(file_id, name.c_str());
142
CHECK_ERROR(dataset_id,
"Error opening dataset in file.");
144
hid_t space_id = H5Dget_space(dataset_id);
147
H5Sget_simple_extent_dims(space_id, dims_out, NULL);
149
dataset = cvflann::Matrix<T>(
new
T[dims_out[0]*dims_out[1]], dims_out[0], dims_out[1]);
151
status = H5Dread(dataset_id, get_hdf5_type<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dataset[0]);
152
CHECK_ERROR(status,
"Error reading dataset");
155
H5Dclose(dataset_id);
171
void
load_from_file(cvflann::Matrix<T>& dataset,
const
String& filename,
const
String& name)
173
MPI_Comm comm = MPI_COMM_WORLD;
174
MPI_Info info = MPI_INFO_NULL;
176
int
mpi_size, mpi_rank;
177
MPI_Comm_size(comm, &mpi_size);
178
MPI_Comm_rank(comm, &mpi_rank);
182
hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
183
H5Pset_fapl_mpio(plist_id, comm, info);
184
hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, plist_id);
185
CHECK_ERROR(file_id,
"Error opening hdf5 file.");
188
#if H5Dopen_vers == 2
189
dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
191
dataset_id = H5Dopen(file_id, name.c_str());
193
CHECK_ERROR(dataset_id,
"Error opening dataset in file.");
195
hid_t space_id = H5Dget_space(dataset_id);
197
H5Sget_simple_extent_dims(space_id, dims, NULL);
202
hsize_t item_cnt = dims[0]/mpi_size+(dims[0]%mpi_size==0 ? 0 : 1);
203
hsize_t cnt = (mpi_rank<mpi_size-1 ? item_cnt : dims[0]-item_cnt*(mpi_size-1));
207
offset[0] = mpi_rank*item_cnt;
210
hid_t memspace_id = H5Screate_simple(2,count,NULL);
212
H5Sselect_hyperslab(space_id, H5S_SELECT_SET, offset, NULL, count, NULL);
214
dataset.rows = count[0];
215
dataset.cols = count[1];
216
dataset.data =
new
T[dataset.rows*dataset.cols];
218
plist_id = H5Pcreate(H5P_DATASET_XFER);
219
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
220
status = H5Dread(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, plist_id, dataset.data);
221
CHECK_ERROR(status,
"Error reading dataset");
225
H5Sclose(memspace_id);
226
H5Dclose(dataset_id);