5 #include <opencv2/opencv.hpp>
9 #if defined(CUFFT) || defined(CUFFTW)
10 #include "cuda_runtime.h"
12 #include "cuda_error_check.hpp"
16 template <typename T> class DynMem_ {
24 const size_t num_elem;
26 DynMem_(size_t num_elem) : num_elem(num_elem)
29 CudaSafeCall(cudaHostAlloc(reinterpret_cast<void **>(&ptr_h), num_elem * sizeof(T), cudaHostAllocMapped));
30 CudaSafeCall(cudaHostGetDevicePointer(reinterpret_cast<void **>(&ptr_d), reinterpret_cast<void *>(ptr_h), 0));
32 ptr_h = new T[num_elem];
35 DynMem_(const DynMem_ &other) : DynMem_(other.num_elem)
37 memcpy(ptr_h, other.ptr_h, num_elem * sizeof(T));
39 DynMem_(DynMem_ &&other) : num_elem(other.num_elem)
42 other.ptr_h = nullptr;
45 other.ptr_d = nullptr;
52 T *hostMem() { return ptr_h; }
53 const T *hostMem() const { return ptr_h; }
55 T *deviceMem() { return ptr_d; }
57 void operator=(DynMem_ &rhs) {
58 assert(num_elem == rhs.num_elem);
59 memcpy(ptr_h, rhs.ptr_h, num_elem * sizeof(T));
61 void operator=(DynMem_ &&rhs)
63 assert(num_elem == rhs.num_elem);
72 T operator[](uint i) const { return ptr_h[i]; }
77 CudaSafeCall(cudaFreeHost(ptr_h));
84 typedef DynMem_<float> DynMem;
87 class MatDynMem : public DynMem, public cv::Mat {
89 MatDynMem(cv::Size size, int type)
90 : DynMem(size.area() * CV_MAT_CN(type)), cv::Mat(size, type, hostMem())
92 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
94 MatDynMem(int height, int width, int type)
95 : DynMem(width * height * CV_MAT_CN(type)), cv::Mat(height, width, type, hostMem())
97 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
99 MatDynMem(int ndims, const int *sizes, int type)
100 : DynMem(volume(ndims, sizes) * CV_MAT_CN(type)), cv::Mat(ndims, sizes, type, hostMem())
102 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
104 MatDynMem(std::vector<int> size, int type)
105 : DynMem(std::accumulate(size.begin(), size.end(), 1, std::multiplies<int>()))
106 , cv::Mat(size.size(), size.data(), type, hostMem()) {}
107 MatDynMem(MatDynMem &&other) = default;
108 MatDynMem(const cv::Mat &other)
109 : DynMem(other.total()) , cv::Mat(other) {}
111 void operator=(const cv::MatExpr &expr) {
112 static_cast<cv::Mat>(*this) = expr;
116 static int volume(int ndims, const int *sizes)
119 for (int i = 0; i < ndims; i++)
124 using cv::Mat::create;
127 class Mat3d : public MatDynMem
130 Mat3d(uint dim0, cv::Size size) : MatDynMem({{int(dim0), size.height, size.width}}, CV_32F) {}
132 cv::Mat plane(uint idx) {
134 assert(int(idx) < size[0]);
135 return cv::Mat(size[1], size[2], cv::Mat::type(), ptr(idx));
137 const cv::Mat plane(uint idx) const {
139 assert(int(idx) < size[0]);
140 return cv::Mat(size[1], size[2], cv::Mat::type(), const_cast<uchar*>(ptr(idx)));
145 class MatFeats : public Mat3d
148 MatFeats(uint num_features, cv::Size size) : Mat3d(num_features, size) {}
150 class MatScales : public Mat3d
153 MatScales(uint num_scales, cv::Size size) : Mat3d(num_scales, size) {}
156 class MatScaleFeats : public MatDynMem
159 MatScaleFeats(uint num_scales, uint num_features, cv::Size size)
160 : MatDynMem({{int(num_scales), int(num_features), size.height, size.width}}, CV_32F) {}
162 cv::Mat plane(uint scale, uint feature) {
164 assert(int(scale) < size[0]);
165 assert(int(feature) < size[1]);
166 return cv::Mat(size[2], size[3], cv::Mat::type(), ptr(scale, feature));
168 cv::Mat scale(uint scale) {
170 assert(int(scale) < size[0]);
171 return cv::Mat(3, std::vector<int>({size[1], size[2], size[3]}).data(), cv::Mat::type(), ptr(scale));