CuTe Matrix Transpose

Introduction

CuTe is a C++ template library that provides a high-level abstraction for layout and tensor operations in CUDA kernels. CUTLASS 3.0 and beyond adopts CuTe throughout the GEMM hierarchy in its templates, allowing the implementation to be more readable and maintainable.

Previously, I have created an article “CuTe Layout Algebra” on the mathematical foundations of CuTe. In this blog post, we will have some hands-on experience and have a better understanding of CuTe by implementing matrix transpose CUDA kernels.

CuTe Matrix Transpose

Matrix transpose CUDA kernels are probably the most example CUDA kernels that I have ever implemented. In my previous examples, the thread and data index mappings in the CUDA kernels are completely manual. There were also some hard-coded assumptions, such as each CUDA thread will only process one single element in matrix transpose to make the implementation easier and more human readable. To have both the configuration complexity and human readability in the implementation, we can create matrix transpose CUDA kernels using CuTe.

To transpose a matrix in a CUDA kernel, performing strided memory reads or writes in a warp is inevitable and it will lead to uncoalesced memory accesses, resulting in performance degradation. To mitigate the performance degradation, the strided memory reads or writes could be performed on shared memory instead of global memory. When the strided memory reads or writes are performed on shared memory, special optimizations have also to be performed to avoid shared memory bank conflicts.

All the CuTe matrix transpose CUDA kernels implemented in this article and their unit tests could be found from my CUTLASS Examples GitHub repository.

CuTe Naive Matrix Transpose

In the CuTe naive matrix transpose CUDA kernel implementation, we will not use shared memory. Two slightly different CUDA kernel variants have been implemented. One performs coalesced global memory reads and strided global memory writes, and the other performs strided global memory reads and coalesced global memory writes. It turns out that the difference between the implementations of the two variants is just one line of code.

CuTe Naive Matrix Transpose Implementation

The CuTe naive matrix transpose CUDA kernel implementation could also be found from my CUTLASS Examples GitHub repository.

cute_matrix_transpose_naive.cu
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
#include <cuda_runtime.h>

#include <cute/tensor.hpp>

#include "cute_matrix_transpose.hpp"

template <class TensorSrc, class TensorDst, class ThreadLayout>
static __global__ void matrix_transpose_naive(TensorSrc tensor_src,
TensorDst tensor_dst_transposed,
ThreadLayout)
{
using Element = typename TensorSrc::value_type;

auto global_tile_src{tensor_src(cute::make_coord(cute::_, cute::_),
blockIdx.y,
blockIdx.x)}; // (TileSizeY, TileSizeX)
auto global_tile_dst_transposed{
tensor_dst_transposed(cute::make_coord(cute::_, cute::_), blockIdx.y,
blockIdx.x)}; // (TileSizeY, TileSizeX)

auto thread_global_tile_src{cute::local_partition(
global_tile_src, ThreadLayout{},
threadIdx.x)}; // (ThreadValueSizeY, ThreadValueSizeX)
auto thread_global_tile_dst_transposed{cute::local_partition(
global_tile_dst_transposed, ThreadLayout{},
threadIdx.x)}; // (ThreadValueSizeY, ThreadValueSizeX)

// A 2D array of tuples that maps (x, y) to (x, y).
auto const identity_tensor{cute::make_identity_tensor(cute::make_shape(
cute::size<0>(global_tile_src), cute::size<1>(global_tile_src)))};
auto const thread_identity_tensor{
cute::local_partition(identity_tensor, ThreadLayout{}, threadIdx.x)};
auto fragment{cute::make_tensor_like(thread_global_tile_src)};
auto predicator{cute::make_tensor<bool>(
cute::make_shape(cute::size<0>(fragment), cute::size<1>(fragment)))};

auto const num_max_columns{cute::stride<0>(global_tile_src)};
auto const num_max_rows{cute::stride<1>(global_tile_dst_transposed)};
constexpr auto global_tile_columns{cute::size<1>(global_tile_src)};
constexpr auto global_tile_rows{cute::size<0>(global_tile_src)};

CUTE_UNROLL
for (unsigned int i{0}; i < cute::size<0>(predicator); ++i)
{
CUTE_UNROLL
for (unsigned int j{0}; j < cute::size<1>(predicator); ++j)
{
auto const thread_identity{thread_identity_tensor(i, j)};
bool const is_row_in_bound{cute::get<0>(thread_identity) +
blockIdx.y * global_tile_rows <
num_max_rows};
bool const is_column_in_bound{cute::get<1>(thread_identity) +
blockIdx.x * global_tile_columns <
num_max_columns};
predicator(i, j) = is_row_in_bound && is_column_in_bound;
}
}

cute::copy_if(predicator, thread_global_tile_src, fragment);
cute::copy_if(predicator, fragment, thread_global_tile_dst_transposed);

// Alternatively, we could just do the following instead.
// cute::copy_if(predicator, thread_global_tile_src,
// thread_global_tile_dst_transposed);
}

enum class GlobalMemoryCoalescedAccessMode
{
Read,
Write
};

template <typename T>
static cudaError_t launch_matrix_transpose_naive_base(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
GlobalMemoryCoalescedAccessMode coalesced_access_mode, cudaStream_t stream)
{
auto const tensor_shape{cute::make_shape(M, N)};
auto const tensor_shape_transposed{cute::make_shape(N, M)};

// Input matrix: row-major M x N matrix.
auto const global_memory_layout_src{cute::make_layout(
tensor_shape, cute::GenRowMajor{})}; // (M, N) : (N, 1)
// Output matrix: row-major N x M matrix.
auto const global_memory_layout_dst{cute::make_layout(
tensor_shape_transposed, cute::GenRowMajor{})}; // (N, M) : (M, 1)
// Same output matrix, but different view: column-major M x N matrix.
auto const global_memory_layout_dst_transposed{cute::make_layout(
tensor_shape, cute::GenColMajor{})}; // (M, N) : (1, M)

auto const tensor_src{cute::make_tensor(cute::make_gmem_ptr(input_matrix),
global_memory_layout_src)};
auto const tensor_dst{cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst)};
auto const tensor_dst_transposed{
cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst_transposed)};

using TileSizeX = cute::Int<64>; // bN
using TileSizeY = cute::Int<32>; // bM

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};

auto const tiled_tensor_src{cute::tiled_divide(
tensor_src, block_shape)}; // ((TileSizeY, TileSizeX), M /
// TileSizeY, N / TileSizeX)
auto const tiled_tensor_dst_transposed{cute::tiled_divide(
tensor_dst_transposed, block_shape)}; // ((TileSizeY, TileSizeX), M
// / TileSizeY, N / TileSizeX)

using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
// Coalesced memory read.
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
// Coalesced memory write.
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{
cute::size(ThreadBlockSizeX::value * ThreadBlockSizeY::value)};

if (coalesced_access_mode == GlobalMemoryCoalescedAccessMode::Read)
{
CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");
matrix_transpose_naive<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed, thread_layout);
}
else
{
CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeY");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeX");
matrix_transpose_naive<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
thread_layout_transposed);
}

return cudaGetLastError();
}

template <typename T>
cudaError_t launch_matrix_transpose_naive_coalesced_read(T const* input_matrix,
T* output_matrix,
unsigned int M,
unsigned int N,
cudaStream_t stream)
{
return launch_matrix_transpose_naive_base(
input_matrix, output_matrix, M, N,
GlobalMemoryCoalescedAccessMode::Read, stream);
}

template <typename T>
cudaError_t launch_matrix_transpose_naive_coalesced_write(T const* input_matrix,
T* output_matrix,
unsigned int M,
unsigned int N,
cudaStream_t stream)
{
return launch_matrix_transpose_naive_base(
input_matrix, output_matrix, M, N,
GlobalMemoryCoalescedAccessMode::Write, stream);
}

// Explicit instantiation.
template cudaError_t launch_matrix_transpose_naive_coalesced_read<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t launch_matrix_transpose_naive_coalesced_read<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

template cudaError_t launch_matrix_transpose_naive_coalesced_write<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t launch_matrix_transpose_naive_coalesced_write<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

Matrix Layout

There are typically two ways to describe a matrix stored in linear storages: row-major and column-major. In row-major layout, the elements of each row are contiguous in memory, while in column-major layout, the elements of each column are contiguous in memory. Given a $M \times N$ input matrix $A$ with $M$ rows and $N$ columns in row-major layout, typically we want to transpose the input matrix $A$ to a $N \times M$ output matrix $A^{\top}$ with $N$ rows and $M$ columns that is also in row-major layout. In this case, the output matrix $A^{\top}$ can not only be viewed as a row-major $N \times M$ matrix but also as a column-major $M \times N$ matrix.

The matrix transpose operation maps the element $A_{i, j}$ from the input matrix $A$ to the element $A^{\top}_{j, i}$ in the output matrix $A^{\top}$. On one hand, if the input matrix is described using row-major layout, the input matrix $A$ is of shape $(M, N)$, and the element $A_{i, j}$ is stored at the coordinate $(i, j)$ in the row-major layout of $A$. The output matrix $A^{\top}$ is of shape $(N, M)$, and the element $A^{\top}_{j, i}$ is stored at the coordinate $(j, i)$ in the row-major layout of $A^{\top}$. On the other hand, if the input matrix is described using column-major layout, the same input matrix $A$ is of shape $(M, N)$, and the element $A_{i, j}$ is stored at the coordinate $(j, i)$ in the column-major layout of $A$. The output matrix $A^{\top}$ is of shape $(M, N)$, and the element $A^{\top}_{j, i}$ is stored at the coordinate $(i, j)$ in the column-major layout of $A^{\top}$.

