Tensor Tiling Library
 
Loading...
Searching...
No Matches
TTL_cpp/opencl/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
22
23#ifdef TTL_COPY_3D
24/*
25 * async_work_group_copy_3D3D is not supported by all OpenCL drivers
26 * including some V3.0 drivers. To resolve this define TTL_COPY_3D
27 */
29#endif
30
31/**
32 * @brief Return an empty event of type TTL_event
33 *
34 * @see TTL_event for information about how event and TTL_event relate
35 *
36 * @return The return value allows an empty event to be passed to APIs that
37 * require an event and return/update the value with a new event value.
38 */
39static inline TTL_event TTL_get_event() {
40 return (event_t)0;
41}
42
43/**
44 * @def TTL_wait
45 *
46 * Wait for the array of events passed to enter the complete state.
47 */
48static inline void TTL_wait(const int num_events, TTL_event *const events) {
49 __TTL_dump_wait(num_events, events, __LINE__);
50
51 wait_group_events(num_events, events);
52}
53
54/**
55 * @brief TTL_import
56 *
57 * Begin the asynchronous import of the external tensor to the internal tensor
58 *
59 * @param internal_tensor internal_tensor A TTL_tensor describing the internal tensor.
60 * @param external_tensor external_tensor A TTL_tensor describing the external tensor.
61 * @param event event_ptr A pointer to the event which describe the transfer.
62 */
63template <typename INT_TENSORTYPE, typename EXT_TENSORTYPE>
64void TTL_import_base(const TTL_tensor<INT_TENSORTYPE> &internal_tensor,
65 const TTL_tensor<EXT_TENSORTYPE> &external_tensor, TTL_event *const event) {
66 *event = async_work_group_copy_3D3D((__local void *)internal_tensor.base,
67 0,
68 (__global void *)external_tensor.base,
69 0,
70 internal_tensor.elem_size,
71 internal_tensor.shape.width,
72 internal_tensor.shape.height,
73 internal_tensor.shape.depth,
74 external_tensor.layout.row_spacing,
75 external_tensor.layout.plane_spacing,
76 internal_tensor.layout.row_spacing,
77 internal_tensor.layout.plane_spacing,
78 *event);
79
80 __TTL_dump_transaction(false, internal_tensor, external_tensor, 0, event, __LINE__);
81}
82
83/**
84 * @brief Export the external tensor to the internal tensor returning when complete
85 *
86 * @param internal_tensor A TTL_tensor describing the internal tensor.
87 * @param external_tensor A TTL_tensor describing the external tensor.
88 * Complete description of what not how here.
89 */
90template <typename INT_TENSORTYPE, typename EXT_TENSORTYPE>
92 const TTL_tensor<EXT_TENSORTYPE> &external_tensor) {
93 TTL_event event = TTL_get_event();
94 TTL_import_base(internal_tensor, external_tensor, &event);
95 TTL_wait(1, &event);
96}
97
98/**
99 * @brief Begin the asynchronous export of the external tensor to the internal tensor
100 *
101 * @param internal_tensor internal_tensor A TTL_tensor describing the internal tile.
102 * @param external_tensor external_tensor A TTL_tensor describing the external tile.
103 * @param event event_ptr A pointer to the event which describe the transfer.
104 *
105 * @note async_work_group_copy_3D3D is not supported by all OpenCL drivers
106 * including some V3.0 drivers. To resolve this define TTL_COPY_3D
107 */
108template <typename INT_TENSORTYPE, typename EXT_TENSORTYPE>
109static inline void TTL_export_base(const TTL_tensor<INT_TENSORTYPE> internal_tensor,
110 const TTL_tensor<EXT_TENSORTYPE> external_tensor, TTL_event *const event) {
111 *event = async_work_group_copy_3D3D((__global void *)external_tensor.base,
112 0,
113 (__local void *)internal_tensor.base,
114 0,
115 internal_tensor.elem_size,
116 internal_tensor.shape.width,
117 internal_tensor.shape.height,
118 internal_tensor.shape.depth,
119 internal_tensor.layout.row_spacing,
120 internal_tensor.layout.plane_spacing,
121 external_tensor.layout.row_spacing,
122 external_tensor.layout.plane_spacing,
123 *event);
124
125 __TTL_dump_transaction(true, internal_tensor, external_tensor, 0, event, __LINE__);
126}
127
128/**
129 * @brief Export the external tensor to the internal tensor returning when complete
130 *
131 * @param internal_tensor A TTL_tensor describing the internal tile.
132 * @param external_tensor A TTL_tensor describing the external tile.
133 *
134 * Complete description of what not how here.
135 */
136template <typename INT_TENSORTYPE, typename EXT_TENSORTYPE>
137static inline void TTL_blocking_export_base(const TTL_tensor<INT_TENSORTYPE> internal_tensor,
138 const TTL_tensor<EXT_TENSORTYPE> external_tensor) {
139 TTL_event event = TTL_get_event();
140 TTL_export_base(internal_tensor, external_tensor, &event);
141 TTL_wait(1, &event);
142}
static void wait_group_events(int num_events, event_t *event_list)
Wait for events that identify the async_work_group_copy operations to complete.
static event_t async_work_group_copy_3D3D(void *const dst, size_t dst_offset, const void *const src, size_t src_offset, size_t num_bytes_per_element, size_t num_elements_per_line, size_t num_lines, size_t num_planes, size_t src_total_line_length, size_t src_total_plane_spacing, size_t dst_total_line_length, size_t dst_total_plane_spacing, event_t event)
#define __global
The opencl __global namespace is not supported in C.
#define __local
The opencl __local namespace is not supported in C.
unsigned char event_t
event_t is not supported, so provide a harmless placeholder
event_t TTL_event
TTL_event is a pseudonym for OpenCL event_t.
void TTL_blocking_import_base(const TTL_tensor< INT_TENSORTYPE > &internal_tensor, const TTL_tensor< EXT_TENSORTYPE > &external_tensor)
Export the external tensor to the internal tensor returning when complete.
static void TTL_blocking_export_base(const TTL_tensor< INT_TENSORTYPE > internal_tensor, const TTL_tensor< EXT_TENSORTYPE > external_tensor)
Export the external tensor to the internal tensor returning when complete.
static void TTL_wait(const int num_events, TTL_event *const events)
static TTL_event TTL_get_event()
Return an empty event of type TTL_event.
static void TTL_export_base(const TTL_tensor< INT_TENSORTYPE > internal_tensor, const TTL_tensor< EXT_TENSORTYPE > external_tensor, TTL_event *const event)
Begin the asynchronous export of the external tensor to the internal tensor.
void TTL_import_base(const TTL_tensor< INT_TENSORTYPE > &internal_tensor, const TTL_tensor< EXT_TENSORTYPE > &external_tensor, TTL_event *const event)
TTL_import.
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.
TTL_dim height
Number of rows along dimension y.
TTL_dim depth
Number of planes along dimension z.
TTL_dim width
Number of elements along dimension x.
A poor mans base class for an a tensor in the passed address space.
TENSORTYPE * base