blob: 65bb76744d800ba62fa5192c6702bcca04ef263a [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#ifndef SRC_COMMON_ICONTEXT_H
25#define SRC_COMMON_ICONTEXT_H
26
27#include "src/common/Types.h"
28#include "src/common/utils/Log.h"
29#include "src/common/utils/Object.h"
30
31#include <atomic>
Matthew Bentham1d062042023-07-06 13:13:59 +000032#include <tuple>
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000033
34struct AclContext_
35{
36 arm_compute::detail::Header header{ arm_compute::detail::ObjectType::Context, nullptr };
37
38protected:
39 AclContext_() = default;
40 ~AclContext_() = default;
41};
42
43namespace arm_compute
44{
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000045// Forward declarations
46class ITensorV2;
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000047class IQueue;
Georgios Pinitas41648142021-08-03 08:24:00 +010048class IOperator;
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000049
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000050/**< Context interface */
51class IContext : public AclContext_
52{
53public:
54 IContext(Target target)
55 : AclContext_(), _target(target), _refcount(0)
56 {
57 }
58 /** Virtual Destructor */
59 virtual ~IContext()
60 {
61 header.type = detail::ObjectType::Invalid;
62 };
63 /** Target type accessor
64 *
65 * @return Target that the context is associated with
66 */
67 Target type() const
68 {
69 return _target;
70 }
71 /** Increment context refcount */
72 void inc_ref() const
73 {
74 ++_refcount;
75 }
76 /** Decrement context refcount */
77 void dec_ref() const
78 {
79 --_refcount;
80 }
81 /** Reference counter accessor
82 *
83 * @return The number of references pointing to this object
84 */
85 int refcount() const
86 {
87 return _refcount;
88 }
89 /** Checks if an object is valid
90 *
91 * @return True if sucessful otherwise false
92 */
93 bool is_valid() const
94 {
95 return header.type == detail::ObjectType::Context;
96 }
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000097 /** Create a tensor object
98 *
99 * @param[in] desc Descriptor to use
100 * @param[in] allocate Flag to allocate tensor
101 *
102 * @return A pointer to the created tensor object
103 */
104 virtual ITensorV2 *create_tensor(const AclTensorDescriptor &desc, bool allocate) = 0;
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000105 /** Create a queue object
106 *
107 * @param[in] options Queue options to be used
108 *
109 * @return A pointer to the created queue object
110 */
111 virtual IQueue *create_queue(const AclQueueOptions *options) = 0;
Georgios Pinitas41648142021-08-03 08:24:00 +0100112 virtual std::tuple<IOperator *, StatusCode> create_activation(const AclTensorDescriptor &src,
113 const AclTensorDescriptor &dst,
114 const AclActivationDescriptor &act,
115 bool is_validate) = 0;
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000116
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000117private:
118 Target _target; /**< Target type of context */
119 mutable std::atomic<int> _refcount; /**< Reference counter */
120};
121
122/** Extract internal representation of a Context
123 *
124 * @param[in] ctx Opaque context pointer
125 *
126 * @return The internal representation as an IContext
127 */
128inline IContext *get_internal(AclContext ctx)
129{
130 return static_cast<IContext *>(ctx);
131}
132
133namespace detail
134{
135/** Check if an internal context is valid
136 *
137 * @param[in] ctx Internal context to check
138 *
139 * @return A status code
140 */
141inline StatusCode validate_internal_context(const IContext *ctx)
142{
143 if(ctx == nullptr || !ctx->is_valid())
144 {
145 ARM_COMPUTE_LOG_ERROR_ACL("Invalid context object");
146 return StatusCode::InvalidArgument;
147 }
148 return StatusCode::Success;
149}
150} // namespace detail
151} // namespace arm_compute
152#endif /* SRC_COMMON_ICONTEXT_H */