]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/threadctx.hpp
Merge branch 'rotation' of https://github.com/Shanigen/kcf into rotation
[hercules2020/kcf.git] / src / threadctx.hpp
1 #ifndef SCALE_VARS_HPP
2 #define SCALE_VARS_HPP
3
4 #include <future>
5 #include "dynmem.hpp"
6
7 #ifdef CUFFT
8 #include "complexmat.cuh"
9 #else
10 #include "complexmat.hpp"
11 #ifndef CUFFTW
12 // For compatibility reasons between CuFFT and FFTW, OpenCVfft versions.
13 typedef int *cudaStream_t;
14 #endif
15 #endif
16
17 struct ThreadCtx {
18   public:
19     ThreadCtx(cv::Size roi, uint num_of_feats, double scale, int angle, uint num_of_scales = 1, uint num_of_angles = 1)
20         : scale(scale), angle(angle)
21     {
22         this->xf_sqr_norm = DynMem(num_of_scales * num_of_angles * sizeof(float));
23         this->yf_sqr_norm = DynMem(sizeof(float));
24
25         uint cells_size = roi.width * roi.height * sizeof(float);
26
27 #if  !defined(BIG_BATCH) && defined(CUFFT) && (defined(ASYNC) || defined(OPENMP))
28         CudaSafeCall(cudaStreamCreate(&this->stream));
29 #endif
30
31 #if defined(CUFFT) || defined(FFTW)
32         this->gauss_corr_res = DynMem(cells_size * num_of_scales * num_of_angles);
33         this->data_features = DynMem(cells_size * num_of_feats);
34
35         uint width_freq = roi.width / 2 + 1;
36
37         this->in_all = cv::Mat(roi.height * num_of_scales * num_of_angles, roi.width, CV_32F, this->gauss_corr_res.hostMem());
38         this->fw_all = cv::Mat(roi.height * num_of_feats, roi.width, CV_32F, this->data_features.hostMem());
39 #else
40         uint width_freq = roi.width;
41
42         this->in_all = cv::Mat(roi, CV_32F);
43 #endif
44
45         this->data_i_features = DynMem(cells_size * num_of_feats);
46         this->data_i_1ch = DynMem(cells_size * num_of_scales * num_of_angles);
47
48         this->ifft2_res = cv::Mat(roi, CV_32FC(num_of_feats), this->data_i_features.hostMem());
49         this->response = cv::Mat(roi, CV_32FC(num_of_scales * num_of_angles), this->data_i_1ch.hostMem());
50
51 #ifdef CUFFT
52         this->zf.create(roi.height, width_freq, num_of_feats, num_of_scales * num_of_angles, this->stream);
53         this->kzf.create(roi.height, width_freq, num_of_scales * num_of_angles, this->stream);
54         this->kf.create(roi.height, width_freq, num_of_scales * num_of_angles, this->stream);
55 #else
56         this->zf.create(roi.height, width_freq, num_of_feats, num_of_scales * num_of_angles);
57         this->kzf.create(roi.height, width_freq, num_of_scales * num_of_angles);
58         this->kf.create(roi.height, width_freq, num_of_scales * num_of_angles);
59 #endif
60
61 #ifdef BIG_BATCH
62         if (num_of_scales > 1) {
63             this->max_responses.reserve(num_of_scales * num_of_angles);
64             this->max_locs.reserve(num_of_scales * num_of_angles);
65             this->response_maps.reserve(num_of_scales * num_of_angles);
66         }
67 #endif
68     }
69     ThreadCtx(ThreadCtx &&) = default;
70     ~ThreadCtx()
71     {
72 #if  !defined(BIG_BATCH) && defined(CUFFT) && (defined(ASYNC) || defined(OPENMP))
73         CudaSafeCall(cudaStreamDestroy(this->stream));
74 #endif
75     }
76
77     const double scale;
78     const int angle;
79 #ifdef ASYNC
80     std::future<void> async_res;
81 #endif
82
83     DynMem xf_sqr_norm, yf_sqr_norm;
84
85     cv::Mat in_all, fw_all, ifft2_res, response;
86     ComplexMat zf, kzf, kf, xyf;
87
88     DynMem data_i_features, data_i_1ch;
89     // CuFFT and FFTW variables
90     DynMem gauss_corr_res, data_features;
91
92     // CuFFT variables
93     cudaStream_t stream = nullptr;
94     ComplexMat model_alphaf, model_xf;
95
96     // Variables used during non big batch mode and in big batch mode with ThreadCtx in p_threadctxs in kcf  on zero index.
97     cv::Point2i max_loc;
98     double max_val, max_response;
99
100 #ifdef BIG_BATCH
101     // Stores value of responses, location of maximal response and response maps for each scale
102     std::vector<double> max_responses;
103     std::vector<cv::Point2i> max_locs;
104     std::vector<cv::Mat> response_maps;
105 #endif
106 };
107
108 #endif // SCALE_VARS_HPP