-
Notifications
You must be signed in to change notification settings - Fork 0
/
qHelper.h
60 lines (49 loc) · 1.1 KB
/
qHelper.h
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
#pragma once
namespace __template_unroll__
{
template<int N> struct templateIntWrapper
{
enum
{
VAL = N,
};
};
template<int k, int N> struct forwardUnroll
{
template<typename F> CUDA_FUNC_IN static void exec_iteration(F clb)
{
clb(templateIntWrapper<k>());
forwardUnroll<k + 1, N>::exec_iteration(clb);
}
};
template<int N> struct forwardUnroll<N, N>
{
template<typename F> CUDA_FUNC_IN static void exec_iteration(F clb)
{
}
};
template<typename F, int N> struct down_iter_callback
{
F& clb;
down_iter_callback(F& clb)
: clb(clb)
{
}
template<int I> void operator()(templateIntWrapper<I> kT)
{
const int k = decltype(kT)::VAL;
clb(__template_unroll__::templateIntWrapper<N - k>());
}
};
}
//unrolls i = k; i < N; i++
template<int k, int N, typename F> CUDA_FUNC_IN void for_i(F clb)
{
__template_unroll__::forwardUnroll<k, N>::exec_iteration(clb);
}
//unrolls i = N; i >= k; i--
template<int N, int k, typename F> CUDA_FUNC_IN void for_i_down(F clb)
{
auto iter = __template_unroll__::down_iter_callback<F, N>(clb);
__template_unroll__::forwardUnroll<k, N + 1>::exec_iteration(iter);
}