Although being a little bit brain-twisting, matrix transpose maps an element at the coordinate $(i, j)$ in the row-major layout of a matrix to the element at the coordinate $(i, j)$ in the column-major layout of the output matrix. In CuTe, given an 1D input coordinate and the input matrix and the output matrix both have a shape of $(M, N)$ in the layout, the 1D input coordinate will always be mapped to the same natural coordinate in both the input matrix and the output matrix. When CuTe iterates over $M \times N$ 1D coordinates, the corresponding elements in the input matrix and the output matrix is in a relationship of transpose. This is the key reason why we have to use row-major layout and column-major layout to describe the input matrix and the output matrix, respectively, in CuTe. Otherwise, if the input matrix and the output matrix are both described using the same layout, when CuTe iterates over $M \times N$ 1D coordinates, the corresponding elements in the input matrix and the output matrix will not be in a relationship of transpose.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
auto const tensor_shape{cute::make_shape(M, N)};
auto const tensor_shape_transposed{cute::make_shape(N, M)};

// Input matrix: row-major M x N matrix.
auto const global_memory_layout_src{cute::make_layout(
tensor_shape, cute::GenRowMajor{})}; // (M, N) : (N, 1)
// Output matrix: row-major N x M matrix.
auto const global_memory_layout_dst{cute::make_layout(
tensor_shape_transposed, cute::GenRowMajor{})}; // (N, M) : (M, 1)
// Same output matrix, but different view: column-major M x N matrix.
auto const global_memory_layout_dst_transposed{cute::make_layout(
tensor_shape, cute::GenColMajor{})}; // (M, N) : (1, M)

auto const tensor_src{cute::make_tensor(cute::make_gmem_ptr(input_matrix),
global_memory_layout_src)};
auto const tensor_dst{cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst)};
auto const tensor_dst_transposed{
cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst_transposed)};

Divide and Conquer and Matrix Tiling

To accelerate matrix transpose for large problems, we will have to divide the input matrix and the output matrix into smaller tiles and compute the transpose of each tile in parallel. In this example, we divide the input matrix and the output matrix into tiles of shape $(bM, bN)$, where $bM$ and $bN$ are the number of rows and columns in each tile, respectively. Both the input matrix and the output matrix will be divided into $\left\lceil \frac{M}{bM} \right\rceil \times \left\lceil \frac{N}{bN} \right\rceil$ tiles. The matrix transpose in each tile is independent and can be processed in parallel.

The divided input matrix and the divided output matrix now have new layouts, whose shapes are both $\left((bM, bN), \left\lceil \frac{M}{bM} \right\rceil \times \left\lceil \frac{N}{bN} \right\rceil\right)$. The row-major and column-major notations are no longer applicable for describing the divided matrices, because the shapes now have 3 modes, i.e., a rank of 3. To describe the storage layout of a tensor that has higher rank (any rank), CuTe uses stride. In our particular problem, it’s not too important, because CuTe automatically handles those concepts for us. In other problems, it might not be the case though.

1
2
3
4
5
6
7
8
9
10
11
using TileSizeX = cute::Int<64>; // bN
using TileSizeY = cute::Int<32>; // bM

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};

auto const tiled_tensor_src{cute::tiled_divide(
tensor_src, block_shape)}; // ((TileSizeY, TileSizeX), M /
// TileSizeY, N / TileSizeX)
auto const tiled_tensor_dst_transposed{cute::tiled_divide(
tensor_dst_transposed, block_shape)}; // ((TileSizeY, TileSizeX), M
// / TileSizeY, N / TileSizeX)

CUDA Thread Block Layout and Coalesced Memory Access

Each tile of the input matrix and the output matrix is processed by a CUDA thread block that consists of multiple CUDA threads. In our case, we use a thread block of shape $(tM, tN)$ and row-major layout or $(tN, tM)$ and column-major layout. The number of threads in a CUDA thread block is $tM \times tN$. The number of CUDA thread blocks to issue is, obviously, just the number of tiles, i.e., $\left\lceil \frac{M}{bM} \right\rceil \times \left\lceil \frac{N}{bN} \right\rceil$. This is feasible because $bM$ and $bN$, $tM$ and $tN$, are all compile-time constants.

Because the input matrix and its tiles are of row-major layout, and the output matrix and its tiles are of column-major layout, when the thread block is of row-major layout, each warp in the thread block will read from the input matrix on global memory in a coalesced fashion but write to the output matrix on global memory in a strided fashion. Similarly, when the thread block is of column-major layout, each warp in the thread block will read from the input matrix on global memory in a strided fashion but write to the output matrix on global memory in a coalesced fashion.

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
using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
// Coalesced memory read.
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
// Coalesced memory write.
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{
cute::size(ThreadBlockSizeX::value * ThreadBlockSizeY::value)};

if (coalesced_access_mode == GlobalMemoryCoalescedAccessMode::Read)
{
CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");
matrix_transpose_naive<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed, thread_layout);
}
else
{
CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeY");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeX");
matrix_transpose_naive<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
thread_layout_transposed);
}

Tensor Partitions

There are three major partitions in CuTe, inner-partition, outer-partition, and thread-value partition.

Inner-partition has been performed previously, where we divide the input matrix and the output matrix into tiles. Usually inner-partition is performed at the CUDA thread block level that distributes the large problems into smaller problems that can be solved by a single CUDA thread block.

Outer-partition is usually performed at the CUDA thread level that distributes the smaller problems into even smaller problems that can be solved by a single CUDA thread. There is a different between inner-partition and outer-partition, without understanding which the implementation can work correctly.

Suppose we have a CuTe layout $(8, 4) : (4, 1)$ and a tile layout $(4, 2) : (2, 1)$. Inner-partition will result in a layout of shape $\left((4, 2), \frac{8}{4}, \frac{4}{2}\right) = \left((4, 2), 2, 2\right)$, and outer-partition will result in a a layout of shape $\left(\left(\frac{8}{4}, \frac{4}{2}\right), 4, 2\right) = \left((2, 2), 4, 2\right)$. Assuming the partitions are accessed using the last two modes layout, the inner-partition layout has 4 partitions whereas the out-partition layout has 8 partitions. The starting coordinates of inner-partition and outer-partition are also different. In this case, the starting coordinates of inner-partition is $(0, 0)$, $(4, 0)$, $(0, 2)$, and $(4, 2)$, whereas the starting coordinates of outer-partition is $(0, 0)$, $(1, 0)$, $(2, 0)$, $(3, 0)$, $(0, 1)$, $(1, 1)$, $(2, 1)$, and $(3, 1)$. Outer-partition is usually performed at the CUDA thread level because all the consecutive threads in a warp, if accessing a piece of contiguous data synergistically especially on global memory, can have a better performance because of the CUDA coalesced memory access.

In each partition, the partition tensor will follow the layout algebra and apply the correct strides to access the data during iteration.

1
2
3
4
5
6
7
8
9
10
11
12
13
auto global_tile_src{tensor_src(cute::make_coord(cute::_, cute::_),
blockIdx.y,
blockIdx.x)}; // (TileSizeY, TileSizeX)
auto global_tile_dst_transposed{
tensor_dst_transposed(cute::make_coord(cute::_, cute::_), blockIdx.y,
blockIdx.x)}; // (TileSizeY, TileSizeX)

auto thread_global_tile_src{cute::local_partition(
global_tile_src, ThreadLayout{},
threadIdx.x)}; // (ThreadValueSizeY, ThreadValueSizeX)
auto thread_global_tile_dst_transposed{cute::local_partition(
global_tile_dst_transposed, ThreadLayout{},
threadIdx.x)}; // (ThreadValueSizeY, ThreadValueSizeX)

Predicates and Boundary Checking

CUDA memory access boundary checking is critical in a CUDA kernel in practice, when the problem distribution is not perfect. In CuTe, CUDA memory access boundary checking is performed via predicates. In our particular, we could query the matrix sizes $M$ and $N$ from the strides of the tiled tensor. It’s also more common to just pass these two values to the CUDA kernel.

During the iteration of the CuTe tensor, the iterator has to know its 2D coordinate and check if the element it is about to access is within the boundary. So we will create a 2D identity tensor (it’s an 2D array of tuples though) whose shape is exactly the same as the partition tensor from the global memory. The 2D identity tensor takes a 2D coordinate as input and produces the same 2D coordinate as output. If the partition tensor abd the identity tensor are iterated together, the iterator could get the information of its current coordinate within the partition tensor, making boundary checking possible. At the CUDA thread level, the 2D identity tensor is further partitioned into a 2D thread identity tensor according the same thread layout that is used for partitioning the data. Then the predicates used for accessing the partitioned input tensor and the output tensor can be prepared using the 2D coordinates of the iterator, the partitioned tensor index, the partitioned tensor and the original full tensor shape information.

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
// A 2D array of tuples that maps (x, y) to (x, y).
auto const identity_tensor{cute::make_identity_tensor(cute::make_shape(
cute::size<0>(global_tile_src), cute::size<1>(global_tile_src)))};
auto const thread_identity_tensor{
cute::local_partition(identity_tensor, ThreadLayout{}, threadIdx.x)};
auto fragment{cute::make_tensor_like(thread_global_tile_src)};
auto predicator{cute::make_tensor<bool>(
cute::make_shape(cute::size<0>(fragment), cute::size<1>(fragment)))};

