Newer
Older
#ifndef CUDIFY_SEQUENCIAL_HPP_
#define CUDIFY_SEQUENCIAL_HPP_
constexpr int default_kernel_wg_threads_ = 1024;
#ifdef HAVE_BOOST_CONTEXT
#ifndef CUDIFY_BOOST_CONTEXT_STACK_SIZE
#define CUDIFY_BOOST_CONTEXT_STACK_SIZE 8192
#endif
extern std::vector<void *>mem_stack;
extern thread_local dim3 threadIdx;
extern thread_local dim3 blockIdx;
static dim3 blockDim;
static dim3 gridDim;
extern std::vector<void *> mem_stack;
extern std::vector<boost::context::detail::fcontext_t> contexts;
extern thread_local void * par_glob;
extern thread_local boost::context::detail::fcontext_t main_ctx;
static void __syncthreads()
{
boost::context::detail::jump_fcontext(main_ctx,par_glob);
};
extern int thread_local vct_atomic_add;
extern int thread_local vct_atomic_rem;
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
static void cudaMemcpyToSymbol(unsigned char * global_cuda_error_array,const void * mem,size_t sz,int offset,int unused)
{
memcpy(global_cuda_error_array+offset,mem,sz);
}
namespace cub
{
template<typename T, unsigned int dim>
class BlockScan
{
public:
typedef std::array<T,dim> TempStorage;
private:
TempStorage & tmp;
public:
BlockScan(TempStorage & tmp)
:tmp(tmp)
{};
void ExclusiveSum(T & in, T & out)
{
tmp[threadIdx.x] = in;
__syncthreads();
if (threadIdx.x == 0)
{
T prec = tmp[0];
tmp[0] = 0;
for (int i = 1 ; i < dim ; i++)
{
auto next = tmp[i-1] + prec;
prec = tmp[i];
tmp[i] = next;
}
}
__syncthreads();
out = tmp[threadIdx.x];
return;
}
};
}
template<typename T, typename T2>
static T atomicAdd(T * address, T2 val)
{
T old = *address;
*address += val;
return old;
};
#define MGPU_HOST_DEVICE
namespace mgpu
{
template<typename type_t>
struct less_t : public std::binary_function<type_t, type_t, bool> {
bool operator()(type_t a, type_t b) const {
return a < b;
}
template<typename type2_t, typename type3_t>
bool operator()(type2_t a, type3_t b) const {
return a < b;
}
};
/* template<typename type_t>
struct less_equal_t : public std::binary_function<type_t, type_t, bool> {
MGPU_HOST_DEVICE bool operator()(type_t a, type_t b) const {
return a <= b;
}
};*/
template<typename type_t>
struct greater_t : public std::binary_function<type_t, type_t, bool> {
MGPU_HOST_DEVICE bool operator()(type_t a, type_t b) const {
return a > b;
}
template<typename type2_t, typename type3_t>
MGPU_HOST_DEVICE bool operator()(type2_t a, type3_t b) const {
return a > b;
}
};
/* template<typename type_t>
struct greater_equal_t : public std::binary_function<type_t, type_t, bool> {
MGPU_HOST_DEVICE bool operator()(type_t a, type_t b) const {
return a >= b;
}
};
template<typename type_t>
struct equal_to_t : public std::binary_function<type_t, type_t, bool> {
MGPU_HOST_DEVICE bool operator()(type_t a, type_t b) const {
return a == b;
}
};
template<typename type_t>
struct not_equal_to_t : public std::binary_function<type_t, type_t, bool> {
MGPU_HOST_DEVICE bool operator()(type_t a, type_t b) const {
return a != b;
}
};*/
////////////////////////////////////////////////////////////////////////////////
// Device-side arithmetic operators.
template<typename type_t>
struct plus_t : public std::binary_function<type_t, type_t, type_t> {
type_t operator()(type_t a, type_t b) const {
return a + b;
}
};
/* template<typename type_t>
struct minus_t : public std::binary_function<type_t, type_t, type_t> {
MGPU_HOST_DEVICE type_t operator()(type_t a, type_t b) const {
return a - b;
}
};
template<typename type_t>
struct multiplies_t : public std::binary_function<type_t, type_t, type_t> {
MGPU_HOST_DEVICE type_t operator()(type_t a, type_t b) const {
return a * b;
}
};*/
template<typename type_t>
struct maximum_t : public std::binary_function<type_t, type_t, type_t> {
type_t operator()(type_t a, type_t b) const {
return std::max(a, b);
}
};
template<typename type_t>
struct minimum_t : public std::binary_function<type_t, type_t, type_t> {
type_t operator()(type_t a, type_t b) const {
return std::min(a, b);
}
};
}
namespace mgpu
{
template<typename input_it,
typename segments_it, typename output_it, typename op_t, typename type_t, typename context_t>
void segreduce(input_it input, int count, segments_it segments,
int num_segments, output_it output, op_t op, type_t init,
context_t& context)
{
int i = 0;
for ( ; i < num_segments - 1; i++)
{
int j = segments[i];
output[i] = input[j];
++j;
for ( ; j < segments[i+1] ; j++)
{
output[i] = op(output[i],input[j]);
}
}
// Last segment
int j = segments[i];
output[i] = input[j];
++j;
for ( ; j < count ; j++)
{
output[i] = op(output[i],input[j]);
}
}
// Key-value merge.
template<typename a_keys_it, typename a_vals_it,
typename b_keys_it, typename b_vals_it,
typename c_keys_it, typename c_vals_it,
typename comp_t, typename context_t>
void merge(a_keys_it a_keys, a_vals_it a_vals, int a_count,
b_keys_it b_keys, b_vals_it b_vals, int b_count,
c_keys_it c_keys, c_vals_it c_vals, comp_t comp, context_t& context)
{
int a_it = 0;
int b_it = 0;
int c_it = 0;
while (a_it < a_count || b_it < b_count)
{
if (a_it < a_count)
{
if (b_it < b_count)
{
if (comp(b_keys[b_it],a_keys[a_it]))
{
c_keys[c_it] = b_keys[b_it];
c_vals[c_it] = b_vals[b_it];
c_it++;
b_it++;
}
else
{
c_keys[c_it] = a_keys[a_it];
c_vals[c_it] = a_vals[a_it];
c_it++;
a_it++;
}
}
else
{
c_keys[c_it] = a_keys[a_it];
c_vals[c_it] = a_vals[a_it];
c_it++;
a_it++;
}
}
else
{
c_keys[c_it] = b_keys[b_it];
c_vals[c_it] = b_vals[b_it];
c_it++;
b_it++;
}
}
}
}
static void init_wrappers()
{}
template<typename lambda_f>
struct Fun_enc
{
lambda_f Fn;
Fun_enc(lambda_f Fn)
:Fn(Fn)
{}
void run()
{
Fn();
}
};
template<typename lambda_f>
struct Fun_enc_bt
{
lambda_f Fn;
dim3 & blockIdx;
dim3 & threadIdx;
Fun_enc_bt(lambda_f Fn,dim3 & blockIdx,dim3 & threadIdx)
:Fn(Fn),blockIdx(blockIdx),threadIdx(threadIdx)
{}
void run()
{
Fn(blockIdx,threadIdx);
}
};
template<typename Fun_enc_type>
void launch_kernel(boost::context::detail::transfer_t par)
{
main_ctx = par.fctx;
par_glob = par.data;
Fun_enc_type * ptr = (Fun_enc_type *)par.data;
ptr->run();
boost::context::detail::jump_fcontext(par.fctx,0);
}
template<typename lambda_f, typename ite_type>
static void exe_kernel(lambda_f f, ite_type & ite)
{
if (ite.nthrs() == 0 || ite.nblocks() == 0) {return;}
if (mem_stack.size() < ite.nthrs())
{
int old_size = mem_stack.size();
mem_stack.resize(ite.nthrs());
for (int i = old_size ; i < mem_stack.size() ; i++)
{
mem_stack[i] = new char [CUDIFY_BOOST_CONTEXT_STACK_SIZE];
}
}
// Resize contexts
contexts.resize(mem_stack.size());
Fun_enc<lambda_f> fe(f);
for (int i = 0 ; i < ite.wthr.z ; i++)
{
blockIdx.z = i;
for (int j = 0 ; j < ite.wthr.y ; j++)
{
blockIdx.y = j;
for (int k = 0 ; k < ite.wthr.x ; k++)
{
blockIdx.x = k;
int nc = 0;
for (int it = 0 ; it < ite.thr.z ; it++)
{
for (int jt = 0 ; jt < ite.thr.y ; jt++)
{
for (int kt = 0 ; kt < ite.thr.x ; kt++)
{
contexts[nc] = boost::context::detail::make_fcontext((char *)mem_stack[nc]+CUDIFY_BOOST_CONTEXT_STACK_SIZE-16,CUDIFY_BOOST_CONTEXT_STACK_SIZE,launch_kernel<Fun_enc<lambda_f>>);
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
nc++;
}
}
}
bool work_to_do = true;
while(work_to_do)
{
nc = 0;
// Work threads
for (int it = 0 ; it < ite.thr.z ; it++)
{
threadIdx.z = it;
for (int jt = 0 ; jt < ite.thr.y ; jt++)
{
threadIdx.y = jt;
for (int kt = 0 ; kt < ite.thr.x ; kt++)
{
threadIdx.x = kt;
auto t = boost::context::detail::jump_fcontext(contexts[nc],&fe);
contexts[nc] = t.fctx;
work_to_do &= (t.data != 0);
nc++;
}
}
}
}
}
}
}
}
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
template<typename lambda_f, typename ite_type>
static void exe_kernel_lambda(lambda_f f, ite_type & ite)
{
if (ite.nthrs() == 0 || ite.nblocks() == 0) {return;}
if (mem_stack.size() < ite.nthrs())
{
int old_size = mem_stack.size();
mem_stack.resize(ite.nthrs());
for (int i = old_size ; i < mem_stack.size() ; i++)
{
mem_stack[i] = new char [CUDIFY_BOOST_CONTEXT_STACK_SIZE];
}
}
// Resize contexts
contexts.resize(mem_stack.size());
bool is_sync_free = true;
bool first_block = true;
for (int i = 0 ; i < ite.wthr.z ; i++)
{
for (int j = 0 ; j < ite.wthr.y ; j++)
{
for (int k = 0 ; k < ite.wthr.x ; k++)
{
dim3 blockIdx;
dim3 threadIdx;
Fun_enc_bt<lambda_f> fe(f,blockIdx,threadIdx);
if (first_block == true || is_sync_free == false)
{
blockIdx.z = i;
blockIdx.y = j;
blockIdx.x = k;
int nc = 0;
for (int it = 0 ; it < ite.thr.z ; it++)
{
for (int jt = 0 ; jt < ite.thr.y ; jt++)
{
for (int kt = 0 ; kt < ite.thr.x ; kt++)
{
contexts[nc] = boost::context::detail::make_fcontext((char *)mem_stack[nc]+CUDIFY_BOOST_CONTEXT_STACK_SIZE-16,CUDIFY_BOOST_CONTEXT_STACK_SIZE,launch_kernel<Fun_enc_bt<lambda_f>>);
nc++;
}
}
}
bool work_to_do = true;
while(work_to_do)
{
nc = 0;
// Work threads
for (int it = 0 ; it < ite.thr.z ; it++)
{
threadIdx.z = it;
for (int jt = 0 ; jt < ite.thr.y ; jt++)
{
threadIdx.y = jt;
for (int kt = 0 ; kt < ite.thr.x ; kt++)
{
threadIdx.x = kt;
auto t = boost::context::detail::jump_fcontext(contexts[nc],&fe);
contexts[nc] = t.fctx;
work_to_do &= (t.data != 0);
is_sync_free &= !(work_to_do);
nc++;
}
}
}
}
}
else
{
blockIdx.z = i;
blockIdx.y = j;
blockIdx.x = k;
int fb = 0;
// Work threads
for (int it = 0 ; it < ite.thr.z ; it++)
{
threadIdx.z = it;
for (int jt = 0 ; jt < ite.thr.y ; jt++)
{
threadIdx.y = jt;
for (int kt = 0 ; kt < ite.thr.x ; kt++)
{
threadIdx.x = kt;
f(blockIdx,threadIdx);
}
}
}
}
first_block = false;
}
}
}
}
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
template<typename lambda_f, typename ite_type>
static void exe_kernel_no_sync(lambda_f f, ite_type & ite)
{
for (int i = 0 ; i < ite.wthr.z ; i++)
{
blockIdx.z = i;
for (int j = 0 ; j < ite.wthr.y ; j++)
{
blockIdx.y = j;
for (int k = 0 ; k < ite.wthr.x ; k++)
{
blockIdx.x = k;
int fb = 0;
// Work threads
for (int it = 0 ; it < ite.wthr.z ; it++)
{
threadIdx.z = it;
for (int jt = 0 ; jt < ite.wthr.y ; jt++)
{
threadIdx.y = jt;
for (int kt = 0 ; kt < ite.wthr.x ; kt++)
{
threadIdx.x = kt;
f();
}
}
}
}
}
}
}
#ifdef PRINT_CUDA_LAUNCHES
#define CUDA_LAUNCH(cuda_call,ite, ...)\
\
gridDim.x = ite.wthr.x;\
gridDim.y = ite.wthr.y;\
gridDim.z = ite.wthr.z;\
\
blockDim.x = ite.thr.x;\
blockDim.y = ite.thr.y;\
blockDim.z = ite.thr.z;\
\
CHECK_SE_CLASS1_PRE\
\
std::cout << "Launching: " << #cuda_call << std::endl;\
\
exe_kernel(\
[&](boost::context::fiber && main) -> void {\
\
\
\
cuda_call(__VA_ARGS__);\
},ite);\
CHECK_SE_CLASS1_POST(#cuda_call,__VA_ARGS__)\
}
#define CUDA_LAUNCH_DIM3(cuda_call,wthr_,thr_, ...)\
{\
dim3 wthr__(wthr_);\
dim3 thr__(thr_);\
\
ite_gpu<1> itg;\
itg.wthr = wthr;\
itg.thr = thr;\
\
gridDim.x = wthr__.x;\
gridDim.y = wthr__.y;\
gridDim.z = wthr__.z;\
\
blockDim.x = thr__.x;\
blockDim.y = thr__.y;\
blockDim.z = thr__.z;\
\
CHECK_SE_CLASS1_PRE\
std::cout << "Launching: " << #cuda_call << std::endl;\
\
exe_kernel(\
[&] (boost::context::fiber && main) -> void {\
\
\
\
cuda_call(__VA_ARGS__);\
\
\
});\
CHECK_SE_CLASS1_POST(#cuda_call,__VA_ARGS__)\
}
#define CUDA_CHECK()
#else
#define CUDA_LAUNCH(cuda_call,ite, ...) \
{\
gridDim.x = ite.wthr.x;\
gridDim.y = ite.wthr.y;\
gridDim.z = ite.wthr.z;\
\
blockDim.x = ite.thr.x;\
blockDim.y = ite.thr.y;\
blockDim.z = ite.thr.z;\
\
CHECK_SE_CLASS1_PRE\
\
exe_kernel([&]() -> void {\
\
\
cuda_call(__VA_ARGS__);\
\
},ite);\
\
CHECK_SE_CLASS1_POST(#cuda_call,__VA_ARGS__)\
}
#define CUDA_LAUNCH_LAMBDA(ite,lambda_f) \
{\
gridDim.x = ite.wthr.x;\
gridDim.y = ite.wthr.y;\
gridDim.z = ite.wthr.z;\
\
blockDim.x = ite.thr.x;\
blockDim.y = ite.thr.y;\
blockDim.z = ite.thr.z;\
\
CHECK_SE_CLASS1_PRE\
\
exe_kernel_lambda(lambda_f,ite);\
\
CHECK_SE_CLASS1_POST("lambda")\
}
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#define CUDA_LAUNCH_DIM3(cuda_call,wthr_,thr_, ...)\
{\
dim3 wthr__(wthr_);\
dim3 thr__(thr_);\
\
ite_gpu<1> itg;\
itg.wthr = wthr_;\
itg.thr = thr_;\
\
gridDim.x = wthr__.x;\
gridDim.y = wthr__.y;\
gridDim.z = wthr__.z;\
\
blockDim.x = thr__.x;\
blockDim.y = thr__.y;\
blockDim.z = thr__.z;\
\
CHECK_SE_CLASS1_PRE\
\
exe_kernel([&]() -> void {\
\
cuda_call(__VA_ARGS__);\
\
},itg);\
\
CHECK_SE_CLASS1_POST(#cuda_call,__VA_ARGS__)\
}
#define CUDA_CHECK()
#endif
constexpr int default_kernel_wg_threads_ = 1024;
#endif /* CUDIFY_SEQUENCIAL_HPP_ */