]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blobdiff - src/fft_cufft.cpp
Fix CUDA bugs that manifest themselves in BIG_BATCH mode
[hercules2020/kcf.git] / src / fft_cufft.cpp
index 6269992707e010ace175206c310c12061dc40f8e..963c8b7084ff42d3ed24971cb627d2181897bdf9 100644 (file)
 #include "fft_cufft.h"
 
-#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
-inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
+cuFFT::cuFFT()
 {
-   if (code != cudaSuccess)
-   {
-      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
-      if (abort) exit(code);
-   }
+    CudaSafeCall(cudaSetDeviceFlags(cudaDeviceMapHost));
+    cudaErrorCheck(cublasCreate(&cublas));
 }
 
-cuFFT::cuFFT(): m_num_of_streams(4)
-{}
-void cuFFT::init(unsigned width, unsigned height, unsigned num_of_feats, unsigned num_of_scales)
+cufftHandle cuFFT::create_plan_fwd(uint howmany) const
 {
-    m_width = width;
-    m_height = height;
-    m_num_of_feats = num_of_feats;
-    m_num_of_scales = num_of_scales;
-
-    std::cout << "FFT: cuFFT" << std::endl;
+    int rank = 2;
+    int n[] = {(int)m_height, (int)m_width};
+    int idist = m_height * m_width, odist = m_height * (m_width / 2 + 1);
+    int istride = 1, ostride = 1;
+    int *inembed = n, onembed[] = {(int)m_height, (int)m_width / 2 + 1};
 
-    for (unsigned i = 0; i < m_num_of_streams; i++) gpuErrchk(cudaStreamCreate(&streams[i]));
+    cufftHandle plan;
+    cudaErrorCheck(cufftPlanMany(&plan, rank, n, inembed, istride, idist, onembed, ostride, odist, CUFFT_R2C, howmany));
+    cudaErrorCheck(cufftSetStream(plan, cudaStreamPerThread));
+    return plan;
+}
 
-    //FFT forward one scale
-    {
-    cufftPlan2d(&plan_f, m_height, m_width, CUFFT_C2R);
-    }
-    //FFT forward all scales
-    if(m_num_of_scales > 1)
-    {
+cufftHandle cuFFT::create_plan_inv(uint howmany) const
+{
     int rank = 2;
     int n[] = {(int)m_height, (int)m_width};
-    int howmany = m_num_of_scales;
-    int idist = m_height*m_width, odist = m_height*(m_width/2+1);
+    int idist = m_height * (m_width / 2 + 1), odist = m_height * m_width;
     int istride = 1, ostride = 1;
-    int *inembed = NULL, *onembed = NULL;
+    int inembed[] = {(int)m_height, (int)m_width / 2 + 1}, *onembed = n;
 
-    cufftPlanMany(&plan_f_all_scales, rank, n,
-                 inembed, istride, idist,
-                 onembed, ostride, odist,
-                 CUFFT_R2C, howmany);
-    }
-    //FFT forward window one scale
-    {
-        int rank = 2;
-        int n[] = {(int)m_height, (int)m_width};
-        int howmany = m_num_of_feats;
-        int idist = m_height*m_width, odist = m_height*(m_width/2+1);
-        int istride = 1, ostride = 1;
-        int *inembed = NULL, *onembed = NULL;
-
-        cufftPlanMany(&plan_fw, rank, n,
-                 inembed, istride, idist,
-                 onembed, ostride, odist,
-                 CUFFT_R2C, howmany);
-    }
-    //FFT forward window all scales all feats
-    if(m_num_of_scales > 1)
-    {
-        int rank = 2;
-        int n[] = {(int)m_height, (int)m_width};
-        int howmany = m_num_of_scales*m_num_of_feats;
-        int idist = m_height*m_width, odist = m_height*(m_width/2+1);
-        int istride = 1, ostride = 1;
-        int *inembed = NULL, *onembed = NULL;
-
-        cufftPlanMany(&plan_fw_all_scales, rank, n,
-                 inembed, istride, idist,
-                 onembed, ostride, odist,
-                 CUFFT_R2C, howmany);
-    }
-    //FFT inverse one scale
-    {
-        int rank = 2;
-        int n[] = {(int)m_height, (int)m_width};
-        int howmany = m_num_of_feats;
-        int idist = m_height*(m_width/2+1), odist = 1;
-        int istride = 1, ostride = m_num_of_feats;
-        int inembed[] = {(int)m_height, (int)m_width/2+1}, *onembed = n;
-
-        cufftPlanMany(&plan_i_features, rank, n,
-                 inembed, istride, idist,
-                 onembed, ostride, odist,
-                 CUFFT_C2R, howmany);
-    }
-    //FFT inverse all scales
-    if(m_num_of_scales > 1)
-    {
-        int rank = 2;
-        int n[] = {(int)m_height, (int)m_width};
-        int howmany = m_num_of_feats*m_num_of_scales;
-        int idist = m_height*(m_width/2+1), odist = 1;
-        int istride = 1, ostride = m_num_of_feats*m_num_of_scales;
-        int inembed[] = {(int)m_height, (int)m_width/2+1}, *onembed = n;
-
-        cufftPlanMany(&plan_i_features_all_scales, rank, n,
-                 inembed, istride, idist,
-                 onembed, ostride, odist,
-                 CUFFT_C2R, howmany);
-    }
-    //FFT inver one channel one scale
-    {
-        int rank = 2;
-        int n[] = {(int)m_height, (int)m_width};
-        int howmany = 1;
-        int idist = m_height*(m_width/2+1), odist = 1;
-        int istride = 1, ostride = 1;
-        int inembed[] = {(int)m_height, (int)m_width/2+1}, *onembed = n;
-
-        cufftPlanMany(&plan_i_1ch, rank, n,
-                 inembed, istride, idist,
-                 onembed, ostride, odist,
-                 CUFFT_C2R, howmany);
-    }
-    //FFT inver one channel all scales
-    if(m_num_of_scales > 1)
-    {
-        int rank = 2;
-        int n[] = {(int)m_height, (int)m_width};
-        int howmany = m_num_of_scales;
-        int idist = m_height*(m_width/2+1), odist = 1;
-        int istride = 1, ostride = m_num_of_scales;
-        int inembed[] = {(int)m_height, (int)m_width/2+1}, *onembed = n;
-
-        cufftPlanMany(&plan_i_1ch_all_scales, rank, n,
-                 inembed, istride, idist,
-                 onembed, ostride, odist,
-                 CUFFT_C2R, howmany);
-    }
+    cufftHandle plan;
+    cudaErrorCheck(cufftPlanMany(&plan, rank, n, inembed, istride, idist, onembed, ostride, odist, CUFFT_C2R, howmany));
+    cudaErrorCheck(cufftSetStream(plan, cudaStreamPerThread));
+    return plan;
 }
 
