-
Notifications
You must be signed in to change notification settings - Fork 34
/
ArborX_DetailsKokkosExtStdAlgorithms.hpp
154 lines (138 loc) · 6.58 KB
/
ArborX_DetailsKokkosExtStdAlgorithms.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/****************************************************************************
* Copyright (c) 2017-2022 by the ArborX authors *
* All rights reserved. *
* *
* This file is part of the ArborX library. ArborX is *
* distributed under a BSD 3-clause license. For the licensing terms see *
* the LICENSE file in the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
#ifndef ARBORX_DETAILS_KOKKOS_EXT_STD_ALGORITHMS_HPP
#define ARBORX_DETAILS_KOKKOS_EXT_STD_ALGORITHMS_HPP
#include <ArborX_DetailsKokkosExtAccessibilityTraits.hpp>
#include <ArborX_Exception.hpp>
#include <Kokkos_Core.hpp>
namespace ArborX::Details::KokkosExt
{
template <typename ExecutionSpace, typename SrcView, typename DstView,
typename InitValueType>
void exclusive_scan(ExecutionSpace const &space, SrcView const &src,
DstView const &dst, InitValueType init)
{
static_assert(Kokkos::is_execution_space<ExecutionSpace>::value);
static_assert(Kokkos::is_view<SrcView>::value);
static_assert(Kokkos::is_view<DstView>::value);
static_assert(
is_accessible_from<typename SrcView::memory_space, ExecutionSpace>::value,
"Source view must be accessible from the execution space");
static_assert(
is_accessible_from<typename DstView::memory_space, ExecutionSpace>::value,
"Destination view must be accessible from the execution "
"space");
static_assert(std::is_same_v<typename SrcView::value_type,
typename DstView::non_const_value_type>,
"exclusive_scan requires non-const destination type");
static_assert(unsigned(DstView::rank) == unsigned(SrcView::rank) &&
unsigned(DstView::rank) == unsigned(1),
"exclusive_scan requires Views of rank 1");
using ValueType = typename DstView::value_type;
auto const n = src.extent(0);
ARBORX_ASSERT(n == dst.extent(0));
Kokkos::parallel_scan(
"ArborX::Algorithms::exclusive_scan",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n),
KOKKOS_LAMBDA(int i, ValueType &update, bool final_pass) {
auto const tmp = src(i);
if (final_pass)
dst(i) = update + init;
update += tmp;
});
}
template <typename ExecutionSpace, typename ViewType>
typename ViewType::non_const_value_type
reduce(ExecutionSpace const &space, ViewType const &v,
typename ViewType::non_const_value_type init)
{
static_assert(Kokkos::is_execution_space<ExecutionSpace>::value);
static_assert(Kokkos::is_view<ViewType>::value);
static_assert(is_accessible_from<typename ViewType::memory_space,
ExecutionSpace>::value,
"Source view must be accessible from the execution space");
static_assert(ViewType::rank == 1, "accumulate requires a View of rank 1");
// NOTE: Passing the argument init directly to the parallel_reduce() while
// using a lambda does not yield the expected result because Kokkos will
// supply a default init method that sets the reduction result to zero.
// Rather than going through the hassle of defining a custom functor for
// the reduction, introduce here a temporary variable and add it to init
// before returning.
typename ViewType::non_const_value_type tmp;
Kokkos::parallel_reduce(
"ArborX::Algorithms::accumulate",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, v.extent(0)),
KOKKOS_LAMBDA(int i, typename ViewType::non_const_value_type &update) {
update += v(i);
},
tmp);
init += tmp;
return init;
}
template <typename ExecutionSpace, typename SrcView, typename DstView>
void adjacent_difference(ExecutionSpace const &space, SrcView const &src,
DstView const &dst)
{
static_assert(Kokkos::is_execution_space<ExecutionSpace>::value);
static_assert(Kokkos::is_view<SrcView>::value);
static_assert(Kokkos::is_view<DstView>::value);
static_assert(
is_accessible_from<typename SrcView::memory_space, ExecutionSpace>::value,
"Source view must be accessible from the execution space");
static_assert(
is_accessible_from<typename DstView::memory_space, ExecutionSpace>::value,
"Destination view must be accessible from the execution space");
static_assert(SrcView::rank == 1 && DstView::rank == 1,
"adjacent_difference operates on rank-1 views");
static_assert(
std::is_same_v<typename DstView::value_type,
typename DstView::non_const_value_type>,
"adjacent_difference requires non-const destination value type");
static_assert(std::is_same_v<typename SrcView::non_const_value_type,
typename DstView::value_type>,
"adjacent_difference requires same value type for source and "
"destination");
auto const n = src.extent(0);
ARBORX_ASSERT(n == dst.extent(0));
ARBORX_ASSERT(src != dst);
Kokkos::parallel_for(
"ArborX::Algorithms::adjacent_difference",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n), KOKKOS_LAMBDA(int i) {
if (i > 0)
dst(i) = src(i) - src(i - 1);
else
dst(i) = src(i);
});
}
template <typename ExecutionSpace, typename ViewType>
void iota(ExecutionSpace const &space, ViewType const &v,
typename ViewType::value_type value = 0)
{
static_assert(Kokkos::is_execution_space<ExecutionSpace>::value);
static_assert(Kokkos::is_view<ViewType>::value);
static_assert(is_accessible_from<typename ViewType::memory_space,
ExecutionSpace>::value,
"View must be accessible from the execution space");
static_assert(unsigned(ViewType::rank) == unsigned(1),
"iota requires a View of rank 1");
using ValueType = typename ViewType::value_type;
static_assert(std::is_arithmetic_v<ValueType>,
"iota requires a View with an arithmetic value type");
static_assert(
std::is_same_v<ValueType, typename ViewType::non_const_value_type>,
"iota requires a View with non-const value type");
Kokkos::parallel_for(
"ArborX::Algorithms::iota",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, v.extent(0)),
KOKKOS_LAMBDA(int i) { v(i) = value + (ValueType)i; });
}
} // namespace ArborX::Details::KokkosExt
#endif