hdf5.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #ifndef OPENCV_FLANN_HDF5_H_
  29. #define OPENCV_FLANN_HDF5_H_
  30. //! @cond IGNORED
  31. #include <hdf5.h>
  32. #include "matrix.h"
  33. namespace cvflann
  34. {
  35. namespace
  36. {
  37. template<typename T>
  38. hid_t get_hdf5_type()
  39. {
  40. throw FLANNException("Unsupported type for IO operations");
  41. }
  42. template<>
  43. hid_t get_hdf5_type<char>() { return H5T_NATIVE_CHAR; }
  44. template<>
  45. hid_t get_hdf5_type<unsigned char>() { return H5T_NATIVE_UCHAR; }
  46. template<>
  47. hid_t get_hdf5_type<short int>() { return H5T_NATIVE_SHORT; }
  48. template<>
  49. hid_t get_hdf5_type<unsigned short int>() { return H5T_NATIVE_USHORT; }
  50. template<>
  51. hid_t get_hdf5_type<int>() { return H5T_NATIVE_INT; }
  52. template<>
  53. hid_t get_hdf5_type<unsigned int>() { return H5T_NATIVE_UINT; }
  54. template<>
  55. hid_t get_hdf5_type<long>() { return H5T_NATIVE_LONG; }
  56. template<>
  57. hid_t get_hdf5_type<unsigned long>() { return H5T_NATIVE_ULONG; }
  58. template<>
  59. hid_t get_hdf5_type<float>() { return H5T_NATIVE_FLOAT; }
  60. template<>
  61. hid_t get_hdf5_type<double>() { return H5T_NATIVE_DOUBLE; }
  62. }
  63. #define CHECK_ERROR(x,y) if ((x)<0) throw FLANNException((y));
  64. template<typename T>
  65. void save_to_file(const cvflann::Matrix<T>& dataset, const String& filename, const String& name)
  66. {
  67. #if H5Eset_auto_vers == 2
  68. H5Eset_auto( H5E_DEFAULT, NULL, NULL );
  69. #else
  70. H5Eset_auto( NULL, NULL );
  71. #endif
  72. herr_t status;
  73. hid_t file_id;
  74. file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
  75. if (file_id < 0) {
  76. file_id = H5Fcreate(filename.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
  77. }
  78. CHECK_ERROR(file_id,"Error creating hdf5 file.");
  79. hsize_t dimsf[2]; // dataset dimensions
  80. dimsf[0] = dataset.rows;
  81. dimsf[1] = dataset.cols;
  82. hid_t space_id = H5Screate_simple(2, dimsf, NULL);
  83. hid_t memspace_id = H5Screate_simple(2, dimsf, NULL);
  84. hid_t dataset_id;
  85. #if H5Dcreate_vers == 2
  86. dataset_id = H5Dcreate2(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  87. #else
  88. dataset_id = H5Dcreate(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT);
  89. #endif
  90. if (dataset_id<0) {
  91. #if H5Dopen_vers == 2
  92. dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
  93. #else
  94. dataset_id = H5Dopen(file_id, name.c_str());
  95. #endif
  96. }
  97. CHECK_ERROR(dataset_id,"Error creating or opening dataset in file.");
  98. status = H5Dwrite(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, H5P_DEFAULT, dataset.data );
  99. CHECK_ERROR(status, "Error writing to dataset");
  100. H5Sclose(memspace_id);
  101. H5Sclose(space_id);
  102. H5Dclose(dataset_id);
  103. H5Fclose(file_id);
  104. }
  105. template<typename T>
  106. void load_from_file(cvflann::Matrix<T>& dataset, const String& filename, const String& name)
  107. {
  108. herr_t status;
  109. hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
  110. CHECK_ERROR(file_id,"Error opening hdf5 file.");
  111. hid_t dataset_id;
  112. #if H5Dopen_vers == 2
  113. dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
  114. #else
  115. dataset_id = H5Dopen(file_id, name.c_str());
  116. #endif
  117. CHECK_ERROR(dataset_id,"Error opening dataset in file.");
  118. hid_t space_id = H5Dget_space(dataset_id);
  119. hsize_t dims_out[2];
  120. H5Sget_simple_extent_dims(space_id, dims_out, NULL);
  121. dataset = cvflann::Matrix<T>(new T[dims_out[0]*dims_out[1]], dims_out[0], dims_out[1]);
  122. status = H5Dread(dataset_id, get_hdf5_type<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dataset[0]);
  123. CHECK_ERROR(status, "Error reading dataset");
  124. H5Sclose(space_id);
  125. H5Dclose(dataset_id);
  126. H5Fclose(file_id);
  127. }
  128. #ifdef HAVE_MPI
  129. namespace mpi
  130. {
  131. /**
  132. * Loads a the hyperslice corresponding to this processor from a hdf5 file.
  133. * @param flann_dataset Dataset where the data is loaded
  134. * @param filename HDF5 file name
  135. * @param name Name of dataset inside file
  136. */
  137. template<typename T>
  138. void load_from_file(cvflann::Matrix<T>& dataset, const String& filename, const String& name)
  139. {
  140. MPI_Comm comm = MPI_COMM_WORLD;
  141. MPI_Info info = MPI_INFO_NULL;
  142. int mpi_size, mpi_rank;
  143. MPI_Comm_size(comm, &mpi_size);
  144. MPI_Comm_rank(comm, &mpi_rank);
  145. herr_t status;
  146. hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
  147. H5Pset_fapl_mpio(plist_id, comm, info);
  148. hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, plist_id);
  149. CHECK_ERROR(file_id,"Error opening hdf5 file.");
  150. H5Pclose(plist_id);
  151. hid_t dataset_id;
  152. #if H5Dopen_vers == 2
  153. dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
  154. #else
  155. dataset_id = H5Dopen(file_id, name.c_str());
  156. #endif
  157. CHECK_ERROR(dataset_id,"Error opening dataset in file.");
  158. hid_t space_id = H5Dget_space(dataset_id);
  159. hsize_t dims[2];
  160. H5Sget_simple_extent_dims(space_id, dims, NULL);
  161. hsize_t count[2];
  162. hsize_t offset[2];
  163. hsize_t item_cnt = dims[0]/mpi_size+(dims[0]%mpi_size==0 ? 0 : 1);
  164. hsize_t cnt = (mpi_rank<mpi_size-1 ? item_cnt : dims[0]-item_cnt*(mpi_size-1));
  165. count[0] = cnt;
  166. count[1] = dims[1];
  167. offset[0] = mpi_rank*item_cnt;
  168. offset[1] = 0;
  169. hid_t memspace_id = H5Screate_simple(2,count,NULL);
  170. H5Sselect_hyperslab(space_id, H5S_SELECT_SET, offset, NULL, count, NULL);
  171. dataset.rows = count[0];
  172. dataset.cols = count[1];
  173. dataset.data = new T[dataset.rows*dataset.cols];
  174. plist_id = H5Pcreate(H5P_DATASET_XFER);
  175. H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
  176. status = H5Dread(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, plist_id, dataset.data);
  177. CHECK_ERROR(status, "Error reading dataset");
  178. H5Pclose(plist_id);
  179. H5Sclose(space_id);
  180. H5Sclose(memspace_id);
  181. H5Dclose(dataset_id);
  182. H5Fclose(file_id);
  183. }
  184. }
  185. #endif // HAVE_MPI
  186. } // namespace cvflann::mpi
  187. //! @endcond
  188. #endif /* OPENCV_FLANN_HDF5_H_ */