-void cuFFT::set_window(const cv::Mat &window)
+
+void cuFFT::init(unsigned width, unsigned height, unsigned num_of_feats, unsigned num_of_scales)
 {
-     m_window = window;
+    Fft::init(width, height, num_of_feats, num_of_scales);
+
+    std::cout << "FFT: cuFFT" << std::endl;
+
+    plan_f = create_plan_fwd(1);
+    plan_fw = create_plan_fwd(m_num_of_feats);
+    plan_i_1ch = create_plan_inv(1);
+
+#ifdef BIG_BATCH
+    plan_f_all_scales = create_plan_fwd(m_num_of_scales);
+    plan_fw_all_scales = create_plan_fwd(m_num_of_scales * m_num_of_feats);
+    plan_i_all_scales = create_plan_inv(m_num_of_scales);
+#endif
 }
 
-ComplexMat cuFFT::forward(const cv::Mat &input)
+void cuFFT::set_window(const MatDynMem &window)
 {
-    ComplexMat complex_result;
-    if(input.rows == (int)(m_height*m_num_of_scales)){
-        complex_result.create(m_height, m_width / 2 + 1, m_num_of_scales);
-        cufftExecR2C(plan_f_all_scales, reinterpret_cast<float*>(input.data),
-                                reinterpret_cast<cufftComplex*>(complex_result.get_p_data()));
-    } else {
-        complex_result.create(m_height, m_width / 2 + 1, 1);
-        cufftExecR2C(plan_f, reinterpret_cast<float*>(input.data),
-                                reinterpret_cast<cufftComplex*>(complex_result.get_p_data()));
-    }
-    return complex_result;
+    Fft::set_window(window);
+    m_window = window;
 }
 
-ComplexMat cuFFT::forward_window(const std::vector<cv::Mat> &input)
+void cuFFT::forward(const MatScales &real_input, ComplexMat &complex_result)
 {
-    int n_channels = input.size();
-    cv::Mat in_all(m_height * n_channels, m_width, CV_32F);
-    for (int i = 0; i < n_channels; ++i) {
-        cv::Mat in_roi(in_all, cv::Rect(0, i*m_height, m_width, m_height));
-        in_roi = input[i].mul(m_window);
-    }
-    ComplexMat result;
-    if(n_channels > (int) m_num_of_feats)
-        result.create(m_height, m_width/2 + 1, n_channels,m_num_of_scales);
+    Fft::forward(real_input, complex_result);
+    auto in = static_cast<cufftReal *>(const_cast<MatScales&>(real_input).deviceMem());
+
+    if (real_input.size[0] == 1)
+        cudaErrorCheck(cufftExecR2C(plan_f, in, complex_result.get_dev_data()));
+#ifdef BIG_BATCH
     else
-        result.create(m_height, m_width/2 + 1, n_channels);
+        cudaErrorCheck(cufftExecR2C(plan_f_all_scales, in, complex_result.get_dev_data()));
+#endif
+}
 
