-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathblas1_linalg_add.hpp
More file actions
269 lines (250 loc) · 9.88 KB
/
Copy pathblas1_linalg_add.hpp
File metadata and controls
269 lines (250 loc) · 9.88 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
//@HEADER
// ************************************************************************
//
// Kokkos v. 2.0
// Copyright (2019) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software. //
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Christian R. Trott (crtrott@sandia.gov)
//
// ************************************************************************
//@HEADER
*/
#ifndef LINALG_INCLUDE_EXPERIMENTAL___P1673_BITS_BLAS1_LINALG_ADD_HPP_
#define LINALG_INCLUDE_EXPERIMENTAL___P1673_BITS_BLAS1_LINALG_ADD_HPP_
namespace std {
namespace experimental {
inline namespace __p1673_version_0 {
namespace linalg {
namespace {
template<class ElementType_x,
class SizeType_x,
::std::size_t ext_x,
class Layout_x,
class Accessor_x,
class ElementType_y,
class SizeType_y,
::std::size_t ext_y,
class Layout_y,
class Accessor_y,
class ElementType_z,
class SizeType_z,
::std::size_t ext_z,
class Layout_z,
class Accessor_z>
void add_rank_1(
std::experimental::mdspan<ElementType_x, std::experimental::extents<SizeType_x, ext_x>, Layout_x, Accessor_x> x,
std::experimental::mdspan<ElementType_y, std::experimental::extents<SizeType_y, ext_y>, Layout_y, Accessor_y> y,
std::experimental::mdspan<ElementType_z, std::experimental::extents<SizeType_z, ext_z>, Layout_z, Accessor_z> z)
{
static_assert(x.static_extent(0) == dynamic_extent ||
z.static_extent(0) == dynamic_extent ||
x.static_extent(0) == z.static_extent(0));
static_assert(y.static_extent(0) == dynamic_extent ||
z.static_extent(0) == dynamic_extent ||
y.static_extent(0) == z.static_extent(0));
static_assert(x.static_extent(0) == dynamic_extent ||
y.static_extent(0) == dynamic_extent ||
x.static_extent(0) == y.static_extent(0));
using size_type = std::common_type_t<SizeType_x, SizeType_y, SizeType_z>;
for (size_type i = 0; i < z.extent(0); ++i) {
z(i) = x(i) + y(i);
}
}
template<class ElementType_x,
class SizeType_x,
::std::size_t numRows_x,
::std::size_t numCols_x,
class Layout_x,
class Accessor_x,
class ElementType_y,
class SizeType_y,
::std::size_t numRows_y,
::std::size_t numCols_y,
class Layout_y,
class Accessor_y,
class ElementType_z,
class SizeType_z,
::std::size_t numRows_z,
::std::size_t numCols_z,
class Layout_z,
class Accessor_z>
void add_rank_2(
std::experimental::mdspan<ElementType_x, std::experimental::extents<SizeType_x, numRows_x, numCols_x>, Layout_x, Accessor_x> x,
std::experimental::mdspan<ElementType_y, std::experimental::extents<SizeType_y, numRows_y, numCols_y>, Layout_y, Accessor_y> y,
std::experimental::mdspan<ElementType_z, std::experimental::extents<SizeType_z, numRows_z, numCols_z>, Layout_z, Accessor_z> z)
{
static_assert(x.static_extent(0) == dynamic_extent ||
z.static_extent(0) == dynamic_extent ||
x.static_extent(0) == z.static_extent(0));
static_assert(y.static_extent(0) == dynamic_extent ||
z.static_extent(0) == dynamic_extent ||
y.static_extent(0) == z.static_extent(0));
static_assert(x.static_extent(0) == dynamic_extent ||
y.static_extent(0) == dynamic_extent ||
x.static_extent(0) == y.static_extent(0));
static_assert(x.static_extent(1) == dynamic_extent ||
z.static_extent(1) == dynamic_extent ||
x.static_extent(1) == z.static_extent(1));
static_assert(y.static_extent(1) == dynamic_extent ||
z.static_extent(1) == dynamic_extent ||
y.static_extent(1) == z.static_extent(1));
static_assert(x.static_extent(1) == dynamic_extent ||
y.static_extent(1) == dynamic_extent ||
x.static_extent(1) == y.static_extent(1));
using size_type = std::common_type_t<SizeType_x, SizeType_y, SizeType_z>;
for (size_type j = 0; j < x.extent(1); ++j) {
for (size_type i = 0; i < x.extent(0); ++i) {
z(i,j) = x(i,j) + y(i,j);
}
}
}
template <class Exec, class x_t, class y_t, class z_t, class = void>
struct is_custom_add_avail : std::false_type {};
template <class Exec, class x_t, class y_t, class z_t>
struct is_custom_add_avail<
Exec, x_t, y_t, z_t,
std::enable_if_t<
std::is_void_v<
decltype(add
(std::declval<Exec>(),
std::declval<x_t>(),
std::declval<y_t>(),
std::declval<z_t>()
)
)
>
&& !linalg::impl::is_inline_exec_v<Exec>
>
>
: std::true_type{};
} // end anonymous namespace
MDSPAN_TEMPLATE_REQUIRES(
class ElementType_x,
class SizeType_x,
::std::size_t ... ext_x,
class Layout_x,
class Accessor_x,
class ElementType_y,
class SizeType_y,
::std::size_t ... ext_y,
class Layout_y,
class Accessor_y,
class ElementType_z,
class SizeType_z,
::std::size_t ... ext_z,
class Layout_z,
class Accessor_z,
/* requires */ (sizeof...(ext_x) == sizeof...(ext_y) && sizeof...(ext_x) == sizeof...(ext_z))
)
void add(
std::experimental::linalg::impl::inline_exec_t&& /* exec */,
std::experimental::mdspan<ElementType_x, std::experimental::extents<SizeType_x, ext_x ...>, Layout_x, Accessor_x> x,
std::experimental::mdspan<ElementType_y, std::experimental::extents<SizeType_y, ext_y ...>, Layout_y, Accessor_y> y,
std::experimental::mdspan<ElementType_z, std::experimental::extents<SizeType_z, ext_z ...>, Layout_z, Accessor_z> z)
{
// this static assert is only here because for
// the default case we support rank-1 and rank2.
static_assert(z.rank() <= 2);
if constexpr (z.rank() == 1) {
add_rank_1 (x, y, z);
}
else if constexpr (z.rank() == 2) {
add_rank_2 (x, y, z);
}
}
MDSPAN_TEMPLATE_REQUIRES(
class ExecutionPolicy,
class ElementType_x,
class SizeType_x,
::std::size_t ... ext_x,
class Layout_x,
class Accessor_x,
class ElementType_y,
class SizeType_y,
::std::size_t ... ext_y,
class Layout_y,
class Accessor_y,
class ElementType_z,
class SizeType_z,
::std::size_t ... ext_z,
class Layout_z,
class Accessor_z,
/* requires */ (sizeof...(ext_x) == sizeof...(ext_y) && sizeof...(ext_x) == sizeof...(ext_z))
)
void add(
ExecutionPolicy&& exec,
std::experimental::mdspan<ElementType_x, std::experimental::extents<SizeType_x, ext_x ...>, Layout_x, Accessor_x> x,
std::experimental::mdspan<ElementType_y, std::experimental::extents<SizeType_y, ext_y ...>, Layout_y, Accessor_y> y,
std::experimental::mdspan<ElementType_z, std::experimental::extents<SizeType_z, ext_z ...>, Layout_z, Accessor_z> z)
{
constexpr bool use_custom = is_custom_add_avail<
decltype(detail::map_execpolicy_with_check(exec)), decltype(x), decltype(y), decltype(z)
>::value;
if constexpr (use_custom) {
// for the customization point, it is up to impl to check requirements
add(detail::map_execpolicy_with_check(exec), x, y, z);
}
else {
add(std::experimental::linalg::impl::inline_exec_t(), x, y, z);
}
}
MDSPAN_TEMPLATE_REQUIRES(
class ElementType_x,
class SizeType_x,
::std::size_t ... ext_x,
class Layout_x,
class Accessor_x,
class ElementType_y,
class SizeType_y,
::std::size_t ... ext_y,
class Layout_y,
class Accessor_y,
class ElementType_z,
class SizeType_z,
::std::size_t ... ext_z,
class Layout_z,
class Accessor_z,
/* requires */ (sizeof...(ext_x) == sizeof...(ext_y) && sizeof...(ext_x) == sizeof...(ext_z))
)
void add(
std::experimental::mdspan<ElementType_x, std::experimental::extents<SizeType_x, ext_x ...>, Layout_x, Accessor_x> x,
std::experimental::mdspan<ElementType_y, std::experimental::extents<SizeType_y, ext_y ...>, Layout_y, Accessor_y> y,
std::experimental::mdspan<ElementType_z, std::experimental::extents<SizeType_z, ext_z ...>, Layout_z, Accessor_z> z)
{
add(std::experimental::linalg::impl::default_exec_t(), x, y, z);
}
} // end namespace linalg
} // end inline namespace __p1673_version_0
} // end namespace experimental
} // end namespace std
#endif //LINALG_INCLUDE_EXPERIMENTAL___P1673_BITS_BLAS1_LINALG_ADD_HPP_