Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:22

0001 // @(#)root/mathcore:$Id$
0002 // Author: L. Moneta Tue Nov 14 15:44:38 2006
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // Utility functions for all ROOT Math classes
0012 
0013 #ifndef ROOT_Math_Util
0014 #define ROOT_Math_Util
0015 
0016 #include <string>
0017 #include <sstream>
0018 
0019 #include <cmath>
0020 #include <limits>
0021 #include <numeric>
0022 
0023 
0024 // This can be protected against by defining ROOT_Math_VecTypes
0025 // This is only used for the R__HAS_VECCORE define
0026 // and a single VecCore function in EvalLog
0027 #ifndef ROOT_Math_VecTypes
0028 #include "Types.h"
0029 #endif
0030 
0031 
0032 // for defining unused variables in the interfaces
0033 //  and have still them in the documentation
0034 #define MATH_UNUSED(var)   (void)var
0035 
0036 
0037 namespace ROOT {
0038 
0039    namespace Math {
0040 
0041    /**
0042       namespace defining Utility functions needed by mathcore
0043    */
0044    namespace Util {
0045 
0046    /**
0047       Utility function for conversion to strings
0048    */
0049    template <class T>
0050    std::string ToString(const T &val)
0051    {
0052       std::ostringstream buf;
0053       buf << val;
0054 
0055       std::string ret = buf.str();
0056       return ret;
0057    }
0058 
0059    /// safe evaluation of log(x) with a protections against negative or zero argument to the log
0060    /// smooth linear extrapolation below function values smaller than  epsilon
0061    /// (better than a simple cut-off)
0062 
0063    template<class T>
0064    inline T EvalLog(T x) {
0065       static const T epsilon = T(2.0 * std::numeric_limits<double>::min());
0066 #ifdef R__HAS_VECCORE
0067       T logval = vecCore::Blend<T>(x <= epsilon, x / epsilon + std::log(epsilon) - T(1.0), std::log(x));
0068 #else
0069       T logval = x <= epsilon ? x / epsilon + std::log(epsilon) - T(1.0) : std::log(x);
0070 #endif
0071       return logval;
0072    }
0073 
0074    } // end namespace Util
0075 
0076    /// \class KahanSum
0077    /// The Kahan summation is a compensated summation algorithm, which significantly reduces numerical errors
0078    /// when adding a sequence of finite-precision floating point numbers.
0079    /// This is done by keeping a separate running compensation (a variable to accumulate small errors).
0080    ///
0081    /// ### Auto-vectorisable accumulation
0082    /// This class can internally use multiple accumulators (template parameter `N`).
0083    /// When filled from a collection that supports index access from a *contiguous* block of memory,
0084    /// compilers such as gcc, clang and icc can auto-vectorise the accumulation. This happens by cycling
0085    /// through the internal accumulators based on the value of "`index % N`", so `N` accumulators can be filled from a block
0086    /// of `N` numbers in a single instruction.
0087    ///
0088    /// The usage of multiple accumulators might slightly increase the precision in comparison to the single-accumulator version
0089    /// with `N = 1`.
0090    /// This depends on the order and magnitude of the numbers being accumulated. Therefore, in rare cases, the accumulation
0091    /// result can change *in dependence of N*, even when the data are identical.
0092    /// The magnitude of such differences is well below the precision of the floating point type, and will therefore mostly show
0093    /// in the compensation sum(see Carry()). Increasing the number of accumulators therefore only makes sense to
0094    /// speed up the accumulation, but not to increase precision.
0095    ///
0096    /// \param T The type of the values to be accumulated.
0097    /// \param N Number of accumulators. Defaults to 1. Ideal values are the widths of a vector register on the relevant architecture.
0098    /// Depending on the instruction set, good values are:
0099    /// - AVX2-float: 8
0100    /// - AVX2-double: 4
0101    /// - AVX512-float: 16
0102    /// - AVX512-double: 8
0103    ///
0104    /// ### Examples
0105    ///
0106    /// ~~~{.cpp}
0107    /// std::vector<double> numbers(1000);
0108    /// for (std::size_t i=0; i<1000; ++i) {
0109    ///    numbers[i] = rand();
0110    /// }
0111    ///
0112    /// ROOT::Math::KahanSum<double, 4> k;
0113    /// k.Add(numbers.begin(), numbers.end());
0114    /// // or
0115    /// k.Add(numbers);
0116    /// ~~~
0117    /// ~~~{.cpp}
0118    /// double offset = 10.;
0119    /// auto result = ROOT::Math::KahanSum<double, 4>::Accumulate(numbers.begin(), numbers.end(), offset);
0120    /// ~~~
0121    template<typename T = double, unsigned int N = 1>
0122    class KahanSum {
0123      public:
0124        /// Initialise the sum.
0125        /// \param[in] initialValue Initialise with this value. Defaults to 0.
0126        explicit KahanSum(T initialValue = T{}) {
0127          fSum[0] = initialValue;
0128          std::fill(std::begin(fSum)+1, std::end(fSum), 0.);
0129          std::fill(std::begin(fCarry), std::end(fCarry), 0.);
0130        }
0131 
0132        /// Initialise with a sum value and a carry value.
0133        /// \param[in] initialSumValue Initialise the sum with this value.
0134        /// \param[in] initialCarryValue Initialise the carry with this value.
0135        KahanSum(T initialSumValue, T initialCarryValue) {
0136           fSum[0] = initialSumValue;
0137           fCarry[0] = initialCarryValue;
0138           std::fill(std::begin(fSum)+1, std::end(fSum), 0.);
0139           std::fill(std::begin(fCarry)+1, std::end(fCarry), 0.);
0140        }
0141 
0142        /// Initialise the sum with a pre-existing state.
0143        /// \param[in] sumBegin Begin of iterator range with values to initialise the sum with.
0144        /// \param[in] sumEnd End of iterator range with values to initialise the sum with.
0145        /// \param[in] carryBegin Begin of iterator range with values to initialise the carry with.
0146        /// \param[in] carryEnd End of iterator range with values to initialise the carry with.
0147        template<class Iterator>
0148        KahanSum(Iterator sumBegin, Iterator sumEnd, Iterator carryBegin, Iterator carryEnd) {
0149           assert(std::distance(sumBegin, sumEnd) == N);
0150           assert(std::distance(carryBegin, carryEnd) == N);
0151           std::copy(sumBegin, sumEnd, std::begin(fSum));
0152           std::copy(carryBegin, carryEnd, std::begin(fCarry));
0153        }
0154 
0155        /// Constructor to create a KahanSum from another KahanSum with a different number of accumulators
0156        template <unsigned int M>
0157        KahanSum(KahanSum<T,M> const& other) {
0158          fSum[0] = other.Sum();
0159          fCarry[0] = other.Carry();
0160          std::fill(std::begin(fSum)+1, std::end(fSum), 0.);
0161          std::fill(std::begin(fCarry)+1, std::end(fCarry), 0.);
0162        }
0163 
0164        /// Single-element accumulation. Will not vectorise.
0165        void Add(T x) {
0166           auto y = x - fCarry[0];
0167           auto t = fSum[0] + y;
0168           fCarry[0] = (t - fSum[0]) - y;
0169           fSum[0] = t;
0170        }
0171 
0172 
0173        /// Accumulate from a range denoted by iterators.
0174        ///
0175        /// This function will auto-vectorise with random-access iterators.
0176        /// \param[in] begin Beginning of a range. Needs to be a random access iterator for automatic
0177        /// vectorisation, because a contiguous block of memory needs to be read.
0178        /// \param[in] end   End of the range.
0179        template <class Iterator>
0180        void Add(Iterator begin, Iterator end) {
0181            static_assert(std::is_floating_point<
0182                typename std::remove_reference<decltype(*begin)>::type>::value,
0183                "Iterator needs to point to floating-point values.");
0184            const std::size_t n = std::distance(begin, end);
0185 
0186            for (std::size_t i=0; i<n; ++i) {
0187              AddIndexed(*(begin++), i);
0188            }
0189        }
0190 
0191 
0192        /// Fill from a container that supports index access.
0193        /// \param[in] inputs Container with index access such as std::vector or array.
0194        template<class Container_t>
0195        void Add(const Container_t& inputs) {
0196          static_assert(std::is_floating_point<typename Container_t::value_type>::value,
0197              "Container does not hold floating-point values.");
0198          for (std::size_t i=0; i < inputs.size(); ++i) {
0199            AddIndexed(inputs[i], i);
0200          }
0201        }
0202 
0203 
0204        /// Iterate over a range and return an instance of a KahanSum.
0205        ///
0206        /// See Add(Iterator,Iterator) for details.
0207        /// \param[in] begin Beginning of a range.
0208        /// \param[in] end   End of the range.
0209        /// \param[in] initialValue Optional initial value.
0210        template <class Iterator>
0211        static KahanSum<T, N> Accumulate(Iterator begin, Iterator end,
0212            T initialValue = T{}) {
0213            KahanSum<T, N> theSum(initialValue);
0214            theSum.Add(begin, end);
0215 
0216            return theSum;
0217        }
0218 
0219 
0220        /// Add `input` to the sum.
0221        ///
0222        /// Particularly helpful when filling from a for loop.
0223        /// This function can be inlined and auto-vectorised if
0224        /// the index parameter is used to enumerate *consecutive* fills.
0225        /// Use Add() or Accumulate() when no index is available.
0226        /// \param[in] input Value to accumulate.
0227        /// \param[in] index Index of the value. Determines internal accumulator that this
0228        /// value is added to. Make sure that consecutive fills have consecutive indices
0229        /// to make a loop auto-vectorisable. The actual value of the index does not matter,
0230        /// as long as it is consecutive.
0231        void AddIndexed(T input, std::size_t index) {
0232          const unsigned int i = index % N;
0233          const T y = input - fCarry[i];
0234          const T t = fSum[i] + y;
0235          fCarry[i] = (t - fSum[i]) - y;
0236          fSum[i] = t;
0237        }
0238 
0239        /// \return Compensated sum.
0240        T Sum() const {
0241          return std::accumulate(std::begin(fSum), std::end(fSum), 0.);
0242        }
0243 
0244        /// \return Compensated sum.
0245        T Result() const {
0246          return Sum();
0247        }
0248 
0249        /// \return The sum used for compensation.
0250        T Carry() const {
0251          return std::accumulate(std::begin(fCarry), std::end(fCarry), 0.);
0252        }
0253 
0254        /// Add `arg` into accumulator. Does not vectorise.
0255        KahanSum<T, N>& operator+=(T arg) {
0256          Add(arg);
0257          return *this;
0258        }
0259 
0260        /// Add other KahanSum into accumulator. Does not vectorise.
0261        ///
0262        /// Based on KahanIncrement from:
0263        /// Y. Tian, S. Tatikonda and B. Reinwald, "Scalable and Numerically Stable Descriptive Statistics in SystemML," 2012 IEEE 28th International Conference on Data Engineering, 2012, pp. 1351-1359, doi: 10.1109/ICDE.2012.12.
0264        /// Note that while Tian et al. add the carry in the first step, we subtract
0265        /// the carry, in accordance with the Add(Indexed) implementation(s) above.
0266        /// This is purely an implementation choice that has no impact on performance.
0267        ///
0268        /// \note Take care when using += (and -=) to add other KahanSums into a zero-initialized
0269        ///       KahanSum. The operator behaves correctly in this case, but the result may be slightly
0270        ///       off if you expect 0 + x to yield exactly x (where 0 is the zero-initialized KahanSum
0271        ///       and x another KahanSum). In particular, x's carry term may get lost. This doesn't
0272        ///       just happen with zero-initialized KahanSums; see the SubtractWithABitTooSmallCarry
0273        ///       test case in the testKahan unittest for other examples. This behavior is internally
0274        ///       consistent: the carry also gets lost if you switch the operands and it also happens with
0275        ///       other KahanSum operators.
0276        template<typename U, unsigned int M>
0277        KahanSum<T, N>& operator+=(const KahanSum<U, M>& other) {
0278          U corrected_arg_sum = other.Sum() - (fCarry[0] + other.Carry());
0279          U sum = fSum[0] + corrected_arg_sum;
0280          U correction = (sum - fSum[0]) - corrected_arg_sum;
0281          fSum[0] = sum;
0282          fCarry[0] = correction;
0283          return *this;
0284        }
0285 
0286        /// Subtract other KahanSum. Does not vectorise.
0287        ///
0288        /// Based on KahanIncrement from: Tian et al., 2012 (see operator+= documentation).
0289        template<typename U, unsigned int M>
0290        KahanSum<T, N>& operator-=(KahanSum<U, M> const& other) {
0291          U corrected_arg_sum = -other.Sum() - (fCarry[0] - other.Carry());
0292          U sum = fSum[0] + corrected_arg_sum;
0293          U correction = (sum - fSum[0]) - corrected_arg_sum;
0294          fSum[0] = sum;
0295          fCarry[0] = correction;
0296          return *this;
0297        }
0298 
0299        KahanSum<T, N> operator-()
0300        {
0301           return {-this->fSum[0], -this->fCarry[0]};
0302        }
0303 
0304        template<typename U, unsigned int M>
0305        bool operator ==(KahanSum<U, M> const& other) const {
0306           return (this->Sum() == other.Sum()) && (this->Carry() == other.Carry());
0307        }
0308 
0309        template<typename U, unsigned int M>
0310        bool operator !=(KahanSum<U, M> const& other) const {
0311           return !(*this == other);
0312        }
0313 
0314      private:
0315        T fSum[N];
0316        T fCarry[N];
0317    };
0318 
0319    /// Add two non-vectorized KahanSums.
0320    template<typename T, unsigned int N, typename U, unsigned int M>
0321    KahanSum<T, N> operator+(const KahanSum<T, N>& left, const KahanSum<U, M>& right) {
0322       KahanSum<T, N> sum(left);
0323       sum += right;
0324       return sum;
0325    }
0326 
0327    /// Subtract two non-vectorized KahanSums.
0328    template<typename T, unsigned int N, typename U, unsigned int M>
0329    KahanSum<T, N> operator-(const KahanSum<T, N>& left, const KahanSum<U, M>& right) {
0330       KahanSum<T, N> sum(left);
0331       sum -= right;
0332       return sum;
0333    }
0334 
0335    } // end namespace Math
0336 
0337 } // end namespace ROOT
0338 
0339 
0340 #endif /* ROOT_Math_Util */