-
Notifications
You must be signed in to change notification settings - Fork 21
/
coop_load.hpp
223 lines (191 loc) · 9.36 KB
/
coop_load.hpp
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
/*******************************************************************************
*
* MIT License
*
* Copyright (C) 2021-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#ifndef ROCWMMA_COOP_LOAD_HPP
#define ROCWMMA_COOP_LOAD_HPP
#include "io_traits.hpp"
#include "layout.hpp"
#include "opaque_load.hpp"
#include "types.hpp"
#include "utils.hpp"
namespace rocwmma
{
template <uint32_t BlockDim,
uint32_t BlockK,
typename DataT,
class DataLayout,
class MatrixLayout,
uint32_t VectorWidth>
struct CooperativeLoad
{
using IOTraits = IOTraits<BlockDim, BlockK, DataT, VectorWidth>;
struct Traits
{
enum : uint32_t
{
MaxSplit = IOTraits::IOCount
};
// Load implementation
using Loader = detail::amdgcn_opaque_load<DataT, VectorWidth>;
using LoadT = typename Loader::LoadT;
// Block output vector
using OutputT = VecT<DataT, IOTraits::UnpackedSize>;
};
using LoadVecTraits = VecTraits<typename Traits::LoadT>;
// Outer loop = index 0,
// Inner loop = index N-1
template <size_t Depth = 0, typename Iterator, typename StrideSpace, typename Strides2d>
ROCWMMA_DEVICE static inline auto unroll_right(Iterator& out,
DataT const* dataPtr,
uint32_t ldm,
StrideSpace&& strideSpace,
Strides2d&& strides2d)
{
static_assert(VecTraits<decay_t<StrideSpace>>::size()
== VecTraits<decay_t<Strides2d>>::size(),
"Mismatched size");
auto strideOffset = DataLayout::fromMatrixCoord(get<Depth>(strides2d), ldm);
auto strideCount = get<Depth>(strideSpace);
// Last depth layer will invoke the load
if constexpr(Depth == (VecTraits<decay_t<StrideSpace>>::size() - 1u))
{
#pragma unroll
for(int i = 0; i < strideCount; i++)
{
Traits::Loader::exec(*out, dataPtr);
dataPtr += strideOffset;
out++;
}
}
// Recurse to the next nested layer
else
{
for(int i = 0; i < strideCount; i++)
{
unroll_right<Depth + 1>(out, dataPtr, ldm, strideSpace, strides2d);
dataPtr += strideOffset;
}
}
}
constexpr static uint32_t calcMaxWaves(uint32_t workItems, uint32_t waveCount)
{
return (workItems % waveCount == 0 ? waveCount
: calcMaxWaves(workItems, waveCount / 2));
};
ROCWMMA_DEVICE static inline void exec(typename Traits::OutputT& data,
DataT const* dataPtr,
uint32_t ldm,
uint32_t waveIndex,
uint32_t waveCount)
{
// Full fragment work
constexpr auto strideSpace = MatrixLayout::strideCounts();
constexpr auto strides = MatrixLayout::strides();
// Drop the VW strides for splitting (reduced stride space).
constexpr auto strideSpaceR = pop_right(strideSpace);
constexpr auto stridesR = pop_right(strides);
constexpr auto totalWorkItems
= flatten_coord_left((strideSpaceR - 1u), strideSpaceR) + 1u;
// Determine max waves possible.
auto maxWaves = calcMaxWaves((uint32_t)totalWorkItems, (uint32_t)waveCount);
// maxWaves is the maximum amount of waves split the work into.
// For the rest of the waves, bail out
if(__builtin_amdgcn_readfirstlane(waveIndex) >= maxWaves)
{
return;
}
// Split the reduced stride space.
auto workItemsPerWave = max(totalWorkItems / maxWaves, 1u);
auto strideSpaceS = inflate_coord_left(workItemsPerWave - 1u, strideSpaceR) + 1u;
// Add back in the VW dimension, for the full stride
// space of the current wave
auto strideSpaceW = vector_cat(strideSpaceS, make_vector(get_last(strideSpace)));
auto it = makeVectorIterator<LoadVecTraits::size()>(data).begin();
// Align threads to starting matrix offset coordinates
auto baseOffset = MatrixLayout::baseOffset();
// Find current wave offset
constexpr auto sum = [](auto... items) { return (items + ...); };
auto currentWaveOffset = apply(
sum, inflate_coord_left(waveIndex * workItemsPerWave, strideSpaceR) * stridesR);
unroll_right(it,
dataPtr + DataLayout::fromMatrixCoord(baseOffset + currentWaveOffset, ldm),
ldm,
strideSpaceW,
strides);
}
template <uint32_t WaveCount>
ROCWMMA_DEVICE static inline void exec(typename Traits::OutputT& data,
DataT const* dataPtr,
uint32_t ldm,
uint32_t waveIndex)
{
// Full fragment work
constexpr auto strideSpace = MatrixLayout::strideCounts();
constexpr auto strides = MatrixLayout::strides();
// Drop the VW strides for splitting (reduced stride space).
constexpr auto strideSpaceR = pop_right(strideSpace);
constexpr auto stridesR = pop_right(strides);
constexpr auto totalWorkItems
= flatten_coord_left((strideSpaceR - 1u), strideSpaceR) + 1u;
// Determine max waves possible.
constexpr auto maxWaves = calcMaxWaves((uint32_t)totalWorkItems, (uint32_t)WaveCount);
static_assert(maxWaves <= WaveCount, "Max waves cannot exceed given WaveCount");
// maxWaves is the maximum amount of waves split the work into.
// For the rest of the waves, bail out
if constexpr(WaveCount != maxWaves)
{
if(__builtin_amdgcn_readfirstlane(waveIndex) >= maxWaves)
{
return;
}
}
// Split the reduced stride space.
constexpr auto workItemsPerWave = max(totalWorkItems / maxWaves, 1u);
constexpr auto strideSpaceS
= inflate_coord_left(workItemsPerWave - 1u, strideSpaceR) + 1u;
// Add back in the VW dimension, for the full stride
// space of the current wave
constexpr auto strideSpaceW
= vector_cat(strideSpaceS, make_vector(get_last(strideSpace)));
// Alias the original frag due to smaller split size
auto& dataR
= (typename LoadVecTraits::
template VecT<DataT, workItemsPerWave * LoadVecTraits::size()>&)(data);
auto it = makeVectorIterator<LoadVecTraits::size()>(dataR).begin();
// Align threads to starting matrix offset coordinates
auto baseOffset = MatrixLayout::baseOffset();
// Find current wave offset
constexpr auto sum = [](auto... items) { return (items + ...); };
auto currentWaveOffset = apply(
sum, inflate_coord_left(waveIndex * workItemsPerWave, strideSpaceR) * stridesR);
unroll_right(it,
dataPtr + DataLayout::fromMatrixCoord(baseOffset + currentWaveOffset, ldm),
ldm,
strideSpaceW,
strides);
}
};
} // namespace rocwmma
#endif // ROCWMMA_COOP_LOAD_HPP