ONE - On-device Neural Engine
Loading...
Searching...
No Matches
example_no_array.cpp File Reference
#include <array>
#include <vector>
#include <algorithm>
#include <cassert>
#include <iostream>

Go to the source code of this file.

Functions

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 ()
 

Function Documentation

◆ gather_no_array()

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 
)

Definition at line 23 of file example_no_array.cpp.

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}

Referenced by main().

◆ main()

int main ( void  )

Definition at line 51 of file example_no_array.cpp.

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)

References gather_no_array().