Peano 4
Loading...
Searching...
No Matches
Loop.cpp
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
3#include "Loop.h"
4
5namespace {
6 int dLinearisedNotOptimised( const tarch::la::Vector<Dimensions,int> & counter, int max ) {
7 int result = counter(Dimensions - 1);
8 #if !defined(SharedSYCL)
9 assertion2(counter(Dimensions - 1) >= 0, counter, max);
10 assertion2(counter(Dimensions - 1) < max, counter, max);
11 #endif
12 for (int d = Dimensions - 2; d >= 0; d--) {
13 #if !defined(SharedSYCL)
14 assertion2(counter(d) >= 0, counter(d), max);
15 assertion2(counter(d) < max, counter(d), max);
16 #endif
17 result = result * max + counter(d);
18 }
19 return result;
20 }
21
22
23 tarch::la::Vector<Dimensions,int> dDelinearisedNotOptimised(int value, int max ) {
24 assertion2(value >= 0, value, max);
25
27 for (int d=Dimensions-1; d>=0; d--) {
28 result(d) = value / tarch::la::aPowI(d,max);
29 value -= result(d) * tarch::la::aPowI(d,max);
30 }
31 return result;
32 }
33
34
35 #ifdef DloopOptimiseAggressive
36 //const int MaxMax = 4;
37 //const int MaxIndexOfLinearization = FOUR_POWER_D;
38 const int MaxMax = 5;
39 const int MaxIndexOfLinearization = FIVE_POWER_D;
40 int LookupTableDLinearised[MaxIndexOfLinearization*(MaxMax-1)];
41 tarch::la::Vector<Dimensions,int> LookupTableDDeLinearised[MaxIndexOfLinearization*(MaxMax-1)];
42
43 int getKeyForDLinearised(const tarch::la::Vector<Dimensions,int>& counter, int max) {
44 int result = 0;
45 int key = 1;
46
47 for (int d=0; d<Dimensions; d++) {
48 result += key * counter(d);
49 key *= MaxMax;
50 }
51
52 assertionEquals( key, MaxIndexOfLinearization );
53 // Nothing to be held for max=0 and max=1, so decrement the offset
54 result += (max-2)*MaxIndexOfLinearization;
55
56 return result;
57 }
58
59
60 int getKeyForDDeLinearised(int value, int max) {
61 // Nothing to be held for max=0 and max=1, so decrement the offset
62 return value + (max-2)*MaxIndexOfLinearization;
63 }
64 #endif
65}
66
67
69 #ifdef DloopOptimiseAggressive
70 for (int i=2; i<=MaxMax; i++) {
71 dfor(k,i) {
72 LookupTableDLinearised[getKeyForDLinearised(k,i)] = dLinearisedNotOptimised(k,i);
73 }
74 }
75 #endif
76}
77
78
80 #ifdef DloopOptimiseAggressive
81 for (int value=0; value<MaxIndexOfLinearization; value++) {
82 for (int i=2; i<=MaxMax; i++) {
83 LookupTableDDeLinearised[getKeyForDDeLinearised(value,i)] = dDelinearisedNotOptimised(value,i);
84 }
85 }
86 #endif
87}
88
89
91 return dLinearisedNotOptimised(counter,max);
92}
93
94
95int peano4::utils::dLinearised( const std::bitset<Dimensions>& counter ) {
96 int result = 0;
97 int base = 1;
98 for (int d=0; d<Dimensions; d++) {
99 result += (counter[d] ? 1 : 0) * base;
100 base *= 2;
101 }
102 return result;
103}
104
105
107 #ifdef DloopOptimiseAggressive
108 assertionEquals3(LookupTableDLinearised[getKeyForDLinearised(counter,max)], dLinearisedNotOptimised(counter,max), counter,max,getKeyForDLinearised(counter,max));
109 return LookupTableDLinearised[getKeyForDLinearised(counter,max)];
110 #else
111 return dLinearisedNotOptimised(counter,max);
112 #endif
113}
114
115
117 int result = counter(2 - 1);
118 assertion2(counter(2 - 1) >= 0, counter, max);
119 assertion2(counter(2 - 1) < max, counter, max);
120 for (int d = 2 - 2; d >= 0; d--) {
121 assertion3(counter(d) >= 0, counter, d, max);
122 assertion3(counter(d) < max, counter, d, max);
123 result = result * max + counter(d);
124 }
125 return result;
126}
127
128
130 int result = counter(3 - 1);
131 assertion2(counter(3 - 1) >= 0, counter, max);
132 assertion2(counter(3 - 1) < max, counter, max);
133 for (int d = 3 - 2; d >= 0; d--) {
134 assertion2(counter(d) >= 0, counter(d), max);
135 assertion2(counter(d) < max, counter(d), max);
136 result = result * max + counter(d);
137 }
138 return result;
139}
140
141
143 return dDelinearisedNotOptimised(value, max);
144}
145
147 #ifdef DloopOptimiseAggressive
148 for (int d=0; d<Dimensions; d++) {
149 assertionEquals2(LookupTableDDeLinearised[getKeyForDDeLinearised(value,max)](d), dDelinearisedNotOptimised(value,max)(d),value,max);
150 }
151 return LookupTableDDeLinearised[getKeyForDDeLinearised(value,max)];
152 #else
153 return dDelinearisedNotOptimised(value,max);
154 #endif
155}
156
157
159 counter(0)++;
160 for (int i=0; i<Dimensions-1; i++) {
161 if ( counter(i) >= max ) {
162 counter(i) = 0;
163 counter(i+1)++;
164 }
165 }
166}
167
168
170 for (int i = 0; i < Dimensions - 1; i++) {
171 assertion(counter(i) >= 0);
172 }
173
174 counter(0)--;
175 for (int i=0; i<Dimensions-1; i++) {
176 if ( counter(i) < 0 ) {
177 counter(i) = max-1;
178 counter(i+1)--;
179 }
180 }
181}
182
183
185 counter(0)++;
186 for (int i=0; i<Dimensions-1; i++) {
187 if ( counter(i) >= max(i) ) {
188 counter(i) = 0;
189 counter(i+1)++;
190 } else {
191 break;
192 }
193 }
194}
195
197 counter(0) += increment;
198 for (int i=0; i<Dimensions-1; i++) {
199 if ( counter(i) >= max ) {
200 counter(i) %= max;
201 counter(i+1) += increment;
202 } else {
203 break;
204 }
205 }
206}
207
208
210 counter(0) += increment;
211 for (int i=0; i<Dimensions-1; i++) {
212 while ( counter(i) >= max ) {
213 counter(i) -= max;
214 counter(i+1) ++;
215 }
216 }
217}
218
219
220void peano4::utils::dInc(tarch::la::Vector<Dimensions,int>& counter, int max, int doNotExamine) {
221 assertion( doNotExamine<Dimensions);
222 assertion( doNotExamine>=-1);
223
224 if (doNotExamine!=0) {
225 counter(0)++;
226 }
227 else {
228 counter(1)++;
229 }
230 for (int i=0; i<Dimensions-1; i++) {
231 if ( counter(i) >= max ) {
232 counter(i) = 0;
233 if ( doNotExamine != i+1) {
234 counter(i+1)++;
235 }
236 else {
237 if ( doNotExamine != Dimensions-1 ) {
238 counter(i+2)++;
239 }
240 else {
241 counter(Dimensions-1) = max;
242 }
243 }
244 }
245 }
246}
247
248
250 for (int i=0; i<Dimensions; i++) {
251 if ( direction[i] ) {
252 counter(i)++;
253 bool greater = counter(i) >= max;
254 if (greater && (i==Dimensions-1)) {
255 counter(i)=max;
256 return;
257 }
258 else if (greater) {
259 counter(i)--;
260 direction[i] = !direction[i];
261 }
262 else return;
263 }
264 else {
265 counter(i)--;
266 bool smaller = counter(i) < 0;
267 if (smaller && (i==Dimensions-1)) {
268 counter(i)=max;
269 return;
270 }
271 else if (smaller) {
272 counter(i)++;
273 direction[i] = !direction[i];
274 }
275 else return;
276 }
277 }
278}
279
280
282 int result = true;
283 for (int i=0; i<Dimensions; i++) {
284 assertion2( counter(i)<=max(i), counter,max );
285 assertion2( counter(i)>=0, counter,max );
286 result &= (counter(i)<max(i));
287 }
288 return result;
289}
290
291
293 int result = true;
294 for (int i=0; i<Dimensions; i++) {
295 #if !defined(SharedSYCL)
296 assertion2( counter(i)<=max, counter,max );
297 assertion2( counter(i)>=0, counter,max );
298 #endif
299 result &= (counter(i)<max);
300 }
301 return result;
302}
303
304
306 for (int i = Dimensions - 1; i >= 0; i--) {
307 if (counter(i) > max(i)) {
308 return false;
309 } else if (counter(i) < max(i)) {
310 return true;
311 }
312 }
313 return false;
314}
315
320
321
324 assertion2( dim >= 0, dim, value );
325 assertion2( dim < Dimensions, dim, value );
326 assertion2( value >= 0, dim, value );
327 result( dim ) = value;
328 return result;
329}
330
331
334 assertion2( max>0 && max<=3, max, direction );
335 for (int i=0; i<Dimensions; i++) {
336 if (direction[i]) {
337 result(i) = 0;
338 }
339 else {
340 result(i) = max-1;
341 }
342 }
343 return result;
344}
345
346
347
349 switch (arg) {
351 return "serial";
353 return "nested";
355 return "spread-out";
356 }
357 assertion(false);
358 return "<undef>";
359}
#define assertion2(expr, param0, param1)
#define assertionEquals3(lhs, rhs, a, b, c)
#define assertion3(expr, param0, param1, param2)
#define assertionEquals(lhs, rhs)
#define assertion(expr)
#define assertionEquals2(lhs, rhs, a, b)
we integrate over each cell and then take the sum across each of the cells We also consider the terms that enter the f$ k
std::string toString(peano4::utils::LoopPlacement arg)
Definition Loop.cpp:348
#define dfor(counter, max)
d-dimensional Loop
Definition Loop.h:308
int d2Linearised(const tarch::la::Vector< 2, int > &counter, int max)
Special 2d variant of dLinearised that works also if you compile with other dimensions.
Definition Loop.cpp:116
bool dCmpLinearOrder(const tarch::la::Vector< Dimensions, int > &counter, const tarch::la::Vector< Dimensions, int > &max)
Compares two vectors with regards to their linearised value.
Definition Loop.cpp:305
CPUGPUMethod void dInc(tarch::la::Vector< Dimensions, int > &counter, int max)
d-dimensional counterpart of increment operator
Definition Loop.cpp:158
std::bitset< Dimensions > LoopDirection
Is used by the z-loop.
Definition Loop.h:65
LoopPlacement
Guide loop-level parallelism.
Definition Loop.h:60
CPUGPUMethod tarch::la::Vector< Dimensions, int > dStartVector()
Construct start vector (0,0,....) for d-dimensional loop.
Definition Loop.cpp:316
void dIncByScalar(tarch::la::Vector< Dimensions, int > &counter, int max, int increment)
Perform a scalar increment of a vector: The operation equals a sequence of increment calls to dInc().
Definition Loop.cpp:209
void dIncByVector(tarch::la::Vector< Dimensions, int > &counter, int max, int increment)
Perform a d-dimensional increment by value increment: The first component of the counter is increment...
Definition Loop.cpp:196
void setupLookupTableForDLinearised()
Definition Loop.cpp:68
CPUGPUMethod int dLinearised(const tarch::la::Vector< Dimensions, int > &counter, int max)
Map d-dimensional vector onto integer index.
Definition Loop.cpp:106
tarch::la::Vector< Dimensions, int > dDelinearised(int value, int max)
Counterpart of dLinearised().
Definition Loop.cpp:146
int dLinearisedWithoutLookup(const tarch::la::Vector< Dimensions, int > &counter, int max)
Linearisation not Optimised.
Definition Loop.cpp:90
void dDec(tarch::la::Vector< Dimensions, int > &counter, int max)
d-dimensional counterpart of decrement operator
Definition Loop.cpp:169
CPUGPUMethod int dCmp(const tarch::la::Vector< Dimensions, int > &counter, int max)
Element-wise comparison for the for d-dimensional for loops.
Definition Loop.cpp:292
int d3Linearised(const tarch::la::Vector< 3, int > &counter, int max)
Special 3d variant of dLinearised that works also if you compile with other dimensions.
Definition Loop.cpp:129
void setupLookupTableForDDelinearised()
Definition Loop.cpp:79
tarch::la::Vector< Dimensions, int > dDelinearisedWithoutLookup(int value, int max)
Delinearization not optimised.
Definition Loop.cpp:142
double max(double a, double b, double c)
I need the maximum of three values all the time, to I decided to write a function for this.
Definition Scalar.cpp:8
int aPowI(int i, int a)
Computes the i-th power of a in integer arithmetic.
Simple vector class.
Definition Vector.h:134