auto const num_max_columns{cute::stride<0>(global_tile_src)};
auto const num_max_rows{cute::stride<1>(global_tile_dst_transposed)};
constexpr auto global_tile_columns{cute::size<1>(global_tile_src)};
constexpr auto global_tile_rows{cute::size<0>(global_tile_src)};

CUTE_UNROLL
for (unsigned int i{0}; i < cute::size<0>(predicator); ++i)
{
CUTE_UNROLL
for (unsigned int j{0}; j < cute::size<1>(predicator); ++j)
{
auto const thread_identity{thread_identity_tensor(i, j)};
bool const is_row_in_bound{cute::get<0>(thread_identity) +
blockIdx.y * global_tile_rows <
num_max_rows};
bool const is_column_in_bound{cute::get<1>(thread_identity) +
blockIdx.x * global_tile_columns <
num_max_columns};
predicator(i, j) = is_row_in_bound && is_column_in_bound;
}
}

cute::copy_if(predicator, thread_global_tile_src, fragment);
cute::copy_if(predicator, fragment, thread_global_tile_dst_transposed);

Using predicates and performing boundary checking can have degrade CUDA kernel performance, because the warp instruction, such as load from global memory, has to stall before all the predicates from all the threads in the warp are evaluated. To accelerate the computing the most, usually specialized kernels are used for each of the problem configurations and boundary checking are eliminated from the CUDA kernel.

So instead of using cute::copy_if, cute::copy should be used.

1
2
cute::copy(thread_global_tile_src, fragment);
cute::copy(fragment, thread_global_tile_dst_transposed);

CuTe Matrix Transpose Using Shared Memory

In the CuTe matrix transpose CUDA kernel implementation using shared memory, we will perform strided memory reads and writes on shared memory instead of global memory. Using shared memory naively will result in shared memory bank conflicts when performing strided memory reads or writes on shared memory, which will degrade the performance. To mitigate the shared memory bank conflicts, we will also perform special optimizations, such as shared memory padding and swizzling.

CuTe Matrix Transpose Using Shared Memory Implementation

The CuTe matrix transpose using shared memory CUDA kernel implementation could also be found from my CUTLASS Examples GitHub repository.

cute_matrix_transpose_shared_memory.cu
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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
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
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
#include <iomanip>
#include <iostream>

#include <cuda_runtime.h>

#include <cute/tensor.hpp>

#include "cute_matrix_transpose.hpp"

template <class TensorSrc, class TensorDst, class SharedMemoryLayoutSrc,
class SharedMemoryLayoutDst, class ThreadLayoutSrc,
class ThreadLayoutDst>
__global__ void static matrix_transpose_shared_memory(
TensorSrc tensor_src, TensorDst tensor_dst, SharedMemoryLayoutSrc,
SharedMemoryLayoutDst, ThreadLayoutSrc, ThreadLayoutDst)
{
using Element = typename TensorSrc::value_type;
CUTE_STATIC_ASSERT_V(cute::size(SharedMemoryLayoutSrc{}) ==
cute::size(SharedMemoryLayoutDst{}),
"SharedMemoryLayoutSrc and SharedMemoryLayoutDst "
"must have the same size.");
__shared__ Element shared_memory[cute::cosize(SharedMemoryLayoutSrc{})];

auto tensor_cache_src{cute::make_tensor(cute::make_smem_ptr(shared_memory),
SharedMemoryLayoutSrc{})};
auto tensor_cache_dst{cute::make_tensor(cute::make_smem_ptr(shared_memory),
SharedMemoryLayoutDst{})};

auto global_tile_src{tensor_src(cute::make_coord(cute::_, cute::_),
blockIdx.y,
blockIdx.x)}; // (TileSizeY, TileSizeX)
auto global_tile_dst{tensor_dst(cute::make_coord(cute::_, cute::_),
blockIdx.y,
blockIdx.x)}; // (TileSizeY, TileSizeX)

auto thread_global_tile_src{cute::local_partition(
global_tile_src, ThreadLayoutSrc{},
threadIdx.x)}; // (ThreadValueSizeY, ThreadValueSizeX)
auto thread_global_tile_dst{cute::local_partition(
global_tile_dst, ThreadLayoutDst{},
threadIdx.x)}; // (ThreadValueSizeX, ThreadValueSizeY)

auto thread_shared_tile_src{cute::local_partition(
tensor_cache_src, ThreadLayoutSrc{},
threadIdx.x)}; // (ThreadValueSizeY, ThreadValueSizeX)
auto thread_shared_tile_dst{cute::local_partition(
tensor_cache_dst, ThreadLayoutDst{},
threadIdx.x)}; // (ThreadValueSizeX, ThreadValueSizeY)

// A 2D array of tuples that maps (x, y) to (x, y).
auto const identity_tensor_src{cute::make_identity_tensor(cute::make_shape(
cute::size<0>(global_tile_src), cute::size<1>(global_tile_src)))};
auto const thread_identity_tensor_src{cute::local_partition(
identity_tensor_src, ThreadLayoutSrc{}, threadIdx.x)};
auto predicator_src{cute::make_tensor<bool>(
cute::make_shape(cute::size<0>(thread_global_tile_src),
cute::size<1>(thread_global_tile_src)))};

auto const identity_tensor_dst{cute::make_identity_tensor(cute::make_shape(
cute::size<0>(global_tile_dst), cute::size<1>(global_tile_dst)))};
auto const thread_identity_tensor_dst{cute::local_partition(
identity_tensor_dst, ThreadLayoutDst{}, threadIdx.x)};
auto predicator_dst{cute::make_tensor<bool>(
cute::make_shape(cute::size<0>(thread_global_tile_dst),
cute::size<1>(thread_global_tile_dst)))};

auto const num_max_columns{cute::stride<0>(global_tile_src)};
auto const num_max_rows{cute::stride<1>(global_tile_dst)};
constexpr auto global_tile_columns{cute::size<1>(global_tile_src)};
constexpr auto global_tile_rows{cute::size<0>(global_tile_src)};

CUTE_UNROLL
for (unsigned int i{0}; i < cute::size<0>(predicator_src); ++i)
{
CUTE_UNROLL
for (unsigned int j{0}; j < cute::size<1>(predicator_src); ++j)
{
auto const thread_identity{thread_identity_tensor_src(i, j)};
bool const is_row_in_bound{cute::get<0>(thread_identity) +
blockIdx.y * global_tile_rows <
num_max_rows};
bool const is_column_in_bound{cute::get<1>(thread_identity) +
blockIdx.x * global_tile_columns <
num_max_columns};
predicator_src(i, j) = is_row_in_bound && is_column_in_bound;
}
}

CUTE_UNROLL
for (unsigned int i{0}; i < cute::size<0>(predicator_dst); ++i)
{
CUTE_UNROLL
for (unsigned int j{0}; j < cute::size<1>(predicator_dst); ++j)
{
auto const thread_identity{thread_identity_tensor_dst(i, j)};
bool const is_row_in_bound{cute::get<0>(thread_identity) +
blockIdx.y * global_tile_rows <
num_max_rows};
bool const is_column_in_bound{cute::get<1>(thread_identity) +
blockIdx.x * global_tile_columns <
num_max_columns};
predicator_dst(i, j) = is_row_in_bound && is_column_in_bound;
}
}

cute::copy_if(predicator_src, thread_global_tile_src,
thread_shared_tile_src);
cute::cp_async_fence();
cute::cp_async_wait<0>();
__syncthreads();
cute::copy_if(predicator_dst, thread_shared_tile_dst,
thread_global_tile_dst);
}

