| 25 | |
| 26 | template<typename T> |
| 27 | Array<T> index(const Array<T>& in, const af_index_t idxrs[]) { |
| 28 | IndexKernelParam p; |
| 29 | std::vector<af_seq> seqs(4, af_span); |
| 30 | // create seq vector to retrieve output |
| 31 | // dimensions, offsets & offsets |
| 32 | for (dim_t x = 0; x < 4; ++x) { |
| 33 | if (idxrs[x].isSeq) { seqs[x] = idxrs[x].idx.seq; } |
| 34 | } |
| 35 | |
| 36 | // retrieve dimensions, strides and offsets |
| 37 | const dim4& iDims = in.dims(); |
| 38 | dim4 dDims = in.getDataDims(); |
| 39 | dim4 oDims = toDims(seqs, iDims); |
| 40 | dim4 iOffs = toOffset(seqs, dDims); |
| 41 | dim4 iStrds = in.strides(); |
| 42 | |
| 43 | for (dim_t i = 0; i < 4; ++i) { |
| 44 | p.isSeq[i] = idxrs[i].isSeq; |
| 45 | p.offs[i] = iOffs[i]; |
| 46 | p.strds[i] = iStrds[i]; |
| 47 | p.steps[i] = 0; |
| 48 | if (idxrs[i].isSeq) { |
| 49 | af_seq seq = idxrs[i].idx.seq; |
| 50 | // The step for af_span used in the kernel must be 1 |
| 51 | if (seq.begin == af_span.begin && seq.end == af_span.end && |
| 52 | seq.step == af_span.step) |
| 53 | p.steps[i] = 1; |
| 54 | else |
| 55 | p.steps[i] = seq.step; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | std::vector<Array<uint>> idxArrs(4, createEmptyArray<uint>(dim4(1))); |
| 60 | // look through indexs to read af_array indexs |
| 61 | for (dim_t x = 0; x < 4; ++x) { |
| 62 | if (!p.isSeq[x]) { |
| 63 | idxArrs[x] = castArray<uint>(idxrs[x].idx.arr); |
| 64 | oDims[x] = idxArrs[x].elements(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | Array<T> out = createEmptyArray<T>(oDims); |
| 69 | if (oDims.elements() == 0) { return out; } |
| 70 | kernel::index<T>(out, in, p, idxArrs); |
| 71 | |
| 72 | return out; |
| 73 | } |
| 74 | |
| 75 | #define INSTANTIATE(T) \ |
| 76 | template Array<T> index<T>(const Array<T>& in, const af_index_t idxrs[]); |