]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/dynmem.hpp
Merge branch 'rotation' of https://github.com/Shanigen/kcf into rotation
[hercules2020/kcf.git] / src / dynmem.hpp
1 #ifndef DYNMEM_HPP
2 #define DYNMEM_HPP
3
4 #include <cstdlib>
5
6 #if defined(CUFFT) || defined(CUFFTW)
7 #include "cuda_runtime.h"
8 #ifdef CUFFT
9 #include "cuda/cuda_error_check.cuh"
10 #endif
11 #endif
12
13 template <typename T> class DynMem_ {
14     T *ptr = nullptr;
15     T *ptr_d = nullptr;
16
17   public:
18     DynMem_()
19     {}
20     DynMem_(size_t size)
21     {
22 #ifdef CUFFT
23         CudaSafeCall(cudaHostAlloc(reinterpret_cast<void **>(&this->ptr), size, cudaHostAllocMapped));
24         CudaSafeCall(
25             cudaHostGetDevicePointer(reinterpret_cast<void **>(&this->ptr_d), reinterpret_cast<void *>(this->ptr), 0));
26 #else
27         this->ptr = new float[size];
28 #endif
29     }
30     ~DynMem_()
31     {
32 #ifdef CUFFT
33         CudaSafeCall(cudaFreeHost(this->ptr));
34 #else
35         delete this->ptr;
36 #endif
37     }
38     T *hostMem() { return ptr; }
39     T *deviceMem() { return ptr_d; }
40
41     void operator=(DynMem_ &&rhs)
42     {
43         this->ptr = rhs.ptr;
44         this->ptr_d = rhs.ptr_d;
45
46         rhs.ptr = nullptr;
47         rhs.ptr_d = nullptr;
48     }
49 };
50 typedef DynMem_<float> DynMem;
51 #endif // DYNMEM_HPP