template <class TensorSrc, class TensorDst, class SharedMemoryLayoutSrc,
class SharedMemoryLayoutDst, class ThreadLayoutSrc,
class ThreadLayoutDst, class VectorLayout>
__global__ void static matrix_transpose_shared_memory_vectorized(
TensorSrc tensor_src, TensorDst tensor_dst, SharedMemoryLayoutSrc,
SharedMemoryLayoutDst, ThreadLayoutSrc, ThreadLayoutDst, VectorLayout)
{
using Element = typename TensorSrc::value_type;
CUTE_STATIC_ASSERT_V(cute::size(SharedMemoryLayoutSrc{}) ==
cute::size(SharedMemoryLayoutDst{}),
"SharedMemoryLayoutSrc and SharedMemoryLayoutDst "
"must have the same size.");
__shared__ Element shared_memory[cute::cosize(SharedMemoryLayoutSrc{})];

auto tensor_cache_src{cute::make_tensor(cute::make_smem_ptr(shared_memory),
SharedMemoryLayoutSrc{})};
auto tensor_cache_dst{cute::make_tensor(cute::make_smem_ptr(shared_memory),
SharedMemoryLayoutDst{})};

auto global_tile_src{tensor_src(cute::make_coord(cute::_, cute::_),
blockIdx.y,
blockIdx.x)}; // (TileSizeY, TileSizeX)
auto global_tile_dst{tensor_dst(cute::make_coord(cute::_, cute::_),
blockIdx.y,
blockIdx.x)}; // (TileSizeY, TileSizeX)

using AccessType =
cutlass::AlignedArray<Element, cute::size(VectorLayout{})>;
using CopyAtom = cute::Copy_Atom<cute::UniversalCopy<AccessType>, Element>;
auto tiled_copy_src{
cute::make_tiled_copy(CopyAtom{}, ThreadLayoutSrc{}, VectorLayout{})};
auto thread_copy_src{tiled_copy_src.get_thread_slice(threadIdx.x)};

auto thread_global_tile_src{thread_copy_src.partition_S(
global_tile_src)}; // (CopyAtomShape, NumCopyTile)
auto thread_shared_tile_src{thread_copy_src.partition_D(
tensor_cache_src)}; // (CopyAtomShape, NumCopyTile)

auto thread_global_tile_dst{cute::local_partition(
global_tile_dst, ThreadLayoutDst{},
threadIdx.x)}; // (ThreadValueSizeX, ThreadValueSizeY)
auto thread_shared_tile_dst{cute::local_partition(
tensor_cache_dst, ThreadLayoutDst{},
threadIdx.x)}; // (ThreadValueSizeX, ThreadValueSizeY)

auto const num_max_columns{cute::stride<0>(global_tile_src)};
auto const num_max_rows{cute::stride<1>(global_tile_dst)};
constexpr auto global_tile_columns{cute::size<1>(global_tile_src)};
constexpr auto global_tile_rows{cute::size<0>(global_tile_src)};

// A 2D array of tuples that maps (x, y) to (x, y).
auto const identity_tensor_src{cute::make_identity_tensor(cute::make_shape(
cute::size<0>(global_tile_src), cute::size<1>(global_tile_src)))};
auto thread_identity_tensor_src{thread_copy_src.partition_S(
identity_tensor_src)}; // (CopyAtomShape, NumCopyTile)
auto predicator_src{cute::make_tensor<bool>(
cute::make_shape(cute::size<1>(thread_global_tile_src),
cute::size<2>(thread_global_tile_src)))};

CUTE_UNROLL
for (unsigned int i{0}; i < cute::size<0>(predicator_src); ++i)
{
CUTE_UNROLL
for (unsigned int j{0}; j < cute::size<1>(predicator_src); ++j)
{
auto const thread_identity{thread_identity_tensor_src(0, i, j)};
bool const is_row_in_bound{cute::get<0>(thread_identity) +
blockIdx.y * global_tile_rows <
num_max_rows};
bool const is_column_in_bound{cute::get<1>(thread_identity) +
blockIdx.x * global_tile_columns <
num_max_columns};
predicator_src(i, j) = is_row_in_bound && is_column_in_bound;
}
}

auto const identity_tensor_dst{cute::make_identity_tensor(cute::make_shape(
cute::size<0>(global_tile_dst), cute::size<1>(global_tile_dst)))};
auto const thread_identity_tensor_dst{cute::local_partition(
identity_tensor_dst, ThreadLayoutDst{}, threadIdx.x)};
auto predicator_dst{cute::make_tensor<bool>(
cute::make_shape(cute::size<0>(thread_global_tile_dst),
cute::size<1>(thread_global_tile_dst)))};

CUTE_UNROLL
for (unsigned int i{0}; i < cute::size<0>(predicator_dst); ++i)
{
CUTE_UNROLL
for (unsigned int j{0}; j < cute::size<1>(predicator_dst); ++j)
{
auto const thread_identity{thread_identity_tensor_dst(i, j)};
bool const is_row_in_bound{cute::get<0>(thread_identity) +
blockIdx.y * global_tile_rows <
num_max_rows};
bool const is_column_in_bound{cute::get<1>(thread_identity) +
blockIdx.x * global_tile_columns <
num_max_columns};
predicator_dst(i, j) = is_row_in_bound && is_column_in_bound;
}
}

cute::copy_if(tiled_copy_src, predicator_src, thread_global_tile_src,
thread_shared_tile_src);
cute::cp_async_fence();
cute::cp_async_wait<0>();
__syncthreads();
cute::copy_if(predicator_dst, thread_shared_tile_dst,
thread_global_tile_dst);
}

enum class SharedMemoryBankConflictAccessMode
{
Read,
Write
};

template <typename T>
static cudaError_t launch_matrix_transpose_shared_memory_bank_conflict_base(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
SharedMemoryBankConflictAccessMode bank_conflict_access_mode,
cudaStream_t stream)
{
auto const tensor_shape{cute::make_shape(M, N)};
auto const tensor_shape_transposed{cute::make_shape(N, M)};

// Input matrix: row-major M x N matrix.
auto const global_memory_layout_src{cute::make_layout(
tensor_shape, cute::GenRowMajor{})}; // (M, N) : (N, 1)
// Output matrix: row-major N x M matrix.
auto const global_memory_layout_dst{cute::make_layout(
tensor_shape_transposed, cute::GenRowMajor{})}; // (N, M) : (M, 1)
// Same output matrix, but different view: column-major M x N matrix.
auto const global_memory_layout_dst_transposed{cute::make_layout(
tensor_shape, cute::GenColMajor{})}; // (M, N) : (1, M)

auto const tensor_src{cute::make_tensor(cute::make_gmem_ptr(input_matrix),
global_memory_layout_src)};
auto const tensor_dst{cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst)};
auto const tensor_dst_transposed{
cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst_transposed)};

using TileSizeX = cute::Int<128>; // bN
using TileSizeY = cute::Int<32>; // bM

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};
constexpr auto block_shape_transposed{
cute::make_shape(TileSizeX{}, TileSizeY{})};

auto const shared_memory_layout_src{cute::make_layout(
block_shape, cute::GenRowMajor{})}; // (bM, bN) : (bN, 1)
auto const shared_memory_layout_dst{cute::make_layout(
block_shape_transposed, cute::GenRowMajor{})}; // (bN, bM) : (bM, 1)
auto const shared_memory_layout_dst_transposed{cute::make_layout(
block_shape, cute::GenColMajor{})}; // (bM, bN) : (1, bM)

auto const tiled_tensor_src{cute::tiled_divide(
tensor_src, block_shape)}; // ((TileSizeY, TileSizeX), M /
// TileSizeY, N / TileSizeX)
auto const tiled_tensor_dst{cute::tiled_divide(
tensor_dst, block_shape_transposed)}; // ((TileSizeX, TileSizeY), N
// / TileSizeX, M / TileSizeY)
auto const tiled_tensor_dst_transposed{cute::tiled_divide(
tensor_dst_transposed, block_shape)}; // ((TileSizeY, TileSizeX), M
// / TileSizeY, N / TileSizeX)

using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{ThreadBlockSizeX::value * ThreadBlockSizeY::value};

if (bank_conflict_access_mode == SharedMemoryBankConflictAccessMode::Read)
{
matrix_transpose_shared_memory<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_src, shared_memory_layout_src, thread_layout,
thread_layout_transposed);
}
else
{
matrix_transpose_shared_memory<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_dst_transposed,
shared_memory_layout_dst_transposed, thread_layout,
thread_layout_transposed);
}

return cudaGetLastError();
}

template <typename T>
static cudaError_t
launch_matrix_transpose_shared_memory_vectorized_bank_conflict_base(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
SharedMemoryBankConflictAccessMode bank_conflict_access_mode,
cudaStream_t stream)
{
using VectorType = cute::uint128_t;
CUTE_STATIC_ASSERT(sizeof(VectorType) % sizeof(T) == 0,
"sizeof(VectorType) must be a multiple of sizeof(T)");
constexpr unsigned int NUM_VECTOR_ELEMENTS{sizeof(VectorType) / sizeof(T)};

if (N % NUM_VECTOR_ELEMENTS != 0)
{
return cudaErrorInvalidValue;
}

auto const tensor_shape{cute::make_shape(M, N)};
auto const tensor_shape_transposed{cute::make_shape(N, M)};

// Input matrix: row-major M x N matrix.
auto const global_memory_layout_src{cute::make_layout(
tensor_shape, cute::GenRowMajor{})}; // (M, N) : (N, 1)
// Output matrix: row-major N x M matrix.
auto const global_memory_layout_dst{cute::make_layout(
tensor_shape_transposed, cute::GenRowMajor{})}; // (N, M) : (M, 1)
// Same output matrix, but different view: column-major M x N matrix.
auto const global_memory_layout_dst_transposed{cute::make_layout(
tensor_shape, cute::GenColMajor{})}; // (M, N) : (1, M)

auto const tensor_src{cute::make_tensor(cute::make_gmem_ptr(input_matrix),
global_memory_layout_src)};
auto const tensor_dst{cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst)};
auto const tensor_dst_transposed{
cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst_transposed)};

using TileSizeX = cute::Int<128>; // bN
using TileSizeY = cute::Int<32>; // bM

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};
constexpr auto block_shape_transposed{
cute::make_shape(TileSizeX{}, TileSizeY{})};

