blob: 61c1311905d4110964a7dda9a2b499eaa7daebb1 [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#pragma once
7
8#include "RefWorkloadUtils.hpp"
9
David Beckac42efd2018-09-26 17:41:13 +010010#include <backends/WorkloadData.hpp>
telsoa014fcda012018-03-09 14:13:49 +000011#include <armnn/Tensor.hpp>
12
13namespace armnn
14{
15
16template <typename DataType>
17void Merger(const MergerQueueDescriptor& data)
18{
19 const TensorInfo& outputInfo0 = GetTensorInfo(data.m_Outputs[0]);
20
21 for (unsigned int index = 0 ; index < outputInfo0.GetNumElements(); ++index)
22 {
surmeh013537c2c2018-05-18 16:31:43 +010023 unsigned int indices[MaxNumOfTensorDimensions] = { 0 };
telsoa014fcda012018-03-09 14:13:49 +000024
25 unsigned int indexRemainder = index;
26 unsigned int dimensionStride = outputInfo0.GetNumElements();
27
28 for (unsigned int i=0; i<outputInfo0.GetNumDimensions(); i++)
29 {
30 dimensionStride /= outputInfo0.GetShape()[i];
telsoa01c577f2c2018-08-31 09:22:23 +010031 indices[i] = indexRemainder / dimensionStride; // Use integer division to round down.
telsoa014fcda012018-03-09 14:13:49 +000032 indexRemainder -= indices[i] * dimensionStride;
33 }
34
35 for (unsigned int viewIdx = 0; viewIdx < data.m_ViewOrigins.size(); ++viewIdx)
36 {
37 MergerQueueDescriptor::ViewOrigin const& view = data.m_ViewOrigins[viewIdx];
38
telsoa01c577f2c2018-08-31 09:22:23 +010039 //Split view extents are defined by the size of (the corresponding) input tensor.
telsoa014fcda012018-03-09 14:13:49 +000040 const TensorInfo& inputInfo = GetTensorInfo(data.m_Inputs[viewIdx]);
surmeh01bceff2f2018-03-29 16:29:27 +010041 BOOST_ASSERT(inputInfo.GetNumDimensions() == outputInfo0.GetNumDimensions());
telsoa014fcda012018-03-09 14:13:49 +000042
telsoa01c577f2c2018-08-31 09:22:23 +010043 // Check all dimensions to see if this element is inside the given input view.
telsoa014fcda012018-03-09 14:13:49 +000044 bool insideView = true;
45 for (unsigned int i=0; i<inputInfo.GetNumDimensions(); i++)
46 {
47 if (indices[i] < view.m_Origin[i])
48 {
49 insideView = false;
50 }
51 if (indices[i] >= view.m_Origin[i] + inputInfo.GetShape()[i])
52 {
53 insideView = false;
54 }
55 }
56
57 if (insideView)
58 {
59 unsigned int inIndex = 0;
60 unsigned int dimensionStride = 1;
61
62 for (unsigned int i = inputInfo.GetNumDimensions(); i-- > 0;)
63 {
64 inIndex += dimensionStride * (indices[i] - view.m_Origin[i]);
65 dimensionStride *= inputInfo.GetShape()[i];
66 }
67
telsoa01c577f2c2018-08-31 09:22:23 +010068 //We are within the view, copy input data to the output corresponding to this view.
telsoa014fcda012018-03-09 14:13:49 +000069 (GetOutputTensorData<DataType>(0, data))[index] =
70 (GetInputTensorData<DataType>(viewIdx, data))[inIndex];
71
telsoa01c577f2c2018-08-31 09:22:23 +010072 //What should we do if input views overlap on the output tensor?
73 //We could error, take the average, or shm else...
74 //For now just stop after finding first view (input) that matches.
telsoa014fcda012018-03-09 14:13:49 +000075 break;
76 }
77 }
78 }
79}
80
81} //namespace armnn