blob: ee7eac768831e52cd034c84bca8d48b2694c2e75 [file] [log] [blame]
Georgios Pinitas3f26ef42021-02-23 10:01:33 +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_ITENSOR_H_
25#define SRC_COMMON_ITENSOR_H_
26
27#include "src/common/IContext.h"
28#include "src/common/utils/Validate.h"
29
30struct AclTensor_
31{
32 arm_compute::detail::Header header{ arm_compute::detail::ObjectType::Tensor, nullptr };
33
34protected:
35 AclTensor_() = default;
36 ~AclTensor_() = default;
37};
38
39namespace arm_compute
40{
41// Forward declaration
42class ITensor;
43
44/** Base class specifying the tensor interface */
45class ITensorV2 : public AclTensor_
46{
47public:
48 /** Explict Operator Constructor
49 *
50 * @param[in] ctx Context to be used by the operator
51 */
52 explicit ITensorV2(IContext *ctx)
53 : AclTensor_()
54 {
55 ARM_COMPUTE_ASSERT_NOT_NULLPTR(ctx);
56 this->header.ctx = ctx;
57 this->header.ctx->inc_ref();
58 }
59 /** Destructor */
60 virtual ~ITensorV2()
61 {
62 this->header.ctx->dec_ref();
63 this->header.type = detail::ObjectType::Invalid;
64 };
65 /** Checks if a queue is valid
66 *
67 * @return True if successful otherwise false
68 */
69 bool is_valid() const
70 {
71 return this->header.type == detail::ObjectType::Tensor;
72 };
73 /** Map tensor to a host pointer
74 *
75 * @return A pointer to the underlying backing memory if successful else nullptr
76 */
77 virtual void *map() = 0;
78 /** Unmap tensor
79 *
80 * @return AclStatus A status cod
81 */
82 virtual StatusCode unmap() = 0;
83 /** Import external memory handle
84 *
85 * @param[in] handle Memory to import
86 * @param[in] type Type of imported memory
87 *
88 * @return Status code
89 */
90 virtual StatusCode import(void *handle, ImportMemoryType type) = 0;
91 /** Get the legacy tensor object
92 *
93 * @return The legacy underlying tensor object
94 */
95 virtual arm_compute::ITensor *tensor() = 0;
96};
97
98/** Extract internal representation of a Tensor
99 *
100 * @param[in] tensor Opaque tensor pointer
101 *
102 * @return The internal representation as an ITensor
103 */
104inline ITensorV2 *get_internal(AclTensor tensor)
105{
106 return static_cast<ITensorV2 *>(tensor);
107}
108
109namespace detail
110{
111/** Check if an internal tensor is valid
112 *
113 * @param[in] tensor Internal tensor to check
114 *
115 * @return A status code
116 */
117inline StatusCode validate_internal_tensor(const ITensorV2 *tensor)
118{
119 if(tensor == nullptr || !tensor->is_valid())
120 {
121 ARM_COMPUTE_LOG_ERROR_ACL("[ITensorV2]: Invalid tensor object");
122 return StatusCode::InvalidArgument;
123 }
124 return StatusCode::Success;
125}
126} // namespace detail
127} // namespace arm_compute
128#endif /* SRC_COMMON_ITENSOR_H_ */