-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathscan.cpp
More file actions
283 lines (252 loc) · 9.2 KB
/
scan.cpp
File metadata and controls
283 lines (252 loc) · 9.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <backend.hpp>
#include <common/err_common.hpp>
#include <handle.hpp>
#include <optypes.hpp>
#include <scan.hpp>
#include <scan_by_key.hpp>
#include <af/algorithm.h>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <complex>
using detail::cdouble;
using detail::cfloat;
using detail::intl;
using detail::schar;
using detail::uchar;
using detail::uint;
using detail::uintl;
using detail::ushort;
template<af_op_t op, typename Ti, typename To>
static inline af_array scan(const af_array in, const int dim,
bool inclusive_scan = true) {
return getHandle(scan<op, Ti, To>(getArray<Ti>(in), dim, inclusive_scan));
}
template<af_op_t op, typename Ti, typename To>
static inline af_array scan_key(const af_array key, const af_array in,
const int dim, bool inclusive_scan = true) {
const ArrayInfo& key_info = getInfo(key);
af_dtype type = key_info.getType();
af_array out;
switch (type) {
case s32:
out = getHandle(scan<op, Ti, int, To>(
getArray<int>(key), castArray<Ti>(in), dim, inclusive_scan));
break;
case u32:
out = getHandle(scan<op, Ti, uint, To>(
getArray<uint>(key), castArray<Ti>(in), dim, inclusive_scan));
break;
case s64:
out = getHandle(scan<op, Ti, intl, To>(
getArray<intl>(key), castArray<Ti>(in), dim, inclusive_scan));
break;
case u64:
out = getHandle(scan<op, Ti, uintl, To>(
getArray<uintl>(key), castArray<Ti>(in), dim, inclusive_scan));
break;
default: TYPE_ERROR(1, type);
}
return out;
}
template<typename Ti, typename To>
static inline af_array scan_op(const af_array key, const af_array in,
const int dim, af_binary_op op,
bool inclusive_scan = true) {
af_array out;
switch (op) {
case AF_BINARY_ADD:
out = scan_key<af_add_t, Ti, To>(key, in, dim, inclusive_scan);
break;
case AF_BINARY_MUL:
out = scan_key<af_mul_t, Ti, To>(key, in, dim, inclusive_scan);
break;
case AF_BINARY_MIN:
out = scan_key<af_min_t, Ti, To>(key, in, dim, inclusive_scan);
break;
case AF_BINARY_MAX:
out = scan_key<af_max_t, Ti, To>(key, in, dim, inclusive_scan);
break;
default:
AF_ERROR("Incorrect binary operation enum for argument number 3",
AF_ERR_ARG);
break;
}
return out;
}
template<typename Ti, typename To>
static inline af_array scan_op(const af_array in, const int dim,
af_binary_op op, bool inclusive_scan) {
af_array out;
switch (op) {
case AF_BINARY_ADD:
out = scan<af_add_t, Ti, To>(in, dim, inclusive_scan);
break;
case AF_BINARY_MUL:
out = scan<af_mul_t, Ti, To>(in, dim, inclusive_scan);
break;
case AF_BINARY_MIN:
out = scan<af_min_t, Ti, To>(in, dim, inclusive_scan);
break;
case AF_BINARY_MAX:
out = scan<af_max_t, Ti, To>(in, dim, inclusive_scan);
break;
default:
AF_ERROR("Incorrect binary operation enum for argument number 2",
AF_ERR_ARG);
break;
}
return out;
}
af_err af_accum(af_array* out, const af_array in, const int dim) {
try {
ARG_ASSERT(2, dim >= 0);
ARG_ASSERT(2, dim < 4);
const ArrayInfo& in_info = getInfo(in);
if (dim >= static_cast<int>(in_info.ndims())) {
*out = retain(in);
return AF_SUCCESS;
}
af_dtype type = in_info.getType();
af_array res;
switch (type) {
case f32: res = scan<af_add_t, float, float>(in, dim); break;
case f64: res = scan<af_add_t, double, double>(in, dim); break;
case c32: res = scan<af_add_t, cfloat, cfloat>(in, dim); break;
case c64: res = scan<af_add_t, cdouble, cdouble>(in, dim); break;
case u32: res = scan<af_add_t, uint, uint>(in, dim); break;
case s32: res = scan<af_add_t, int, int>(in, dim); break;
case u64: res = scan<af_add_t, uintl, uintl>(in, dim); break;
case s64: res = scan<af_add_t, intl, intl>(in, dim); break;
case u16: res = scan<af_add_t, ushort, uint>(in, dim); break;
case s16: res = scan<af_add_t, short, int>(in, dim); break;
case u8: res = scan<af_add_t, uchar, uint>(in, dim); break;
case s8: res = scan<af_add_t, schar, int>(in, dim); break;
// Make sure you are adding only "1" for every non zero value, even
// if op == af_add_t
case b8: res = scan<af_notzero_t, char, uint>(in, dim); break;
default: TYPE_ERROR(1, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_scan(af_array* out, const af_array in, const int dim, af_binary_op op,
bool inclusive_scan) {
try {
ARG_ASSERT(2, dim >= 0);
ARG_ASSERT(2, dim < 4);
const ArrayInfo& in_info = getInfo(in);
if (dim >= static_cast<int>(in_info.ndims())) {
*out = retain(in);
return AF_SUCCESS;
}
af_dtype type = in_info.getType();
af_array res;
switch (type) {
case f32:
res = scan_op<float, float>(in, dim, op, inclusive_scan);
break;
case f64:
res = scan_op<double, double>(in, dim, op, inclusive_scan);
break;
case c32:
res = scan_op<cfloat, cfloat>(in, dim, op, inclusive_scan);
break;
case c64:
res = scan_op<cdouble, cdouble>(in, dim, op, inclusive_scan);
break;
case u32:
res = scan_op<uint, uint>(in, dim, op, inclusive_scan);
break;
case s32:
res = scan_op<int, int>(in, dim, op, inclusive_scan);
break;
case u64:
res = scan_op<uintl, uintl>(in, dim, op, inclusive_scan);
break;
case s64:
res = scan_op<intl, intl>(in, dim, op, inclusive_scan);
break;
case u16:
res = scan_op<ushort, uint>(in, dim, op, inclusive_scan);
break;
case s16:
res = scan_op<short, int>(in, dim, op, inclusive_scan);
break;
case u8:
res = scan_op<uchar, uint>(in, dim, op, inclusive_scan);
break;
case s8:
res = scan_op<schar, int>(in, dim, op, inclusive_scan);
break;
case b8:
res = scan_op<char, uint>(in, dim, op, inclusive_scan);
break;
default: TYPE_ERROR(1, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_scan_by_key(af_array* out, const af_array key, const af_array in,
const int dim, af_binary_op op, bool inclusive_scan) {
try {
ARG_ASSERT(2, dim >= 0);
ARG_ASSERT(2, dim < 4);
const ArrayInfo& in_info = getInfo(in);
const ArrayInfo& key_info = getInfo(key);
if (dim >= static_cast<int>(in_info.ndims())) {
*out = retain(in);
return AF_SUCCESS;
}
ARG_ASSERT(2, in_info.dims() == key_info.dims());
af_dtype type = in_info.getType();
af_array res;
switch (type) {
case f32:
res = scan_op<float, float>(key, in, dim, op, inclusive_scan);
break;
case f64:
res = scan_op<double, double>(key, in, dim, op, inclusive_scan);
break;
case c32:
res = scan_op<cfloat, cfloat>(key, in, dim, op, inclusive_scan);
break;
case c64:
res =
scan_op<cdouble, cdouble>(key, in, dim, op, inclusive_scan);
break;
case s16:
case s32:
case s8:
res = scan_op<int, int>(key, in, dim, op, inclusive_scan);
break;
case u64:
res = scan_op<uintl, uintl>(key, in, dim, op, inclusive_scan);
break;
case s64:
res = scan_op<intl, intl>(key, in, dim, op, inclusive_scan);
break;
case u16:
case u32:
case u8:
case b8:
res = scan_op<uint, uint>(key, in, dim, op, inclusive_scan);
break;
default: TYPE_ERROR(1, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}