"tests/nn/pipe/vscode:/vscode.git/clone" did not exist on "7d7edf6d37576fb6eda65db6db43fda54a7f06ba"
threadwise_tensor_op.cuh 6.15 KB
Newer Older
1
2
3
#pragma once
#include "constant_tensor_descriptor.cuh"

Chao Liu's avatar
faster  
Chao Liu committed
4
#define THREADWISE_TENSOR_OP_METHOD 0
Chao Liu's avatar
Chao Liu committed
5
6

#if THREADWISE_TENSOR_OP_METHOD == 0
Chao Liu's avatar
Chao Liu committed
7
template <class TFloat, class DstDesc, class F>
Chao Liu's avatar
Chao Liu committed
8
__device__ void threadwise_4d_tensor_op_unary(DstDesc, TFloat* __restrict__ p_dst, F f)
9
10
11
12
13
14
15
16
17
18
19
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

    constexpr auto dst_desc = DstDesc{};

#if 0
    if(threadIdx.x == 0)
    {
Chao Liu's avatar
Chao Liu committed
20
        print_ConstantTensorDescriptor(dst_desc, "threadwise_4d_tensor_op_unary: ");
21
22
23
    }
#endif

Chao Liu's avatar
Chao Liu committed
24
    for(unsigned did0 = 0; did0 < dst_desc.GetLength(I0); ++did0)
Chao Liu's avatar
Chao Liu committed
25
    {
Chao Liu's avatar
Chao Liu committed
26
        for(unsigned did1 = 0; did1 < dst_desc.GetLength(I1); ++did1)
Chao Liu's avatar
Chao Liu committed
27
        {
Chao Liu's avatar
Chao Liu committed
28
            for(unsigned did2 = 0; did2 < dst_desc.GetLength(I2); ++did2)
Chao Liu's avatar
Chao Liu committed
29
            {
Chao Liu's avatar
Chao Liu committed
30
                for(unsigned did3 = 0; did3 < dst_desc.GetLength(I3); ++did3)
Chao Liu's avatar
Chao Liu committed
31
32
33
34
35
                {
                    const unsigned dindex =
                        dst_desc.GetStride(I0) * did0 + dst_desc.GetStride(I1) * did1 +
                        dst_desc.GetStride(I2) * did2 + dst_desc.GetStride(I3) * did3;

Chao Liu's avatar
Chao Liu committed
36
                    f(p_dst[dindex]);
Chao Liu's avatar
Chao Liu committed
37
38
39
40
41
42
43
44
45
46
47

#if 0
                    if(threadIdx.x == 0)
                    {
                        printf("threadwise_4d_tensor_op_unary: thread id %u, \t"
                               "dindex %u, p_dst[dindex] %f\n",
                               threadIdx.x,
                               dindex,
                               p_dst[dindex]);
                    }
#endif
Chao Liu's avatar
Chao Liu committed
48
49
50
51
52
53
54
                }
            }
        }
    }
}

