Compute Library
 22.11
IContext.h
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  */
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"
30 
31 #include <atomic>
32 
34 {
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:
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  }
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  */
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  */
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 */
Base class specifying the queue interface.
Definition: IQueue.h:41
Base class specifying the tensor interface.
Definition: ITensorV2.h:45
StatusCode validate_internal_context(const IContext *ctx)
Check if an internal context is valid.
Definition: IContext.h:140
#define ARM_COMPUTE_LOG_ERROR_ACL(msg)
Log an error message to the logger.
Definition: Log.h:77
virtual ~IContext()
Virtual Destructor.
Definition: IContext.h:58
Context interface.
Definition: IContext.h:50
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
IContext(Target target)
Definition: IContext.h:53
void dec_ref() const
Decrement context refcount.
Definition: IContext.h:76
int refcount() const
Reference counter accessor.
Definition: IContext.h:84
T create_tensor(const TensorInfo &info, IRuntimeContext *ctx=nullptr)
Create and initialize a tensor of the given type.
Definition: Utils.h:427
StatusCode
Definition: Types.h:32
Target type() const
Target type accessor.
Definition: IContext.h:66
arm_compute::detail::Header header
Definition: IContext.h:35
void inc_ref() const
Increment context refcount.
Definition: IContext.h:71
IContext * get_internal(AclContext ctx)
Extract internal representation of a Context.
Definition: IContext.h:127
bool is_valid() const
Checks if an object is valid.
Definition: IContext.h:92