Debian Codegraph @

src/acl/aclMath/aclReductionAlgGenerator.cxx

raw
1 /*
2 * Advanced Simulation Library <http://asl.org.il>
3 *
4 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5 *
6 *
7 * This file is part of Advanced Simulation Library (ASL).
8 *
9 * ASL is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, version 3 of the License.
12 *
13 * ASL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23
24 #include "aclReductionAlgGenerator.h"
25 #include "aclVectorOfElementsOperations.h"
26 #include <acl/acl.h>
27 #include <acl/DataTypes/aclArray.h>
28 #include <acl/aclGenerators.h>
29 #include <acl/Kernels/aclKernel.h>
30
31 namespace acl
32 {
33
34 inline const unsigned int nLocalGPU(unsigned int length, unsigned int nGroups)
35 {
36 return std::max(1u,std::min(8u, length/nGroups));
37 }
38 inline const unsigned int nLocalCPU(unsigned int length, unsigned int nGroups)
39 {
40 return 1;
41 }
42
43 unsigned int getLPerUnit(unsigned int length, unsigned int nUnits)
44 {
45 return ceil((double)length/nUnits);
46 }
47
48 unsigned int getNSaturatedUnits(unsigned int length, unsigned int nUnits)
49 {
50 return length/getLPerUnit(length,nUnits);
51 }
52
53 unsigned int getLLastUnit(unsigned int length, unsigned int nUnits)
54 {
55 return length % getLPerUnit(length,nUnits);
56 }
57
58 template <typename ResType, enum ReductionOperatorType Op> class GroupReduction
59 {
60 };
61
62 template <typename ResType> class GroupReduction<ResType, ROT_SUM>
63 {
64 public:
65 static void compute(std::vector<std::vector<ResType>> v, unsigned int l, asl::AVec<ResType> & res)
66 {
67 for(unsigned int i(0); i<v.size(); ++i)
68 {
69 vector<ResType> & vv(v[i]);
70 ResType r(vv[0]);
71 for(unsigned int j(1); j<l; ++j)
72 r+=vv[j];
73 res[i]=r;
74 }
75 }
76 static inline VectorOfElements op(VectorOfElements res, VectorOfElements v, acl::TypeID t)
77 {
78 return res+=v;
79 }
80 };
81 // template void GroupReduction<double, ROT_SUM>::compute(std::vector<std::vector<double>> v,
82 // asl::AVec<double> & res);
83 // template void GroupReduction<float, ROT_SUM>::compute(std::vector<std::vector<float>> v,
84 // asl::AVec<float> & res);
85
86 template <typename ResType> class GroupReduction<ResType, ROT_PRODUCT>
87 {
88 public:
89 static void compute(std::vector<std::vector<ResType>> v, unsigned int l, asl::AVec<ResType> & res)
90 {
91 for(unsigned int i(0); i<v.size(); ++i)
92 {
93 vector<ResType> & vv(v[i]);
94 ResType r(vv[0]);
95 for(unsigned int j(1); j<l; ++j)
96 r*=vv[j];
97 res[i]=r;
98 }
99 }
100 static inline VectorOfElements op(VectorOfElements res, VectorOfElements v, acl::TypeID t)
101 {
102 return res*=v;
103 }
104 };
105 // template void GroupReduction<double, ROT_PRODUCT>::compute(std::vector<std::vector<double>> v,
106 // asl::AVec<double> & res);
107 // template void GroupReduction<float, ROT_PRODUCT>::compute(std::vector<std::vector<float>> v,
108 // asl::AVec<float> & res);
109
110 template <typename ResType> class GroupReduction<ResType, ROT_MINIMUM>
111 {
112 public:
113 static void compute(std::vector<std::vector<ResType>> v, unsigned int l, asl::AVec<ResType> & res)
114 {
115 for(unsigned int i(0); i<v.size(); ++i)
116 {
117 vector<ResType> & vv(v[i]);
118 ResType r(vv[0]);
119 for(unsigned int j(1); j<l; ++j)
120 r=std::min(r,vv[j]);
121 res[i]=r;
122 }
123 }
124 static inline VectorOfElements op(VectorOfElements res, VectorOfElements v, acl::TypeID t)
125 {
126 return res=min(res,v,t);
127 }
128 };
129 // template void GroupReduction<double, ROT_MINIMUM>::compute(std::vector<std::vector<double>> v,
130 // asl::AVec<double> & res);
131 // template void GroupReduction<float, ROT_MINIMUM>::compute(std::vector<std::vector<float>> v,
132 // asl::AVec<float> & res);
133
134 template <typename ResType> class GroupReduction<ResType, ROT_MAXIMUM>
135 {
136 public:
137 static void compute(std::vector<std::vector<ResType>> v, unsigned int l, asl::AVec<ResType> & res)
138 {
139 for(unsigned int i(0); i<v.size(); ++i)
140 {
141 vector<ResType> & vv(v[i]);
142 ResType r(vv[0]);
143 for(unsigned int j(1); j<l; ++j)
144 r=std::max(r,vv[j]);
145 res[i]=r;
146 }
147 }
148 static inline VectorOfElements op(VectorOfElements res, VectorOfElements v, acl::TypeID t)
149 {
150 return res=max(res,v,t);
151 }
152 };
153 // template void GroupReduction<double, ROT_MAXIMUM>::compute(std::vector<std::vector<double>> v,
154 // asl::AVec<double> & res);
155 // template void GroupReduction<float, ROT_MAXIMUM>::compute(std::vector<std::vector<float>> v,
156 // asl::AVec<float> & res);
157
158 template <typename ResType, enum ReductionOperatorType Op>
159 void ReductionAlgGenerator<ResType, Op>::compute()
160 {
161 if(kernel)
162 kernel->compute();
163 unsigned int nC(ve.size());
164 unsigned int veLength(ve[0]->getSize());
165 unsigned int nSaturatedUnits(getNSaturatedUnits(veLength, nGroups*nLocal));
166 unsigned int l(std::min(nSaturatedUnits+1,nGroups*nLocal));
167 for (unsigned int i(0); i < nC; ++i)
168 acl::copy(groupResACL[i], groupRes[i]);
169 GroupReduction<ResType, Op>::compute(groupRes, l, res.v());
170 };
171 template void ReductionAlgGenerator<double, ROT_SUM>::compute();
172 template void ReductionAlgGenerator<float, ROT_SUM>::compute();
173 template void ReductionAlgGenerator<double, ROT_PRODUCT>::compute();
174 template void ReductionAlgGenerator<float, ROT_PRODUCT>::compute();
175 template void ReductionAlgGenerator<double, ROT_MINIMUM>::compute();
176 template void ReductionAlgGenerator<float, ROT_MINIMUM>::compute();
177 template void ReductionAlgGenerator<double, ROT_MAXIMUM>::compute();
178 template void ReductionAlgGenerator<float, ROT_MAXIMUM>::compute();
179
180 template <typename ResType, enum ReductionOperatorType Op>
181 void generateKernelCPU(VectorOfElements ve, VectorOfElements groupResACL, Kernel & k)
182 {
183 auto nGroups(k.getGroupsNumber());
184 int veLength(ve[0]->getSize());
185 unsigned int nLocal(nLocalCPU(veLength,nGroups));
186 unsigned int gSize(std::max(k.getSize(),nLocal));
187 auto type(getElementType(ve));
188 auto typeI(TYPE_SELECT[type]);
189 auto lVal(acl::generateVEPrivateVariable(ve.size(),type));
190 auto counter(acl::generateVEPrivateVariable(1,typeI));
191 auto counterEnd(acl::generateVEPrivateVariable(1,typeI));
192 int lPerGroup(getLPerUnit(veLength,nGroups));
193 int lLastGroup(getLLastUnit(veLength, nGroups));
194 int nSaturatedGroups(getNSaturatedUnits(veLength, nGroups));
195
196 k<<(counterEnd = select(generateVEConstant(lPerGroup),
197 generateVEConstant(lLastGroup),
198 generateVEGroupID()==nSaturatedGroups,
199 type));
200 k<<(lVal = select(excerpt(ve,lPerGroup*generateVEGroupID()), counterEnd>0,type));
201 // this line ensuers that the conputations will take place on the 1st item only
202 k<<(counterEnd = select(counterEnd, generateVEIndex(gSize)==0, type));
203 vector<Element> body;
204 body<<(GroupReduction<ResType, Op>::
205 op(lVal,excerpt(ve,
206 int(lPerGroup)*generateVEGroupID() +
207 counter),type));
208
209 auto loop(elementOperators::forLoop((counter = generateVEConstant(1))[0],
210 (counter < counterEnd)[0],
211 (counter += generateVEConstant(1))[0],
212 body));
213 k.addExpression(loop);
214 k<<(excerpt(groupResACL,generateVEGroupID()) = lVal);
215 }
216
217 template <typename ResType, enum ReductionOperatorType Op>
218 void generateKernelGPU(VectorOfElements ve, VectorOfElements groupResACL, Kernel & k)
219 {
220 auto nGroups(k.getGroupsNumber());
221 int veLength(ve[0]->getSize());
222 unsigned int nLocal(nLocalGPU(veLength,nGroups));
223 unsigned int gSize(std::max(k.getSize(),nLocal));
224 auto type(getElementType(ve));
225 auto typeI(TYPE_SELECT[type]);
226 auto lVal(acl::generateVEPrivateVariable(ve.size(),type));
227 auto counter(acl::generateVEPrivateVariable(1,typeI));
228 auto counterEnd(acl::generateVEPrivateVariable(1,typeI));
229 auto nUnits(nGroups*nLocal);
230 int lPerUnit(getLPerUnit(veLength,nUnits));
231 int lLastUnit(getLLastUnit(veLength, nUnits));
232 unsigned int nSaturatedUnits(getNSaturatedUnits(veLength, nUnits));
233 auto unitID(generateVEGroupID()*nLocal +generateVEIndex());
234
235 if(nSaturatedUnits<nUnits)
236 k<<(counterEnd = select(generateVEConstant(lPerUnit),
237 generateVEConstant(lLastUnit),
238 unitID==nSaturatedUnits,
239 typeI));
240 if((nSaturatedUnits+1)<nUnits)
241 k<<(counterEnd = select(counterEnd,
242 unitID<=nSaturatedUnits,
243 typeI));
244
245 // this line ensuers that the conputations will take place on first item only
246 k<<(counterEnd = select(counterEnd, generateVEIndex(gSize)<nLocal, type));
247 k<<(lVal = select(excerpt(ve,lPerUnit*unitID), counterEnd>0,type));
248 vector<Element> body;
249 body<<(GroupReduction<ResType, Op>::
250 op(lVal,excerpt(ve, lPerUnit*unitID + counter),type));
251
252 auto loop(elementOperators::forLoop((counter = generateVEConstant(1))[0],
253 (counter < counterEnd)[0],
254 (counter += generateVEConstant(1))[0],
255 body));
256 k.addExpression(loop);
257 k<<(excerpt(groupResACL,unitID) = lVal);
258 }
259
260 template <typename ResType, enum ReductionOperatorType Op>
261 void ReductionAlgGenerator<ResType, Op>::generateAlg(Kernel & k)
262 {
263 if(!k.getConfiguration().local)
264 asl::errorMessage("ReductionAlgGenerator::generateAlg: The kernel should be local");
265
266 nGroups= k.getGroupsNumber();
267 unsigned int veLength(ve[0]->getSize());
268 nLocal=(getDeviceType(ve[0]->getQueue())== CL_DEVICE_TYPE_CPU ?
269 nLocalCPU(veLength,nGroups) : nLocalGPU(veLength,nGroups));
270 for(unsigned int i(0); i<ve.size(); ++i)
271 groupRes[i]=std::vector<ResType>(nGroups*nLocal);
272 copy(generateVEData<ResType>(nGroups*nLocal,ve.size()),groupResACL);
273
274
275 switch (getDeviceType(ve[0]->getQueue()))
276 {
277 case CL_DEVICE_TYPE_CPU:
278 generateKernelCPU<ResType, Op>(ve,groupResACL,k);
279 k.setup();
280 break;
281 case CL_DEVICE_TYPE_GPU:
282 generateKernelGPU<ResType, Op>(ve,groupResACL,k);
283 k.setup();
284 break;
285 default: errorMessage("ReductionAlgGenerator: device type " +
286 numToStr(getDeviceType(k.getQueue())) + " is unknown!");
287 }
288 }
289
290 template void ReductionAlgGenerator<double, ROT_SUM>::generateAlg(Kernel & k);
291 template void ReductionAlgGenerator<float, ROT_SUM>::generateAlg(Kernel & k);
292 template void ReductionAlgGenerator<double, ROT_PRODUCT>::generateAlg(Kernel & k);
293 template void ReductionAlgGenerator<float, ROT_PRODUCT>::generateAlg(Kernel & k);
294 template void ReductionAlgGenerator<double, ROT_MINIMUM>::generateAlg(Kernel & k);
295 template void ReductionAlgGenerator<float, ROT_MINIMUM>::generateAlg(Kernel & k);
296 template void ReductionAlgGenerator<double, ROT_MAXIMUM>::generateAlg(Kernel & k);
297 template void ReductionAlgGenerator<float, ROT_MAXIMUM>::generateAlg(Kernel & k);
298
299 template <typename ResType, enum ReductionOperatorType Op>
300 void ReductionAlgGenerator<ResType, Op>::generateAlg()
301 {
302 KernelConfiguration kConf(KERNEL_BASIC);
303 kConf.local = true;
304
305 kernel=std::make_shared<Kernel>(kConf);
306 unsigned int groupsNumber = getNComputeUnits(ve[0]->getQueue());
307 kernel->setGroupsNumber(groupsNumber);
308
309 generateAlg(*kernel);
310 };
311 template void ReductionAlgGenerator<double, ROT_SUM>::generateAlg();
312 template void ReductionAlgGenerator<float, ROT_SUM>::generateAlg();
313 template void ReductionAlgGenerator<double, ROT_PRODUCT>::generateAlg();
314 template void ReductionAlgGenerator<float, ROT_PRODUCT>::generateAlg();
315 template void ReductionAlgGenerator<double, ROT_MINIMUM>::generateAlg();
316 template void ReductionAlgGenerator<float, ROT_MINIMUM>::generateAlg();
317 template void ReductionAlgGenerator<double, ROT_MAXIMUM>::generateAlg();
318 template void ReductionAlgGenerator<float, ROT_MAXIMUM>::generateAlg();
319
320 template <typename ResType, enum ReductionOperatorType Op>
321 ReductionAlgGenerator<ResType, Op>::ReductionAlgGenerator(VectorOfElements v):
322 ve(v),
323 res(asl::AVec<ResType>(ve.size())),
324 groupRes(ve.size())
325 {
326 }
327
328 template <typename ResType> std::shared_ptr<ReductionAlgGenerator<ResType, ROT_SUM>>
329 generateSumAlg(VectorOfElements v)
330 {
331 return make_shared<ReductionAlgGenerator<ResType, ROT_SUM>>(v);
332 }
333
334 template std::shared_ptr<ReductionAlgGenerator<double, ROT_SUM>>
335 generateSumAlg<double>(VectorOfElements v);
336 template std::shared_ptr<ReductionAlgGenerator<float, ROT_SUM>>
337 generateSumAlg<float>(VectorOfElements v);
338
339 template <typename ResType> std::shared_ptr<ReductionAlgGenerator<ResType, ROT_MINIMUM>>
340 generateMinAlg(VectorOfElements v)
341 {
342 return make_shared<ReductionAlgGenerator<ResType, ROT_MINIMUM>>(v);
343 }
344
345 template std::shared_ptr<ReductionAlgGenerator<double, ROT_MINIMUM>>
346 generateMinAlg<double>(VectorOfElements v);
347 template std::shared_ptr<ReductionAlgGenerator<float, ROT_MINIMUM>>
348 generateMinAlg<float>(VectorOfElements v);
349
350 template <typename ResType> std::shared_ptr<ReductionAlgGenerator<ResType, ROT_MAXIMUM>>
351 generateMaxAlg(VectorOfElements v)
352 {
353 return make_shared<ReductionAlgGenerator<ResType, ROT_MAXIMUM>>(v);
354 }
355
356 template std::shared_ptr<ReductionAlgGenerator<double, ROT_MAXIMUM>>
357 generateMaxAlg<double>(VectorOfElements v);
358 template std::shared_ptr<ReductionAlgGenerator<float, ROT_MAXIMUM>>
359 generateMaxAlg<float>(VectorOfElements v);
360
361 template <typename ResType> std::shared_ptr<ReductionAlgGenerator<ResType, ROT_PRODUCT>>
362 generateProductAlg(VectorOfElements v)
363 {
364 return make_shared<ReductionAlgGenerator<ResType, ROT_PRODUCT>>(v);
365 }
366
367 template std::shared_ptr<ReductionAlgGenerator<double, ROT_PRODUCT>>
368 generateProductAlg<double>(VectorOfElements v);
369 template std::shared_ptr<ReductionAlgGenerator<float, ROT_PRODUCT>>
370 generateProductAlg<float>(VectorOfElements v);
371
372 } // namespace acl