Compute Library
 22.11
AclContext.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/IContext.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 
38 namespace
39 {
40 template <typename ContextType>
41 arm_compute::IContext *create_backend_ctx(const AclContextOptions *options)
42 {
43  return new(std::nothrow) ContextType(options);
44 }
45 
46 bool is_target_valid(AclTarget target)
47 {
48  return arm_compute::utils::is_in(target, { AclCpu, AclGpuOcl });
49 }
50 
51 bool are_context_options_valid(const AclContextOptions *options)
52 {
54  return arm_compute::utils::is_in(options->mode, { AclPreferFastRerun, AclPreferFastStart });
55 }
56 
57 arm_compute::IContext *create_context(AclTarget target, const AclContextOptions *options)
58 {
59  ARM_COMPUTE_UNUSED(options);
60 
61  switch(target)
62  {
63 #ifdef ARM_COMPUTE_CPU_ENABLED
64  case AclCpu:
65  return create_backend_ctx<arm_compute::cpu::CpuContext>(options);
66 #endif /* ARM_COMPUTE_CPU_ENABLED */
67 #ifdef ARM_COMPUTE_OPENCL_ENABLED
68  case AclGpuOcl:
69  return create_backend_ctx<arm_compute::gpu::opencl::ClContext>(options);
70 #endif /* ARM_COMPUTE_OPENCL_ENABLED */
71  default:
72  return nullptr;
73  }
74  return nullptr;
75 }
76 } // namespace
77 
78 extern "C" AclStatus AclCreateContext(AclContext *external_ctx,
79  AclTarget target,
80  const AclContextOptions *options)
81 {
82  if(!is_target_valid(target))
83  {
84  ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Target is invalid!");
85  return AclUnsupportedTarget;
86  }
87 
88  if(options != nullptr && !are_context_options_valid(options))
89  {
90  ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Context options are invalid!");
91  return AclInvalidArgument;
92  }
93 
94  auto ctx = create_context(target, options);
95  if(ctx == nullptr)
96  {
97  ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Couldn't allocate internal resources for context creation!");
98  return AclOutOfMemory;
99  }
100  *external_ctx = ctx;
101 
102  return AclSuccess;
103 }
104 
105 extern "C" AclStatus AclDestroyContext(AclContext external_ctx)
106 {
107  using namespace arm_compute;
108 
109  IContext *ctx = get_internal(external_ctx);
110 
113 
114  if(ctx->refcount() != 0)
115  {
116  ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL("Context has references on it that haven't been released!");
117  // TODO: Fix the refcount with callback when reaches 0
118  }
119 
120  delete ctx;
121 
122  return utils::as_cenum<AclStatus>(status);
123 }
#define ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status)
Definition: Macros.h:29
OpenCL target for GPU.
Definition: AclTypes.h:66
AclTarget
Definition: AclTypes.h:63
AclStatus AclDestroyContext(AclContext external_ctx)
Destroy a given context object.
Definition: AclContext.cpp:105
StatusCode validate_internal_context(const IContext *ctx)
Check if an internal context is valid.
Definition: IContext.h:140
Context interface.
Definition: IContext.h:50
Copyright (c) 2017-2022 Arm Limited.
AclExecutionMode mode
Execution mode to use.
Definition: AclTypes.h:136
Call failed as invalid argument was passed.
Definition: AclTypes.h:57
Call failed as an invalid backend was requested.
Definition: AclTypes.h:55
Cpu target that uses SIMD extensions.
Definition: AclTypes.h:65
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
int refcount() const
Reference counter accessor.
Definition: IContext.h:84
Call succeeded, leading to valid state for all involved objects/data.
Definition: AclTypes.h:51
StatusCode
Definition: Types.h:32
bool is_in(E check, std::initializer_list< E > list)
Check if the given value is in the given enum value list.
Definition: Utils.h:73
Call failed due to failure to allocate resources.
Definition: AclTypes.h:53
AclStatus AclCreateContext(AclContext *external_ctx, AclTarget target, const AclContextOptions *options)
__cplusplus
Definition: AclContext.cpp:78
#define ARM_COMPUTE_ASSERT_NOT_NULLPTR(ptr)
Definition: Validate.h:38
#define ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL(msg)
Log an error message to the logger with function name before the message.
Definition: Log.h:88
IContext * get_internal(AclContext ctx)
Extract internal representation of a Context.
Definition: IContext.h:127
AclStatus
Definition: AclTypes.h:49