blob: 9b8ffea6198ee520d4617c0588fdf3b1ff28be78 [file] [log] [blame]
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00001/*
Matthew Bentham1d062042023-07-06 13:13:59 +00002 * Copyright (c) 2021,2023 Arm Limited.
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00003 *
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"
25
Matthew Bentham1d062042023-07-06 13:13:59 +000026#include "arm_compute/core/Error.h"
27
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000028#include "src/common/IContext.h"
29#include "src/common/utils/Macros.h"
30#include "src/common/utils/Validate.h"
31
32#ifdef ARM_COMPUTE_CPU_ENABLED
33#include "src/cpu/CpuContext.h"
34#endif /* ARM_COMPUTE_CPU_ENABLED */
35
36#ifdef ARM_COMPUTE_OPENCL_ENABLED
37#include "src/gpu/cl/ClContext.h"
38#endif /* ARM_COMPUTE_OPENCL_ENABLED */
39
40namespace
41{
42template <typename ContextType>
43arm_compute::IContext *create_backend_ctx(const AclContextOptions *options)
44{
45 return new(std::nothrow) ContextType(options);
46}
47
48bool is_target_valid(AclTarget target)
49{
50 return arm_compute::utils::is_in(target, { AclCpu, AclGpuOcl });
51}
52
53bool are_context_options_valid(const AclContextOptions *options)
54{
55 ARM_COMPUTE_ASSERT_NOT_NULLPTR(options);
56 return arm_compute::utils::is_in(options->mode, { AclPreferFastRerun, AclPreferFastStart });
57}
58
59arm_compute::IContext *create_context(AclTarget target, const AclContextOptions *options)
60{
Georgios Pinitasc6939472021-05-07 17:56:31 +010061 ARM_COMPUTE_UNUSED(options);
62
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000063 switch(target)
64 {
65#ifdef ARM_COMPUTE_CPU_ENABLED
66 case AclCpu:
67 return create_backend_ctx<arm_compute::cpu::CpuContext>(options);
68#endif /* ARM_COMPUTE_CPU_ENABLED */
69#ifdef ARM_COMPUTE_OPENCL_ENABLED
70 case AclGpuOcl:
71 return create_backend_ctx<arm_compute::gpu::opencl::ClContext>(options);
72#endif /* ARM_COMPUTE_OPENCL_ENABLED */
73 default:
74 return nullptr;
75 }
76 return nullptr;
77}
78} // namespace
79
Georgios Pinitas41648142021-08-03 08:24:00 +010080extern "C" AclStatus AclCreateContext(AclContext *external_ctx,
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000081 AclTarget target,
82 const AclContextOptions *options)
83{
84 if(!is_target_valid(target))
85 {
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000086 ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Target is invalid!");
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000087 return AclUnsupportedTarget;
88 }
89
90 if(options != nullptr && !are_context_options_valid(options))
91 {
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000092 ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Context options are invalid!");
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000093 return AclInvalidArgument;
94 }
95
Georgios Pinitas41648142021-08-03 08:24:00 +010096 auto ctx = create_context(target, options);
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000097 if(ctx == nullptr)
98 {
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000099 ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Couldn't allocate internal resources for context creation!");
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000100 return AclOutOfMemory;
101 }
Georgios Pinitas41648142021-08-03 08:24:00 +0100102 *external_ctx = ctx;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000103
104 return AclSuccess;
105}
106
107extern "C" AclStatus AclDestroyContext(AclContext external_ctx)
108{
109 using namespace arm_compute;
110
111 IContext *ctx = get_internal(external_ctx);
112
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000113 StatusCode status = detail::validate_internal_context(ctx);
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000114 ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status);
115
116 if(ctx->refcount() != 0)
117 {
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000118 ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Context has references on it that haven't been released!");
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000119 // TODO: Fix the refcount with callback when reaches 0
120 }
121
122 delete ctx;
123
124 return utils::as_cenum<AclStatus>(status);
125}