blob: 8e42cf551039de8cfba74667f18df4d27b834fb4 [file] [log] [blame]
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00001/*
2 * Copyright (c) 2021 Arm Limited.
3 *
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 */
24#include "arm_compute/AclOpenClExt.h"
25
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026#include "arm_compute/core/CL/ICLTensor.h"
27
Sang-Hoon Parkc6fcfb42021-03-31 15:18:16 +010028#include "src/common/ITensorV2.h"
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000029#include "src/common/Types.h"
30#include "src/gpu/cl/ClContext.h"
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000031#include "src/gpu/cl/ClQueue.h"
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000032#include "support/Cast.h"
33
34extern "C" AclStatus AclGetClContext(AclContext external_ctx, cl_context *opencl_context)
35{
36 using namespace arm_compute;
37 IContext *ctx = get_internal(external_ctx);
38
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 if (detail::validate_internal_context(ctx) != StatusCode::Success)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000040 {
41 return AclStatus::AclInvalidArgument;
42 }
43
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044 if (ctx->type() != Target::GpuOcl)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000045 {
46 return AclStatus::AclInvalidTarget;
47 }
48
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010049 if (opencl_context == nullptr)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000050 {
51 return AclStatus::AclInvalidArgument;
52 }
53
54 *opencl_context = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClContext *>(ctx)->cl_ctx().get();
55
56 return AclStatus::AclSuccess;
57}
58
59extern "C" AclStatus AclSetClContext(AclContext external_ctx, cl_context opencl_context)
60{
61 using namespace arm_compute;
62 IContext *ctx = get_internal(external_ctx);
63
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 if (detail::validate_internal_context(ctx) != StatusCode::Success)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000065 {
66 return AclStatus::AclInvalidArgument;
67 }
68
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 if (ctx->type() != Target::GpuOcl)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000070 {
71 return AclStatus::AclInvalidTarget;
72 }
73
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 if (ctx->refcount() != 0)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000075 {
76 return AclStatus::AclUnsupportedConfig;
77 }
78
79 auto cl_ctx = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClContext *>(ctx);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 if (!cl_ctx->set_cl_ctx(::cl::Context(opencl_context)))
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000081 {
82 return AclStatus::AclRuntimeError;
83 }
84
85 return AclStatus::AclSuccess;
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000086}
87
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000088extern "C" AclStatus AclGetClDevice(AclContext external_ctx, cl_device_id *opencl_device)
89{
90 using namespace arm_compute;
91 IContext *ctx = get_internal(external_ctx);
92
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093 if (detail::validate_internal_context(ctx) != StatusCode::Success)
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000094 {
95 return AclStatus::AclInvalidArgument;
96 }
97
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 if (ctx->type() != Target::GpuOcl)
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000099 {
100 return AclStatus::AclInvalidTarget;
101 }
102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 if (opencl_device == nullptr)
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000104 {
105 return AclStatus::AclInvalidArgument;
106 }
107
108 *opencl_device = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClContext *>(ctx)->cl_dev().get();
109
110 return AclStatus::AclSuccess;
111}
112
113extern "C" AclStatus AclGetClQueue(AclQueue external_queue, cl_command_queue *opencl_queue)
114{
115 using namespace arm_compute;
116 IQueue *queue = get_internal(external_queue);
117
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 if (detail::validate_internal_queue(queue) != StatusCode::Success)
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000119 {
120 return AclStatus::AclInvalidArgument;
121 }
122
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 if (queue->header.ctx->type() != Target::GpuOcl)
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000124 {
125 return AclStatus::AclInvalidTarget;
126 }
127
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 if (opencl_queue == nullptr)
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000129 {
130 return AclStatus::AclInvalidArgument;
131 }
132
133 *opencl_queue = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClQueue *>(queue)->cl_queue().get();
134
135 return AclStatus::AclSuccess;
136}
137
138extern "C" AclStatus AclSetClQueue(AclQueue external_queue, cl_command_queue opencl_queue)
139{
140 using namespace arm_compute;
141 IQueue *queue = get_internal(external_queue);
142
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 if (detail::validate_internal_queue(queue) != StatusCode::Success)
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000144 {
145 return AclStatus::AclInvalidArgument;
146 }
147
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 if (queue->header.ctx->type() != Target::GpuOcl)
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000149 {
150 return AclStatus::AclInvalidTarget;
151 }
152
153 auto cl_queue = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClQueue *>(queue);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 if (!cl_queue->set_cl_queue(::cl::CommandQueue(opencl_queue)))
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000155 {
156 return AclStatus::AclRuntimeError;
157 }
158
159 return AclStatus::AclSuccess;
160}
161
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000162extern "C" AclStatus AclGetClMem(AclTensor external_tensor, cl_mem *opencl_mem)
163{
164 using namespace arm_compute;
165 ITensorV2 *tensor = get_internal(external_tensor);
166
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 if (detail::validate_internal_tensor(tensor) != StatusCode::Success)
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000168 {
169 return AclStatus::AclInvalidArgument;
170 }
171
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 if (tensor->header.ctx->type() != Target::GpuOcl)
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000173 {
174 return AclStatus::AclInvalidTarget;
175 }
176
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 if (opencl_mem == nullptr)
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000178 {
179 return AclStatus::AclInvalidArgument;
180 }
181
182 auto cl_tensor = utils::cast::polymorphic_downcast<arm_compute::ICLTensor *>(tensor->tensor());
183 *opencl_mem = cl_tensor->cl_buffer().get();
184
185 return AclStatus::AclSuccess;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100186}