Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
DenseVector/Serial/Morpheus_Reduction_Impl.hpp
1
24#ifndef MORPHEUS_DENSEVECTOR_SERIAL_REDUCTION_IMPL_HPP
25#define MORPHEUS_DENSEVECTOR_SERIAL_REDUCTION_IMPL_HPP
26
27#include <Morpheus_Macros.hpp>
28#if defined(MORPHEUS_ENABLE_SERIAL)
29
30#include <Morpheus_SpaceTraits.hpp>
31#include <Morpheus_FormatTraits.hpp>
32#include <Morpheus_FormatTags.hpp>
33#include <Morpheus_Spaces.hpp>
34
35namespace Morpheus {
36namespace Impl {
37
38// Compute sum reduction using Kahan summation for an accurate sum of large
39// arrays. http://en.wikipedia.org/wiki/Kahan_summation_algorithm
40template <typename ExecSpace, typename Vector>
41typename Vector::value_type reduce(
42 const Vector& in, typename Vector::size_type size,
43 typename std::enable_if_t<
44 Morpheus::is_dense_vector_format_container_v<Vector> &&
45 Morpheus::has_custom_backend_v<ExecSpace> &&
46 Morpheus::has_serial_execution_space_v<ExecSpace> &&
47 Morpheus::has_access_v<ExecSpace, Vector>>* = nullptr) {
48 using value_type = typename Vector::value_type;
49 using size_type = typename Vector::size_type;
50 value_type sum = in[0];
51 value_type c = (value_type)0.0;
52
53 for (size_type i = 1; i < size; i++) {
54 value_type y = in[i] - c;
55 value_type t = sum + y;
56 c = (t - sum) - y;
57 sum = t;
58 }
59
60 return sum;
61}
62
63} // namespace Impl
64} // namespace Morpheus
65
66#endif // MORPHEUS_ENABLE_SERIAL
67#endif // MORPHEUS_DENSEVECTOR_SERIAL_REDUCTION_IMPL_HPP
Generic Morpheus interfaces.
Definition: dummy.cpp:24