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