Debian Codegraph @

src/data/aslDataUtilities.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 "aslDataUtilities.h"
25 #include "aslGenerators.h"
26 #include "acl/aclElementBase.h"
27 #include "math/aslVectorsDynamicLengthOperations.h"
28 #include "acl/aclGenerators.h"
29 #include "acl/aclMath/aclVectorOfElements.h"
30 #include "acl/DataTypes/aclIndex.h"
31 #include "acl/DataTypes/aclConstant.h"
32 #include "acl/acl.h"
33 #include "acl/Kernels/aclKernel.h"
34 #include "math/aslIndex2Position.h"
35 #include "acl/aclUtilities.h"
36 #include "acl/Operators/aclElementIfElse.h"
37
38
39 namespace asl
40 {
41
42 void checkDimensionsNumCompatibility(AbstractData & source,
43 const AVec<int> & size)
44 {
45 if (nD(source.getBlock()) != nD(size))
46 errorMessage("uploadToLocalMem() - dimensions mismatch");
47 }
48
49
50 void checkTessellability(AbstractData & source,
51 const AVec<int> & size)
52 {
53 const AVec<int> s(source.getBlock().getSize());
54 for (unsigned int i(0); i < nD(s); ++i)
55 if (s[i] % size[i] != 0)
56 errorMessage("uploadToLocalMem() - size of source is not evenly divisible by the block\'s size");
57 }
58
59
60 acl::VectorOfElements uploadToLocalMem(AbstractData & source,
61 const AVec<int> & size,
62 unsigned int groupSize,
63 acl::Kernel & kernel)
64 {
65 checkDimensionsNumCompatibility(source,size);
66 checkTessellability(source, size);
67
68 AVec<int> groupTableSize(divisionOfElements(source.getBlock().getSize(),
69 size));
70
71 unsigned int totalPointsNum = productOfElements(size);
72 unsigned int uploadsPerWorkItem = totalPointsNum / groupSize;
73 unsigned int uploadsPerWorkItemRemainder = totalPointsNum % groupSize;
74
75 unsigned int componentsNum = source.getEContainer().size();
76 acl::VectorOfElements uploadedData(componentsNum);
77
78 copy(generateVELocalArray(totalPointsNum,
79 source.getEContainer()[0]->getTypeID(),
80 componentsNum),
81 uploadedData);
82
83 Block localBlock(size);
84 Index2PositionDiscreteACL ind(localBlock, false);
85 Block groupTableBlock(groupTableSize);
86 Index2PositionDiscreteACL indGroup(groupTableBlock);
87
88 acl::VectorOfElements transformVec(acl::generateVEConstant(source.getBlock().c2iTransformVector));
89
90 acl::VectorOfElements offset(productOfElements(acl::excerpt(indGroup.positionWithInit,
91 acl::generateVEGroupID()),
92 acl::generateVEConstant(size)));
93
94 acl::VectorOfElements singleUpload(uploadedData = acl::excerpt(source.getEContainer(),
95 (ind.positionWithInit + offset) * transformVec));
96
97 acl::VectorOfElements index(acl::generateVEIndex(groupSize));
98
99 for (unsigned int i = 0; i < uploadsPerWorkItem; ++i)
100 {
101 kernel << (excerpt(singleUpload,
102 groupSize * i + index));
103 }
104
105 acl::Element uploadsPerWorkItemRem(new acl::Constant<cl_int>(uploadsPerWorkItemRemainder));
106 acl::Element indexRemainder(new acl::Index());
107
108 using namespace acl::elementOperators;
109 acl::Element ife(ifElse(indexRemainder < uploadsPerWorkItemRem,
110 excerpt(singleUpload,
111 groupSize * uploadsPerWorkItem + index),
112 {}));
113 kernel.addExpression(ife);
114
115 return uploadedData;
116 }
117
118 SPDataWrapperACL generateSubData(SPDataWrapperACL d, AVec<int> a, AVec<int> b)
119 {
120 const Block & bIn(d->getBlock());
121 unsigned int nd(nD(a));
122 Block bOut(b - a + AVec<int>(nd,1), bIn.dx, bIn.position+bIn.dx*AVec<>(a));
123
124 acl::VectorOfElements c(d->getEContainer().size());
125
126 Index2PositionDiscreteACL t2p(bOut);
127 acl::VectorOfElements newIndex((t2p.positionWithInit + a) *
128 bIn.c2iTransformVector);
129
130 copy(acl::excerpt(d->getEContainer(), newIndex), c);
131
132 return generateDataContainer_SP(bOut,c);
133 }
134
135
136 } // namespace asl