github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/flann/hdf5.h (about)

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