Tensor Tiling Library
 
Loading...
Searching...
No Matches
TTL_cpp/TTL_import_export.h
Go to the documentation of this file.
1/*
2 * TTL_import_export.h
3 *
4 * Copyright (c) 2025 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#pragma once
20
21#include "TTL_core.h"
22#include TTL_IMPORT_EXPORT_INCLUDE_H
23
24#define TTL_MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
25
26/**
27 * @brief Import the external tensor to the internal tensor returning when complete
28 *
29 * @param internal_tensor A TTL_int_tensor describing the internal tensor.
30 * @param external_tensor A TTL_int_tensor describing the const external tensor.
31 * @param event A TTL_event type to allow detection of import completion.
32 *
33 * Complete description of what not how here.
34 */
35template <typename TENSORTYPE>
36void TTL_import(const TTL_tensor<TENSORTYPE> internal_tensor, const TTL_tensor<TENSORTYPE> external_tensor,
37 TTL_event *event) {
38 return TTL_import_base(internal_tensor, external_tensor, event);
39}
40
41/**
42 * @brief Import the external tensor to the internal tensor returning when complete
43 *
44 * @param internal_tensor A TTL_int_tensor describing the internal tensor.
45 * @param external_tensor A TTL_int_tensor describing the external tensor.
46 *
47 * Complete description of what not how here.
48 */
49template <typename TENSORTYPE>
50void TTL_blocking_import(const TTL_tensor<TENSORTYPE> &internal_tensor, const TTL_tensor<TENSORTYPE> &external_tensor) {
51 TTL_blocking_import_base(internal_tensor, external_tensor);
52}
53
54/**
55 * @brief Implementation of TTL_import_sub_tensor
56 *
57 * @param internal_sub_tensor A TTL_int_tensor describing the internal tensor.
58 * @param const_external_tensor A TTL_const_ext_tensor describing the external tensor.
59 * @param event A TTL_event type to allow detection of import completion.
60 *
61 * @see TTL_import for full API and parameter information
62 */
63template <typename TENSORTYPE>
64void TTL_import_sub_tensor(const TTL_sub_tensor<TENSORTYPE> &internal_sub_tensor,
65 const TTL_tensor<TENSORTYPE> const_external_tensor, TTL_event *event) {
66 TTL_local(TENSORTYPE *) dst_address;
67 TTL_global(TENSORTYPE *) src_address;
68
69 const TTL_shape import_shape =
70 TTL_import_pre_fill(internal_sub_tensor, const_external_tensor, &dst_address, &src_address);
71
72 const TTL_tensor<TENSORTYPE> import_int_tensor(
73 dst_address, import_shape, internal_sub_tensor.tensor.layout, internal_sub_tensor.tensor.elem_size);
74
75 const TTL_tensor<TENSORTYPE> import_ext_tensor(
76 src_address, import_shape, const_external_tensor.layout, TTL_offset(), const_external_tensor.elem_size);
77
78 TTL_import(import_int_tensor, import_ext_tensor, event);
79}
80
81/**
82 * @brief Export the external tensor to the internal tensor returning when complete
83 *
84 * @param internal_tensor A TTL_int_tensor describing the internal tensor.
85 * @param external_tensor A TTL_int_tensor describing the const external tensor.
86 * @param event A TTL_event type to allow detection of import completion.
87 *
88 * Complete description of what not how here.
89 */
90template <typename TENSORTYPE>
91void TTL_export(const TTL_tensor<TENSORTYPE> &internal_tensor, const TTL_tensor<TENSORTYPE> &external_tensor,
92 TTL_event *event) {
93 return TTL_export_base(internal_tensor, external_tensor, event);
94}
95
96/**
97 * @brief Export the external tensor to the internal tensor returning when complete
98 *
99 * @param internal_tensor A TTL_int_tensor describing the internal tensor.
100 * @param external_tensor A TTL_int_tensor describing the const external tensor.
101 *
102 * Complete description of what not how here.
103 */
104template <typename TENSORTYPE>
105void TTL_blocking_export(const TTL_tensor<TENSORTYPE> &internal_tensor, const TTL_tensor<TENSORTYPE> &external_tensor) {
106 TTL_blocking_export_base(internal_tensor, external_tensor);
107}
108
109/**
110 * @brief Fill block of local memory
111 *
112 * Sets the first num bytes of the block of memory pointed by ptr to the
113 * specified value (interpreted as an unsigned char).
114 *
115 * @param ptr Pointer to the block of memory to fill.
116 * @param value Value to be set. The value is passed as an int, but the function
117 * fills the block of memory using the unsigned char conversion of this value.
118 * @param num Number of bytes to be set to the value.
119 *
120 * @return ptr is to the output is returned.
121 */
122static inline TTL_local(void *) TTL_local_memset(TTL_local(void *) const ptr, char value, int num) {
123 TTL_local(char *) const dst = (TTL_local(char *))ptr;
124
125 for (int byte = 0; byte < num; byte++)
126 dst[byte] = value;
127
128 return ptr;
129}
130
131/**
132 * @brief Clear any unpopulated space in the target area.
133 *
134 * @param dst
135 * @param x_offset
136 * @param y_offset
137 * @param num_bytes_per_element
138 * @param num_elements_per_line
139 * @param dst_total_line_length
140 * @param num_lines
141 * @param total_lines
142 * @param num_planes
143 */
144static inline void TTL_clear_void_space(TTL_local(void *) const dst, const size_t x_offset, const size_t y_offset,
145 size_t num_bytes_per_element, size_t num_elements_per_line,
146 size_t dst_total_line_length, size_t num_lines, size_t total_lines,
147 size_t num_planes) {
148 TTL_local(char *) dst_ptr = (TTL_local(char *))dst;
149 unsigned int left_trim_bytes = x_offset * num_bytes_per_element;
150 unsigned int right_trim_bytes = (dst_total_line_length - num_elements_per_line) * num_bytes_per_element;
151
152 for (size_t plane = 0; plane < num_planes; plane++) {
153 for (size_t line = 0; line < total_lines; line++) {
154 if ((line < y_offset) || (line >= num_lines)) {
155 TTL_local_memset(dst_ptr, 0, dst_total_line_length * num_bytes_per_element);
156 } else {
157 // Clear anything not being copied to zero - will make the 'clear value'
158 // definable at some level.
159 TTL_local_memset(dst_ptr, 0, left_trim_bytes);
160 TTL_local_memset(dst_ptr + (num_elements_per_line * num_bytes_per_element), 0, right_trim_bytes);
161 }
162 dst_ptr += dst_total_line_length * num_bytes_per_element;
163 }
164 }
165}
166
167template <typename INT_TENSORTYPE, typename EXT_TENSORTYPE>
168static inline TTL_shape TTL_import_pre_fill(const TTL_sub_tensor<INT_TENSORTYPE> internal_sub_tensor,
169 const TTL_tensor<EXT_TENSORTYPE> const_external_tensor,
170 TTL_local(INT_TENSORTYPE *) *const dst_address,
171 TTL_global(EXT_TENSORTYPE *) *const src_address) {
172 size_t x_offset;
173 size_t x_cut;
174 size_t y_offset;
175 size_t y_cut;
176 size_t z_offset;
177 size_t z_cut;
178
179 x_offset = TTL_MAX(-internal_sub_tensor.origin.sub_offset.x, 0);
180 x_cut = (internal_sub_tensor.origin.sub_offset.x + internal_sub_tensor.tensor.shape.width) >
181 internal_sub_tensor.origin.shape.width
182 ? (internal_sub_tensor.origin.sub_offset.x + internal_sub_tensor.tensor.shape.width) -
183 internal_sub_tensor.origin.shape.width
184 : 0;
185
186 y_offset = TTL_MAX(-internal_sub_tensor.origin.sub_offset.y, 0);
187 y_cut = (internal_sub_tensor.origin.sub_offset.y + internal_sub_tensor.tensor.shape.height) >
188 internal_sub_tensor.origin.shape.height
189 ? (internal_sub_tensor.origin.sub_offset.y + internal_sub_tensor.tensor.shape.height) -
190 internal_sub_tensor.origin.shape.height
191 : 0;
192
193 z_offset = 0; // TTL_MAX(-internal_sub_tensor.origin.sub_offset.z, 0);
194 z_cut = 0; // TTL_MAX((internal_sub_tensor.origin.sub_offset.z + internal_sub_tensor.tensor.shape.depth) -
195 // 1 /* Internal_sub_tensor.origin.shape.depth */
196 // 0);
197
198 *dst_address = internal_sub_tensor.tensor.base + x_offset +
199 (y_offset * internal_sub_tensor.tensor.layout.row_spacing) +
200 (z_offset * internal_sub_tensor.tensor.layout.plane_spacing);
201
202 *src_address = const_external_tensor.base + x_offset + (y_offset * const_external_tensor.layout.row_spacing) +
203 (z_offset * const_external_tensor.layout.plane_spacing);
204
205 TTL_clear_void_space(internal_sub_tensor.tensor.base,
206 x_offset,
207 y_offset,
208 internal_sub_tensor.tensor.elem_size,
209 internal_sub_tensor.tensor.shape.width - x_cut,
210 internal_sub_tensor.tensor.layout.row_spacing,
211 internal_sub_tensor.tensor.shape.height - y_cut,
212 internal_sub_tensor.tensor.shape.height,
213 internal_sub_tensor.tensor.shape.depth);
214
215 return TTL_shape(internal_sub_tensor.tensor.shape.width - x_offset - x_cut,
216 internal_sub_tensor.tensor.shape.height - y_offset - y_cut,
217 internal_sub_tensor.tensor.shape.depth - z_offset - z_cut);
218}
static void TTL_import_base(const TTL_int_tensor_t internal_tensor, const TTL_const_ext_tensor_t external_tensor, TTL_event_t *event)
TTL_import.
static void TTL_export_base(const TTL_const_int_tensor_t internal_tensor, const TTL_ext_tensor_t external_tensor, TTL_event_t *const event)
Begin the asynchronous export of the external tensor to the internal tensor.
static void TTL_blocking_export_base(const TTL_const_int_tensor_t internal_tensor, const TTL_ext_tensor_t external_tensor)
Export the external tensor to the internal tensor returning when complete.
static void TTL_blocking_import_base(const TTL_int_tensor_t internal_tensor, const TTL_const_ext_tensor_t external_tensor)
Export the external tensor to the internal tensor returning when complete.
void TTL_blocking_import(const TTL_tensor< TENSORTYPE > &internal_tensor, const TTL_tensor< TENSORTYPE > &external_tensor)
Import the external tensor to the internal tensor returning when complete.
static TTL_shape TTL_import_pre_fill(const TTL_sub_tensor< INT_TENSORTYPE > internal_sub_tensor, const TTL_tensor< EXT_TENSORTYPE > const_external_tensor, INT_TENSORTYPE **const dst_address, EXT_TENSORTYPE **const src_address)
void TTL_export(const TTL_tensor< TENSORTYPE > &internal_tensor, const TTL_tensor< TENSORTYPE > &external_tensor, TTL_event *event)
Export the external tensor to the internal tensor returning when complete.
void TTL_import_sub_tensor(const TTL_sub_tensor< TENSORTYPE > &internal_sub_tensor, const TTL_tensor< TENSORTYPE > const_external_tensor, TTL_event *event)
Implementation of TTL_import_sub_tensor.
static void * TTL_local_memset(void *const ptr, char value, int num)
Fill block of local memory.
void TTL_import(const TTL_tensor< TENSORTYPE > internal_tensor, const TTL_tensor< TENSORTYPE > external_tensor, TTL_event *event)
Import the external tensor to the internal tensor returning when complete.
#define TTL_MAX(X, Y)
static void TTL_clear_void_space(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.
void TTL_blocking_export(const TTL_tensor< TENSORTYPE > &internal_tensor, const TTL_tensor< TENSORTYPE > &external_tensor)
Export the external tensor to the internal tensor returning when complete.
event_t TTL_event
TTL_event is a pseudonym for OpenCL event_t.
#define TTL_global(type)
Create a typed reference in the __global address space.
#define TTL_local(type)
Create a typed reference in the __local address space.
TTL_dim plane_spacing
The distance between the start of consequtive planes in units of elements.
TTL_dim row_spacing
The distance between the start of consequtive rows in units of elements.
Description of the 3D offset of an object.
TTL_offset_dim y
Offset in dimension y.
TTL_offset_dim x
Offset in dimension x.
Description of a Shape.
TTL_dim height
Number of rows along dimension y.
TTL_dim width
Number of elements along dimension x.
TTL_offset sub_offset
The offset of the sub tensor from the origin sensor.
TTL_shape shape
The shape of the origin tensor in 3 dimensions.
A tensor plus its reference to its parent tensor.
TTL_tensor< TENSORTYPE > tensor
A poor mans base class for an a tensor in the passed address space.
TENSORTYPE * base