-    float *in = reinterpret_cast<float*>(in_all.data);
-    cufftComplex *out = reinterpret_cast<cufftComplex*>(result.get_p_data());
+void cuFFT::forward_window(MatScaleFeats &feat, ComplexMat &complex_result, MatScaleFeats &temp)
+{
+    Fft::forward_window(feat, complex_result, temp);
 
-    if (n_channels <= (int) m_num_of_feats)
-        cufftExecR2C(plan_fw, in, out);
-    else
-       cufftExecR2C(plan_fw_all_scales, in, out);
+    cufftReal *temp_data = temp.deviceMem();
+    uint n_scales = feat.size[0];
+
+    for (uint s = 0; s < n_scales; ++s) {
+        for (uint ch = 0; ch < uint(feat.size[1]); ++ch) {
+            cv::Mat feat_plane = feat.plane(s, ch);
+            cv::Mat temp_plane = temp.plane(s, ch);
+            temp_plane = feat_plane.mul(m_window);
+        }
+    }
 
-    return result;
+    if (n_scales == 1)
+        cudaErrorCheck(cufftExecR2C(plan_fw, temp_data, complex_result.get_dev_data()));
+#ifdef BIG_BATCH
+    else
+        cudaErrorCheck(cufftExecR2C(plan_fw_all_scales, temp_data, complex_result.get_dev_data()));
+#endif
+    CudaSafeCall(cudaStreamSynchronize(cudaStreamPerThread));
 }
 
-cv::Mat cuFFT::inverse(const ComplexMat &inputf)
+void cuFFT::inverse(ComplexMat &complex_input, MatScales &real_result)
 {
-    int n_channels = inputf.n_channels;
-    cv::Mat real_result(m_height, m_width, CV_32FC(n_channels));
-    cufftComplex *in = reinterpret_cast<cufftComplex*>(inputf.get_p_data());
-    float *out = reinterpret_cast<float*>(real_result.data);
-
-    if(n_channels == 1)
-        cufftExecC2R(plan_i_1ch, in, out);
-    else if(n_channels == (int) m_num_of_scales)
-        cufftExecC2R(plan_i_1ch_all_scales, in, out);
-    else if(n_channels == (int) m_num_of_feats * (int) m_num_of_scales)
-        cufftExecC2R(plan_i_features_all_scales, in, out);
-    else
-        cufftExecC2R(plan_i_features, in, out);
+    Fft::inverse(complex_input, real_result);
 
-    return real_result/(m_width*m_height);
+    uint n_channels = complex_input.n_channels;
+    cufftComplex *in = reinterpret_cast<cufftComplex *>(complex_input.get_dev_data());
+    cufftReal *out = real_result.deviceMem();
+    float alpha = 1.0 / (m_width * m_height);
+
+    if (n_channels == 1)
+        cudaErrorCheck(cufftExecC2R(plan_i_1ch, in, out));
+#ifdef BIG_BATCH
+    else
+        cudaErrorCheck(cufftExecC2R(plan_i_all_scales, in, out));
+#endif
+    // TODO: Investigate whether this scalling is needed or not
+    cudaErrorCheck(cublasSscal(cublas, real_result.total(), &alpha, out, 1));
+    CudaSafeCall(cudaStreamSynchronize(cudaStreamPerThread));
 }
 
 cuFFT::~cuFFT()
 {
+    cudaErrorCheck(cublasDestroy(cublas));
 
-  for(unsigned i = 0; i < m_num_of_streams; i++) gpuErrchk(cudaStreamDestroy(streams[i]));
+    cudaErrorCheck(cufftDestroy(plan_f));
+    cudaErrorCheck(cufftDestroy(plan_fw));
+    cudaErrorCheck(cufftDestroy(plan_i_1ch));
 
-  cudaDeviceReset();
+#ifdef BIG_BATCH
+    cudaErrorCheck(cufftDestroy(plan_f_all_scales));
+    cudaErrorCheck(cufftDestroy(plan_fw_all_scales));
+    cudaErrorCheck(cufftDestroy(plan_i_all_scales));
+#endif
 }