blob: 1294d05e08abbe6364493b9f7016482031463760 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// See LICENSE file in the project root for full license information.
4//
5
6#pragma once
7
8#include "RefWorkloadUtils.hpp"
9
10#include "backends/WorkloadData.hpp"
11
12#include <armnn/Tensor.hpp>
13
14namespace armnn
15{
16
17template <typename DataType>
18void Merger(const MergerQueueDescriptor& data)
19{
20 const TensorInfo& outputInfo0 = GetTensorInfo(data.m_Outputs[0]);
21
22 for (unsigned int index = 0 ; index < outputInfo0.GetNumElements(); ++index)
23 {
surmeh013537c2c2018-05-18 16:31:43 +010024 unsigned int indices[MaxNumOfTensorDimensions] = { 0 };
telsoa014fcda012018-03-09 14:13:49 +000025
26 unsigned int indexRemainder = index;
27 unsigned int dimensionStride = outputInfo0.GetNumElements();
28
29 for (unsigned int i=0; i<outputInfo0.GetNumDimensions(); i++)
30 {
31 dimensionStride /= outputInfo0.GetShape()[i];
telsoa01c577f2c2018-08-31 09:22:23 +010032 indices[i] = indexRemainder / dimensionStride; // Use integer division to round down.
telsoa014fcda012018-03-09 14:13:49 +000033 indexRemainder -= indices[i] * dimensionStride;
34 }
35
36 for (unsigned int viewIdx = 0; viewIdx < data.m_ViewOrigins.size(); ++viewIdx)
37 {
38 MergerQueueDescriptor::ViewOrigin const& view = data.m_ViewOrigins[viewIdx];
39
telsoa01c577f2c2018-08-31 09:22:23 +010040 //Split view extents are defined by the size of (the corresponding) input tensor.
telsoa014fcda012018-03-09 14:13:49 +000041 const TensorInfo& inputInfo = GetTensorInfo(data.m_Inputs[viewIdx]);
surmeh01bceff2f2018-03-29 16:29:27 +010042 BOOST_ASSERT(inputInfo.GetNumDimensions() == outputInfo0.GetNumDimensions());
telsoa014fcda012018-03-09 14:13:49 +000043
telsoa01c577f2c2018-08-31 09:22:23 +010044 // Check all dimensions to see if this element is inside the given input view.
telsoa014fcda012018-03-09 14:13:49 +000045 bool insideView = true;
46 for (unsigned int i=0; i<inputInfo.GetNumDimensions(); i++)
47 {
48 if (indices[i] < view.m_Origin[i])
49 {
50 insideView = false;
51 }
52 if (indices[i] >= view.m_Origin[i] + inputInfo.GetShape()[i])
53 {
54 insideView = false;
55 }
56 }
57
58 if (insideView)
59 {
60 unsigned int inIndex = 0;
61 unsigned int dimensionStride = 1;
62
63 for (unsigned int i = inputInfo.GetNumDimensions(); i-- > 0;)
64 {
65 inIndex += dimensionStride * (indices[i] - view.m_Origin[i]);
66 dimensionStride *= inputInfo.GetShape()[i];
67 }
68
telsoa01c577f2c2018-08-31 09:22:23 +010069 //We are within the view, copy input data to the output corresponding to this view.
telsoa014fcda012018-03-09 14:13:49 +000070 (GetOutputTensorData<DataType>(0, data))[index] =
71 (GetInputTensorData<DataType>(viewIdx, data))[inIndex];
72
telsoa01c577f2c2018-08-31 09:22:23 +010073 //What should we do if input views overlap on the output tensor?
74 //We could error, take the average, or shm else...
75 //For now just stop after finding first view (input) that matches.
telsoa014fcda012018-03-09 14:13:49 +000076 break;
77 }
78 }
79 }
80}
81
82} //namespace armnn