blob: 98501280af3e715bc10bf96eccf4ae5b1a297be9 [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Jenkinsb3a371b2018-05-23 11:36:53 +01002 * Copyright (c) 2018 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +01003 *
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 */
Kaizen8938bd32017-09-28 14:38:23 +010024#include "arm_compute/graph/Tensor.h"
25
Jenkinsb3a371b2018-05-23 11:36:53 +010026namespace arm_compute
Kaizen8938bd32017-09-28 14:38:23 +010027{
Jenkinsb3a371b2018-05-23 11:36:53 +010028namespace graph
Kaizen8938bd32017-09-28 14:38:23 +010029{
Jenkinsb3a371b2018-05-23 11:36:53 +010030Tensor::Tensor(TensorID id, TensorDescriptor desc)
31 : _id(id), _desc(std::move(desc)), _handle(nullptr), _accessor(nullptr), _bound_edges()
Kaizen8938bd32017-09-28 14:38:23 +010032{
33}
34
Jenkinsb3a371b2018-05-23 11:36:53 +010035TensorID Tensor::id() const
Kaizen8938bd32017-09-28 14:38:23 +010036{
Jenkinsb3a371b2018-05-23 11:36:53 +010037 return _id;
Kaizen8938bd32017-09-28 14:38:23 +010038}
39
Jenkinsb3a371b2018-05-23 11:36:53 +010040TensorDescriptor &Tensor::desc()
Kaizen8938bd32017-09-28 14:38:23 +010041{
Jenkinsb3a371b2018-05-23 11:36:53 +010042 return _desc;
43}
44
45const TensorDescriptor &Tensor::desc() const
46{
47 return _desc;
48}
49
50void Tensor::set_handle(std::unique_ptr<ITensorHandle> backend_tensor)
51{
52 _handle = std::move(backend_tensor);
53}
54
55ITensorHandle *Tensor::handle()
56{
57 return _handle.get();
58}
59
60void Tensor::set_accessor(std::unique_ptr<ITensorAccessor> accessor)
61{
62 _accessor = std::move(accessor);
63}
64
65ITensorAccessor *Tensor::accessor()
66{
67 return _accessor.get();
Kaizen8938bd32017-09-28 14:38:23 +010068}
69
Jenkins52ba29e2018-08-29 15:32:11 +000070std::unique_ptr<ITensorAccessor> Tensor::extract_accessor()
71{
72 return std::move(_accessor);
73}
74
Kaizen8938bd32017-09-28 14:38:23 +010075bool Tensor::call_accessor()
76{
Jenkinsb3a371b2018-05-23 11:36:53 +010077 // Early exit guard
78 if(!_accessor || !_handle)
Kaizen8938bd32017-09-28 14:38:23 +010079 {
Jenkinsb3a371b2018-05-23 11:36:53 +010080 return false;
Kaizen8938bd32017-09-28 14:38:23 +010081 }
Jenkinsb3a371b2018-05-23 11:36:53 +010082
83 // Map tensor
84 _handle->map(true);
85
86 // Return in case of null backend buffer
87 if(_handle->tensor().buffer() == nullptr)
Kaizen8938bd32017-09-28 14:38:23 +010088 {
Jenkinsb3a371b2018-05-23 11:36:53 +010089 return false;
Kaizen8938bd32017-09-28 14:38:23 +010090 }
Jenkinsb3a371b2018-05-23 11:36:53 +010091
92 // Call accessor
Jenkins52ba29e2018-08-29 15:32:11 +000093 bool retval = _accessor->access_tensor(_handle->tensor());
Jenkinsb3a371b2018-05-23 11:36:53 +010094
95 // Unmap tensor
96 _handle->unmap();
97
Jenkins52ba29e2018-08-29 15:32:11 +000098 return retval;
Kaizen8938bd32017-09-28 14:38:23 +010099}
100
Jenkinsb3a371b2018-05-23 11:36:53 +0100101void Tensor::bind_edge(EdgeID eid)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000102{
Jenkinsb3a371b2018-05-23 11:36:53 +0100103 _bound_edges.insert(eid);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000104}
105
Jenkinsb3a371b2018-05-23 11:36:53 +0100106void Tensor::unbind_edge(EdgeID eid)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000107{
Jenkinsb3a371b2018-05-23 11:36:53 +0100108 _bound_edges.erase(eid);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000109}
110
Jenkinsb3a371b2018-05-23 11:36:53 +0100111const std::set<EdgeID> Tensor::bound_edges() const
Kaizen8938bd32017-09-28 14:38:23 +0100112{
Jenkinsb3a371b2018-05-23 11:36:53 +0100113 return _bound_edges;
Kaizen8938bd32017-09-28 14:38:23 +0100114}
Jenkinsb3a371b2018-05-23 11:36:53 +0100115} // namespace graph
116} // namespace arm_compute