xref: /aosp_15_r20/external/ComputeLibrary/src/common/IContext.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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  */
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 
33 struct AclContext_
34 {
35     arm_compute::detail::Header header{ arm_compute::detail::ObjectType::Context, nullptr };
36 
37 protected:
38     AclContext_()  = default;
39     ~AclContext_() = default;
40 };
41 
42 namespace arm_compute
43 {
44 // Forward declarations
45 class ITensorV2;
46 class IQueue;
47 class IOperator;
48 
49 /**< Context interface */
50 class IContext : public AclContext_
51 {
52 public:
IContext(Target target)53     IContext(Target target)
54         : AclContext_(), _target(target), _refcount(0)
55     {
56     }
57     /** Virtual Destructor */
~IContext()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      */
type()66     Target type() const
67     {
68         return _target;
69     }
70     /** Increment context refcount */
inc_ref()71     void inc_ref() const
72     {
73         ++_refcount;
74     }
75     /** Decrement context refcount */
dec_ref()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      */
refcount()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      */
is_valid()92     bool is_valid() const
93     {
94         return header.type == detail::ObjectType::Context;
95     }
96     /** 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;
104     /** 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;
111     virtual std::tuple<IOperator *, StatusCode> create_activation(const AclTensorDescriptor &src,
112                                                                   const AclTensorDescriptor     &dst,
113                                                                   const AclActivationDescriptor &act,
114                                                                   bool                           is_validate) = 0;
115 
116 private:
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  */
get_internal(AclContext ctx)127 inline IContext *get_internal(AclContext ctx)
128 {
129     return static_cast<IContext *>(ctx);
130 }
131 
132 namespace 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  */
validate_internal_context(const IContext * ctx)140 inline 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 */
152