Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
DenseVector/Kokkos/Morpheus_Dot_Impl.hpp
1
24#ifndef MORPHEUS_DENSEVECTOR_KOKKOS_DOT_IMPL_HPP
25#define MORPHEUS_DENSEVECTOR_KOKKOS_DOT_IMPL_HPP
26
27#include <Morpheus_SpaceTraits.hpp>
28#include <Morpheus_FormatTraits.hpp>
29#include <Morpheus_FormatTags.hpp>
30#include <Morpheus_Spaces.hpp>
31
32namespace Morpheus {
33namespace Impl {
34
35template <typename ExecSpace, typename Vector1, typename Vector2>
36inline typename Vector2::value_type dot(
37 typename Vector1::size_type n, const Vector1& x, const Vector2& y,
38 typename std::enable_if_t<
39 Morpheus::is_dense_vector_format_container_v<Vector1> &&
40 Morpheus::is_dense_vector_format_container_v<Vector2> &&
41 Morpheus::has_generic_backend_v<ExecSpace> &&
42 Morpheus::has_access_v<ExecSpace, Vector1, Vector2>>* = nullptr) {
43 using execution_space = typename ExecSpace::execution_space;
44 using IndexType = Kokkos::IndexType<typename Vector1::size_type>;
45 using range_policy = Kokkos::RangePolicy<IndexType, execution_space>;
46 using ValueArray = typename Vector1::value_array_type;
47 using value_type = typename Vector2::non_const_value_type;
48 using size_type = typename Vector1::size_type;
49
50 const ValueArray x_view = x.const_view(), y_view = y.const_view();
51 range_policy policy(0, n);
52
53 value_type result = value_type(0);
54
55 if (y.data() == x.data()) {
56 Kokkos::parallel_reduce(
57 "dot_same_data", policy,
58 KOKKOS_LAMBDA(const size_type& i, value_type& lsum) {
59 lsum += x_view[i] * x_view[i];
60 },
61 result);
62 } else {
63 Kokkos::parallel_reduce(
64 "dot", policy,
65 KOKKOS_LAMBDA(const size_type& i, value_type& lsum) {
66 lsum += x_view[i] * y_view[i];
67 },
68 result);
69 }
70
71 return result;
72}
73
74} // namespace Impl
75} // namespace Morpheus
76
77#endif // MORPHEUS_DENSEVECTOR_KOKKOS_DOT_IMPL_HPP
Generic Morpheus interfaces.
Definition: dummy.cpp:24