ONE - On-device Neural Engine
Loading...
Searching...
No Matches
example_no_array.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <array>
18#include <vector>
19#include <algorithm>
20#include <cassert>
21#include <iostream>
22
23void gather_no_array(const float *in_data, const std::array<size_t, 3> &dims, float *out_data,
24 const std::array<size_t, 3> &out_dims, //[nselections,
25 const int *indices, const std::array<size_t, 3> &indices_dims)
26{
27 assert(indices_dims[1] == dims.size());
28
29 for (int i = 0; i < indices_dims[0]; ++i)
30 {
31 for (int j = 0; j < indices_dims[1]; ++j)
32 {
33 const int *index_ptr = indices + i * indices_dims[2] * indices_dims[1] + j * indices_dims[2];
34
35 size_t in_offset = index_ptr[0] * dims[2] * dims[1] + index_ptr[1] * dims[2];
36
37 const float *in_ptr = in_data + in_offset;
38
39 size_t out_offset = i * out_dims[2] * out_dims[1] + j * out_dims[2];
40
41 float *out_ptr = out_data + out_offset;
42
43 for (int k = 0; k < dims[2]; ++k)
44 {
45 out_ptr[k] = in_ptr[k];
46 }
47 }
48 }
49}
50
51int main()
52{
53 std::array<size_t, 3> in_dims{3, 3, 4};
54 std::vector<float> input(3 * 3 * 4);
55 for (size_t i = 0; i < 3 * 3 * 4; ++i)
56 input[i] = i;
57
58 std::array<size_t, 3> indices_shape{1, 3, 2};
59 std::vector<int> indices(1 * 3 * 2);
60
61 indices[0] = 0;
62 indices[1] = 0;
63 indices[2] = 1;
64 indices[3] = 1;
65 indices[4] = 2;
66 indices[5] = 2;
67
68 std::array<size_t, 3> output_dims{1, 3, 4};
69 std::vector<float> output(1 * 3 * 4);
70
71 gather_no_array(input.data(), in_dims, output.data(), output_dims, indices.data(), indices_shape);
72
73 for (size_t i = 0; i < output_dims[0]; ++i)
74 {
75 for (size_t j = 0; j < output_dims[1]; ++j)
76 {
77 auto out_ptr = output.data() + i * output_dims[1] * output_dims[2] + j * output_dims[2];
78 for (size_t k = 0; k < output_dims[2]; ++k)
79 {
80 std::cout << out_ptr[k] << ", ";
81 }
82 std::cout << std::endl;
83 }
84 }
85}
void gather_no_array(const float *in_data, const std::array< size_t, 3 > &dims, float *out_data, const std::array< size_t, 3 > &out_dims, const int *indices, const std::array< size_t, 3 > &indices_dims)
int main()