Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
DenseVector/OpenMP/Morpheus_Scan_Impl.hpp
1
24#ifndef MORPHEUS_DENSEVECTOR_OPENMP_SCAN_IMPL_HPP
25#define MORPHEUS_DENSEVECTOR_OPENMP_SCAN_IMPL_HPP
26
27#include <Morpheus_Macros.hpp>
28#if defined(MORPHEUS_ENABLE_OPENMP)
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
38template <typename ExecSpace, typename Vector>
39void inclusive_scan(const Vector& in, Vector& out,
40 typename Vector::index_type size,
41 typename Vector::index_type start,
42 typename std::enable_if_t<
43 Morpheus::is_dense_vector_format_container_v<Vector> &&
44 Morpheus::has_custom_backend_v<ExecSpace> &&
45 Morpheus::has_openmp_space_v<ExecSpace> &&
46 Morpheus::has_access_v<ExecSpace, Vector>>* = nullptr) {
47 using index_type = typename Vector::index_type;
48 using value_type = typename Vector::value_type;
49
50#if _OPENMP >= 201511
51 value_type initial = 0;
52// TODO: Scan semantics require OpenMP5
53#pragma omp simd reduction(inscan, + : initial)
54 for (index_type i = start; i < size; i++) {
55 initial += in[i];
56#pragma omp scan inclusive(initial)
57 out[i] = initial;
58 }
59#else
60 static_assert("Requires OpenMP4.5 and above");
61#endif
62}
63
64template <typename ExecSpace, typename Vector>
65void exclusive_scan(const Vector& in, Vector& out,
66 typename Vector::index_type size,
67 typename Vector::index_type start,
68 typename std::enable_if_t<
69 Morpheus::is_dense_vector_format_container_v<Vector> &&
70 Morpheus::has_custom_backend_v<ExecSpace> &&
71 Morpheus::has_openmp_space_v<ExecSpace> &&
72 Morpheus::has_access_v<ExecSpace, Vector>>* = nullptr) {
73 using index_type = typename Vector::index_type;
74 using value_type = typename Vector::value_type;
75
76#if _OPENMP >= 201511
77 if (size > 0) {
78 value_type initial = 0;
79 // TODO: Scan semantics require OpenMP5
80#pragma omp simd reduction(inscan, + : initial)
81 for (index_type i = start; i < size - 1; i++) {
82 out[i] = initial;
83#pragma omp scan exclusive(initial)
84 initial += in[i];
85 }
86 out[size - 1] = initial;
87 }
88#else
89 static_assert("Requires OpenMP4.5 and above");
90#endif
91}
92
93} // namespace Impl
94} // namespace Morpheus
95
96#endif // MORPHEUS_ENABLE_OPENMP
97#endif // MORPHEUS_DENSEVECTOR_OPENMP_SCAN_IMPL_HPP
Generic Morpheus interfaces.
Definition: dummy.cpp:24