6 cudaErrorCheck(cublasCreate(&cublas));
9 void cuFFT::init(unsigned width, unsigned height, unsigned num_of_feats, unsigned num_of_scales)
11 Fft::init(width, height, num_of_feats, num_of_scales);
13 std::cout << "FFT: cuFFT" << std::endl;
18 int n[] = {int(m_height), int(m_width)};
19 int howmany = IF_BIG_BATCH(m_num_of_scales, 1);
20 int idist = m_height * m_width, odist = m_height * (m_width / 2 + 1);
21 int istride = 1, ostride = 1;
22 int *inembed = n, onembed[] = {int(m_height), int(m_width) / 2 + 1};
24 cudaErrorCheck(cufftPlanMany(&plan_f, rank, n, inembed, istride, idist, onembed, ostride, odist, CUFFT_R2C, howmany));
25 cudaErrorCheck(cufftSetStream(plan_f, cudaStreamPerThread));
31 int n[] = {int(m_height), int(m_width)};
32 int howmany = m_num_of_feats * IF_BIG_BATCH(m_num_of_scales, 1);
33 int idist = m_height * m_width, odist = m_height * (m_width / 2 + 1);
34 int istride = 1, ostride = 1;
35 int *inembed = n, onembed[] = {int(m_height), int(m_width) / 2 + 1};
37 cudaErrorCheck(cufftPlanMany(&plan_fw, rank, n, inembed, istride, idist, onembed, ostride, odist, CUFFT_R2C, howmany));
38 cudaErrorCheck(cufftSetStream(plan_fw, cudaStreamPerThread));
40 // FFT inverse all channels
43 int n[] = {int(m_height), int(m_width)};
44 int howmany = m_num_of_feats * IF_BIG_BATCH(m_num_of_scales, 1);
45 int idist = int(m_height * (m_width / 2 + 1)), odist = 1;
46 int istride = 1, ostride = m_num_of_feats * IF_BIG_BATCH(m_num_of_scales, 1);
47 int inembed[] = {int(m_height), int(m_width / 2 + 1)}, *onembed = n;
49 cudaErrorCheck(cufftPlanMany(&plan_i_features, rank, n, inembed, istride, idist, onembed, ostride, odist, CUFFT_C2R, howmany));
50 cudaErrorCheck(cufftSetStream(plan_i_features, cudaStreamPerThread));
52 // FFT inverse one channel
55 int n[] = {int(m_height), int(m_width)};
56 int howmany = IF_BIG_BATCH(m_num_of_scales, 1);
57 int idist = m_height * (m_width / 2 + 1), odist = 1;
58 int istride = 1, ostride = IF_BIG_BATCH(m_num_of_scales, 1);
59 int inembed[] = {int(m_height), int(m_width / 2 + 1)}, *onembed = n;
61 cudaErrorCheck(cufftPlanMany(&plan_i_1ch, rank, n, inembed, istride, idist, onembed, ostride, odist, CUFFT_C2R, howmany));
62 cudaErrorCheck(cufftSetStream(plan_i_1ch, cudaStreamPerThread));
66 void cuFFT::set_window(const MatDynMem &window)
68 Fft::set_window(window);
72 void cuFFT::forward(const MatScales &real_input, ComplexMat &complex_result)
74 Fft::forward(real_input, complex_result);
75 auto in = static_cast<cufftReal *>(const_cast<MatScales&>(real_input).deviceMem());
77 cudaErrorCheck(cufftExecR2C(plan_f, in, complex_result.get_p_data()));
80 void cuFFT::forward_window(MatScaleFeats &feat, ComplexMat &complex_result, MatScaleFeats &temp)
82 Fft::forward_window(feat, complex_result, temp);
84 uint n_channels = feat.size[0];
85 cufftReal *temp_data = temp.deviceMem();
87 for (uint i = 0; i < n_channels; ++i) {
88 for (uint j = 0; j < uint(feat.size[1]); ++j) {
89 cv::Mat feat_plane = feat.plane(i, j);
90 cv::Mat temp_plane = temp.plane(i, j);
91 temp_plane = feat_plane.mul(m_window);
94 cudaErrorCheck(cufftExecR2C(plan_fw, temp_data, complex_result.get_p_data()));
97 void cuFFT::inverse(ComplexMat &complex_input, MatDynMem &real_result)
99 Fft::inverse(complex_input, real_result);
101 uint n_channels = complex_input.n_channels;
102 cufftComplex *in = reinterpret_cast<cufftComplex *>(complex_input.get_p_data());
103 cufftReal *out = real_result.deviceMem();
104 float alpha = 1.0 / (m_width * m_height);
106 if (n_channels == 1) {
107 cudaErrorCheck(cufftExecC2R(plan_i_1ch, in, out));
109 cudaErrorCheck(cufftExecC2R(plan_i_features, in, out));
111 // TODO: Investigate whether this scalling is needed or not
112 cudaErrorCheck(cublasSscal(cublas, real_result.total(), &alpha, out, 1));
117 cudaErrorCheck(cublasDestroy(cublas));
119 cudaErrorCheck(cufftDestroy(plan_f));
120 cudaErrorCheck(cufftDestroy(plan_fw));
121 cudaErrorCheck(cufftDestroy(plan_i_1ch));
122 cudaErrorCheck(cufftDestroy(plan_i_features));