Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 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 | */ |
| 24 | #include "arm_compute/AclOpenClExt.h" |
| 25 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | |
Sang-Hoon Park | c6fcfb4 | 2021-03-31 15:18:16 +0100 | [diff] [blame] | 28 | #include "src/common/ITensorV2.h" |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 29 | #include "src/common/Types.h" |
| 30 | #include "src/gpu/cl/ClContext.h" |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 31 | #include "src/gpu/cl/ClQueue.h" |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 32 | #include "support/Cast.h" |
| 33 | |
| 34 | extern "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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | if (detail::validate_internal_context(ctx) != StatusCode::Success) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 40 | { |
| 41 | return AclStatus::AclInvalidArgument; |
| 42 | } |
| 43 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 44 | if (ctx->type() != Target::GpuOcl) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 45 | { |
| 46 | return AclStatus::AclInvalidTarget; |
| 47 | } |
| 48 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | if (opencl_context == nullptr) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 50 | { |
| 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 | |
| 59 | extern "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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | if (detail::validate_internal_context(ctx) != StatusCode::Success) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 65 | { |
| 66 | return AclStatus::AclInvalidArgument; |
| 67 | } |
| 68 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | if (ctx->type() != Target::GpuOcl) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 70 | { |
| 71 | return AclStatus::AclInvalidTarget; |
| 72 | } |
| 73 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | if (ctx->refcount() != 0) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 75 | { |
| 76 | return AclStatus::AclUnsupportedConfig; |
| 77 | } |
| 78 | |
| 79 | auto cl_ctx = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClContext *>(ctx); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | if (!cl_ctx->set_cl_ctx(::cl::Context(opencl_context))) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 81 | { |
| 82 | return AclStatus::AclRuntimeError; |
| 83 | } |
| 84 | |
| 85 | return AclStatus::AclSuccess; |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 88 | extern "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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 93 | if (detail::validate_internal_context(ctx) != StatusCode::Success) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 94 | { |
| 95 | return AclStatus::AclInvalidArgument; |
| 96 | } |
| 97 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | if (ctx->type() != Target::GpuOcl) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 99 | { |
| 100 | return AclStatus::AclInvalidTarget; |
| 101 | } |
| 102 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | if (opencl_device == nullptr) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 104 | { |
| 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 | |
| 113 | extern "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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | if (detail::validate_internal_queue(queue) != StatusCode::Success) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 119 | { |
| 120 | return AclStatus::AclInvalidArgument; |
| 121 | } |
| 122 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | if (queue->header.ctx->type() != Target::GpuOcl) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 124 | { |
| 125 | return AclStatus::AclInvalidTarget; |
| 126 | } |
| 127 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | if (opencl_queue == nullptr) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 129 | { |
| 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 | |
| 138 | extern "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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | if (detail::validate_internal_queue(queue) != StatusCode::Success) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 144 | { |
| 145 | return AclStatus::AclInvalidArgument; |
| 146 | } |
| 147 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 148 | if (queue->header.ctx->type() != Target::GpuOcl) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 149 | { |
| 150 | return AclStatus::AclInvalidTarget; |
| 151 | } |
| 152 | |
| 153 | auto cl_queue = utils::cast::polymorphic_downcast<arm_compute::gpu::opencl::ClQueue *>(queue); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | if (!cl_queue->set_cl_queue(::cl::CommandQueue(opencl_queue))) |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 155 | { |
| 156 | return AclStatus::AclRuntimeError; |
| 157 | } |
| 158 | |
| 159 | return AclStatus::AclSuccess; |
| 160 | } |
| 161 | |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 162 | extern "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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 167 | if (detail::validate_internal_tensor(tensor) != StatusCode::Success) |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 168 | { |
| 169 | return AclStatus::AclInvalidArgument; |
| 170 | } |
| 171 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 172 | if (tensor->header.ctx->type() != Target::GpuOcl) |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 173 | { |
| 174 | return AclStatus::AclInvalidTarget; |
| 175 | } |
| 176 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 177 | if (opencl_mem == nullptr) |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 178 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 186 | } |