blob: aecec6757ace7871b6460d56a9e5b731c486eae1 [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
Matteo Martincigh21350152018-11-28 16:22:22 +000012#include <DataLayoutIndexed.hpp>
13
telsoa014fcda012018-03-09 14:13:49 +000014namespace armnn
15{
16
telsoa01c577f2c2018-08-31 09:22:23 +010017// Utility class providing access to raw tensor memory based on indices along each dimension.
telsoa014fcda012018-03-09 14:13:49 +000018template <typename DataType>
19class TensorBufferArrayView
20{
21public:
Matteo Martincigh21350152018-11-28 16:22:22 +000022 TensorBufferArrayView(const TensorShape& shape, DataType* data,
23 armnnUtils::DataLayoutIndexed dataLayout = DataLayout::NCHW)
telsoa014fcda012018-03-09 14:13:49 +000024 : m_Shape(shape)
25 , m_Data(data)
James Conroy59540822018-10-11 12:39:05 +010026 , m_DataLayout(dataLayout)
telsoa014fcda012018-03-09 14:13:49 +000027 {
Matteo Martincigh97a06fd2018-10-16 16:17:34 +010028 BOOST_ASSERT(m_Shape.GetNumDimensions() == 4);
telsoa014fcda012018-03-09 14:13:49 +000029 }
30
31 DataType& Get(unsigned int b, unsigned int c, unsigned int h, unsigned int w) const
32 {
James Conroy59540822018-10-11 12:39:05 +010033 BOOST_ASSERT( b < m_Shape[0] || ( m_Shape[0] == 0 && b == 0 ) );
34 BOOST_ASSERT( c < m_Shape[m_DataLayout.GetChannelsIndex()] ||
35 ( m_Shape[m_DataLayout.GetChannelsIndex()] == 0 && c == 0) );
36 BOOST_ASSERT( h < m_Shape[m_DataLayout.GetHeightIndex()] ||
37 ( m_Shape[m_DataLayout.GetHeightIndex()] == 0 && h == 0) );
38 BOOST_ASSERT( w < m_Shape[m_DataLayout.GetWidthIndex()] ||
39 ( m_Shape[m_DataLayout.GetWidthIndex()] == 0 && w == 0) );
telsoa014fcda012018-03-09 14:13:49 +000040
Matteo Martincigh97a06fd2018-10-16 16:17:34 +010041 // Offset the given indices appropriately depending on the data layout.
42 switch (m_DataLayout.GetDataLayout())
43 {
44 case DataLayout::NHWC:
45 b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index;
46 h *= m_Shape[m_DataLayout.GetWidthIndex()] * m_Shape[m_DataLayout.GetChannelsIndex()];
47 w *= m_Shape[m_DataLayout.GetChannelsIndex()];
48 // c stays unchanged
49 break;
50 case DataLayout::NCHW:
51 default:
52 b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index;
53 c *= m_Shape[m_DataLayout.GetHeightIndex()] * m_Shape[m_DataLayout.GetWidthIndex()];
54 h *= m_Shape[m_DataLayout.GetWidthIndex()];
55 // w stays unchanged
56 break;
57 }
58
59 // Get the value using the correct offset.
60 return m_Data[b + c + h + w];
telsoa014fcda012018-03-09 14:13:49 +000061 }
62
63private:
Matteo Martincigh21350152018-11-28 16:22:22 +000064 const TensorShape m_Shape;
65 DataType* m_Data;
66 armnnUtils::DataLayoutIndexed m_DataLayout;
telsoa014fcda012018-03-09 14:13:49 +000067};
68
69} //namespace armnn