auto const shared_memory_layout_src{cute::make_layout(
block_shape, cute::GenRowMajor{})}; // (bM, bN) : (bN, 1)
auto const shared_memory_layout_dst{cute::make_layout(
block_shape_transposed, cute::GenRowMajor{})}; // (bN, bM) : (bM, 1)
auto const shared_memory_layout_dst_transposed{cute::make_layout(
block_shape, cute::GenColMajor{})}; // (bM, bN) : (1, bM)

auto const tiled_tensor_src{cute::tiled_divide(
tensor_src, block_shape)}; // ((TileSizeY, TileSizeX), M /
// TileSizeY, N / TileSizeX)
auto const tiled_tensor_dst{cute::tiled_divide(
tensor_dst, block_shape_transposed)}; // ((TileSizeX, TileSizeY), N
// / TileSizeX, M / TileSizeY)
auto const tiled_tensor_dst_transposed{cute::tiled_divide(
tensor_dst_transposed, block_shape)}; // ((TileSizeY, TileSizeX), M
// / TileSizeY, N / TileSizeX)

using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

using VECTOR_SIZE_X = cute::Int<NUM_VECTOR_ELEMENTS>;
constexpr auto vector_shape{
cute::make_shape(cute::Int<1>{}, VECTOR_SIZE_X{})};
// Copy atom vector layout.
constexpr auto vector_layout{
cute::make_layout(vector_shape, cute::GenRowMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{ThreadBlockSizeX::value * ThreadBlockSizeY::value};

if (bank_conflict_access_mode == SharedMemoryBankConflictAccessMode::Read)
{
matrix_transpose_shared_memory_vectorized<<<grid_dim, thread_dim, 0,
stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_src, shared_memory_layout_src, thread_layout,
thread_layout_transposed, vector_layout);
}
else
{
return cudaErrorInvalidValue;
}

return cudaGetLastError();
}

template <typename T>
cudaError_t launch_matrix_transpose_shared_memory_bank_conflict_read(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
cudaStream_t stream)
{
return launch_matrix_transpose_shared_memory_bank_conflict_base(
input_matrix, output_matrix, M, N,
SharedMemoryBankConflictAccessMode::Read, stream);
}

template <typename T>
cudaError_t launch_matrix_transpose_shared_memory_bank_conflict_write(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
cudaStream_t stream)
{
return launch_matrix_transpose_shared_memory_bank_conflict_base(
input_matrix, output_matrix, M, N,
SharedMemoryBankConflictAccessMode::Write, stream);
}

template <typename T>
cudaError_t launch_matrix_transpose_shared_memory_vectorized_bank_conflict_read(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
cudaStream_t stream)
{
return launch_matrix_transpose_shared_memory_vectorized_bank_conflict_base<
T>(input_matrix, output_matrix, M, N,
SharedMemoryBankConflictAccessMode::Read, stream);
}

template <typename T>
static cudaError_t launch_matrix_transpose_shared_memory_padded(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
cudaStream_t stream)
{
auto const tensor_shape{cute::make_shape(M, N)};
auto const tensor_shape_transposed{cute::make_shape(N, M)};

// Input matrix: row-major M x N matrix.
auto const global_memory_layout_src{cute::make_layout(
tensor_shape, cute::GenRowMajor{})}; // (M, N) : (N, 1)
// Output matrix: row-major N x M matrix.
auto const global_memory_layout_dst{cute::make_layout(
tensor_shape_transposed, cute::GenRowMajor{})}; // (N, M) : (M, 1)
// Same output matrix, but different view: column-major M x N matrix.
auto const global_memory_layout_dst_transposed{cute::make_layout(
tensor_shape, cute::GenColMajor{})}; // (M, N) : (1, M)

auto const tensor_src{cute::make_tensor(cute::make_gmem_ptr(input_matrix),
global_memory_layout_src)};
auto const tensor_dst{cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst)};
auto const tensor_dst_transposed{
cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst_transposed)};

using TileSizeX = cute::Int<64>; // bN
using TILE_SIZE_X_PADDED = cute::Int<65>; // bN + 1
using TileSizeY = cute::Int<32>; // bM

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};
constexpr auto block_shape_transposed{
cute::make_shape(TileSizeX{}, TileSizeY{})};

auto const shared_memory_layout_src{cute::make_layout(
block_shape, cute::GenRowMajor{})}; // (bM, bN) : (bN, 1)
auto const shared_memory_layout_src_padded{cute::make_layout(
block_shape,
cute::make_stride(TILE_SIZE_X_PADDED{},
cute::Int<1>{}))}; // (bM, bN) : (bN + 1, 1)
auto const shared_memory_layout_dst{cute::make_layout(
block_shape_transposed, cute::GenRowMajor{})}; // (bN, bM) : (bM, 1)
auto const shared_memory_layout_dst_transposed{cute::make_layout(
block_shape, cute::GenColMajor{})}; // (bM, bN) : (1, bM)

auto const tiled_tensor_src{cute::tiled_divide(
tensor_src, block_shape)}; // ((TileSizeY, TileSizeX), M /
// TileSizeY, N / TileSizeX)
auto const tiled_tensor_dst{cute::tiled_divide(
tensor_dst, block_shape_transposed)}; // ((TileSizeX, TileSizeY), N
// / TileSizeX, M / TileSizeY)
auto const tiled_tensor_dst_transposed{cute::tiled_divide(
tensor_dst_transposed, block_shape)}; // ((TileSizeY, TileSizeX), M
// / TileSizeY, N / TileSizeX)

using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{ThreadBlockSizeX::value * ThreadBlockSizeY::value};

matrix_transpose_shared_memory<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_src_padded, shared_memory_layout_src_padded,
thread_layout, thread_layout_transposed);

return cudaGetLastError();
}

template <typename T>
static cudaError_t launch_matrix_transpose_shared_memory_vectorized_padded(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
cudaStream_t stream)
{
using VectorType = cute::uint128_t;
CUTE_STATIC_ASSERT(sizeof(VectorType) % sizeof(T) == 0,
"sizeof(VectorType) must be a multiple of sizeof(T)");
constexpr unsigned int NUM_VECTOR_ELEMENTS{sizeof(VectorType) / sizeof(T)};

if (N % NUM_VECTOR_ELEMENTS != 0)
{
return cudaErrorInvalidValue;
}

auto const tensor_shape{cute::make_shape(M, N)};
auto const tensor_shape_transposed{cute::make_shape(N, M)};

// Input matrix: row-major M x N matrix.
auto const global_memory_layout_src{cute::make_layout(
tensor_shape, cute::GenRowMajor{})}; // (M, N) : (N, 1)
// Output matrix: row-major N x M matrix.
auto const global_memory_layout_dst{cute::make_layout(
tensor_shape_transposed, cute::GenRowMajor{})}; // (N, M) : (M, 1)
// Same output matrix, but different view: column-major M x N matrix.
auto const global_memory_layout_dst_transposed{cute::make_layout(
tensor_shape, cute::GenColMajor{})}; // (M, N) : (1, M)

auto const tensor_src{cute::make_tensor(cute::make_gmem_ptr(input_matrix),
global_memory_layout_src)};
auto const tensor_dst{cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst)};
auto const tensor_dst_transposed{
cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst_transposed)};

using TileSizeX = cute::Int<128>; // bN
// Such padding is necessary for the byte alignment of the vectorized
// access. However, the shared memory bank conflict mitigation can be
// compromised.
using TILE_SIZE_X_PADDED =
cute::Int<128 + NUM_VECTOR_ELEMENTS>; // bN + NUM_VECTOR_ELEMENTS
using TileSizeY = cute::Int<32>; // bM

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};
constexpr auto block_shape_transposed{
cute::make_shape(TileSizeX{}, TileSizeY{})};

auto const shared_memory_layout_src{cute::make_layout(
block_shape, cute::GenRowMajor{})}; // (bM, bN) : (bN, 1)
auto const shared_memory_layout_src_padded{cute::make_layout(
block_shape,
cute::make_stride(TILE_SIZE_X_PADDED{},
cute::Int<1>{}))}; // (bM, bN) : (bN + 1, 1)
auto const shared_memory_layout_dst{cute::make_layout(
block_shape_transposed, cute::GenRowMajor{})}; // (bN, bM) : (bM, 1)
auto const shared_memory_layout_dst_transposed{cute::make_layout(
block_shape, cute::GenColMajor{})}; // (bM, bN) : (1, bM)

auto const tiled_tensor_src{cute::tiled_divide(
tensor_src, block_shape)}; // ((TileSizeY, TileSizeX), M /
// TileSizeY, N / TileSizeX)
auto const tiled_tensor_dst{cute::tiled_divide(
tensor_dst, block_shape_transposed)}; // ((TileSizeX, TileSizeY), N
// / TileSizeX, M / TileSizeY)
auto const tiled_tensor_dst_transposed{cute::tiled_divide(
tensor_dst_transposed, block_shape)}; // ((TileSizeY, TileSizeX), M
// / TileSizeY, N / TileSizeX)

using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{ThreadBlockSizeX::value * ThreadBlockSizeY::value};