template <class TFloat, class SrcDesc, class DstDesc, class F>
Chao Liu's avatar
Chao Liu committed
55
__device__ void threadwise_4d_tensor_op_binary(
Chao Liu's avatar
Chao Liu committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    SrcDesc, TFloat* const __restrict__ p_src, DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

    constexpr auto src_desc = SrcDesc{};
    constexpr auto dst_desc = DstDesc{};

    static_assert(is_same<decltype(src_desc.GetLengths()), decltype(dst_desc.GetLengths())>::value);

#if 0
    if(threadIdx.x == 0)
    {
Chao Liu's avatar
Chao Liu committed
71
72
        print_ConstantTensorDescriptor(src_desc, "threadwise_4d_tensor_op_binary: src_desc: ");
        print_ConstantTensorDescriptor(dst_desc, "threadwise_4d_tensor_op_binary: dst_desc: ");
Chao Liu's avatar
Chao Liu committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    }
#endif

    for(unsigned did0 = 0; did0 < src_desc.GetLength(I0); ++did0)
    {
        for(unsigned did1 = 0; did1 < src_desc.GetLength(I1); ++did1)
        {
            for(unsigned did2 = 0; did2 < src_desc.GetLength(I2); ++did2)
            {
                for(unsigned did3 = 0; did3 < src_desc.GetLength(I3); ++did3)
                {
                    const unsigned sindex =
                        src_desc.GetStride(I0) * did0 + src_desc.GetStride(I1) * did1 +
                        src_desc.GetStride(I2) * did2 + src_desc.GetStride(I3) * did3;

                    const unsigned dindex =
                        dst_desc.GetStride(I0) * did0 + dst_desc.GetStride(I1) * did1 +
                        dst_desc.GetStride(I2) * did2 + dst_desc.GetStride(I3) * did3;

                    f(p_src[sindex], p_dst[dindex]);
Chao Liu's avatar
Chao Liu committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#if 0
                    if(threadIdx.x == 0)
                    {
                        printf("threadwise_4d_tensor_op_binary: thread id %u, \t"
                               "sindex %u, p_src[sindex] %f, \t"
                               "dindex %u, p_dst[dindex] %f\n",
                               threadIdx.x,
                               sindex,
                               p_src[sindex],
                               dindex,
                               p_dst[dindex]);
                    }
#endif
107
108
109
110
111
                }
            }
        }
    }
}
Chao Liu's avatar
Chao Liu committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#endif

#if THREADWISE_TENSOR_OP_METHOD == 1
template <class TFloat, class SrcDesc, class DstDesc, class F>
__device__ void threadwise_4d_tensor_op(
    SrcDesc, TFloat* const __restrict__ p_src, DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

    constexpr auto src_desc = SrcDesc{};
    constexpr auto dst_desc = DstDesc{};

    static_assert(is_same<decltype(src_desc.GetLengths()), decltype(dst_desc.GetLengths())>::value);

#if 0
    if(threadIdx.x == 0)
    {
Chao Liu's avatar
Chao Liu committed
132
133
        print_ConstantTensorDescriptor(src_desc, "threadwise_4d_tensor_op: src_desc: ");
        print_ConstantTensorDescriptor(dst_desc, "threadwise_4d_tensor_op: dst_desc: ");
Chao Liu's avatar
Chao Liu committed
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
    }
#endif

    unsigned sindex = 0;
    unsigned dindex = 0;

    for(unsigned did0 = 0; did0 < src_desc.GetLength(I0); ++did0)
    {
        for(unsigned did1 = 0; did1 < src_desc.GetLength(I1); ++did1)
        {
            for(unsigned did2 = 0; did2 < src_desc.GetLength(I2); ++did2)
            {
                for(unsigned did3 = 0; did3 < src_desc.GetLength(I3); ++did3)
                {
                    f(p_src[sindex], p_dst[dindex]);

#if 0
                    if(threadIdx.x == 0)
                    {
                        printf("threadwise_4d_tensor_op: 1: thread id %u, \t"
                               "sindex %u, p_src[sindex] %f, \t"
                               "dindex %u, p_dst[dindex] %f\n",
                               threadIdx.x,
                               sindex,
                               p_src[sindex],
                               dindex,
                               p_dst[dindex]);
                    }
#endif
                    sindex += src_desc.GetStride(I3);
                    dindex += dst_desc.GetStride(I3);
                }

                sindex += src_desc.GetStride(I2) - src_desc.GetLength(I3) * src_desc.GetStride(I3);
                dindex += dst_desc.GetStride(I2) - dst_desc.GetLength(I3) * dst_desc.GetStride(I3);
            }

            sindex += src_desc.GetStride(I1) - src_desc.GetLength(I2) * src_desc.GetStride(I2);
            dindex += dst_desc.GetStride(I1) - dst_desc.GetLength(I2) * dst_desc.GetStride(I2);
        }

        sindex += src_desc.GetStride(I0) - src_desc.GetLength(I1) * src_desc.GetStride(I1);
        dindex += dst_desc.GetStride(I0) - dst_desc.GetLength(I1) * dst_desc.GetStride(I1);
    }
}
#endif