blob: b149073ab76f02f5503ac27ebe812e6668a44c59 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
6#include <armnn/Tensor.hpp>
7
8#include <boost/assert.hpp>
9
10namespace armnn
11{
12
telsoa01c577f2c2018-08-31 09:22:23 +010013// Utility class providing access to raw tensor memory based on indices along each dimension.
telsoa014fcda012018-03-09 14:13:49 +000014template <typename DataType>
15class TensorBufferArrayView
16{
17public:
James Conroy59540822018-10-11 12:39:05 +010018 TensorBufferArrayView(const TensorShape& shape, DataType* data, DataLayoutIndexed dataLayout = DataLayout::NCHW)
telsoa014fcda012018-03-09 14:13:49 +000019 : m_Shape(shape)
20 , m_Data(data)
James Conroy59540822018-10-11 12:39:05 +010021 , m_DataLayout(dataLayout)
telsoa014fcda012018-03-09 14:13:49 +000022 {
Matteo Martincigh97a06fd2018-10-16 16:17:34 +010023 BOOST_ASSERT(m_Shape.GetNumDimensions() == 4);
telsoa014fcda012018-03-09 14:13:49 +000024 }
25
26 DataType& Get(unsigned int b, unsigned int c, unsigned int h, unsigned int w) const
27 {
James Conroy59540822018-10-11 12:39:05 +010028 BOOST_ASSERT( b < m_Shape[0] || ( m_Shape[0] == 0 && b == 0 ) );
29 BOOST_ASSERT( c < m_Shape[m_DataLayout.GetChannelsIndex()] ||
30 ( m_Shape[m_DataLayout.GetChannelsIndex()] == 0 && c == 0) );
31 BOOST_ASSERT( h < m_Shape[m_DataLayout.GetHeightIndex()] ||
32 ( m_Shape[m_DataLayout.GetHeightIndex()] == 0 && h == 0) );
33 BOOST_ASSERT( w < m_Shape[m_DataLayout.GetWidthIndex()] ||
34 ( m_Shape[m_DataLayout.GetWidthIndex()] == 0 && w == 0) );
telsoa014fcda012018-03-09 14:13:49 +000035
Matteo Martincigh97a06fd2018-10-16 16:17:34 +010036 // Offset the given indices appropriately depending on the data layout.
37 switch (m_DataLayout.GetDataLayout())
38 {
39 case DataLayout::NHWC:
40 b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index;
41 h *= m_Shape[m_DataLayout.GetWidthIndex()] * m_Shape[m_DataLayout.GetChannelsIndex()];
42 w *= m_Shape[m_DataLayout.GetChannelsIndex()];
43 // c stays unchanged
44 break;
45 case DataLayout::NCHW:
46 default:
47 b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index;
48 c *= m_Shape[m_DataLayout.GetHeightIndex()] * m_Shape[m_DataLayout.GetWidthIndex()];
49 h *= m_Shape[m_DataLayout.GetWidthIndex()];
50 // w stays unchanged
51 break;
52 }
53
54 // Get the value using the correct offset.
55 return m_Data[b + c + h + w];
telsoa014fcda012018-03-09 14:13:49 +000056 }
57
58private:
59 const TensorShape m_Shape;
James Conroy59540822018-10-11 12:39:05 +010060 DataType* m_Data;
61 DataLayoutIndexed m_DataLayout;
telsoa014fcda012018-03-09 14:13:49 +000062};
63
64} //namespace armnn