forked from hunter-packages/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.cpp
More file actions
234 lines (197 loc) · 8.65 KB
/
index.cpp
File metadata and controls
234 lines (197 loc) · 8.65 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
/*******************************************************
* 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 <vector>
#include <cassert>
#include <af/array.h>
#include <af/index.h>
#include <af/arith.h>
#include <af/data.h>
#include <ArrayInfo.hpp>
#include <err_common.hpp>
#include <handle.hpp>
#include <backend.hpp>
#include <Array.hpp>
#include <lookup.hpp>
#include <index.hpp>
using namespace detail;
using std::vector;
using std::swap;
template<typename T>
static void indexArray(af_array &dest, const af_array &src, const unsigned ndims, const af_seq *index)
{
const Array<T> &parent = getArray<T>(src);
vector<af_seq> index_(index, index+ndims);
Array<T> dst = createSubArray(parent, index_);
dest = getHandle(dst);
}
af_err af_index(af_array *result, const af_array in, const unsigned ndims, const af_seq* index)
{
af_array out;
try {
ArrayInfo iInfo = getInfo(in);
if (ndims == 1 && ndims != (dim_t)iInfo.ndims()) {
af_array tmp_in;
AF_CHECK(af_flat(&tmp_in, in));
AF_CHECK(af_index(result, tmp_in, ndims, index));
AF_CHECK(af_release_array(tmp_in));
return AF_SUCCESS;
}
af_dtype in_type = iInfo.getType();
switch(in_type) {
case f32: indexArray<float> (out, in, ndims, index); break;
case c32: indexArray<cfloat> (out, in, ndims, index); break;
case f64: indexArray<double> (out, in, ndims, index); break;
case c64: indexArray<cdouble> (out, in, ndims, index); break;
case b8: indexArray<char> (out, in, ndims, index); break;
case s32: indexArray<int> (out, in, ndims, index); break;
case u32: indexArray<unsigned>(out, in, ndims, index); break;
case s16: indexArray<short> (out, in, ndims, index); break;
case u16: indexArray<ushort> (out, in, ndims, index); break;
case s64: indexArray<intl> (out, in, ndims, index); break;
case u64: indexArray<uintl> (out, in, ndims, index); break;
case u8: indexArray<uchar> (out, in, ndims, index); break;
default: TYPE_ERROR(1, in_type);
}
}
CATCHALL
swap(*result, out);
return AF_SUCCESS;
}
template<typename idx_t>
static af_array lookup(const af_array &in, const af_array &idx, const unsigned dim)
{
ArrayInfo inInfo = getInfo(in);
af_dtype inType = inInfo.getType();
switch(inType) {
case f32: return getHandle(lookup<float , idx_t > (getArray<float >(in), getArray<idx_t>(idx), dim));
case c32: return getHandle(lookup<cfloat , idx_t > (getArray<cfloat >(in), getArray<idx_t>(idx), dim));
case f64: return getHandle(lookup<double , idx_t > (getArray<double >(in), getArray<idx_t>(idx), dim));
case c64: return getHandle(lookup<cdouble , idx_t > (getArray<cdouble >(in), getArray<idx_t>(idx), dim));
case s32: return getHandle(lookup<int , idx_t > (getArray<int >(in), getArray<idx_t>(idx), dim));
case u32: return getHandle(lookup<unsigned, idx_t > (getArray<unsigned>(in), getArray<idx_t>(idx), dim));
case s64: return getHandle(lookup<intl , idx_t > (getArray<intl >(in), getArray<idx_t>(idx), dim));
case u64: return getHandle(lookup<uintl , idx_t > (getArray<uintl >(in), getArray<idx_t>(idx), dim));
case s16: return getHandle(lookup<short , idx_t > (getArray<short >(in), getArray<idx_t>(idx), dim));
case u16: return getHandle(lookup<ushort , idx_t > (getArray<ushort >(in), getArray<idx_t>(idx), dim));
case u8: return getHandle(lookup<uchar , idx_t > (getArray<uchar >(in), getArray<idx_t>(idx), dim));
case b8: return getHandle(lookup<char , idx_t > (getArray<char >(in), getArray<idx_t>(idx), dim));
default : TYPE_ERROR(1, inType);
}
}
af_err af_lookup(af_array *out, const af_array in, const af_array indices, const unsigned dim)
{
af_array output = 0;
try {
ARG_ASSERT(3, (dim>=0 && dim<=3));
ArrayInfo idxInfo= getInfo(indices);
ARG_ASSERT(2, idxInfo.isVector() || idxInfo.isScalar());
af_dtype idxType = idxInfo.getType();
ARG_ASSERT(2, (idxType!=c32));
ARG_ASSERT(2, (idxType!=c64));
ARG_ASSERT(2, (idxType!=b8));
switch(idxType) {
case f32: output = lookup<float >(in, indices, dim); break;
case f64: output = lookup<double >(in, indices, dim); break;
case s32: output = lookup<int >(in, indices, dim); break;
case u32: output = lookup<unsigned>(in, indices, dim); break;
case s16: output = lookup<short >(in, indices, dim); break;
case u16: output = lookup<ushort >(in, indices, dim); break;
case s64: output = lookup<intl >(in, indices, dim); break;
case u64: output = lookup<uintl >(in, indices, dim); break;
case u8: output = lookup<uchar >(in, indices, dim); break;
default : TYPE_ERROR(1, idxType);
}
}
CATCHALL;
std::swap(*out, output);
return AF_SUCCESS;
}
// idxrs parameter to the below static function
// expects 4 values which is handled appropriately
// by the C-API af_index_gen
template<typename T>
static inline
af_array genIndex(const af_array& in, const af_index_t idxrs[])
{
return getHandle<T>(index<T>(getArray<T>(in), idxrs));
}
af_err af_index_gen(af_array *out, const af_array in, const dim_t ndims, const af_index_t* indexs)
{
af_array output = 0;
// spanner is sequence index used for indexing along the
// dimensions after ndims
af_index_t spanner;
spanner.idx.seq = af_span;
spanner.isSeq = true;
try {
ARG_ASSERT(2, (ndims>0));
ARG_ASSERT(3, (indexs!=NULL));
ArrayInfo iInfo = getInfo(in);
if (ndims == 1 && ndims != (dim_t)iInfo.ndims()) {
af_array tmp_in;
AF_CHECK(af_flat(&tmp_in, in));
AF_CHECK(af_index_gen(out, tmp_in, ndims, indexs));
AF_CHECK(af_release_array(tmp_in));
return AF_SUCCESS;
}
int track = 0;
af_seq seqs[] = {af_span, af_span, af_span, af_span};
for (dim_t i = 0; i < ndims; i++) {
if (indexs[i].isSeq) {
track++;
seqs[i] = indexs[i].idx.seq;
}
}
if (track==(int)ndims) {
// all indexs are sequences, redirecting to af_index
return af_index(out, in, ndims, seqs);
}
af_index_t idxrs[4];
// set all dimensions above ndims to spanner index
for (dim_t i=ndims; i<4; ++i) idxrs[i] = spanner;
for (dim_t i=0; i<ndims; ++i) {
if (!indexs[i].isSeq) {
// check if all af_arrays have atleast one value
// to enable indexing along that dimension
ArrayInfo idxInfo = getInfo(indexs[i].idx.arr);
af_dtype idxType = idxInfo.getType();
ARG_ASSERT(3, (idxType!=c32));
ARG_ASSERT(3, (idxType!=c64));
ARG_ASSERT(3, (idxType!=b8 ));
idxrs[i].idx.arr = indexs[i].idx.arr;
idxrs[i].isSeq = indexs[i].isSeq;
} else {
// af_seq is being used for this dimension
// just copy the index to local variable
idxrs[i] = indexs[i];
}
}
dim4 iDims = iInfo.dims();
ARG_ASSERT(1, (iDims.ndims()>0));
af_dtype inType = getInfo(in).getType();
switch(inType) {
case c64: output = genIndex<cdouble>(in, idxrs); break;
case f64: output = genIndex<double >(in, idxrs); break;
case c32: output = genIndex<cfloat >(in, idxrs); break;
case f32: output = genIndex<float >(in, idxrs); break;
case u64: output = genIndex<uintl >(in, idxrs); break;
case s64: output = genIndex<intl >(in, idxrs); break;
case u32: output = genIndex<uint >(in, idxrs); break;
case s32: output = genIndex<int >(in, idxrs); break;
case u16: output = genIndex<ushort >(in, idxrs); break;
case s16: output = genIndex<short >(in, idxrs); break;
case u8: output = genIndex<uchar >(in, idxrs); break;
case b8: output = genIndex<char >(in, idxrs); break;
default: TYPE_ERROR(1, inType);
}
}
CATCHALL;
std::swap(*out, output);
return AF_SUCCESS;
}