]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/threadctx.hpp
Merge remote-tracking branch 'upstream/master' 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         this->patch_feats.reserve(uint(num_of_feats));
25
26         uint cells_size = roi.width * roi.height * sizeof(float);
27
28 #if  !defined(BIG_BATCH) && defined(CUFFT) && (defined(ASYNC) || defined(OPENMP))
29         CudaSafeCall(cudaStreamCreate(&this->stream));
30 #endif
31
32 #if defined(CUFFT) || defined(FFTW)
33         this->gauss_corr_res = DynMem(cells_size * num_of_scales * num_of_angles);
34         this->data_features = DynMem(cells_size * num_of_feats);
35
36         uint width_freq = roi.width / 2 + 1;
37
38         this->in_all = cv::Mat(roi.height * num_of_scales * num_of_angles, roi.width, CV_32F, this->gauss_corr_res.hostMem());
39         this->fw_all = cv::Mat(roi.height * num_of_feats, roi.width, CV_32F, this->data_features.hostMem());
40 #else
41         uint width_freq = roi.width;
42
43         this->in_all = cv::Mat(roi, CV_32F);
44 #endif
45
46         this->data_i_features = DynMem(cells_size * num_of_feats);
47         this->data_i_1ch = DynMem(cells_size * num_of_scales * num_of_angles);
48
49         this->ifft2_res = cv::Mat(roi, CV_32FC(num_of_feats), this->data_i_features.hostMem());
50         this->response = cv::Mat(roi, CV_32FC(num_of_scales * num_of_angles), this->data_i_1ch.hostMem());
51
52         this->patch_feats.reserve(num_of_feats);
53
54 #ifdef CUFFT
55         this->zf.create(roi.height, width_freq, num_of_feats, num_of_scales * num_of_angles, this->stream);
56         this->kzf.create(roi.height, width_freq, num_of_scales * num_of_angles, this->stream);
57         this->kf.create(roi.height, width_freq, num_of_scales * num_of_angles, this->stream);
58 #else
59         this->zf.create(roi.height, width_freq, num_of_feats, num_of_scales * num_of_angles);
60         this->kzf.create(roi.height, width_freq, num_of_scales * num_of_angles);
61         this->kf.create(roi.height, width_freq, num_of_scales * num_of_angles);
62 #endif
63
64         if (num_of_scales > 1) {
65             this->max_responses.reserve(num_of_scales * num_of_angles);
66             this->max_locs.reserve(num_of_scales * num_of_angles);
67             this->response_maps.reserve(num_of_scales * num_of_angles);
68         }
69     }
70     ThreadCtx(ThreadCtx &&) = default;
71     ~ThreadCtx()
72     {
73 #if  !defined(BIG_BATCH) && defined(CUFFT) && (defined(ASYNC) || defined(OPENMP))
74         CudaSafeCall(cudaStreamDestroy(this->stream));
75 #endif
76     }
77
78     const double scale;
79     const int angle;
80 #ifdef ASYNC
81     std::future<void> async_res;
82 #endif
83
84     DynMem xf_sqr_norm, yf_sqr_norm;
85     std::vector<cv::Mat> patch_feats;
86
87     cv::Mat in_all, fw_all, ifft2_res, response;
88     ComplexMat zf, kzf, kf, xyf;
89
90     DynMem data_i_features, data_i_1ch;
91     // CuFFT and FFTW variables
92     DynMem gauss_corr_res, data_features;
93
94     // CuFFT variables
95     cudaStream_t stream = nullptr;
96     ComplexMat model_alphaf, model_xf;
97
98     // Variables used during non big batch mode and in big batch mode with ThreadCtx in p_threadctxs in kcf  on zero index.
99     cv::Point2i max_loc;
100     double max_val, max_response;
101
102     // Big batch variables
103     // Stores value of responses, location of maximal response and response maps for each scale
104     std::vector<double> max_responses;
105     std::vector<cv::Point2i> max_locs;
106     std::vector<cv::Mat> response_maps;
107 };
108
109 #endif // SCALE_VARS_HPP