Tensor Tiling Library
 
Loading...
Searching...
No Matches
TTL_import_export.h
Go to the documentation of this file.
1/*
2 * TTL_import_export.h
3 *
4 * Copyright (c) 2023 Mobileye
5 *
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/**
20 * @brief Fill block of local memory
21 *
22 * Sets the first num bytes of the block of memory pointed by ptr to the
23 * specified value (interpreted as an unsigned char).
24 *
25 * @param ptr Pointer to the block of memory to fill.
26 * @param value Value to be set. The value is passed as an int, but the function
27 * fills the block of memory using the unsigned char conversion of this value.
28 * @param num Number of bytes to be set to the value.
29 *
30 * @return ptr is to the output is returned.
31 */
32static inline __local void *TTL_local_memset(__local void *const ptr, char value, size_t num) {
33 __local char *const dst = (__local char *)ptr;
34
35 for (size_t byte = 0; byte < num; byte++)
36 dst[byte] = value;
37
38 return ptr;
39}
40
41/**
42 * @brief Clear any unpopulated space in the target area.
43 *
44 * @param dst
45 * @param x_offset
46 * @param y_offset
47 * @param num_bytes_per_element
48 * @param num_elements_per_line
49 * @param dst_total_line_length
50 * @param num_lines
51 * @param total_lines
52 * @param num_planes
53 */
54static inline void TTL_clear_void_space(__local void *const dst, const size_t x_offset, const size_t y_offset,
55 size_t num_bytes_per_element, size_t num_elements_per_line,
56 size_t dst_total_line_length, size_t num_lines, size_t total_lines,
57 size_t num_planes) {
58 __local char *dst_ptr = (__local char *)dst;
59 unsigned int left_trim_bytes = x_offset * num_bytes_per_element;
60 unsigned int right_trim_bytes = (dst_total_line_length - num_elements_per_line) * num_bytes_per_element;
61
62 for (size_t plane = 0; plane < num_planes; plane++) {
63 for (size_t line = 0; line < total_lines; line++) {
64 if ((line < y_offset) || (line >= num_lines)) {
65 TTL_local_memset(dst_ptr, 0, dst_total_line_length * num_bytes_per_element);
66 } else {
67 // Clear anything not being copied to zero - will make the 'clear value'
68 // definable at some level.
69 TTL_local_memset(dst_ptr, 0, left_trim_bytes);
70 TTL_local_memset(dst_ptr + (num_elements_per_line * num_bytes_per_element), 0, right_trim_bytes);
71 }
72 dst_ptr += dst_total_line_length * num_bytes_per_element;
73 }
74 }
75}
76
77static inline TTL_shape_t TTL_import_pre_fill(const TTL_int_sub_tensor_t internal_sub_tensor,
78 const TTL_const_ext_tensor_t const_external_tensor,
79 __local void **const dst_address, __global void **const src_address) {
80 size_t x_offset;
81 size_t x_cut;
82 size_t y_offset;
83 size_t y_cut;
84 size_t z_offset;
85 size_t z_cut;
86
87 x_offset = (((-internal_sub_tensor.origin.sub_offset.x) > (0)) ? (-internal_sub_tensor.origin.sub_offset.x) : (0));
88 x_cut = (internal_sub_tensor.origin.sub_offset.x + internal_sub_tensor.tensor.shape.width) >
89 internal_sub_tensor.origin.shape.width
90 ? (internal_sub_tensor.origin.sub_offset.x + internal_sub_tensor.tensor.shape.width) -
91 internal_sub_tensor.origin.shape.width
92 : 0;
93
94 y_offset = (((-internal_sub_tensor.origin.sub_offset.y) > (0)) ? (-internal_sub_tensor.origin.sub_offset.y) : (0));
95 y_cut = (internal_sub_tensor.origin.sub_offset.y + internal_sub_tensor.tensor.shape.height) >
96 internal_sub_tensor.origin.shape.height
97 ? (internal_sub_tensor.origin.sub_offset.y + internal_sub_tensor.tensor.shape.height) -
98 internal_sub_tensor.origin.shape.height
99 : 0;
100
101 z_offset = 0; // TTL_MAX(-internal_sub_tensor.origin.sub_offset.z, 0);
102 z_cut = 0; // TTL_MAX((internal_sub_tensor.origin.sub_offset.z + internal_sub_tensor.tensor.shape.depth) -
103 // 1
104
105 /*Internal_sub_tensor.origin.shape.depth */
106 // 0);
107
108 *dst_address = (__local char *)internal_sub_tensor.tensor.base + (x_offset * internal_sub_tensor.tensor.elem_size) +
109 (y_offset * internal_sub_tensor.tensor.layout.row_spacing * internal_sub_tensor.tensor.elem_size) +
110 (z_offset * internal_sub_tensor.tensor.layout.plane_spacing * internal_sub_tensor.tensor.elem_size);
111
112 *src_address = (__global char *)const_external_tensor.base + (x_offset * const_external_tensor.elem_size) +
113 (y_offset * const_external_tensor.layout.row_spacing * const_external_tensor.elem_size) +
114 (z_offset * const_external_tensor.layout.plane_spacing * const_external_tensor.elem_size);
115
116 TTL_clear_void_space(internal_sub_tensor.tensor.base,
117 x_offset,
118 y_offset,
119 internal_sub_tensor.tensor.elem_size,
120 internal_sub_tensor.tensor.shape.width - x_cut,
121 internal_sub_tensor.tensor.layout.row_spacing,
122 internal_sub_tensor.tensor.shape.height - y_cut,
123 internal_sub_tensor.tensor.shape.height,
124 internal_sub_tensor.tensor.shape.depth);
125
126 return TTL_create_shape(internal_sub_tensor.tensor.shape.width - x_offset - x_cut,
127 internal_sub_tensor.tensor.shape.height - y_offset - y_cut,
128 internal_sub_tensor.tensor.shape.depth - z_offset - z_cut);
129}
TTL_const_ext_void_tensor_t TTL_const_ext_tensor_t
static TTL_shape_t TTL_import_pre_fill(const TTL_int_sub_tensor_t internal_sub_tensor, const TTL_const_ext_tensor_t const_external_tensor, __local void **const dst_address, __global void **const src_address)
static void TTL_clear_void_space(__local void *const dst, const size_t x_offset, const size_t y_offset, size_t num_bytes_per_element, size_t num_elements_per_line, size_t dst_total_line_length, size_t num_lines, size_t total_lines, size_t num_planes)
Clear any unpopulated space in the target area.
static __local void * TTL_local_memset(__local void *const ptr, char value, size_t num)
Fill block of local memory.
TTL_int_void_sub_tensor_t TTL_int_sub_tensor_t
#define __global
The opencl __global namespace is not supported in C.
Definition c/TTL_types.h:26
#define __local
The opencl __local namespace is not supported in C.
Definition c/TTL_types.h:27
struct TTL_int_void_sub_tensor_t::@266046156211021141270372070244026262143005251323 origin
TTL_dim_t row_spacing
The distance between the start of consequtive rows in units of elements.
TTL_dim_t plane_spacing
The distance between the start of consequtive planes in units of elements.
TTL_offset_dim_t y
Offset in dimension y.
TTL_offset_dim_t x
Offset in dimension x.
Description of a Shape.
TTL_dim_t depth
Number of planes along dimension z.
TTL_dim_t width
Number of elements along dimension x.
TTL_dim_t height
Number of rows along dimension y.
static TTL_shape_t TTL_create_shape(TTL_dim_t width, TTL_dim_t height, TTL_dim_t depth)
Create a description of a Shape.