]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/complexmat.cuh
Integration of ThredCTX to rotation.
[hercules2020/kcf.git] / src / complexmat.cuh
1 #ifndef COMPLEXMAT_H
2 #define COMPLEXMAT_H
3
4 #include <opencv2/opencv.hpp>
5
6 #include "cuda_runtime.h"
7 #include "cufft.h"
8
9 #include "cuda/cuda_error_check.cuh"
10
11 class ComplexMat {
12   public:
13     uint cols;
14     uint rows;
15     uint n_channels;
16     uint n_scales = 1;
17     bool foreign_data = false;
18     cudaStream_t stream = nullptr;
19
20     ComplexMat() : cols(0), rows(0), n_channels(0) {}
21     ComplexMat(uint _rows, uint _cols, uint _n_channels, cudaStream_t _stream)
22         : cols(_cols), rows(_rows), n_channels(_n_channels), stream(_stream)
23     {
24         CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
25     }
26
27     ComplexMat(uint _rows, uint _cols, uint _n_channels, uint _n_scales, cudaStream_t _stream)
28         : cols(_cols), rows(_rows), n_channels(_n_channels), n_scales(_n_scales), stream(_stream)
29     {
30         CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
31     }
32
33     ComplexMat(ComplexMat &&other)
34     {
35         cols = other.cols;
36         rows = other.rows;
37         n_channels = other.n_channels;
38         n_scales = other.n_scales;
39         p_data = other.p_data;
40         stream = other.stream;
41
42         other.p_data = nullptr;
43     }
44
45     ~ComplexMat()
46     {
47         if (p_data != nullptr && !foreign_data) {
48             CudaSafeCall(cudaFree(p_data));
49             p_data = nullptr;
50         }
51     }
52
53     void create(uint _rows, uint _cols, uint _n_channels, cudaStream_t _stream = nullptr)
54     {
55         rows = _rows;
56         cols = _cols;
57         n_channels = _n_channels;
58         stream = _stream;
59         CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
60     }
61
62     void create(uint _rows, uint _cols, uint _n_channels, uint _n_scales, cudaStream_t _stream = nullptr)
63     {
64         rows = _rows;
65         cols = _cols;
66         n_channels = _n_channels;
67         n_scales = _n_scales;
68         stream = _stream;
69         CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
70     }
71     // cv::Mat API compatibility
72     cv::Size size() { return cv::Size(cols, rows); }
73     int channels() { return n_channels; }
74     int channels() const { return n_channels; }
75
76     void set_stream(cudaStream_t _stream)
77     {
78         stream = _stream;
79         return;
80     }
81
82     void sqr_norm(float *result) const;
83
84     ComplexMat sqr_mag() const;
85
86     ComplexMat conj() const;
87
88     ComplexMat sum_over_channels() const;
89
90     cufftComplex *get_p_data() const;
91
92     // element-wise per channel multiplication, division and addition
93     ComplexMat operator*(const ComplexMat &rhs) const;
94     ComplexMat operator/(const ComplexMat &rhs) const;
95     ComplexMat operator+(const ComplexMat &rhs) const;
96
97     // multiplying or adding constant
98     ComplexMat operator*(const float &rhs) const;
99     ComplexMat operator+(const float &rhs) const;
100
101     // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
102     ComplexMat mul(const ComplexMat &rhs) const;
103
104     // multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
105     ComplexMat mul2(const ComplexMat &rhs) const;
106     // text output
107     friend std::ostream &operator<<(std::ostream &os, const ComplexMat &mat)
108     {
109         float *data_cpu =
110             reinterpret_cast<float *>(malloc(mat.rows * mat.cols * mat.n_channels * sizeof(cufftComplex)));
111         CudaSafeCall(cudaMemcpy(data_cpu, mat.p_data, mat.rows * mat.cols * mat.n_channels * sizeof(cufftComplex),
112                                 cudaMemcpyDeviceToHost));
113         // for (int i = 0; i < mat.n_channels; ++i){
114         for (int i = 0; i < 1; ++i) {
115             os << "Channel " << i << std::endl;
116             for (uint j = 0; j < mat.rows; ++j) {
117                 for (uint k = 0; k < 2 * mat.cols - 2; k += 2)
118                     os << "(" << data_cpu[j * 2 * mat.cols + k] << "," << data_cpu[j * 2 * mat.cols + (k + 1)] << ")"
119                        << ", ";
120                 os << "(" << data_cpu[j * 2 * mat.cols + 2 * mat.cols - 2] << ","
121                    << data_cpu[j * 2 * mat.cols + 2 * mat.cols - 1] << ")" << std::endl;
122             }
123         }
124         free(data_cpu);
125         return os;
126     }
127
128     void operator=(ComplexMat &rhs);
129     void operator=(ComplexMat &&rhs);
130
131   private:
132     mutable float *p_data = nullptr;
133 };
134
135 #endif // COMPLEXMAT_H