blob: c2bd01270395198341f94d267471ebba3620f149 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +01002 * Copyright (c) 2022-2023 Arm Limited.
SiCong Lif44bbc52022-08-29 18:25:51 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010024
SiCong Lif44bbc52022-08-29 18:25:51 +010025#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h"
26#include "arm_compute/core/CL/CLCompileContext.h"
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010027#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadContextImpl.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010028
29namespace arm_compute
30{
31namespace experimental
32{
33namespace dynamic_fusion
34{
35GpuWorkloadContext::GpuWorkloadContext(CLCompileContext *cl_compile_ctx)
SiCong Li23882a92023-06-28 09:49:45 +010036 : _impl{ std::make_unique<Impl>(GpuLanguage::OpenCL, cl_compile_ctx) }
SiCong Lif44bbc52022-08-29 18:25:51 +010037{
38}
39
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010040GpuWorkloadContext::~GpuWorkloadContext() = default;
41
42GpuWorkloadContext::GpuWorkloadContext(GpuWorkloadContext &&other) = default;
43
44GpuWorkloadContext &GpuWorkloadContext::operator=(GpuWorkloadContext &&other) = default;
45
SiCong Lif44bbc52022-08-29 18:25:51 +010046GpuTarget GpuWorkloadContext::gpu_target() const
47{
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010048 return _impl->cl_compile_context()->get_gpu_target();
SiCong Lif44bbc52022-08-29 18:25:51 +010049}
50
51GpuLanguage GpuWorkloadContext::gpu_language() const
52{
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010053 return _impl->gpu_language();
SiCong Lif44bbc52022-08-29 18:25:51 +010054}
55
56const CLCompileContext *GpuWorkloadContext::cl_compile_context() const
57{
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010058 return _impl->cl_compile_context();
59}
60
61void GpuWorkloadContext::register_user_tensor(ITensorInfo &tensor_info)
62{
63 _impl->register_user_tensor(tensor_info);
64}
65
66GpuWorkloadContext::Impl &GpuWorkloadContext::implementation()
67{
68 return *_impl;
69}
70
71const GpuWorkloadContext::Impl &GpuWorkloadContext::implementation() const
72{
73 return *_impl;
74}
75
76GpuWorkloadContext::Impl::Impl(GpuLanguage gpu_language, CLCompileContext *cl_compile_ctx)
SiCong Li23882a92023-06-28 09:49:45 +010077 : _gpu_language(gpu_language), _cl_compile_ctx(cl_compile_ctx), _next_tensor_id(1), _mem_map(), _managed_tensor_info()
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010078{
79}
80
81GpuLanguage GpuWorkloadContext::Impl::gpu_language() const
82{
83 return _gpu_language;
84}
85
86const CLCompileContext *GpuWorkloadContext::Impl::cl_compile_context() const
87{
SiCong Lif44bbc52022-08-29 18:25:51 +010088 return _cl_compile_ctx;
89}
90
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +010091const MemoryDescriptorMap &GpuWorkloadContext::Impl::mem_map() const
92{
93 return _mem_map;
94}
95
96void GpuWorkloadContext::Impl::register_user_tensor(ITensorInfo &tensor_info)
97{
98 ARM_COMPUTE_ERROR_ON(tensor_info.has_valid_id());
99
100 const auto tensor_id = next_tensor_id();
101
102 tensor_info.set_id(tensor_id);
103 _mem_map[tensor_id] = MemoryDescriptor{ MemoryType::User };
SiCong Li23882a92023-06-28 09:49:45 +0100104 // Save a *copy* of the user tensor info in workload context for future reference
105 // Note that this means if the user modifies the @p tensor_info, the change will not be reflected in the context
106 _managed_tensor_info.emplace(tensor_info.id(), std::make_unique<TensorInfo>(tensor_info));
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +0100107}
108
SiCong Li23882a92023-06-28 09:49:45 +0100109ITensorInfo *GpuWorkloadContext::Impl::create_virtual_tensor()
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +0100110{
SiCong Li23882a92023-06-28 09:49:45 +0100111 auto tensor_info = std::make_unique<TensorInfo>();
112 const auto tensor_id = -next_tensor_id();
113 tensor_info->set_id(tensor_id);
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +0100114 _mem_map[tensor_id] = MemoryDescriptor{ MemoryType::Virtual };
SiCong Li23882a92023-06-28 09:49:45 +0100115 auto inserted = _managed_tensor_info.emplace(tensor_info->id(), std::move(tensor_info));
116 return inserted.first->second.get();
117}
118
119ITensorInfo *GpuWorkloadContext::Impl::create_auxiliary_tensor(const ITensorInfo &itensor_info)
120{
121 auto tensor_info = std::make_unique<TensorInfo>(itensor_info);
122 const auto tensor_id = next_tensor_id();
123 tensor_info->set_id(tensor_id);
124 _mem_map[tensor_id] = MemoryDescriptor{ MemoryType::Auxiliary, AuxMemoryInfo{ tensor_info->total_size() } };
125 auto inserted = _managed_tensor_info.emplace(tensor_info->id(), std::move(tensor_info));
126 return inserted.first->second.get();
127}
128
129ITensorInfo *GpuWorkloadContext::Impl::get_tensor_info(ITensorInfo::Id id)
130{
131 return _managed_tensor_info.at(id).get();
132}
133
134const ITensorInfo *GpuWorkloadContext::Impl::get_tensor_info(ITensorInfo::Id id) const
135{
136 return _managed_tensor_info.at(id).get();
Viet-Hoa Do3fcf3dc2023-05-17 15:17:48 +0100137}
138
139ITensorInfo::Id GpuWorkloadContext::Impl::next_tensor_id()
140{
141 return _next_tensor_id++;
142}
143
SiCong Lif44bbc52022-08-29 18:25:51 +0100144} // namespace dynamic_fusion
145} // namespace experimental
146} // namespace arm_compute