Tensor Tiling Library
 
Loading...
Searching...
No Matches
p/pipelines/TTL_double_scheme.h
Go to the documentation of this file.
1/*
2 * TTL_double_scheme.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// This file presumes that the following have been pre included.
20// this is not done here for path reasons.
21// #include "TTL_core.h"
22// #include "TTL_import_export.h"
23// #include TTL_IMPORT_EXPORT_INCLUDE_H
25#include "TTL_schemes_common.h"
26
27/**
28 * @brief Wait for the previous import operation to complete before beginning an
29 * import of the next tile.
30 *
31 * @param db TTL_import_double_buffering describing the attributes of the
32 * transfer
33 * @param next_tile A description of the tile to begin importing.
34 *
35 */
36template <typename TENSORTYPE>
38 /**
39 * @brief Create a TTL_import_double_buffering and begin the buffering process
40 *
41 * @param int_base1 A pointer to the 1st local buffer
42 * @param int_base2 A pointer to the 2nd local buffer
43 * @param ext_tensor A tensor describing the input in global memory
44 * @param event A pointer to the event to use for the inward (external to
45 * internal) transfer completion
46 * @param first_tile The first tile to fetch for the scheme
47 *
48 * @return The TTL_import_double_buffering created from the input parameters.
49 *
50 * Example:
51 * @code
52 * TTL_event import_DB_e = TTL_get_event();
53 * TTL_import_double_buffering import_db(l_in1, l_in2, ext_base_in, ext_layout_in, &import_DB_e);
54 * @endcode
55 * \n
56 *
57 * This can be optimized and standardized using the TTL_step_buffering
58 * call.
59 *
60 * @startuml
61 *
62 * start
63 *
64 *
65 * stop
66 *
67 * @enduml
68 */
69 TTL_import_double_buffering(TTL_local(TENSORTYPE *) int_base1, TTL_local(TENSORTYPE *) int_base2,
70 TTL_tensor<TENSORTYPE> ext_tensor, TTL_event *event, TTL_tile first_tile) {
71 m_common.int_base[0] = int_base1;
72 m_common.int_base[1] = int_base2;
73
74 m_common.ext_tensor_in = ext_tensor;
75 m_event = event;
76 m_common.index = 0;
77
79
80 step_buffering(first_tile);
81 }
82
84 // For performance, compute everything possible before waiting for the
85 // previous operations to finish.
86 const TTL_layout int_layout(next_tile.shape.width, next_tile.shape.height);
87 const TTL_sub_tensor<TENSORTYPE> import_to(
88 m_common.int_base[m_common.index], next_tile.shape, int_layout, m_common.ext_tensor_in, next_tile.offset);
89
90 const TTL_tensor<TENSORTYPE> import_from(m_common.ext_tensor_in.base,
91 next_tile.shape,
92 m_common.ext_tensor_in.layout,
93 next_tile.offset,
94 m_common.ext_tensor_in.elem_size);
95
96 TTL_wait(1, m_event);
97
98 if (next_tile.empty() == false) {
99 TTL_import_sub_tensor(import_to, import_from, m_event);
100 }
101
102 m_common.index = (m_common.index + 1) % 2;
103
104 const TTL_layout prev_int_layout(m_prev_tile.shape.width, m_prev_tile.shape.height);
105 const TTL_sub_tensor<TENSORTYPE> result(m_common.int_base[m_common.index],
106 m_prev_tile.shape,
107 prev_int_layout,
108 m_common.ext_tensor_in,
109 m_prev_tile.offset);
110
111 m_prev_tile = next_tile;
112
113 return result;
114 }
115
116 /**
117 * @brief Complete any transfers required to finish the buffering process.
118 *
119 * Any transfers that are still in progress will be completed and any transfers
120 * that need to be started and completed before finish_buffering returns
121 */
123 // Nothing to do.
124 }
125
126 TTL_event *m_event; ///< A pointer to the event that is used to
127 ///< track the progress of the transfer
128 TTL_tile m_prev_tile; ///< Store of the previous imported tile */
129
130 TTL_common_buffering<TENSORTYPE, 2> m_common; ///< The information that is m_common to all pipeline schemes
131};
132
133template <typename TENSORTYPE>
135 /**
136 * @brief Create a TTL_import_double_buffering and begin the buffering process
137 *
138 * @param int_base1 A pointer to the 1st local buffer
139 * @param int_base2 A pointer to the 2nd local buffer
140 * @param ext_tensor A tensor describing the input in global memory
141 * @param event A pointer to the event to use for the inward (external to
142 * internal) transfer completion
143 *
144 * @return The TTL_export_double_buffering created from the input parameters.
145 *
146 * Example:
147 * @code
148 * TTL_event import_DB_e = TTL_get_event();
149 * TTL_export_double_buffering import_db(
150 * l_in1, l_in2, ext_base_in, ext_layout_in, &import_DB_e);
151 * @endcode
152 * \n
153 *
154 * This can be optimized and standardized using the TTL_step_buffering
155 * call.
156 *
157 * @startuml
158 *
159 * start
160 *
161 *
162 * stop
163 *
164 * @enduml
165 */
166 TTL_export_double_buffering(TTL_local(TENSORTYPE *) int_base1, TTL_local(TENSORTYPE *) int_base2,
167 TTL_tensor<TENSORTYPE> ext_tensor, TTL_event *event) {
168 m_common.int_base[0] = int_base1;
169 m_common.int_base[1] = int_base2;
170
171 m_common.ext_tensor_in = ext_tensor;
172 m_event = event;
173 m_common.index = 0;
174
176 }
177
178 /**
179 * @brief Wait for the previous import operation to complete before beginning an
180 * export of next tile.
181 *
182 * @param tile_current A description of the tile to begin exporting.
183 *
184 */
186 const TTL_layout int_layout(m_prev_tile.shape.width, m_prev_tile.shape.height);
187 const TTL_tensor export_from(
188 m_common.int_base[m_common.index], m_prev_tile.shape, int_layout, m_common.ext_tensor_in.elem_size);
189 const TTL_tensor export_to(m_common.ext_tensor_in.base,
190 m_prev_tile.shape,
191 m_common.ext_tensor_in.layout,
192 m_prev_tile.offset,
193 m_common.ext_tensor_in.elem_size);
194
195 TTL_wait(1, m_event);
196
197 if (m_prev_tile.empty() == false) TTL_export(export_from, export_to, m_event);
198
199 m_common.index = (m_common.index + 1) % 2; // TTL_ARRAYSIZE(m_common.int_base);
200 const TTL_layout curr_int_layout(tile_current.shape.width, tile_current.shape.height);
201 const TTL_sub_tensor result(m_common.int_base[m_common.index],
202 tile_current.shape,
203 curr_int_layout,
204 m_common.ext_tensor_in,
205 tile_current.offset);
206 m_prev_tile = tile_current;
207
208 return result;
209 }
210
211 /**
212 * @brief Complete any transfers required to finish the buffering process.
213 *
214 * Any transfers that are still in progress will be completed and any transfers
215 * that need to be started and completed before finish_buffering returns
216 */
221
222 TTL_event *m_event; ///< A pointer to the event that is used to
223 ///< track the progress of the transfer
224 TTL_tile m_prev_tile; ///< Store of the previous imported tile */
225
226 TTL_common_buffering<TENSORTYPE, 2> m_common; ///< The information that is m_common to all pipeline schemes
227};
static void TTL_wait(const int num_events, TTL_event_t *const events)
event_t TTL_event
TTL_event is a pseudonym for OpenCL event_t.
#define TTL_local(type)
Create a typed reference in the __local address space.
static void TTL_import_sub_tensor(const TTL_int_void_sub_tensor_t internal_sub_tensor, const TTL_const_ext_void_tensor_t const_external_tensor, TTL_event_t *event)
Implementation of TTL_import_sub_tensor.
static void TTL_export(const TTL_const_int_void_tensor_t internal_tensor, const TTL_ext_void_tensor_t external_tensor, TTL_event_t *event)
Export the external tensor to the internal tensor returning when complete.
TTL_sub_tensor< TENSORTYPE > step_buffering(TTL_tile tile_current)
Wait for the previous import operation to complete before beginning an export of next tile.
TTL_common_buffering< TENSORTYPE, 2 > m_common
The information that is m_common to all pipeline schemes.
TTL_tile m_prev_tile
Store of the previous imported tile *‍/.
void finish_buffering()
Complete any transfers required to finish the buffering process.
TTL_export_double_buffering(TTL_local(TENSORTYPE *) int_base1, TTL_local(TENSORTYPE *) int_base2, TTL_tensor< TENSORTYPE > ext_tensor, TTL_event *event)
Create a TTL_import_double_buffering and begin the buffering process.
TTL_sub_tensor< TENSORTYPE > step_buffering(const TTL_tile next_tile)
TTL_common_buffering< TENSORTYPE, 2 > m_common
The information that is m_common to all pipeline schemes.
void finish_buffering()
Complete any transfers required to finish the buffering process.
TTL_import_double_buffering(TTL_local(TENSORTYPE *) int_base1, TTL_local(TENSORTYPE *) int_base2, TTL_tensor< TENSORTYPE > ext_tensor, TTL_event *event, TTL_tile first_tile)
Create a TTL_import_double_buffering and begin the buffering process.
TTL_tile m_prev_tile
Store of the previous imported tile *‍/.
Description of a Tensor layout in memory.
TTL_dim height
Number of rows along dimension y.
TTL_dim width
Number of elements along dimension x.
A tensor plus its reference to its parent tensor.
A poor mans base class for an a tensor in the passed address space.
TTL_offset offset
TTL_shape shape
bool empty() const
Check if the tile passed is empty.