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_ {
25 DynMem_(size_t num_elem) : num_elem(num_elem)
28 CudaSafeCall(cudaHostAlloc(reinterpret_cast<void **>(&ptr_h), num_elem * sizeof(T), cudaHostAllocMapped));
29 CudaSafeCall(cudaHostGetDevicePointer(reinterpret_cast<void **>(&ptr_d), reinterpret_cast<void *>(ptr_h), 0));
31 ptr_h = new T[num_elem];
34 DynMem_(DynMem_&& other) {
36 other.ptr_h = nullptr;
39 other.ptr_d = nullptr;
45 CudaSafeCall(cudaFreeHost(ptr_h));
50 T *hostMem() { return ptr_h; }
52 T *deviceMem() { return ptr_d; }
54 void operator=(DynMem_ &rhs) {
55 memcpy(ptr_h, rhs.ptr_h, num_elem * sizeof(T));
57 void operator=(DynMem_ &&rhs)
66 T operator[](uint i) const { return ptr_h[i]; }
69 typedef DynMem_<float> DynMem;
72 class MatDynMem : public DynMem, public cv::Mat {
74 MatDynMem(cv::Size size, int type)
75 : DynMem(size.area() * CV_MAT_CN(type)), cv::Mat(size, type, hostMem())
77 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
79 MatDynMem(int height, int width, int type)
80 : DynMem(width * height * CV_MAT_CN(type)), cv::Mat(height, width, type, hostMem())
82 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
84 MatDynMem(int ndims, const int *sizes, int type)
85 : DynMem(volume(ndims, sizes) * CV_MAT_CN(type)), cv::Mat(ndims, sizes, type, hostMem())
87 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
89 MatDynMem(std::vector<int> size, int type)
90 : DynMem(std::accumulate(size.begin(), size.end(), 1, std::multiplies<int>()))
91 , cv::Mat(size.size(), size.data(), type, hostMem()) {}
92 MatDynMem(MatDynMem &&other) = default;
93 MatDynMem(const cv::Mat &other)
94 : DynMem(other.total()) , cv::Mat(other) {}
96 void operator=(const cv::MatExpr &expr) {
97 static_cast<cv::Mat>(*this) = expr;
101 static int volume(int ndims, const int *sizes)
104 for (int i = 0; i < ndims; i++)
109 using cv::Mat::create;
112 class Mat3d : public MatDynMem
115 Mat3d(uint dim0, cv::Size size) : MatDynMem({{int(dim0), size.height, size.width}}, CV_32F) {}
117 cv::Mat plane(uint idx) {
119 assert(int(idx) < size[0]);
120 return cv::Mat(size[1], size[2], cv::Mat::type(), ptr(idx));
122 const cv::Mat plane(uint idx) const {
124 assert(int(idx) < size[0]);
125 return cv::Mat(size[1], size[2], cv::Mat::type(), const_cast<uchar*>(ptr(idx)));
130 class MatFeats : public Mat3d
133 MatFeats(uint num_features, cv::Size size) : Mat3d(num_features, size) {}
135 class MatScales : public Mat3d
138 MatScales(uint num_scales, cv::Size size) : Mat3d(num_scales, size) {}
141 class MatScaleFeats : public MatDynMem
144 MatScaleFeats(uint num_scales, uint num_features, cv::Size size)
145 : MatDynMem({{int(num_scales), int(num_features), size.height, size.width}}, CV_32F) {}
147 cv::Mat plane(uint scale, uint feature) {
149 assert(int(scale) < size[0]);
150 assert(int(feature) < size[1]);
151 return cv::Mat(size[2], size[3], cv::Mat::type(), ptr(scale, feature));
153 cv::Mat scale(uint scale) {
155 assert(int(scale) < size[0]);
156 return cv::Mat(3, std::vector<int>({size[1], size[2], size[3]}).data(), cv::Mat::type(), ptr(scale));