blob: bff70f3bafe1ca285c60155af3f476ee7b9b2ad6 [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/AclEntrypoints.h"
25
26#include "src/common/IContext.h"
27#include "src/common/utils/Macros.h"
28#include "src/common/utils/Validate.h"
29
30#ifdef ARM_COMPUTE_CPU_ENABLED
31#include "src/cpu/CpuContext.h"
32#endif /* ARM_COMPUTE_CPU_ENABLED */
33
34#ifdef ARM_COMPUTE_OPENCL_ENABLED
35#include "src/gpu/cl/ClContext.h"
36#endif /* ARM_COMPUTE_OPENCL_ENABLED */
37
38namespace
39{
40template <typename ContextType>
41arm_compute::IContext *create_backend_ctx(const AclContextOptions *options)
42{
43 return new(std::nothrow) ContextType(options);
44}
45
46bool is_target_valid(AclTarget target)
47{
48 return arm_compute::utils::is_in(target, { AclCpu, AclGpuOcl });
49}
50
51bool are_context_options_valid(const AclContextOptions *options)
52{
53 ARM_COMPUTE_ASSERT_NOT_NULLPTR(options);
54 return arm_compute::utils::is_in(options->mode, { AclPreferFastRerun, AclPreferFastStart });
55}
56
57arm_compute::IContext *create_context(AclTarget target, const AclContextOptions *options)
58{
59 switch(target)
60 {
61#ifdef ARM_COMPUTE_CPU_ENABLED
62 case AclCpu:
63 return create_backend_ctx<arm_compute::cpu::CpuContext>(options);
64#endif /* ARM_COMPUTE_CPU_ENABLED */
65#ifdef ARM_COMPUTE_OPENCL_ENABLED
66 case AclGpuOcl:
67 return create_backend_ctx<arm_compute::gpu::opencl::ClContext>(options);
68#endif /* ARM_COMPUTE_OPENCL_ENABLED */
69 default:
70 return nullptr;
71 }
72 return nullptr;
73}
74} // namespace
75
76extern "C" AclStatus AclCreateContext(AclContext *ctx,
77 AclTarget target,
78 const AclContextOptions *options)
79{
80 if(!is_target_valid(target))
81 {
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000082 ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Target is invalid!");
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000083 return AclUnsupportedTarget;
84 }
85
86 if(options != nullptr && !are_context_options_valid(options))
87 {
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000088 ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Context options are invalid!");
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000089 return AclInvalidArgument;
90 }
91
92 auto acl_ctx = create_context(target, options);
93 if(ctx == nullptr)
94 {
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000095 ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Couldn't allocate internal resources for context creation!");
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000096 return AclOutOfMemory;
97 }
98 *ctx = acl_ctx;
99
100 return AclSuccess;
101}
102
103extern "C" AclStatus AclDestroyContext(AclContext external_ctx)
104{
105 using namespace arm_compute;
106
107 IContext *ctx = get_internal(external_ctx);
108
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000109 StatusCode status = detail::validate_internal_context(ctx);
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000110 ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status);
111
112 if(ctx->refcount() != 0)
113 {
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000114 ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Context has references on it that haven't been released!");
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000115 // TODO: Fix the refcount with callback when reaches 0
116 }
117
118 delete ctx;
119
120 return utils::as_cenum<AclStatus>(status);
121}