Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 1d06204 | 2023-07-06 13:13:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021,2023 Arm Limited. |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 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/AclEntrypoints.h" |
Matthew Bentham | 1d06204 | 2023-07-06 13:13:59 +0000 | [diff] [blame] | 25 | #include "arm_compute/core/Error.h" |
| 26 | |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 27 | #include "src/common/IContext.h" |
| 28 | #include "src/common/utils/Macros.h" |
| 29 | #include "src/common/utils/Validate.h" |
| 30 | |
| 31 | #ifdef ARM_COMPUTE_CPU_ENABLED |
| 32 | #include "src/cpu/CpuContext.h" |
| 33 | #endif /* ARM_COMPUTE_CPU_ENABLED */ |
| 34 | |
| 35 | #ifdef ARM_COMPUTE_OPENCL_ENABLED |
| 36 | #include "src/gpu/cl/ClContext.h" |
| 37 | #endif /* ARM_COMPUTE_OPENCL_ENABLED */ |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | template <typename ContextType> |
| 42 | arm_compute::IContext *create_backend_ctx(const AclContextOptions *options) |
| 43 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 44 | return new (std::nothrow) ContextType(options); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool is_target_valid(AclTarget target) |
| 48 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | return arm_compute::utils::is_in(target, {AclCpu, AclGpuOcl}); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool are_context_options_valid(const AclContextOptions *options) |
| 53 | { |
| 54 | ARM_COMPUTE_ASSERT_NOT_NULLPTR(options); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | return arm_compute::utils::is_in(options->mode, {AclPreferFastRerun, AclPreferFastStart}); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | arm_compute::IContext *create_context(AclTarget target, const AclContextOptions *options) |
| 59 | { |
Georgios Pinitas | c693947 | 2021-05-07 17:56:31 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_UNUSED(options); |
| 61 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | switch (target) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 63 | { |
| 64 | #ifdef ARM_COMPUTE_CPU_ENABLED |
| 65 | case AclCpu: |
| 66 | return create_backend_ctx<arm_compute::cpu::CpuContext>(options); |
| 67 | #endif /* ARM_COMPUTE_CPU_ENABLED */ |
| 68 | #ifdef ARM_COMPUTE_OPENCL_ENABLED |
| 69 | case AclGpuOcl: |
| 70 | return create_backend_ctx<arm_compute::gpu::opencl::ClContext>(options); |
| 71 | #endif /* ARM_COMPUTE_OPENCL_ENABLED */ |
| 72 | default: |
| 73 | return nullptr; |
| 74 | } |
| 75 | return nullptr; |
| 76 | } |
| 77 | } // namespace |
| 78 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | extern "C" AclStatus AclCreateContext(AclContext *external_ctx, AclTarget target, const AclContextOptions *options) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 80 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | if (!is_target_valid(target)) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 82 | { |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 83 | ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Target is invalid!"); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 84 | return AclUnsupportedTarget; |
| 85 | } |
| 86 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | if (options != nullptr && !are_context_options_valid(options)) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 88 | { |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 89 | ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Context options are invalid!"); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 90 | return AclInvalidArgument; |
| 91 | } |
| 92 | |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 93 | auto ctx = create_context(target, options); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | if (ctx == nullptr) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 95 | { |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 96 | ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Couldn't allocate internal resources for context creation!"); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 97 | return AclOutOfMemory; |
| 98 | } |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 99 | *external_ctx = ctx; |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 100 | |
| 101 | return AclSuccess; |
| 102 | } |
| 103 | |
| 104 | extern "C" AclStatus AclDestroyContext(AclContext external_ctx) |
| 105 | { |
| 106 | using namespace arm_compute; |
| 107 | |
| 108 | IContext *ctx = get_internal(external_ctx); |
| 109 | |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 110 | StatusCode status = detail::validate_internal_context(ctx); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 111 | ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status); |
| 112 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 113 | if (ctx->refcount() != 0) |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 114 | { |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 115 | ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Context has references on it that haven't been released!"); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 116 | // TODO: Fix the refcount with callback when reaches 0 |
| 117 | } |
| 118 | |
| 119 | delete ctx; |
| 120 | |
| 121 | return utils::as_cenum<AclStatus>(status); |
| 122 | } |