]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/dynmem.hpp
Change p_threadctxs from list to vector
[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_(DynMem_&& other) {
31         this->ptr = other.ptr;
32         this->ptr_d = other.ptr_d;
33
34         other.ptr = nullptr;
35         other.ptr_d = nullptr;
36     }
37     ~DynMem_()
38     {
39 #ifdef CUFFT
40         CudaSafeCall(cudaFreeHost(this->ptr));
41 #else
42         delete this->ptr;
43 #endif
44     }
45     T *hostMem() { return ptr; }
46     T *deviceMem() { return ptr_d; }
47
48     void operator=(DynMem_ &&rhs)
49     {
50         this->ptr = rhs.ptr;
51         this->ptr_d = rhs.ptr_d;
52
53         rhs.ptr = nullptr;
54         rhs.ptr_d = nullptr;
55     }
56 };
57 typedef DynMem_<float> DynMem;
58 #endif // DYNMEM_HPP