5 #include <opencv2/opencv.hpp>
8 #if defined(CUFFT) || defined(CUFFTW)
9 #include "cuda_runtime.h"
11 #include "cuda/cuda_error_check.cuh"
15 template <typename T> class DynMem_ {
24 DynMem_(size_t size) : size(size)
27 CudaSafeCall(cudaHostAlloc(reinterpret_cast<void **>(&ptr_h), size, cudaHostAllocMapped));
28 CudaSafeCall(cudaHostGetDevicePointer(reinterpret_cast<void **>(&ptr_d), reinterpret_cast<void *>(ptr_h), 0));
33 DynMem_(DynMem_&& other) {
35 other.ptr_h = nullptr;
38 other.ptr_d = nullptr;
44 CudaSafeCall(cudaFreeHost(ptr_h));
49 T *hostMem() { return ptr_h; }
51 T *deviceMem() { return ptr_d; }
53 void operator=(DynMem_ &rhs) {
54 memcpy(ptr_h, rhs.ptr_h, size * sizeof(T));
56 void operator=(DynMem_ &&rhs)
67 typedef DynMem_<float> DynMem;
70 class MatDynMem : public DynMem, public cv::Mat {
72 MatDynMem(cv::Size size, int type)
73 : DynMem(size.area() * sizeof(DynMem::type) * CV_MAT_CN(type)), cv::Mat(size, type, hostMem())
75 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
77 MatDynMem(int height, int width, int type)
78 : DynMem(width * height * sizeof(DynMem::type) * CV_MAT_CN(type)), cv::Mat(height, width, type, hostMem())
80 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
82 MatDynMem(int ndims, const int *sizes, int type)
83 : DynMem(volume(ndims, sizes) * sizeof(DynMem::type) * CV_MAT_CN(type)), cv::Mat(ndims, sizes, type, hostMem())
85 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
87 void operator=(const cv::MatExpr &expr) {
88 static_cast<cv::Mat>(*this) = expr;
92 static int volume(int ndims, const int *sizes)
95 for (int i = 0; i < ndims; i++)