using VECTOR_SIZE_X = cute::Int<NUM_VECTOR_ELEMENTS>;
constexpr auto vector_shape{
cute::make_shape(cute::Int<1>{}, VECTOR_SIZE_X{})};
// Copy atom vector layout.
constexpr auto vector_layout{
cute::make_layout(vector_shape, cute::GenRowMajor{})};

matrix_transpose_shared_memory_vectorized<<<grid_dim, thread_dim, 0,
stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_src_padded, shared_memory_layout_src_padded,
thread_layout, thread_layout_transposed, vector_layout);

return cudaGetLastError();
}

template <class SHARED_MEMORY_LAYOUT>
static void
print_shared_memory_bank_ids(SHARED_MEMORY_LAYOUT shared_memory_layout)
{
// Print the shared memory bank ids.
for (unsigned int i{0}; i < cute::size<0>(shared_memory_layout); ++i)
{
for (unsigned int j{0}; j < cute::size<1>(shared_memory_layout); ++j)
{
std::cout << std::setw(2) << shared_memory_layout(i, j) % 32 << " ";
}
std::cout << std::endl;
}
}

constexpr int constexpr_log2(int n)
{
return ((n < 2) ? 0 : 1 + constexpr_log2(n / 2));
}

template <typename T>
static cudaError_t launch_matrix_transpose_shared_memory_swizzled(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
cudaStream_t stream)
{
auto const tensor_shape{cute::make_shape(M, N)};
auto const tensor_shape_transposed{cute::make_shape(N, M)};

// Input matrix: row-major M x N matrix.
auto const global_memory_layout_src{cute::make_layout(
tensor_shape, cute::GenRowMajor{})}; // (M, N) : (N, 1)
// Output matrix: row-major N x M matrix.
auto const global_memory_layout_dst{cute::make_layout(
tensor_shape_transposed, cute::GenRowMajor{})}; // (N, M) : (M, 1)
// Same output matrix, but different view: column-major M x N matrix.
auto const global_memory_layout_dst_transposed{cute::make_layout(
tensor_shape, cute::GenColMajor{})}; // (M, N) : (1, M)

auto const tensor_src{cute::make_tensor(cute::make_gmem_ptr(input_matrix),
global_memory_layout_src)};
auto const tensor_dst{cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst)};
auto const tensor_dst_transposed{
cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst_transposed)};

using TileSizeX = cute::Int<64>; // bN
using TileSizeY = cute::Int<32>; // bM
constexpr int NUM_BASE_BITS{constexpr_log2(1)};
constexpr int NUM_MASK_BITS{constexpr_log2(32 * 4 / sizeof(T)) -
NUM_BASE_BITS};
constexpr int NUM_SHIFT_BITS{constexpr_log2(TileSizeX::value) -
NUM_BASE_BITS};

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};
constexpr auto block_shape_transposed{
cute::make_shape(TileSizeX{}, TileSizeY{})};

auto const shared_memory_layout_src{cute::make_layout(
block_shape, cute::GenRowMajor{})}; // (bM, bN) : (bN, 1)
auto const shared_memory_layout_dst{cute::make_layout(
block_shape_transposed, cute::GenRowMajor{})}; // (bN, bM) : (bM, 1)
auto const shared_memory_layout_dst_transposed{cute::make_layout(
block_shape, cute::GenColMajor{})}; // (bM, bN) : (1, bM)

auto const swizzle_src{
cute::Swizzle<NUM_MASK_BITS, NUM_BASE_BITS, NUM_SHIFT_BITS>{}};
auto const shared_memory_layout_swizzled_src{
cute::composition(swizzle_src, shared_memory_layout_src)};

// Inspect if the swizzling reduces the shared memory bank conflicts.
// print_shared_memory_bank_ids(shared_memory_layout_swizzled_src);

auto const tiled_tensor_src{cute::tiled_divide(
tensor_src, block_shape)}; // ((TileSizeY, TileSizeX), M /
// TileSizeY, N / TileSizeX)
auto const tiled_tensor_dst{cute::tiled_divide(
tensor_dst, block_shape_transposed)}; // ((TileSizeX, TileSizeY), N
// / TileSizeX, M / TileSizeY)
auto const tiled_tensor_dst_transposed{cute::tiled_divide(
tensor_dst_transposed, block_shape)}; // ((TileSizeY, TileSizeX), M
// / TileSizeY, N / TileSizeX)

using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{ThreadBlockSizeX::value * ThreadBlockSizeY::value};

matrix_transpose_shared_memory<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_swizzled_src, shared_memory_layout_swizzled_src,
thread_layout, thread_layout_transposed);

return cudaGetLastError();
}

template <typename T>
static cudaError_t launch_matrix_transpose_shared_memory_vectorized_swizzled(
T const* input_matrix, T* output_matrix, unsigned int M, unsigned int N,
cudaStream_t stream)
{
using VectorType = cute::uint128_t;
CUTE_STATIC_ASSERT(sizeof(VectorType) % sizeof(T) == 0,
"sizeof(VectorType) must be a multiple of sizeof(T)");
constexpr unsigned int NUM_VECTOR_ELEMENTS{sizeof(VectorType) / sizeof(T)};

if (N % NUM_VECTOR_ELEMENTS != 0)
{
return cudaErrorInvalidValue;
}

auto const tensor_shape{cute::make_shape(M, N)};
auto const tensor_shape_transposed{cute::make_shape(N, M)};

// Input matrix: row-major M x N matrix.
auto const global_memory_layout_src{cute::make_layout(
tensor_shape, cute::GenRowMajor{})}; // (M, N) : (N, 1)
// Output matrix: row-major N x M matrix.
auto const global_memory_layout_dst{cute::make_layout(
tensor_shape_transposed, cute::GenRowMajor{})}; // (N, M) : (M, 1)
// Same output matrix, but different view: column-major M x N matrix.
auto const global_memory_layout_dst_transposed{cute::make_layout(
tensor_shape, cute::GenColMajor{})}; // (M, N) : (1, M)

auto const tensor_src{cute::make_tensor(cute::make_gmem_ptr(input_matrix),
global_memory_layout_src)};
auto const tensor_dst{cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst)};
auto const tensor_dst_transposed{
cute::make_tensor(cute::make_gmem_ptr(output_matrix),
global_memory_layout_dst_transposed)};

using TileSizeX = cute::Int<128>; // bN
using TileSizeY = cute::Int<32>; // bM
constexpr int NUM_BASE_BITS{constexpr_log2(NUM_VECTOR_ELEMENTS)};
constexpr int NUM_MASK_BITS{constexpr_log2(32 * 4 / sizeof(T)) -
NUM_BASE_BITS};
constexpr int NUM_SHIFT_BITS{constexpr_log2(TileSizeX::value) -
NUM_BASE_BITS};

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};
constexpr auto block_shape_transposed{
cute::make_shape(TileSizeX{}, TileSizeY{})};

auto const shared_memory_layout_src{cute::make_layout(
block_shape, cute::GenRowMajor{})}; // (bM, bN) : (bN, 1)
auto const shared_memory_layout_dst{cute::make_layout(
block_shape_transposed, cute::GenRowMajor{})}; // (bN, bM) : (bM, 1)
auto const shared_memory_layout_dst_transposed{cute::make_layout(
block_shape, cute::GenColMajor{})}; // (bM, bN) : (1, bM)

// Because of the vectorized access, NUM_BASE_BITS cannot be zero.
// The shared memory bank conflict mitigation can be compromised.
// Print the shared memory bank ids to see the details.
auto const swizzle_src{
cute::Swizzle<NUM_MASK_BITS, NUM_BASE_BITS, NUM_SHIFT_BITS>{}};
auto const shared_memory_layout_swizzled_src{
cute::composition(swizzle_src, shared_memory_layout_src)};

// Inspect if the swizzling reduces the shared memory bank conflicts.
// print_shared_memory_bank_ids(shared_memory_layout_swizzled_src);

auto const tiled_tensor_src{cute::tiled_divide(
tensor_src, block_shape)}; // ((TileSizeY, TileSizeX), M /
// TileSizeY, N / TileSizeX)
auto const tiled_tensor_dst{cute::tiled_divide(
tensor_dst, block_shape_transposed)}; // ((TileSizeX, TileSizeY), N
// / TileSizeX, M / TileSizeY)
auto const tiled_tensor_dst_transposed{cute::tiled_divide(
tensor_dst_transposed, block_shape)}; // ((TileSizeY, TileSizeX), M
// / TileSizeY, N / TileSizeX)

using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

using VECTOR_SIZE_X = cute::Int<NUM_VECTOR_ELEMENTS>;
constexpr auto vector_shape{
cute::make_shape(cute::Int<1>{}, VECTOR_SIZE_X{})};
// Copy atom vector layout.
constexpr auto vector_layout{
cute::make_layout(vector_shape, cute::GenRowMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{ThreadBlockSizeX::value * ThreadBlockSizeY::value};

matrix_transpose_shared_memory_vectorized<<<grid_dim, thread_dim, 0,
stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_swizzled_src, shared_memory_layout_swizzled_src,
thread_layout, thread_layout_transposed, vector_layout);

return cudaGetLastError();
}

