hdf5.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *************************************************************************/
28 
29 
30 #ifndef OPENCV_FLANN_HDF5_H_
31 #define OPENCV_FLANN_HDF5_H_
32 
33 #include <hdf5.h>
34 
35 #include "matrix.h"
36 
37 
38 namespace cvflann
39 {
40 
41 namespace
42 {
43 
44 template<typename T>
45 hid_t get_hdf5_type()
46 {
47  throw FLANNException("Unsupported type for IO operations");
48 }
49 
50 template<>
51 hid_t get_hdf5_type<char>() { return H5T_NATIVE_CHAR; }
52 template<>
53 hid_t get_hdf5_type<unsigned char>() { return H5T_NATIVE_UCHAR; }
54 template<>
55 hid_t get_hdf5_type<short int>() { return H5T_NATIVE_SHORT; }
56 template<>
57 hid_t get_hdf5_type<unsigned short int>() { return H5T_NATIVE_USHORT; }
58 template<>
59 hid_t get_hdf5_type<int>() { return H5T_NATIVE_INT; }
60 template<>
61 hid_t get_hdf5_type<unsigned int>() { return H5T_NATIVE_UINT; }
62 template<>
63 hid_t get_hdf5_type<long>() { return H5T_NATIVE_LONG; }
64 template<>
65 hid_t get_hdf5_type<unsigned long>() { return H5T_NATIVE_ULONG; }
66 template<>
67 hid_t get_hdf5_type<float>() { return H5T_NATIVE_FLOAT; }
68 template<>
69 hid_t get_hdf5_type<double>() { return H5T_NATIVE_DOUBLE; }
70 }
71 
72 
73 #define CHECK_ERROR(x,y) if ((x)<0) throw FLANNException((y));
74 
75 template<typename T>
76 void save_to_file(const cvflann::Matrix<T>& dataset, const std::string& filename, const std::string& name)
77 {
78 
79 #if H5Eset_auto_vers == 2
80  H5Eset_auto( H5E_DEFAULT, NULL, NULL );
81 #else
82  H5Eset_auto( NULL, NULL );
83 #endif
84 
85  herr_t status;
86  hid_t file_id;
87  file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
88  if (file_id < 0) {
89  file_id = H5Fcreate(filename.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
90  }
91  CHECK_ERROR(file_id,"Error creating hdf5 file.");
92 
93  hsize_t dimsf[2]; // dataset dimensions
94  dimsf[0] = dataset.rows;
95  dimsf[1] = dataset.cols;
96 
97  hid_t space_id = H5Screate_simple(2, dimsf, NULL);
98  hid_t memspace_id = H5Screate_simple(2, dimsf, NULL);
99 
100  hid_t dataset_id;
101 #if H5Dcreate_vers == 2
102  dataset_id = H5Dcreate2(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
103 #else
104  dataset_id = H5Dcreate(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT);
105 #endif
106 
107  if (dataset_id<0) {
108 #if H5Dopen_vers == 2
109  dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
110 #else
111  dataset_id = H5Dopen(file_id, name.c_str());
112 #endif
113  }
114  CHECK_ERROR(dataset_id,"Error creating or opening dataset in file.");
115 
116  status = H5Dwrite(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, H5P_DEFAULT, dataset.data );
117  CHECK_ERROR(status, "Error writing to dataset");
118 
119  H5Sclose(memspace_id);
120  H5Sclose(space_id);
121  H5Dclose(dataset_id);
122  H5Fclose(file_id);
123 
124 }
125 
126 
127 template<typename T>
128 void load_from_file(cvflann::Matrix<T>& dataset, const std::string& filename, const std::string& name)
129 {
130  herr_t status;
131  hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
132  CHECK_ERROR(file_id,"Error opening hdf5 file.");
133 
134  hid_t dataset_id;
135 #if H5Dopen_vers == 2
136  dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
137 #else
138  dataset_id = H5Dopen(file_id, name.c_str());
139 #endif
140  CHECK_ERROR(dataset_id,"Error opening dataset in file.");
141 
142  hid_t space_id = H5Dget_space(dataset_id);
143 
144  hsize_t dims_out[2];
145  H5Sget_simple_extent_dims(space_id, dims_out, NULL);
146 
147  dataset = cvflann::Matrix<T>(new T[dims_out[0]*dims_out[1]], dims_out[0], dims_out[1]);
148 
149  status = H5Dread(dataset_id, get_hdf5_type<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dataset[0]);
150  CHECK_ERROR(status, "Error reading dataset");
151 
152  H5Sclose(space_id);
153  H5Dclose(dataset_id);
154  H5Fclose(file_id);
155 }
156 
157 
158 #ifdef HAVE_MPI
159 
160 namespace mpi
161 {
168 template<typename T>
169 void load_from_file(cvflann::Matrix<T>& dataset, const std::string& filename, const std::string& name)
170 {
171  MPI_Comm comm = MPI_COMM_WORLD;
172  MPI_Info info = MPI_INFO_NULL;
173 
174  int mpi_size, mpi_rank;
175  MPI_Comm_size(comm, &mpi_size);
176  MPI_Comm_rank(comm, &mpi_rank);
177 
178  herr_t status;
179 
180  hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
181  H5Pset_fapl_mpio(plist_id, comm, info);
182  hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, plist_id);
183  CHECK_ERROR(file_id,"Error opening hdf5 file.");
184  H5Pclose(plist_id);
185  hid_t dataset_id;
186 #if H5Dopen_vers == 2
187  dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
188 #else
189  dataset_id = H5Dopen(file_id, name.c_str());
190 #endif
191  CHECK_ERROR(dataset_id,"Error opening dataset in file.");
192 
193  hid_t space_id = H5Dget_space(dataset_id);
194  hsize_t dims[2];
195  H5Sget_simple_extent_dims(space_id, dims, NULL);
196 
197  hsize_t count[2];
198  hsize_t offset[2];
199 
200  hsize_t item_cnt = dims[0]/mpi_size+(dims[0]%mpi_size==0 ? 0 : 1);
201  hsize_t cnt = (mpi_rank<mpi_size-1 ? item_cnt : dims[0]-item_cnt*(mpi_size-1));
202 
203  count[0] = cnt;
204  count[1] = dims[1];
205  offset[0] = mpi_rank*item_cnt;
206  offset[1] = 0;
207 
208  hid_t memspace_id = H5Screate_simple(2,count,NULL);
209 
210  H5Sselect_hyperslab(space_id, H5S_SELECT_SET, offset, NULL, count, NULL);
211 
212  dataset.rows = count[0];
213  dataset.cols = count[1];
214  dataset.data = new T[dataset.rows*dataset.cols];
215 
216  plist_id = H5Pcreate(H5P_DATASET_XFER);
217  H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
218  status = H5Dread(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, plist_id, dataset.data);
219  CHECK_ERROR(status, "Error reading dataset");
220 
221  H5Pclose(plist_id);
222  H5Sclose(space_id);
223  H5Sclose(memspace_id);
224  H5Dclose(dataset_id);
225  H5Fclose(file_id);
226 }
227 }
228 #endif // HAVE_MPI
229 } // namespace cvflann::mpi
230 
231 #endif /* OPENCV_FLANN_HDF5_H_ */
int dims
Definition: core_c.h:218
const CvArr CvArr CvArr const CvPoint2D32f CvPoint2D32f int CvSize int char * status
Definition: tracking.hpp:73
const char const char ** filename
Definition: core_c.h:1750
size_t cols
Definition: matrix.h:52
void load_from_file(cvflann::Matrix< T > &dataset, const std::string &filename, const std::string &name)
Definition: hdf5.h:128
const CvMat const CvMat const CvMat CvMat CvMat CvMat CvMat CvSize CvMat CvMat * T
Definition: calib3d.hpp:270
Definition: matrix.h:46
void save_to_file(const cvflann::Matrix< T > &dataset, const std::string &filename, const std::string &name)
Definition: hdf5.h:76
GLintptr offset
GLuint GLuint GLsizei count
Definition: core_c.h:973
T * data
Definition: matrix.h:54
GLuint const GLchar * name
Definition: core_c.h:1546
size_t rows
Definition: matrix.h:51
void load_from_file(cvflann::Matrix< T > &dataset, const std::string &filename, const std::string &name)
Definition: hdf5.h:169