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