blob: 53504d63653f87c8f07eb2d5ef5233eb8b286cc0 [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
Matteo Martincighada9cb22018-10-17 14:45:07 +01006#pragma once
7
telsoa014fcda012018-03-09 14:13:49 +00008#include <armnn/Tensor.hpp>
9
10#include <boost/assert.hpp>
11
12namespace armnn
13{
14
telsoa01c577f2c2018-08-31 09:22:23 +010015// Utility class providing access to raw tensor memory based on indices along each dimension.
telsoa014fcda012018-03-09 14:13:49 +000016template <typename DataType>
17class TensorBufferArrayView
18{
19public:
James Conroy59540822018-10-11 12:39:05 +010020 TensorBufferArrayView(const TensorShape& shape, DataType* data, DataLayoutIndexed dataLayout = DataLayout::NCHW)
telsoa014fcda012018-03-09 14:13:49 +000021 : m_Shape(shape)
22 , m_Data(data)
James Conroy59540822018-10-11 12:39:05 +010023 , m_DataLayout(dataLayout)
telsoa014fcda012018-03-09 14:13:49 +000024 {
Matteo Martincigh97a06fd2018-10-16 16:17:34 +010025 BOOST_ASSERT(m_Shape.GetNumDimensions() == 4);
telsoa014fcda012018-03-09 14:13:49 +000026 }
27
28 DataType& Get(unsigned int b, unsigned int c, unsigned int h, unsigned int w) const
29 {
James Conroy59540822018-10-11 12:39:05 +010030 BOOST_ASSERT( b < m_Shape[0] || ( m_Shape[0] == 0 && b == 0 ) );
31 BOOST_ASSERT( c < m_Shape[m_DataLayout.GetChannelsIndex()] ||
32 ( m_Shape[m_DataLayout.GetChannelsIndex()] == 0 && c == 0) );
33 BOOST_ASSERT( h < m_Shape[m_DataLayout.GetHeightIndex()] ||
34 ( m_Shape[m_DataLayout.GetHeightIndex()] == 0 && h == 0) );
35 BOOST_ASSERT( w < m_Shape[m_DataLayout.GetWidthIndex()] ||
36 ( m_Shape[m_DataLayout.GetWidthIndex()] == 0 && w == 0) );
telsoa014fcda012018-03-09 14:13:49 +000037
Matteo Martincigh97a06fd2018-10-16 16:17:34 +010038 // Offset the given indices appropriately depending on the data layout.
39 switch (m_DataLayout.GetDataLayout())
40 {
41 case DataLayout::NHWC:
42 b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index;
43 h *= m_Shape[m_DataLayout.GetWidthIndex()] * m_Shape[m_DataLayout.GetChannelsIndex()];
44 w *= m_Shape[m_DataLayout.GetChannelsIndex()];
45 // c stays unchanged
46 break;
47 case DataLayout::NCHW:
48 default:
49 b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index;
50 c *= m_Shape[m_DataLayout.GetHeightIndex()] * m_Shape[m_DataLayout.GetWidthIndex()];
51 h *= m_Shape[m_DataLayout.GetWidthIndex()];
52 // w stays unchanged
53 break;
54 }
55
56 // Get the value using the correct offset.
57 return m_Data[b + c + h + w];
telsoa014fcda012018-03-09 14:13:49 +000058 }
59
60private:
61 const TensorShape m_Shape;
James Conroy59540822018-10-11 12:39:05 +010062 DataType* m_Data;
63 DataLayoutIndexed m_DataLayout;
telsoa014fcda012018-03-09 14:13:49 +000064};
65
66} //namespace armnn