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; }
56 const T *deviceMem() const { return ptr_d; }
58 void operator=(DynMem_ &rhs) {
59 assert(num_elem == rhs.num_elem);
60 memcpy(ptr_h, rhs.ptr_h, num_elem * sizeof(T));
62 void operator=(DynMem_ &&rhs)
64 assert(num_elem == rhs.num_elem);
73 T operator[](uint i) const { return ptr_h[i]; }
78 CudaSafeCall(cudaFreeHost(ptr_h));
85 typedef DynMem_<float> DynMem;
88 class MatDynMem : public DynMem, public cv::Mat {
90 MatDynMem(cv::Size size, int type)
91 : DynMem(size.area() * CV_MAT_CN(type)), cv::Mat(size, type, hostMem())
93 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
95 MatDynMem(int height, int width, int type)
96 : DynMem(width * height * CV_MAT_CN(type)), cv::Mat(height, width, type, hostMem())
98 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
100 MatDynMem(int ndims, const int *sizes, int type)
101 : DynMem(volume(ndims, sizes) * CV_MAT_CN(type)), cv::Mat(ndims, sizes, type, hostMem())
103 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
105 MatDynMem(std::vector<int> size, int type)
106 : DynMem(std::accumulate(size.begin(), size.end(), 1, std::multiplies<int>()))
107 , cv::Mat(size.size(), size.data(), type, hostMem()) {}
108 MatDynMem(MatDynMem &&other) = default;
109 MatDynMem(const cv::Mat &other)
110 : DynMem(other.total()) , cv::Mat(other) {}
112 void operator=(const cv::MatExpr &expr) {
113 static_cast<cv::Mat>(*this) = expr;
117 static int volume(int ndims, const int *sizes)
120 for (int i = 0; i < ndims; i++)
125 using cv::Mat::create;
128 class Mat3d : public MatDynMem
131 Mat3d(uint dim0, cv::Size size) : MatDynMem({{int(dim0), size.height, size.width}}, CV_32F) {}
133 cv::Mat plane(uint idx) {
135 assert(int(idx) < size[0]);
136 return cv::Mat(size[1], size[2], cv::Mat::type(), ptr(idx));
138 const cv::Mat plane(uint idx) const {
140 assert(int(idx) < size[0]);
141 return cv::Mat(size[1], size[2], cv::Mat::type(), const_cast<uchar*>(ptr(idx)));
146 class MatFeats : public Mat3d
149 MatFeats(uint num_features, cv::Size size) : Mat3d(num_features, size) {}
151 class MatScales : public Mat3d
154 MatScales(uint num_scales, cv::Size size) : Mat3d(num_scales, size) {}
157 class MatScaleFeats : public MatDynMem
160 MatScaleFeats(uint num_scales, uint num_features, cv::Size size)
161 : MatDynMem({{int(num_scales), int(num_features), size.height, size.width}}, CV_32F) {}
163 cv::Mat plane(uint scale, uint feature) {
165 assert(int(scale) < size[0]);
166 assert(int(feature) < size[1]);
167 return cv::Mat(size[2], size[3], cv::Mat::type(), ptr(scale, feature));
169 cv::Mat scale(uint scale) {
171 assert(int(scale) < size[0]);
172 return cv::Mat(3, std::vector<int>({size[1], size[2], size[3]}).data(), cv::Mat::type(), ptr(scale));