Publishing R3
[platform/upstream/dldt.git] / inference-engine / thirdparty / ade / ade / source / memory_descriptor_ref.cpp
1 // Copyright (C) 2018 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5
6 #include <ostream>
7
8 #include "memory/memory_descriptor_ref.hpp"
9
10 #include "memory/memory_descriptor_view.hpp"
11 #include "memory/memory_descriptor.hpp"
12
13 #include "util/md_io.hpp"
14
15 #include <util/iota_range.hpp>
16
17 namespace ade
18 {
19
20 MemoryDescriptorRef::MemoryDescriptorRef()
21 {
22
23 }
24
25 MemoryDescriptorRef::MemoryDescriptorRef(MemoryDescriptorView& view):
26     m_parent(&view)
27 {
28     m_span.redim(view.span().dims_count());
29     for (auto i: util::iota(m_span.dims_count()))
30     {
31         m_span[i] = util::Span(0, view.span()[i].length());
32     }
33 }
34
35 MemoryDescriptorRef::MemoryDescriptorRef(MemoryDescriptorView& view,
36                                          const memory::DynMdSpan& span):
37     m_parent(&view),
38     m_span(span)
39 {
40
41 }
42
43 MemoryDescriptorRef::~MemoryDescriptorRef()
44 {
45
46 }
47
48 MemoryDescriptorView* MemoryDescriptorRef::getView()
49 {
50     return m_parent;
51 }
52
53 const MemoryDescriptorView* MemoryDescriptorRef::getView() const
54 {
55     return m_parent;
56 }
57
58 MemoryDescriptor*MemoryDescriptorRef::getDescriptor()
59 {
60     if (nullptr == m_parent)
61     {
62         return nullptr;
63     }
64     return m_parent->getDescriptor();
65 }
66
67 const MemoryDescriptor* MemoryDescriptorRef::getDescriptor() const
68 {
69     if (nullptr == m_parent)
70     {
71         return nullptr;
72     }
73     return m_parent->getDescriptor();
74 }
75
76 const memory::DynMdSpan& MemoryDescriptorRef::span() const
77 {
78     ASSERT(nullptr != *this);
79     return m_span;
80 }
81
82 memory::DynMdSize MemoryDescriptorRef::size() const
83 {
84     return span().size();
85 }
86
87 std::size_t MemoryDescriptorRef::elementSize() const
88 {
89     ASSERT(nullptr != getDescriptor());
90     return getDescriptor()->elementSize();
91 }
92
93 memory::DynMdSpan MemoryDescriptorRef::originSpan() const
94 {
95     ASSERT(nullptr != *this);
96     return m_span + m_parent->span().origin();
97 }
98
99 memory::DynMdView<void> MemoryDescriptorRef::getExternalView() const
100 {
101     ASSERT(nullptr != getDescriptor());
102     auto srcView = getDescriptor()->getExternalView();
103     if (nullptr == srcView)
104     {
105         return nullptr;
106     }
107     return srcView.slice(originSpan());
108 }
109
110 bool operator==(std::nullptr_t, const MemoryDescriptorRef& ref)
111 {
112     return ref.m_parent == nullptr;
113 }
114
115 bool operator==(const MemoryDescriptorRef& ref, std::nullptr_t)
116 {
117     return ref.m_parent == nullptr;
118 }
119
120 bool operator!=(std::nullptr_t, const MemoryDescriptorRef& ref)
121 {
122     return ref.m_parent != nullptr;
123 }
124
125 bool operator!=(const MemoryDescriptorRef& ref, std::nullptr_t)
126 {
127     return ref.m_parent != nullptr;
128 }
129
130 std::ostream& operator<<(std::ostream& os, const MemoryDescriptorRef& ref)
131 {
132     if (nullptr == ref)
133     {
134         os << static_cast<void*>(nullptr);
135     }
136     else
137     {
138         os << "{";
139         os << "span: " << ref.span() << ", ";
140         if (ref.originSpan() != ref.span())
141         {
142             os << std::endl << "origin span: " << ref.originSpan() << ", ";
143         }
144         os << std::endl << "view: " << ref.getView() << " (" << ref.getView()->span() << "), ";
145         os << std::endl << "descriptor: " << ref.getDescriptor() << " (" << ref.getDescriptor()->dimensions() << ") ";
146         os << "}";
147     }
148     return os;
149 }
150
151 }