// Explicit instantiation.
template cudaError_t
launch_matrix_transpose_shared_memory_bank_conflict_read<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t
launch_matrix_transpose_shared_memory_bank_conflict_read<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

template cudaError_t
launch_matrix_transpose_shared_memory_vectorized_bank_conflict_read<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t
launch_matrix_transpose_shared_memory_vectorized_bank_conflict_read<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

template cudaError_t
launch_matrix_transpose_shared_memory_bank_conflict_write<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t
launch_matrix_transpose_shared_memory_bank_conflict_write<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

template cudaError_t launch_matrix_transpose_shared_memory_padded<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t launch_matrix_transpose_shared_memory_padded<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

template cudaError_t
launch_matrix_transpose_shared_memory_vectorized_padded<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t
launch_matrix_transpose_shared_memory_vectorized_padded<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

template cudaError_t launch_matrix_transpose_shared_memory_swizzled<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t launch_matrix_transpose_shared_memory_swizzled<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

template cudaError_t
launch_matrix_transpose_shared_memory_vectorized_swizzled<float>(
float const* input_matrix, float* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);
template cudaError_t
launch_matrix_transpose_shared_memory_vectorized_swizzled<double>(
double const* input_matrix, double* output_matrix, unsigned int M,
unsigned int N, cudaStream_t stream);

Shared Memory Layout and CUDA Thread Block Layout

Because the strided memory access will be performed on shared memory, the global memory reads and writes can be fully coalesced. Then we have two options about how to perform the strided memory reads or writes on shared memory. The first option is to perform matrix transpose when reading from global memory to shared memory, and then perform matrix copy from shared memory to global memory, resulting in strided memory writes on shared memory. The second option is to perform matrix copy when reading from global memory to shared memory, and then perform matrix transpose when writing from shared memory to global memory, resulting in strided memory reads on shared memory.

To implement the first option, the shared memory layout has to be column-major if the input matrix layout is row-major. Two different CUDA thread block layouts are used for reading from global memory to shared memory and writing from shared memory to global memory. The first CUDA thread block layout is row-major if the input matrix layout is row-major, resulting in coalesced memory reads from global memory and strided memory writes to shared memory. The second CUDA thread block layout is column-major if the input matrix layout is row-major, which is the same as the output matrix layout, resulting in coalesced memory reads from shared memory and coalesced memory writes to global memory.

To implement the second option, the shared memory layout has to be row-major if the input matrix layout is row-major. Two different CUDA thread block layouts are used for reading from global memory to shared memory and writing from shared memory to global memory. The first CUDA thread block layout is row-major if the input matrix layout is row-major, resulting in coalesced memory reads from global memory and coalesced memory writes to shared memory. The second CUDA thread block layout is column-major if the input matrix layout is row-major, which is the same as the output matrix layout, resulting in strided memory reads from shared memory and coalesced memory writes to global memory.

The strided reads and writes on shared memory will result in as severe as 32-way shared memory bank conflicts. On certain platforms, this will significantly reduce the performance.

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
using ThreadBlockSizeX = cute::Int<32>; // tN
using ThreadBlockSizeY = cute::Int<8>; // tM

CUTE_STATIC_ASSERT_V(TileSizeX{} % ThreadBlockSizeX{} == cute::Int<0>{},
"TileSizeX must be divisible by ThreadBlockSizeX");
CUTE_STATIC_ASSERT_V(TileSizeY{} % ThreadBlockSizeY{} == cute::Int<0>{},
"TileSizeY must be divisible by ThreadBlockSizeY");

constexpr auto thread_block_shape{
cute::make_shape(ThreadBlockSizeY{}, ThreadBlockSizeX{})};
constexpr auto thread_block_shape_transposed{
cute::make_shape(ThreadBlockSizeX{}, ThreadBlockSizeY{})};
constexpr auto thread_layout{
cute::make_layout(thread_block_shape, cute::GenRowMajor{})};
constexpr auto thread_layout_transposed{
cute::make_layout(thread_block_shape_transposed, cute::GenColMajor{})};

dim3 const grid_dim{cute::size<2>(tiled_tensor_src),
cute::size<1>(tiled_tensor_src)};
dim3 const thread_dim{ThreadBlockSizeX::value * ThreadBlockSizeY::value};

if (bank_conflict_access_mode == SharedMemoryBankConflictAccessMode::Read)
{
matrix_transpose_shared_memory<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_src, shared_memory_layout_src, thread_layout,
thread_layout_transposed);
}
else
{
matrix_transpose_shared_memory<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_dst_transposed,
shared_memory_layout_dst_transposed, thread_layout,
thread_layout_transposed);
}

Predicates and Boundary Checking

One typical mistake I would make in the implementation is to use the same predicate for both reading from global memory to shared memory and writing from shared memory to global memory because the shapes of the global memory input matrix tile layout, the global memory output matrix tile layout, the shared memory layout are the same. We could not reuse the predicate because the thread layouts for reading from global memory to shared memory and writing from shared memory to global memory are different. Therefore, even for the same thread, different identity tuples are assigned for reading from global memory to shared memory and writing from shared memory to global memory, and we have to use two sets of predicates.

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
// A 2D array of tuples that maps (x, y) to (x, y).
auto const identity_tensor_src{cute::make_identity_tensor(cute::make_shape(
cute::size<0>(global_tile_src), cute::size<1>(global_tile_src)))};
auto const thread_identity_tensor_src{cute::local_partition(
identity_tensor_src, ThreadLayoutSrc{}, threadIdx.x)};
auto predicator_src{cute::make_tensor<bool>(
cute::make_shape(cute::size<0>(thread_global_tile_src),
cute::size<1>(thread_global_tile_src)))};

auto const identity_tensor_dst{cute::make_identity_tensor(cute::make_shape(
cute::size<0>(global_tile_dst), cute::size<1>(global_tile_dst)))};
auto const thread_identity_tensor_dst{cute::local_partition(
identity_tensor_dst, ThreadLayoutDst{}, threadIdx.x)};
auto predicator_dst{cute::make_tensor<bool>(
cute::make_shape(cute::size<0>(thread_global_tile_dst),
cute::size<1>(thread_global_tile_dst)))};

auto const num_max_columns{cute::stride<0>(global_tile_src)};
auto const num_max_rows{cute::stride<1>(global_tile_dst)};
constexpr auto global_tile_columns{cute::size<1>(global_tile_src)};
constexpr auto global_tile_rows{cute::size<0>(global_tile_src)};

CUTE_UNROLL
for (unsigned int i{0}; i < cute::size<0>(predicator_src); ++i)
{
CUTE_UNROLL
for (unsigned int j{0}; j < cute::size<1>(predicator_src); ++j)
{
auto const thread_identity{thread_identity_tensor_src(i, j)};
bool const is_row_in_bound{cute::get<0>(thread_identity) +
blockIdx.y * global_tile_rows <
num_max_rows};
bool const is_column_in_bound{cute::get<1>(thread_identity) +
blockIdx.x * global_tile_columns <
num_max_columns};
predicator_src(i, j) = is_row_in_bound && is_column_in_bound;
}
}

CUTE_UNROLL
for (unsigned int i{0}; i < cute::size<0>(predicator_dst); ++i)
{
CUTE_UNROLL
for (unsigned int j{0}; j < cute::size<1>(predicator_dst); ++j)
{
auto const thread_identity{thread_identity_tensor_dst(i, j)};
bool const is_row_in_bound{cute::get<0>(thread_identity) +
blockIdx.y * global_tile_rows <
num_max_rows};
bool const is_column_in_bound{cute::get<1>(thread_identity) +
blockIdx.x * global_tile_columns <
num_max_columns};
predicator_dst(i, j) = is_row_in_bound && is_column_in_bound;
}
}

Thread Block Synchronization

Because shared memory is used as a cache to store the intermediate matrix tile for transpose and all the threads in the same thread block are synergistically reading the matrix tile from global memory to shared memory, before the writing the matrix tile from shared memory to global memory, we have to make sure all the threads in the same thread block have finished reading the matrix tile from global memory to shared memory. In addition to the commonly used __syncthreads(), cute::cp_async_fence() and cute::cp_async_wait<0>() are also used in CuTe for thread block synchronization. This is because cute::copy_if and cute::copy can be asynchronous operations on SM80 and above platforms. cute::cp_async_fence() and cute::cp_async_wait<0>() are no-ops on platforms lower than SM80.

1
2
3
4
5
6
7
cute::copy_if(predicator_src, thread_global_tile_src,
thread_shared_tile_src);
cute::cp_async_fence();
cute::cp_async_wait<0>();
__syncthreads();
cute::copy_if(predicator_dst, thread_shared_tile_dst,
thread_global_tile_dst);

Shared Memory Padding

The shared memory padding is a common trick to avoid shared memory bank conflicts when a warp of threads is accessing shared memory.

In our case, assuming we have the strided memory read on shared memory. Then instead of using the shared memory layout of $(bM, bN) : (bN, 1)$, the padded shared memory layout should be $(bM, bN) : (bN + 1, 1)$. Notice that the shared memory shape remains unchanged, but the stride of the shared memory layout gets changed, resulting in the shared memory cosize, i.e. the shared memory that needs to be allocated, is also changed. Using the shared memory layout of $(bM, bN + 1) : (bN + 1, 1)$ is incorrect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using TILE_SIZE_X = cute::Int<64>;        // bN
using TILE_SIZE_X_PADDED = cute::Int<65>; // bN + 1
using TILE_SIZE_Y = cute::Int<32>; // bM

