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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
// Do not include this header directly.
// Control flow functionality in common between all the headers.
//
// Copyright 2020-2021 Binomial LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifdef _DEBUG
CPPSPMD_FORCE_INLINE void spmd_kernel::check_masks()
{
assert(!any(andnot(m_kernel_exec, m_exec)));
}
#endif
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_break()
{
#ifdef _DEBUG
assert(m_in_loop);
#endif
m_exec = exec_mask::all_off();
}
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_continue()
{
#ifdef _DEBUG
assert(m_in_loop);
#endif
// Kill any active lanes, and remember which lanes were active so we can re-enable them at the end of the loop body.
m_continue_mask = m_continue_mask | m_exec;
m_exec = exec_mask::all_off();
}
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_return()
{
// Permenantly kill all active lanes
m_kernel_exec = andnot(m_exec, m_kernel_exec);
m_exec = exec_mask::all_off();
}
template<typename UnmaskedBody>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_unmasked(const UnmaskedBody& unmaskedBody)
{
exec_mask orig_exec = m_exec, orig_kernel_exec = m_kernel_exec;
m_kernel_exec = exec_mask::all_on();
m_exec = exec_mask::all_on();
unmaskedBody();
m_kernel_exec = m_kernel_exec & orig_kernel_exec;
m_exec = m_exec & orig_exec;
check_masks();
}
struct scoped_unmasked_restorer
{
spmd_kernel *m_pKernel;
exec_mask m_orig_exec, m_orig_kernel_exec;
CPPSPMD_FORCE_INLINE scoped_unmasked_restorer(spmd_kernel *pKernel) :
m_pKernel(pKernel),
m_orig_exec(pKernel->m_exec),
m_orig_kernel_exec(pKernel->m_kernel_exec)
{
pKernel->m_kernel_exec = exec_mask::all_on();
pKernel->m_exec = exec_mask::all_on();
}
CPPSPMD_FORCE_INLINE ~scoped_unmasked_restorer()
{
m_pKernel->m_kernel_exec = m_pKernel->m_kernel_exec & m_orig_kernel_exec;
m_pKernel->m_exec = m_pKernel->m_exec & m_orig_exec;
m_pKernel->check_masks();
}
};
#define SPMD_UNMASKED_BEGIN { scoped_unmasked_restorer _unmasked_restorer(this);
#define SPMD_UNMASKED_END }
#if 0
template<typename SPMDKernel, typename... Args>
CPPSPMD_FORCE_INLINE decltype(auto) spmd_kernel::spmd_call(Args&&... args)
{
SPMDKernel kernel;
kernel.init(m_exec);
return kernel._call(std::forward<Args>(args)...);
}
#else
template<typename SPMDKernel, typename... Args>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_call(Args&&... args)
{
SPMDKernel kernel;
kernel.init(m_exec);
kernel._call(std::forward<Args>(args)...);
}
#endif
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_if_break(const vbool& cond)
{
#ifdef _DEBUG
assert(m_in_loop);
#endif
exec_mask cond_exec(cond);
m_exec = andnot(m_exec & cond_exec, m_exec);
check_masks();
}
// No SPMD breaks, continues, etc. allowed
template<typename IfBody>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_sif(const vbool& cond, const IfBody& ifBody)
{
exec_mask im = m_exec & exec_mask(cond);
if (any(im))
{
const exec_mask orig_exec = m_exec;
m_exec = im;
ifBody();
m_exec = orig_exec;
}
}
// No SPMD breaks, continues, etc. allowed
template<typename IfBody, typename ElseBody>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_sifelse(const vbool& cond, const IfBody& ifBody, const ElseBody &elseBody)
{
const exec_mask orig_exec = m_exec;
exec_mask im = m_exec & exec_mask(cond);
if (any(im))
{
m_exec = im;
ifBody();
}
exec_mask em = orig_exec & exec_mask(!cond);
if (any(em))
{
m_exec = em;
elseBody();
}
m_exec = orig_exec;
}
template<typename IfBody>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_if(const vbool& cond, const IfBody& ifBody)
{
exec_mask cond_exec(cond);
exec_mask pre_if_exec = cond_exec & m_exec;
if (any(pre_if_exec))
{
exec_mask unexecuted_lanes = andnot(cond_exec, m_exec);
m_exec = pre_if_exec;
ifBody();
// Propagate any lanes that got disabled inside the if body into the exec mask outside the if body, but turn on any lanes that didn't execute inside the if body.
m_exec = m_exec | unexecuted_lanes;
check_masks();
}
}
template<typename IfBody, typename ElseBody>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_ifelse(const vbool& cond, const IfBody& ifBody, const ElseBody& elseBody)
{
bool all_flag = false;
exec_mask cond_exec(cond);
{
exec_mask pre_if_exec = cond_exec & m_exec;
int mask = pre_if_exec.get_movemask();
if (mask != 0)
{
all_flag = ((uint32_t)mask == m_exec.get_movemask());
exec_mask unexecuted_lanes = andnot(cond_exec, m_exec);
m_exec = pre_if_exec;
ifBody();
// Propagate any lanes that got disabled inside the if body into the exec mask outside the if body, but turn on any lanes that didn't execute inside the if body.
m_exec = m_exec | unexecuted_lanes;
check_masks();
}
}
if (!all_flag)
{
exec_mask pre_if_exec = andnot(cond_exec, m_exec);
if (any(pre_if_exec))
{
exec_mask unexecuted_lanes = cond_exec & m_exec;
m_exec = pre_if_exec;
ifBody();
// Propagate any lanes that got disabled inside the if body into the exec mask outside the if body, but turn on any lanes that didn't execute inside the if body.
m_exec = m_exec | unexecuted_lanes;
check_masks();
}
}
}
struct scoped_exec_restorer
{
exec_mask *m_pMask;
exec_mask m_prev_mask;
CPPSPMD_FORCE_INLINE scoped_exec_restorer(exec_mask *pExec_mask) : m_pMask(pExec_mask), m_prev_mask(*pExec_mask) { }
CPPSPMD_FORCE_INLINE ~scoped_exec_restorer() { *m_pMask = m_prev_mask; }
};
// Cannot use SPMD break, continue, or return inside "simple" if/else
#define SPMD_SIF(cond) exec_mask CPPSPMD_GLUER2(_exec_temp, __LINE__)(m_exec & exec_mask(vbool(cond))); if (any(CPPSPMD_GLUER2(_exec_temp, __LINE__))) \
{ CPPSPMD::scoped_exec_restorer CPPSPMD_GLUER2(_exec_restore_, __LINE__)(&m_exec); m_exec = CPPSPMD_GLUER2(_exec_temp, __LINE__);
#define SPMD_SELSE(cond) } exec_mask CPPSPMD_GLUER2(_exec_temp, __LINE__)(m_exec & exec_mask(!vbool(cond))); if (any(CPPSPMD_GLUER2(_exec_temp, __LINE__))) \
{ CPPSPMD::scoped_exec_restorer CPPSPMD_GLUER2(_exec_restore_, __LINE__)(&m_exec); m_exec = CPPSPMD_GLUER2(_exec_temp, __LINE__);
#define SPMD_SENDIF }
// Same as SPMD_SIF, except doesn't use a scoped object
#define SPMD_SIF2(cond) exec_mask CPPSPMD_GLUER2(_exec_temp, __LINE__)(m_exec & exec_mask(vbool(cond))); if (any(CPPSPMD_GLUER2(_exec_temp, __LINE__))) \
{ exec_mask _orig_exec = m_exec; m_exec = CPPSPMD_GLUER2(_exec_temp, __LINE__);
#define SPMD_SELSE2(cond) m_exec = _orig_exec; } exec_mask CPPSPMD_GLUER2(_exec_temp, __LINE__)(m_exec & exec_mask(!vbool(cond))); if (any(CPPSPMD_GLUER2(_exec_temp, __LINE__))) \
{ exec_mask _orig_exec = m_exec; m_exec = CPPSPMD_GLUER2(_exec_temp, __LINE__);
#define SPMD_SEND_IF2 m_exec = _orig_exec; }
// Same as SPMD_SIF(), except the if/else blocks are always executed
#define SPMD_SAIF(cond) exec_mask CPPSPMD_GLUER2(_exec_temp, __LINE__)(m_exec & exec_mask(vbool(cond))); { CPPSPMD::scoped_exec_restorer CPPSPMD_GLUER2(_exec_restore_, __LINE__)(&m_exec); \
m_exec = CPPSPMD_GLUER2(_exec_temp, __LINE__);
#define SPMD_SAELSE(cond) } exec_mask CPPSPMD_GLUER2(_exec_temp, __LINE__)(m_exec & exec_mask(!vbool(cond))); { CPPSPMD::scoped_exec_restorer CPPSPMD_GLUER2(_exec_restore_, __LINE__)(&m_exec); \
m_exec = CPPSPMD_GLUER2(_exec_temp, __LINE__);
#define SPMD_SAENDIF }
// Cannot use SPMD break, continue, or return inside sselect
#define SPMD_SSELECT(var) do { vint_t _select_var = var; scoped_exec_restorer _orig_exec(&m_exec); exec_mask _select_executed(exec_mask::all_off());
#define SPMD_SCASE(value) exec_mask CPPSPMD_GLUER2(_exec_temp, __LINE__)(_orig_exec.m_prev_mask & exec_mask(vbool(_select_var == (value)))); if (any(CPPSPMD_GLUER2(_exec_temp, __LINE__))) \
{ m_exec = CPPSPMD_GLUER2(_exec_temp, __LINE__); _select_executed = _select_executed | m_exec;
//#define SPMD_SCASE_END if (_select_executed.get_movemask() == _orig_exec.m_prev_mask.get_movemask()) break; }
#define SPMD_SCASE_END if (!any(_select_executed ^ _orig_exec.m_prev_mask)) break; }
#define SPMD_SDEFAULT exec_mask _all_other_lanes(andnot(_select_executed, _orig_exec.m_prev_mask)); if (any(_all_other_lanes)) { m_exec = _all_other_lanes;
#define SPMD_SDEFAULT_END }
#define SPMD_SSELECT_END } while(0);
// Same as SPMD_SSELECT, except all cases are executed.
// Cannot use SPMD break, continue, or return inside sselect
#define SPMD_SASELECT(var) do { vint_t _select_var = var; scoped_exec_restorer _orig_exec(&m_exec); exec_mask _select_executed(exec_mask::all_off());
#define SPMD_SACASE(value) exec_mask CPPSPMD_GLUER2(_exec_temp, __LINE__)(_orig_exec.m_prev_mask & exec_mask(vbool(_select_var == (value)))); { m_exec = CPPSPMD_GLUER2(_exec_temp, __LINE__); \
_select_executed = _select_executed | m_exec;
#define SPMD_SACASE_END }
#define SPMD_SADEFAULT exec_mask _all_other_lanes(andnot(_select_executed, _orig_exec.m_prev_mask)); { m_exec = _all_other_lanes;
#define SPMD_SADEFAULT_END }
#define SPMD_SASELECT_END } while(0);
struct scoped_exec_restorer2
{
spmd_kernel *m_pKernel;
exec_mask m_unexecuted_lanes;
CPPSPMD_FORCE_INLINE scoped_exec_restorer2(spmd_kernel *pKernel, const vbool &cond) :
m_pKernel(pKernel)
{
exec_mask cond_exec(cond);
m_unexecuted_lanes = andnot(cond_exec, pKernel->m_exec);
pKernel->m_exec = cond_exec & pKernel->m_exec;
}
CPPSPMD_FORCE_INLINE ~scoped_exec_restorer2()
{
m_pKernel->m_exec = m_pKernel->m_exec | m_unexecuted_lanes;
m_pKernel->check_masks();
}
};
#define SPMD_IF(cond) { CPPSPMD::scoped_exec_restorer2 CPPSPMD_GLUER2(_exec_restore2_, __LINE__)(this, vbool(cond)); if (any(m_exec)) {
#define SPMD_ELSE(cond) } } { CPPSPMD::scoped_exec_restorer2 CPPSPMD_GLUER2(_exec_restore2_, __LINE__)(this, !vbool(cond)); if (any(m_exec)) {
#define SPMD_END_IF } }
// Same as SPMD_IF, except the conditional block is always executed.
#define SPMD_AIF(cond) { CPPSPMD::scoped_exec_restorer2 CPPSPMD_GLUER2(_exec_restore2_, __LINE__)(this, vbool(cond)); {
#define SPMD_AELSE(cond) } } { CPPSPMD::scoped_exec_restorer2 CPPSPMD_GLUER2(_exec_restore2_, __LINE__)(this, !vbool(cond)); {
#define SPMD_AEND_IF } }
class scoped_exec_saver
{
exec_mask m_exec, m_kernel_exec, m_continue_mask;
spmd_kernel *m_pKernel;
#ifdef _DEBUG
bool m_in_loop;
#endif
public:
inline scoped_exec_saver(spmd_kernel *pKernel) :
m_exec(pKernel->m_exec), m_kernel_exec(pKernel->m_kernel_exec), m_continue_mask(pKernel->m_continue_mask),
m_pKernel(pKernel)
{
#ifdef _DEBUG
m_in_loop = pKernel->m_in_loop;
#endif
}
inline ~scoped_exec_saver()
{
m_pKernel->m_exec = m_exec;
m_pKernel->m_continue_mask = m_continue_mask;
m_pKernel->m_kernel_exec = m_kernel_exec;
#ifdef _DEBUG
m_pKernel->m_in_loop = m_in_loop;
m_pKernel->check_masks();
#endif
}
};
#define SPMD_BEGIN_CALL scoped_exec_saver CPPSPMD_GLUER2(_begin_call_scoped_exec_saver, __LINE__)(this); m_continue_mask = exec_mask::all_off();
#define SPMD_BEGIN_CALL_ALL_LANES scoped_exec_saver CPPSPMD_GLUER2(_begin_call_scoped_exec_saver, __LINE__)(this); m_exec = exec_mask::all_on(); m_continue_mask = exec_mask::all_off();
template<typename ForeachBody>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_foreach(int begin, int end, const ForeachBody& foreachBody)
{
if (begin == end)
return;
if (!any(m_exec))
return;
// We don't support iterating backwards.
if (begin > end)
std::swap(begin, end);
exec_mask prev_continue_mask = m_continue_mask, prev_exec = m_exec;
int total_full = (end - begin) / PROGRAM_COUNT;
int total_partial = (end - begin) % PROGRAM_COUNT;
lint_t loop_index = begin + program_index;
const int total_loops = total_full + (total_partial ? 1 : 0);
m_continue_mask = exec_mask::all_off();
for (int i = 0; i < total_loops; i++)
{
int n = PROGRAM_COUNT;
if ((i == (total_loops - 1)) && (total_partial))
{
exec_mask partial_mask = exec_mask(vint_t(total_partial) > vint_t(program_index));
m_exec = m_exec & partial_mask;
n = total_partial;
}
foreachBody(loop_index, n);
m_exec = m_exec | m_continue_mask;
if (!any(m_exec))
break;
m_continue_mask = exec_mask::all_off();
check_masks();
store_all(loop_index, loop_index + PROGRAM_COUNT);
}
m_exec = prev_exec & m_kernel_exec;
m_continue_mask = prev_continue_mask;
check_masks();
}
template<typename WhileCondBody, typename WhileBody>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_while(const WhileCondBody& whileCondBody, const WhileBody& whileBody)
{
exec_mask orig_exec = m_exec;
exec_mask orig_continue_mask = m_continue_mask;
m_continue_mask = exec_mask::all_off();
#ifdef _DEBUG
const bool prev_in_loop = m_in_loop;
m_in_loop = true;
#endif
while(true)
{
exec_mask cond_exec = exec_mask(whileCondBody());
m_exec = m_exec & cond_exec;
if (!any(m_exec))
break;
whileBody();
m_exec = m_exec | m_continue_mask;
m_continue_mask = exec_mask::all_off();
check_masks();
}
#ifdef _DEBUG
m_in_loop = prev_in_loop;
#endif
m_exec = orig_exec & m_kernel_exec;
m_continue_mask = orig_continue_mask;
check_masks();
}
struct scoped_while_restorer
{
spmd_kernel *m_pKernel;
exec_mask m_orig_exec, m_orig_continue_mask;
#ifdef _DEBUG
bool m_prev_in_loop;
#endif
CPPSPMD_FORCE_INLINE scoped_while_restorer(spmd_kernel *pKernel) :
m_pKernel(pKernel),
m_orig_exec(pKernel->m_exec),
m_orig_continue_mask(pKernel->m_continue_mask)
{
pKernel->m_continue_mask.all_off();
#ifdef _DEBUG
m_prev_in_loop = pKernel->m_in_loop;
pKernel->m_in_loop = true;
#endif
}
CPPSPMD_FORCE_INLINE ~scoped_while_restorer()
{
m_pKernel->m_exec = m_orig_exec & m_pKernel->m_kernel_exec;
m_pKernel->m_continue_mask = m_orig_continue_mask;
#ifdef _DEBUG
m_pKernel->m_in_loop = m_prev_in_loop;
m_pKernel->check_masks();
#endif
}
};
#undef SPMD_WHILE
#undef SPMD_WEND
#define SPMD_WHILE(cond) { scoped_while_restorer CPPSPMD_GLUER2(_while_restore_, __LINE__)(this); while(true) { exec_mask CPPSPMD_GLUER2(cond_exec, __LINE__) = exec_mask(vbool(cond)); \
m_exec = m_exec & CPPSPMD_GLUER2(cond_exec, __LINE__); if (!any(m_exec)) break;
#define SPMD_WEND m_exec = m_exec | m_continue_mask; m_continue_mask = exec_mask::all_off(); check_masks(); } }
// Nesting is not supported (although it will compile, but the results won't make much sense).
#define SPMD_FOREACH(loop_var, bi, ei) if (((bi) != (ei)) && (any(m_exec))) { \
scoped_while_restorer CPPSPMD_GLUER2(_while_restore_, __LINE__)(this); \
uint32_t b = (uint32_t)(bi), e = (uint32_t)(ei); if ((b) > (e)) { std::swap(b, e); } const uint32_t total_full = ((e) - (b)) >> PROGRAM_COUNT_SHIFT, total_partial = ((e) - (b)) & (PROGRAM_COUNT - 1); \
lint_t loop_var = program_index + (int)b; const uint32_t total_loops = total_full + (total_partial ? 1U : 0U); \
for (uint32_t CPPSPMD_GLUER2(_foreach_counter, __LINE__) = 0; CPPSPMD_GLUER2(_foreach_counter, __LINE__) < total_loops; ++CPPSPMD_GLUER2(_foreach_counter, __LINE__)) { \
if ((CPPSPMD_GLUER2(_foreach_counter, __LINE__) == (total_loops - 1)) && (total_partial)) { exec_mask partial_mask = exec_mask(vint_t((int)total_partial) > vint_t(program_index)); m_exec = m_exec & partial_mask; }
#define SPMD_FOREACH_END(loop_var) m_exec = m_exec | m_continue_mask; if (!any(m_exec)) break; m_continue_mask = exec_mask::all_off(); check_masks(); store_all(loop_var, loop_var + PROGRAM_COUNT); } }
// Okay to use spmd_continue or spmd_return, but not spmd_break
#define SPMD_FOREACH_ACTIVE(index_var) int64_t index_var; { uint64_t _movemask = m_exec.get_movemask(); if (_movemask) { scoped_while_restorer CPPSPMD_GLUER2(_while_restore_, __LINE__)(this); \
for (uint32_t _i = 0; _i < PROGRAM_COUNT; ++_i) { \
if (_movemask & (1U << _i)) { \
m_exec.enable_lane(_i); m_exec = m_exec & m_kernel_exec; \
(index_var) = _i; \
#define SPMD_FOREACH_ACTIVE_END } } } }
// Okay to use spmd_continue, but not spmd_break/spmd_continue
#define SPMD_FOREACH_UNIQUE_INT(index_var, var) { scoped_while_restorer CPPSPMD_GLUER2(_while_restore_, __LINE__)(this); \
CPPSPMD_DECL(int_t, _vals[PROGRAM_COUNT]); store_linear_all(_vals, var); std::sort(_vals, _vals + PROGRAM_COUNT); \
const int _n = (int)(std::unique(_vals, _vals + PROGRAM_COUNT) - _vals); \
for (int _i = 0; _i < _n; ++_i) { int index_var = _vals[_i]; vbool cond = (vint_t(var) == vint_t(index_var)); m_exec = exec_mask(cond);
#define SPMD_FOREACH_UNIQUE_INT_END } }
struct scoped_simple_while_restorer
{
spmd_kernel* m_pKernel;
exec_mask m_orig_exec;
#ifdef _DEBUG
bool m_prev_in_loop;
#endif
CPPSPMD_FORCE_INLINE scoped_simple_while_restorer(spmd_kernel* pKernel) :
m_pKernel(pKernel),
m_orig_exec(pKernel->m_exec)
{
#ifdef _DEBUG
m_prev_in_loop = pKernel->m_in_loop;
pKernel->m_in_loop = true;
#endif
}
CPPSPMD_FORCE_INLINE ~scoped_simple_while_restorer()
{
m_pKernel->m_exec = m_orig_exec;
#ifdef _DEBUG
m_pKernel->m_in_loop = m_prev_in_loop;
m_pKernel->check_masks();
#endif
}
};
// Cannot use SPMD break, continue, or return inside simple while
#define SPMD_SWHILE(cond) { scoped_simple_while_restorer CPPSPMD_GLUER2(_while_restore_, __LINE__)(this); \
while(true) { \
exec_mask CPPSPMD_GLUER2(cond_exec, __LINE__) = exec_mask(vbool(cond)); m_exec = m_exec & CPPSPMD_GLUER2(cond_exec, __LINE__); if (!any(m_exec)) break;
#define SPMD_SWEND } }
// Cannot use SPMD break, continue, or return inside simple do
#define SPMD_SDO { scoped_simple_while_restorer CPPSPMD_GLUER2(_while_restore_, __LINE__)(this); while(true) {
#define SPMD_SEND_DO(cond) exec_mask CPPSPMD_GLUER2(cond_exec, __LINE__) = exec_mask(vbool(cond)); m_exec = m_exec & CPPSPMD_GLUER2(cond_exec, __LINE__); if (!any(m_exec)) break; } }
#undef SPMD_FOR
#undef SPMD_END_FOR
#define SPMD_FOR(for_init, for_cond) { for_init; scoped_while_restorer CPPSPMD_GLUER2(_while_restore_, __LINE__)(this); while(true) { exec_mask CPPSPMD_GLUER2(cond_exec, __LINE__) = exec_mask(vbool(for_cond)); \
m_exec = m_exec & CPPSPMD_GLUER2(cond_exec, __LINE__); if (!any(m_exec)) break;
#define SPMD_END_FOR(for_inc) m_exec = m_exec | m_continue_mask; m_continue_mask = exec_mask::all_off(); check_masks(); for_inc; } }
template<typename ForInitBody, typename ForCondBody, typename ForIncrBody, typename ForBody>
CPPSPMD_FORCE_INLINE void spmd_kernel::spmd_for(const ForInitBody& forInitBody, const ForCondBody& forCondBody, const ForIncrBody& forIncrBody, const ForBody& forBody)
{
exec_mask orig_exec = m_exec;
forInitBody();
exec_mask orig_continue_mask = m_continue_mask;
m_continue_mask = exec_mask::all_off();
#ifdef _DEBUG
const bool prev_in_loop = m_in_loop;
m_in_loop = true;
#endif
while(true)
{
exec_mask cond_exec = exec_mask(forCondBody());
m_exec = m_exec & cond_exec;
if (!any(m_exec))
break;
forBody();
m_exec = m_exec | m_continue_mask;
m_continue_mask = exec_mask::all_off();
check_masks();
forIncrBody();
}
m_exec = orig_exec & m_kernel_exec;
m_continue_mask = orig_continue_mask;
#ifdef _DEBUG
m_in_loop = prev_in_loop;
check_masks();
#endif
}
|