hdf5.h 7.1 KB

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