]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blobdiff - src/complexmat.hpp
Unify CPU and GPU implementations of ComplexMat
[hercules2020/kcf.git] / src / complexmat.hpp
index 1a0c1d508efbd1f2b4fd891adb2bfcc720e896f2..60c8255912b490a2dd90cd33e81dcdf28031c5a4 100644 (file)
@@ -7,6 +7,10 @@
 #include <functional>
 #include "dynmem.hpp"
 
+#ifdef CUFFT
+#include <cufft.h>
+#endif
+
 class ComplexMat_ {
   public:
     typedef float T;
@@ -50,63 +54,15 @@ class ComplexMat_ {
         }
     }
 
-    T sqr_norm() const
-    {
-        assert(n_scales == 1);
+    T sqr_norm() const;
 
-        int n_channels_per_scale = n_channels / n_scales;
-        T sum_sqr_norm = 0;
-        for (int i = 0; i < n_channels_per_scale; ++i) {
-            for (auto lhs = p_data.hostMem() + i * rows * cols; lhs != p_data.hostMem() + (i + 1) * rows * cols; ++lhs)
-                sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
-        }
-        sum_sqr_norm = sum_sqr_norm / static_cast<T>(cols * rows);
-        return sum_sqr_norm;
-    }
+    void sqr_norm(DynMem_<T> &result) const;
 
-    void sqr_norm(DynMem_<T> &result) const
-    {
-        int n_channels_per_scale = n_channels / n_scales;
-        int scale_offset = n_channels_per_scale * rows * cols;
-        for (uint scale = 0; scale < n_scales; ++scale) {
-            T sum_sqr_norm = 0;
-            for (int i = 0; i < n_channels_per_scale; ++i)
-                for (auto lhs = p_data.hostMem() + i * rows * cols + scale * scale_offset;
-                     lhs != p_data.hostMem() + (i + 1) * rows * cols + scale * scale_offset; ++lhs)
-                    sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
-            result.hostMem()[scale] = sum_sqr_norm / static_cast<T>(cols * rows);
-        }
-        return;
-    }
-
-    ComplexMat_ sqr_mag() const
-    {
-        return mat_const_operator([](std::complex<T> &c) { c = c.real() * c.real() + c.imag() * c.imag(); });
-    }
+    ComplexMat_ sqr_mag() const;
 
-    ComplexMat_ conj() const
-    {
-        return mat_const_operator([](std::complex<T> &c) { c = std::complex<T>(c.real(), -c.imag()); });
-    }
+    ComplexMat_ conj() const;
 
-    ComplexMat_ sum_over_channels() const
-    {
-        assert(p_data.num_elem == n_channels * rows * cols);
-
-        uint n_channels_per_scale = n_channels / n_scales;
-        uint scale_offset = n_channels_per_scale * rows * cols;
-
-        ComplexMat_ result(this->rows, this->cols, 1, n_scales);
-        for (uint scale = 0; scale < n_scales; ++scale) {
-            for (uint i = 0; i < rows * cols; ++i) {
-                std::complex<T> acc = 0;
-                for (uint ch = 0; ch < n_channels_per_scale; ++ch)
-                    acc +=  p_data[scale * scale_offset + i + ch * rows * cols];
-                result.p_data.hostMem()[scale * rows * cols + i] = acc;
-            }
-        }
-        return result;
-    }
+    ComplexMat_ sum_over_channels() const;
 
     // return 2 channels (real, imag) for first complex channel
     cv::Mat to_cv_mat() const
@@ -129,35 +85,22 @@ class ComplexMat_ {
     std::complex<T> *get_p_data() { return p_data.hostMem(); }
     const std::complex<T> *get_p_data() const { return p_data.hostMem(); }
 
+#ifdef CUFFT
+    cufftComplex *get_dev_data() { return (cufftComplex*)p_data.deviceMem(); }
+    const cufftComplex *get_dev_data() const { return (cufftComplex*)p_data.deviceMem(); }
+#endif
+
     // element-wise per channel multiplication, division and addition
-    ComplexMat_ operator*(const ComplexMat_ &rhs) const
-    {
-        return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
-    }
-    ComplexMat_ operator/(const ComplexMat_ &rhs) const
-    {
-        return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs /= c_rhs; }, rhs);
-    }
-    ComplexMat_ operator+(const ComplexMat_ &rhs) const
-    {
-        return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs += c_rhs; }, rhs);
-    }
+    ComplexMat_ operator*(const ComplexMat_ &rhs) const;
+    ComplexMat_ operator/(const ComplexMat_ &rhs) const;
+    ComplexMat_ operator+(const ComplexMat_ &rhs) const;
 
     // multiplying or adding constant
-    ComplexMat_ operator*(const T &rhs) const
-    {
-        return mat_const_operator([&rhs](std::complex<T> &c) { c *= rhs; });
-    }
-    ComplexMat_ operator+(const T &rhs) const
-    {
-        return mat_const_operator([&rhs](std::complex<T> &c) { c += rhs; });
-    }
+    ComplexMat_ operator*(const T &rhs) const;
+    ComplexMat_ operator+(const T &rhs) const;
 
     // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
-    ComplexMat_ mul(const ComplexMat_ &rhs) const
-    {
-        return matn_mat1_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
-    }
+    ComplexMat_ mul(const ComplexMat_ &rhs) const;
 
     // multiplying element-wise multichannel mats - same as operator*(ComplexMat), but without allocating memory for the result
     ComplexMat_ muln(const ComplexMat_ &rhs) const