-
Notifications
You must be signed in to change notification settings - Fork 1
/
hostReduce.hpp
245 lines (191 loc) · 7.83 KB
/
hostReduce.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2020 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#ifndef _HOST_REDUCE_HPP_
#define _HOST_REDUCE_HPP_
#include <vector>
#include <cassert>
static inline void
get_all_indexes(const std::vector<int>& dimLengths, int dim, std::vector<std::vector<int>>& indexes)
{
if(dim < dimLengths.size())
{
std::vector<std::vector<int>> updated_indexes;
if(dim == 0)
{
for(int i = 0; i < dimLengths[dim]; i++)
{
std::vector<int> index = {i};
updated_indexes.push_back(index);
};
}
else
{
// go through all the current indexes
for(const auto& index : indexes)
for(int i = 0; i < dimLengths[dim]; i++)
{
auto index_new = index; // explicit copying
index_new.push_back(i);
updated_indexes.push_back(index_new);
};
};
// update to the indexes (output)
indexes = updated_indexes;
// further to construct the indexes from the updated status
get_all_indexes(dimLengths, dim + 1, indexes);
};
};
static inline int get_offset_from_index(const std::vector<int>& strides,
const std::vector<int>& index)
{
int offset = 0;
assert(index.size() == strides.size());
for(int i = 0; i < index.size(); i++)
offset += strides[i] * index[i];
return (offset);
};
static inline int get_flatten_offset(const std::vector<int>& lengths, const std::vector<int>& index)
{
int offset = 0;
assert(lengths.size() == index.size() && lengths.size() > 0);
int len = lengths.size();
int stride = 1;
// for len==1, the loop is not executed
for(int i = len - 1; i > 0; i--)
{
offset += stride * index[i];
stride *= lengths[i];
};
offset += stride * index[0];
return (offset);
};
template <typename FloatType>
class summationHost
{
public:
summationHost() = default;
summationHost(const std::vector<int> inLengths_,
const std::vector<int> outLengths_,
const std::vector<int> inStrides_,
const std::vector<int> outStrides_,
const std::vector<int>& invariantDims_,
const std::vector<int>& toReduceDims_)
{
this->inLengths = inLengths_;
this->outLengths = outLengths_;
this->inStrides = inStrides_;
this->outStrides = outStrides_;
this->invariantDims = invariantDims_;
this->toReduceDims = toReduceDims_;
assert(this->inLengths.size() == this->outLengths.size());
assert(!this->toReduceDims.empty());
for(const auto dim : this->invariantDims)
this->invariantLengths.push_back(this->inLengths[dim]);
for(const auto dim : this->toReduceDims)
toReduceLengths.push_back(this->inLengths[dim]);
this->reduceAllDims = this->invariantDims.empty();
};
~summationHost(){};
private:
std::vector<int> inLengths;
std::vector<int> outLengths;
std::vector<int> inStrides;
std::vector<int> outStrides;
std::vector<int> invariantLengths;
std::vector<int> toReduceLengths;
std::vector<int> invariantDims;
std::vector<int> toReduceDims;
bool reduceAllDims;
public:
void Run(FloatType alpha, const FloatType* in_data, FloatType beta, FloatType* out_data)
{
if(reduceAllDims)
{
std::vector<std::vector<int>> indexes_1;
get_all_indexes(inLengths, 0, indexes_1); // generate the input indexes space
auto accuVal = static_cast<FloatType>(0.0f);
// go through indexes of the invariant dimensions
for(const auto& src_index : indexes_1)
{
auto src_offset = get_offset_from_index(this->inStrides, src_index);
auto currVal = in_data[src_offset];
accuVal += currVal;
};
// scale the accumulated value
if( alpha != static_cast<FloatType>(1.0f) )
accuVal *= alpha;
// scale the prior dst value and add it to the accumulated value
if( beta != static_cast<FloatType>(0.0f) )
accuVal += out_data[0] * beta;
// store the reduced value to dst location
out_data[0] = accuVal;
}
else
{
std::vector<std::vector<int>> indexes_1, indexes_2;
get_all_indexes(
this->invariantLengths, 0, indexes_1); // generate the invariant indexes space
get_all_indexes(
this->toReduceLengths, 0, indexes_2); // generate the toReduce indexes space
// go through indexes of the invariant dimensions
for(const auto& index_1 : indexes_1)
{
std::vector<int> src_index;
std::vector<int> dst_index;
src_index.resize(this->inLengths.size());
dst_index.resize(this->inLengths.size());
// initialize the src index
std::fill(dst_index.begin(), dst_index.end(), 0);
for(int k = 0; k < invariantDims.size(); k++)
dst_index[invariantDims[k]] = index_1[k];
int dst_offset = get_offset_from_index(this->outStrides, dst_index);
// generate the part of src index belonging to invariant dims
for(int k = 0; k < invariantDims.size(); k++)
src_index[invariantDims[k]] = index_1[k];
FloatType accuVal = static_cast<FloatType>(0.0f);
// go through indexes of the toReduce dimensions
for(const auto& index_2 : indexes_2)
{
// generate the part of src index belonging to toReduce dims
for(int k = 0; k < toReduceDims.size(); k++)
src_index[toReduceDims[k]] = index_2[k];
auto src_offset = get_offset_from_index(this->inStrides, src_index);
auto currVal = in_data[src_offset];
accuVal += currVal;
};
// scale the accumulated value
if( alpha != static_cast<FloatType>(1.0f) )
accuVal *= alpha;
// scale the prior dst value and add it to the accumulated value
if( beta != static_cast<FloatType>(0.0f) )
accuVal += out_data[dst_offset] * beta;
// store the reduced value to dst location
out_data[dst_offset] = accuVal;
};
};
};
};
#endif