Compute Library
 21.11
AclOpenClExt.cpp
Go to the documentation of this file.
1 /*
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  */
25 
26 #include "src/common/ITensorV2.h"
27 #include "src/common/Types.h"
28 #include "src/gpu/cl/ClContext.h"
29 #include "src/gpu/cl/ClQueue.h"
30 
32 
33 #include "support/Cast.h"
34 
35 extern "C" AclStatus AclGetClContext(AclContext external_ctx, cl_context *opencl_context)
36 {
37  using namespace arm_compute;
38  IContext *ctx = get_internal(external_ctx);
39 
41  {
43  }
44 
45  if(ctx->type() != Target::GpuOcl)
46  {
48  }
49 
50  if(opencl_context == nullptr)
51  {
53  }
54 
55  *opencl_context = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClContext *>(ctx)->cl_ctx().get();
56 
57  return AclStatus::AclSuccess;
58 }
59 
60 extern "C" AclStatus AclSetClContext(AclContext external_ctx, cl_context opencl_context)
61 {
62  using namespace arm_compute;
63  IContext *ctx = get_internal(external_ctx);
64 
66  {
68  }
69 
70  if(ctx->type() != Target::GpuOcl)
71  {
73  }
74 
75  if(ctx->refcount() != 0)
76  {
78  }
79 
80  auto cl_ctx = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClContext *>(ctx);
81  if(!cl_ctx->set_cl_ctx(::cl::Context(opencl_context)))
82  {
84  }
85 
86  return AclStatus::AclSuccess;
87 }
88 
89 extern "C" AclStatus AclGetClDevice(AclContext external_ctx, cl_device_id *opencl_device)
90 {
91  using namespace arm_compute;
92  IContext *ctx = get_internal(external_ctx);
93 
95  {
97  }
98 
99  if(ctx->type() != Target::GpuOcl)
100  {
102  }
103 
104  if(opencl_device == nullptr)
105  {
107  }
108 
109  *opencl_device = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClContext *>(ctx)->cl_dev().get();
110 
111  return AclStatus::AclSuccess;
112 }
113 
114 extern "C" AclStatus AclGetClQueue(AclQueue external_queue, cl_command_queue *opencl_queue)
115 {
116  using namespace arm_compute;
117  IQueue *queue = get_internal(external_queue);
118 
120  {
122  }
123 
124  if(queue->header.ctx->type() != Target::GpuOcl)
125  {
127  }
128 
129  if(opencl_queue == nullptr)
130  {
132  }
133 
134  *opencl_queue = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClQueue *>(queue)->cl_queue().get();
135 
136  return AclStatus::AclSuccess;
137 }
138 
139 extern "C" AclStatus AclSetClQueue(AclQueue external_queue, cl_command_queue opencl_queue)
140 {
141  using namespace arm_compute;
142  IQueue *queue = get_internal(external_queue);
143 
145  {
147  }
148 
149  if(queue->header.ctx->type() != Target::GpuOcl)
150  {
152  }
153 
154  auto cl_queue = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClQueue *>(queue);
155  if(!cl_queue->set_cl_queue(::cl::CommandQueue(opencl_queue)))
156  {
158  }
159 
160  return AclStatus::AclSuccess;
161 }
162 
163 extern "C" AclStatus AclGetClMem(AclTensor external_tensor, cl_mem *opencl_mem)
164 {
165  using namespace arm_compute;
166  ITensorV2 *tensor = get_internal(external_tensor);
167 
169  {
171  }
172 
173  if(tensor->header.ctx->type() != Target::GpuOcl)
174  {
176  }
177 
178  if(opencl_mem == nullptr)
179  {
181  }
182 
183  auto cl_tensor = utils::cast::polymorphic_downcast<arm_compute::ICLTensor *>(tensor->tensor());
184  *opencl_mem = cl_tensor->cl_buffer().get();
185 
186  return AclStatus::AclSuccess;
187 }
Base class specifying the queue interface.
Definition: IQueue.h:41
Call failed during execution.
Definition: AclTypes.h:52
AclStatus AclGetClMem(AclTensor external_tensor, cl_mem *opencl_mem)
Extract the underlying OpenCL memory object by a given Compute Library tensor object.
Base class specifying the tensor interface.
Definition: ITensorV2.h:45
arm_compute::detail::Header header
Definition: IQueue.h:31
Call failed as invalid argument was passed.
Definition: AclTypes.h:56
StatusCode validate_internal_context(const IContext *ctx)
Check if an internal context is valid.
Definition: IContext.h:140
StatusCode validate_internal_tensor(const ITensorV2 *tensor)
Check if an internal tensor is valid.
Definition: ITensorV2.h:129
Context interface.
Definition: IContext.h:50
Copyright (c) 2017-2021 Arm Limited.
AclStatus AclSetClQueue(AclQueue external_queue, cl_command_queue opencl_queue)
Set the underlying OpenCL queue to be used by a given Compute Library queue object.
Call failed as invalid argument was passed.
Definition: AclTypes.h:57
arm_compute::detail::Header header
Definition: ITensorV2.h:32
AclStatus AclSetClContext(AclContext external_ctx, cl_context opencl_context)
Set the underlying OpenCL context to be used by a given Compute Library context object.
StatusCode validate_internal_queue(const IQueue *queue)
Check if an internal queue is valid.
Definition: IQueue.h:89
int refcount() const
Reference counter accessor.
Definition: IContext.h:84
Call failed as configuration is unsupported.
Definition: AclTypes.h:58
AclStatus AclGetClQueue(AclQueue external_queue, cl_command_queue *opencl_queue)
Extract the underlying OpenCL queue used by a given Compute Library queue object. ...
Call succeeded, leading to valid state for all involved objects/data.
Definition: AclTypes.h:51
AclStatus AclGetClContext(AclContext external_ctx, cl_context *opencl_context)
Extract the underlying OpenCL context used by a given Compute Library context object.
Target type() const
Target type accessor.
Definition: IContext.h:66
AclStatus AclGetClDevice(AclContext external_ctx, cl_device_id *opencl_device)
Extract the underlying OpenCL device id used by a given Compute Library context object.
virtual arm_compute::ITensor * tensor() const =0
Get the legacy tensor object.
IContext * get_internal(AclContext ctx)
Extract internal representation of a Context.
Definition: IContext.h:127
AclStatus
Definition: AclTypes.h:49