threadwise_tensor_slice_copy.hpp 7.8 KB
Newer Older
1
2
3
#ifndef CK_THREADWISE_TENSOR_SLICE_COPY_HPP
#define CK_THREADWISE_TENSOR_SLICE_COPY_HPP

Chao Liu's avatar
Chao Liu committed
4
#include "ConstantTensorDescriptor.hpp"
5

6
7
namespace ck {

8
// need to assume src and dst is aligned
Chao Liu's avatar
Chao Liu committed
9
template <class Float, class SrcDesc, class DstDesc, class SrcOpLengths, index_t DataPerRead>
Chao Liu's avatar
Chao Liu committed
10
11
12
13
14
15
__device__ void threadwise_tensor_slice_copy(SrcDesc,
                                             const Float* __restrict__ p_src,
                                             DstDesc,
                                             Float* __restrict__ p_dst,
                                             SrcOpLengths,
                                             Number<DataPerRead>)
16
{
Chao Liu's avatar
Chao Liu committed
17
    using vector_t = typename vector_type<Float, DataPerRead>::MemoryType;
18

19
    constexpr index_t nDim = SrcOpLengths::GetSize();
20

Chao Liu's avatar
Chao Liu committed
21
    static_assert(SrcDesc{}.GetNumOfDimension() == nDim && DstDesc{}.GetNumOfDimension() == nDim,
22
                  "wrong! dimension not consistent");
23
24
25

    constexpr auto src_desc = SrcDesc{};
    constexpr auto dst_desc = DstDesc{};
Chao Liu's avatar
Chao Liu committed
26
    constexpr auto ref_desc = make_ConstantTensorDescriptor_packed(SrcOpLengths{});
27

28
29
#if 0
    if(get_thread_local_1d_id() == 0 && get_block_1d_id() == 0)
30
    {
31
32
33
        print_ConstantTensorDescriptor(src_desc, "src_desc");
        print_ConstantTensorDescriptor(dst_desc, "dst_desc");
        print_ConstantTensorDescriptor(ref_desc, "ref_desc");
34
    }
35
#endif
36

37
38
39
    static_assert(DataPerRead == 1 || (SrcDesc{}.GetStride(Number<nDim - 1>{}) == 1 &&
                                       DstDesc{}.GetStride(Number<nDim - 1>{}) == 1),
                  "wrong! only support stride[nDim-1] == 1!\n");
40
41
42
43

    static_assert(DataPerRead == 1 || DataPerRead == 2 || DataPerRead == 4,
                  "wrong! only support DataPerRead == 1, 2 or 4!\n");

44
45
46
47
    static_assert(
        SrcDesc{}.GetStride(Number<nDim - 2>{}) % DataPerRead == 0 &&
            DstDesc{}.GetStride(Number<nDim - 2>{}) % DataPerRead == 0,
        "wrong! src and dst stride[nDim-2] should be multiple of DataPerRead to keep alignment");
48

49
    constexpr index_t L_Back = SrcOpLengths{}.Back();
50

51
52
    static_assert(L_Back % DataPerRead == 0,
                  "wrong! lengths[nDim-1] should be evenly divided by DataPerRead");
53

54
    constexpr index_t nRead = L_Back / DataPerRead;
55

56
    static_ford<decltype(ref_desc.GetLengths().PopBack())>{}([=](auto Ids) {
Chao Liu's avatar
Chao Liu committed
57
        static_for<0, nRead, 1>{}([&](auto IRead) {
58
            constexpr auto multi_id = decltype(Ids){}.PushBack(Number<IRead.Get() * DataPerRead>{});
59

60
            const index_t src_index = src_desc.GetOffsetFromMultiIndex(multi_id);
61

62
            const index_t dst_index = dst_desc.GetOffsetFromMultiIndex(multi_id);
63

64
65
66
67
            *(reinterpret_cast<vector_t*>(&p_dst[dst_index])) =
                *(reinterpret_cast<const vector_t*>(&p_src[src_index]));
        });
    });
68
}
Chao Liu's avatar
Chao Liu committed
69

Chao Liu's avatar
Chao Liu committed
70
// access in order of src
Chao Liu's avatar
Chao Liu committed
71
72
73
74
75
76
77
template <class SrcData,
          class DstData,
          class SrcDesc,
          class DstDesc,
          class SrcOpLengths,
          class MapDst2Src>
__device__ void
Chao Liu's avatar
Chao Liu committed
78
79
80
81
82
83
threadwise_tensor_slice_copy_reorder_given_dst2src_v1(SrcDesc,
                                                      const SrcData* __restrict__ p_src,
                                                      DstDesc,
                                                      DstData* __restrict__ p_dst,
                                                      SrcOpLengths,
                                                      MapDst2Src)
Chao Liu's avatar
Chao Liu committed
84
85
86
87
88
89
90
{
    constexpr auto src_desc = SrcDesc{};
    constexpr auto dst_desc = DstDesc{};

    ford<SrcOpLengths>{}([&](auto src_multi_id) {
        const auto dst_multi_id = reorder_array_given_new2old(src_multi_id, MapDst2Src{});

91
        const index_t dst_index = dst_desc.GetOffsetFromMultiIndex(dst_multi_id);
Chao Liu's avatar
Chao Liu committed
92

93
        const index_t src_index = src_desc.GetOffsetFromMultiIndex(src_multi_id);
Chao Liu's avatar
Chao Liu committed
94
95
96
97
98

        p_dst[dst_index] = p_src[src_index];
    });
}

Chao Liu's avatar
Chao Liu committed
99
// access in order of dst
Chao Liu's avatar
Chao Liu committed
100
101
102
103
104
105
106
template <class SrcData,
          class DstData,
          class SrcDesc,
          class DstDesc,
          class SrcOpLengths,
          class MapDst2Src>
__device__ void
Chao Liu's avatar
Chao Liu committed
107
108
109
110
111
112
threadwise_tensor_slice_copy_reorder_given_dst2src_v2(SrcDesc,
                                                      const SrcData* __restrict__ p_src,
                                                      DstDesc,
                                                      DstData* __restrict__ p_dst,
                                                      SrcOpLengths,
                                                      MapDst2Src)
Chao Liu's avatar
Chao Liu committed
113
114
115
116
117
118
119
120
121
{
    constexpr auto src_desc = SrcDesc{};
    constexpr auto dst_desc = DstDesc{};

    constexpr auto dst_op_lengths = SrcOpLengths{}.ReorderGivenNew2Old(MapDst2Src{});

    ford<decltype(dst_op_lengths)>{}([&](auto dst_multi_id) {
        const auto src_multi_id = reorder_array_given_old2new(dst_multi_id, MapDst2Src{});

122
        const index_t dst_index = dst_desc.GetOffsetFromMultiIndex(dst_multi_id);
Chao Liu's avatar
Chao Liu committed
123

124
        const index_t src_index = src_desc.GetOffsetFromMultiIndex(src_multi_id);
Chao Liu's avatar
Chao Liu committed
125
126
127
128
129

        p_dst[dst_index] = p_src[src_index];
    });
}

Chao Liu's avatar
Chao Liu committed
130
131
// access in order of dst
// manually pack data into vector before write
Chao Liu's avatar
Chao Liu committed
132
133
134
135
136
137
template <class Float,
          class SrcDesc,
          class DstDesc,
          class SrcOpLengths,
          class MapDst2Src,
          index_t DstDataPerWrite>
Chao Liu's avatar
Chao Liu committed
138
139
140
141
142
143
144
145
__device__ void
threadwise_tensor_slice_copy_reorder_given_dst2src_v3(SrcDesc,
                                                      const Float* __restrict__ p_src,
                                                      DstDesc,
                                                      Float* __restrict__ p_dst,
                                                      SrcOpLengths,
                                                      MapDst2Src,
                                                      Number<DstDataPerWrite>)
Chao Liu's avatar
Chao Liu committed
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
{
    using vector_t = typename vector_type<Float, DstDataPerWrite>::MemoryType;

    constexpr index_t nDim = SrcOpLengths::GetSize();

    static_assert(DstDataPerWrite == 1 || DstDesc{}.GetStride(Number<nDim - 1>{}) == 1,
                  "wrong! only support dst.stride[nDim-1] == 1, if DstDataPerWrite != 1");

    static_assert(DstDataPerWrite == 1 || DstDataPerWrite == 2 || DstDataPerWrite == 4,
                  "wrong! only support DstDataPerWrite == 1, 2 or 4");

    static_assert(
        DstDesc{}.GetStride(Number<nDim - 2>{}) % DstDataPerWrite == 0,
        "wrong! dst.stride[nDim-2] should be multiple of DstDataPerWrite to keep alignment");

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

    constexpr auto dst_op_lengths = SrcOpLengths{}.ReorderGivenNew2Old(MapDst2Src{});

    constexpr index_t L_Dst_Back = dst_op_lengths.Back();

    static_assert(L_Dst_Back % DstDataPerWrite == 0,
                  "wrong! dst.lengths[nDim-1] should be evenly divided by DstDataPerWrite");

    constexpr index_t nWrite = L_Dst_Back / DstDataPerWrite;

    ford<decltype(dst_op_lengths.PopBack())>{}([&](auto ids) {
        static_for<0, nWrite, 1>{}([&](auto IWrite) {
            vector_t dst_vec_data;

            // pack data
            static_for<0, DstDataPerWrite, 1>{}([&](auto IDstData) {
                const auto dst_multi_id =
                    ids.PushBack(IWrite.Get() * DstDataPerWrite + IDstData.Get());

                const auto src_multi_id = reorder_array_given_old2new(dst_multi_id, MapDst2Src{});

184
                const index_t src_index = src_desc.GetOffsetFromMultiIndex(src_multi_id);
Chao Liu's avatar
Chao Liu committed
185
186
187
188
189
190
191
192

                vector_type<Float, DstDataPerWrite>::SetScalar(
                    dst_vec_data, p_src[src_index], IDstData);
            });

            // write data
            const auto dst_multi_id = ids.PushBack(IWrite.Get() * DstDataPerWrite);

193
            const index_t dst_index = dst_desc.GetOffsetFromMultiIndex(dst_multi_id);
Chao Liu's avatar
Chao Liu committed
194
195
196
197
198

            *(reinterpret_cast<vector_t*>(&p_dst[dst_index])) = dst_vec_data;
        });
    });
}
199
200
201

} // namespace ck
#endif