constexpr auto block_shape{cute::make_shape(TILE_SIZE_Y{}, TILE_SIZE_X{})};
constexpr auto block_shape_transposed{
cute::make_shape(TILE_SIZE_X{}, TILE_SIZE_Y{})};

auto const shared_memory_layout_src{cute::make_layout(
block_shape, cute::GenRowMajor{})}; // (bM, bN) : (bN, 1)
auto const shared_memory_layout_src_padded{cute::make_layout(
block_shape,
cute::make_stride(TILE_SIZE_X_PADDED{},
cute::Int<1>{}))}; // (bM, bN) : (bN + 1, 1)

transpose_shared_memory<<<grid_dim, thread_dim, 0, stream>>>(
tiled_tensor_src, tiled_tensor_dst_transposed,
shared_memory_layout_src_padded, shared_memory_layout_src_padded,
thread_layout, thread_layout_transposed);

Because the shared memory shape remains the same, the CUDA kernel previously implemented can be just reused.

Shared Memory Swizzling

The shared memory swizzling is another common trick to avoid shared memory bank conflicts when a warp of threads is accessing shared memory. Comparing to the shared memory padding, the shared memory swizzling will not allocate extract shared memory that is not used, and is therefore a more favorable approach. However, the formula of shared memory swizzling is very brain-twisting and the implementation can be very error-prone. In CuTe, fortunately, the shared memory swizzling is implemented as a simple template class, and the shared memory swizzling can be easily applied to the shared memory layout via CuTe layout composition. After verifying the shared memory swizzled bank ids are meeting our requirement, we could just reuse the CUDA kernel previously implemented for the shared memory swizzled layout.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using TileSizeX = cute::Int<64>; // bN
using TileSizeY = cute::Int<32>; // bM
constexpr int NUM_BASE_BITS{constexpr_log2(1)};
constexpr int NUM_MASK_BITS{constexpr_log2(32 * 4 / sizeof(T)) -
NUM_BASE_BITS};
constexpr int NUM_SHIFT_BITS{constexpr_log2(TileSizeX::value) -
NUM_BASE_BITS};

constexpr auto block_shape{cute::make_shape(TileSizeY{}, TileSizeX{})};
constexpr auto block_shape_transposed{
cute::make_shape(TileSizeX{}, TileSizeY{})};

auto const shared_memory_layout_src{cute::make_layout(
block_shape, cute::GenRowMajor{})}; // (bM, bN) : (bN, 1)
auto const shared_memory_layout_dst{cute::make_layout(
block_shape_transposed, cute::GenRowMajor{})}; // (bN, bM) : (bM, 1)
auto const shared_memory_layout_dst_transposed{cute::make_layout(
block_shape, cute::GenColMajor{})}; // (bM, bN) : (1, bM)

auto const swizzle_src{
cute::Swizzle<NUM_MASK_BITS, NUM_BASE_BITS, NUM_SHIFT_BITS>{}};
auto const shared_memory_layout_swizzled_src{
cute::composition(swizzle_src, shared_memory_layout_src)};

In our case, given the shared memory of shape $(bM, bN) : (bN, 1) = (32, 64) : (64, 1)$, the shared memory bank id before and after applying CuTe swizzling are as follows, respectively.

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
0  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  0  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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
0 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 0 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
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
 0  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  0  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
1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30
2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13 18 19 16 17 22 23 20 21 26 27 24 25 30 31 28 29 2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13 18 19 16 17 22 23 20 21 26 27 24 25 30 31 28 29
3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 19 18 17 16 23 22 21 20 27 26 25 24 31 30 29 28 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 19 18 17 16 23 22 21 20 27 26 25 24 31 30 29 28
4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11 20 21 22 23 16 17 18 19 28 29 30 31 24 25 26 27 4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11 20 21 22 23 16 17 18 19 28 29 30 31 24 25 26 27
5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10 21 20 23 22 17 16 19 18 29 28 31 30 25 24 27 26 5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10 21 20 23 22 17 16 19 18 29 28 31 30 25 24 27 26
6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9 22 23 20 21 18 19 16 17 30 31 28 29 26 27 24 25 6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9 22 23 20 21 18 19 16 17 30 31 28 29 26 27 24 25
7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25 26 27 28 29 30 31 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25 26 27 28 29 30 31 16 17 18 19 20 21 22 23
9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6 25 24 27 26 29 28 31 30 17 16 19 18 21 20 23 22 9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6 25 24 27 26 29 28 31 30 17 16 19 18 21 20 23 22
10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5 26 27 24 25 30 31 28 29 18 19 16 17 22 23 20 21 10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5 26 27 24 25 30 31 28 29 18 19 16 17 22 23 20 21
11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4 27 26 25 24 31 30 29 28 19 18 17 16 23 22 21 20 11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4 27 26 25 24 31 30 29 28 19 18 17 16 23 22 21 20
12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 28 29 30 31 24 25 26 27 20 21 22 23 16 17 18 19 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 28 29 30 31 24 25 26 27 20 21 22 23 16 17 18 19
13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2 29 28 31 30 25 24 27 26 21 20 23 22 17 16 19 18 13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2 29 28 31 30 25 24 27 26 21 20 23 22 17 16 19 18
14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 30 31 28 29 26 27 24 25 22 23 20 21 18 19 16 17 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 30 31 28 29 26 27 24 25 22 23 20 21 18 19 16 17
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14
18 19 16 17 22 23 20 21 26 27 24 25 30 31 28 29 2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13 18 19 16 17 22 23 20 21 26 27 24 25 30 31 28 29 2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13
19 18 17 16 23 22 21 20 27 26 25 24 31 30 29 28 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 19 18 17 16 23 22 21 20 27 26 25 24 31 30 29 28 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12
20 21 22 23 16 17 18 19 28 29 30 31 24 25 26 27 4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11 20 21 22 23 16 17 18 19 28 29 30 31 24 25 26 27 4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11
21 20 23 22 17 16 19 18 29 28 31 30 25 24 27 26 5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10 21 20 23 22 17 16 19 18 29 28 31 30 25 24 27 26 5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10
22 23 20 21 18 19 16 17 30 31 28 29 26 27 24 25 6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9 22 23 20 21 18 19 16 17 30 31 28 29 26 27 24 25 6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9
23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8
24 25 26 27 28 29 30 31 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25 26 27 28 29 30 31 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7
25 24 27 26 29 28 31 30 17 16 19 18 21 20 23 22 9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6 25 24 27 26 29 28 31 30 17 16 19 18 21 20 23 22 9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6
26 27 24 25 30 31 28 29 18 19 16 17 22 23 20 21 10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5 26 27 24 25 30 31 28 29 18 19 16 17 22 23 20 21 10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5
27 26 25 24 31 30 29 28 19 18 17 16 23 22 21 20 11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4 27 26 25 24 31 30 29 28 19 18 17 16 23 22 21 20 11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4
28 29 30 31 24 25 26 27 20 21 22 23 16 17 18 19 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 28 29 30 31 24 25 26 27 20 21 22 23 16 17 18 19 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3
29 28 31 30 25 24 27 26 21 20 23 22 17 16 19 18 13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2 29 28 31 30 25 24 27 26 21 20 23 22 17 16 19 18 13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2
30 31 28 29 26 27 24 25 22 23 20 21 18 19 16 17 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 30 31 28 29 26 27 24 25 22 23 20 21 18 19 16 17 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

We could see that when a warp of threads read or write on the column of the shared memory of row-major, it’s shared memory bank conflict free.

Performances

The following tables show the performance measurements of the matrix transpose CUDA kernels on NVIDIA GeForce RTX 3090.

Kernel Name Latency (ms) Effective Bandwidth (GB/s) Peak Bandwidth Percentage (%)
Naive Coalesced Read 7.75992 257.734 27.5329
Naive Coalesced Write 3.12904 639.174 68.2809
Shared Memory Bank Conflict Read 2.98797 669.351 71.5045
Shared Memory Bank Conflict Write 2.9763 671.976 71.7849
Shared Memory Padded 2.98273 670.527 71.6302
Shared Memory Swizzled 2.92828 682.994 72.962

It’s somewhat surprising that except the native coalesced read CUDA kernel, all the other CUDA kernels have similar effective bandwidth and the bandwidth is very close to the ones that can be achieved in practice. Whether having shared memory bank conflicts in this CUDA kernel does not affect the performance significantly, because the performance bottleneck is in the global memory access.

After profiling using NVIDIA Nsight Compute, we could confirm that the global memory access is not fully coalesced for the native coalesced read and the native coalesced write CUDA kernels, shared memory bank load conflicts present in the shared memory bank conflict read CUDA kernel, shared memory bank store conflicts present in the shared memory bank conflict write CUDA kernel, and shared memory bank conflicts are free in the shared memory padded and shared memory swizzled CUDA kernels.

References

Author

Lei Mao

Posted on

11-20-2024

Updated on

12-26-2024

Licensed under


Comments