LCOV - code coverage report
Current view: top level - elsa/storage/reductions - L2.h (source / functions) Hit Total Coverage
Test: coverage-all.lcov Lines: 22 22 100.0 %
Date: 2024-12-21 07:37:52 Functions: 26 27 96.3 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include "Functions.hpp"
       4             : #include "TypeTraits.hpp"
       5             : #include "functions/Abs.hpp"
       6             : #include "functions/Square.hpp"
       7             : 
       8             : #include <thrust/transform_reduce.h>
       9             : 
      10             : namespace elsa
      11             : {
      12             :     /// @brief Compute the squared L2-norm, the sum of squares (\f$\sum_i x_i * x_i\f$.)
      13             :     ///
      14             :     /// @ingroup reductions
      15             :     template <class InputIter>
      16             :     auto squaredL2Norm(InputIter first, InputIter last)
      17             :         -> value_type_of_t<thrust::iterator_value_t<InputIter>>
      18        7480 :     {
      19        7480 :         using data_t = thrust::iterator_value_t<InputIter>;
      20        7480 :         using inner_t = value_type_of_t<data_t>;
      21             : 
      22        7480 :         if constexpr (is_complex_v<data_t>) {
      23        7328 :             return thrust::transform_reduce(
      24        7328 :                 first, last,
      25       60965 :                 [] __host__ __device__(const data_t& val) {
      26       60965 :                     return elsa::fn::square(elsa::abs(val));
      27       60965 :                 },
      28         152 :                 inner_t(0), elsa::plus{});
      29        7328 :         } else {
      30        7328 :             return thrust::transform_reduce(
      31        7328 :                 first, last,
      32      281074 :                 [] __host__ __device__(const data_t& val) { return elsa::fn::square(val); },
      33        7328 :                 data_t(0), elsa::plus{});
      34        7328 :         }
      35        7480 :     }
      36             : 
      37             :     /// @brief Compute the L2-norm, the square root of the sum of squares (\f$\sqrt{\sum_i x_i *
      38             :     /// x_i}\f$.)
      39             :     ///
      40             :     /// @ingroup reductions
      41             :     template <class InputIter>
      42             :     auto l2Norm(InputIter first, InputIter last)
      43             :         -> value_type_of_t<thrust::iterator_value_t<InputIter>>
      44        2287 :     {
      45        2287 :         using std::sqrt;
      46        2287 :         using thrust::sqrt;
      47             : 
      48        2287 :         return sqrt(elsa::squaredL2Norm(first, last));
      49        2287 :     }
      50             : } // namespace elsa

Generated by: LCOV version 1.14