Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
cuda source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
10.0.0 sm_75 CUDA-10.2
10.0.1 sm_75 CUDA-10.2
11.0.0 sm_75 CUDA-10.2
16.0.0 sm_90 CUDA-11.8
17.0.1(libc++) sm_90 CUDA-12.1
18.1.0(libc++) sm_90 CUDA-12.3.1
19.1.0 sm_90 CUDA-12.5.1
20.1.0 sm_90 CUDA-12.5.1
20.1.0 sm_90 CUDA-12.6.1
20.1.0 sm_90 CUDA-12.6.2
NVCC 10.0.130
NVCC 10.1.105
NVCC 10.1.168
NVCC 10.1.243
NVCC 10.2.89
NVCC 11.0.2
NVCC 11.0.3
NVCC 11.1.0
NVCC 11.1.1
NVCC 11.2.0
NVCC 11.2.1
NVCC 11.2.2
NVCC 11.3.0
NVCC 11.3.1
NVCC 11.4.0
NVCC 11.4.1
NVCC 11.4.2
NVCC 11.4.3
NVCC 11.4.4
NVCC 11.5.0
NVCC 11.5.1
NVCC 11.5.2
NVCC 11.6.0
NVCC 11.6.1
NVCC 11.6.2
NVCC 11.7.0
NVCC 11.7.1
NVCC 11.8.0
NVCC 12.0.0
NVCC 12.0.1
NVCC 12.1.0
NVCC 12.2.1
NVCC 12.3.1
NVCC 12.4.1
NVCC 12.5.1
NVCC 12.6.1
NVCC 12.6.2
NVCC 12.8.1
NVCC 12.9.0
NVCC 12.9.1
NVCC 13.0.0
NVCC 13.0.1
NVCC 9.1.85
NVCC 9.2.88
NVRTC 11.0.2
NVRTC 11.0.3
NVRTC 11.1.0
NVRTC 11.1.1
NVRTC 11.2.0
NVRTC 11.2.1
NVRTC 11.2.2
NVRTC 11.3.0
NVRTC 11.3.1
NVRTC 11.4.0
NVRTC 11.4.1
NVRTC 11.5.0
NVRTC 11.5.1
NVRTC 11.5.2
NVRTC 11.6.0
NVRTC 11.6.1
NVRTC 11.6.2
NVRTC 11.7.0
NVRTC 11.7.1
NVRTC 11.8.0
NVRTC 12.0.0
NVRTC 12.0.1
NVRTC 12.1.0
NVRTC 12.2.1
NVRTC 12.3.1
NVRTC 12.4.1
NVRTC 12.5.1
NVRTC 12.6.1
NVRTC 12.6.2
NVRTC 12.8.1
NVRTC 12.9.0
NVRTC 12.9.1
NVRTC 13.0.0
NVRTC 13.0.1
clang 7.0.0 sm_70 CUDA-9.1
clang 8.0.0 sm_75 CUDA-10.0
clang 9.0.0 sm_75 CUDA-10.1
clang rocm-4.5.2
clang rocm-5.0.2
clang rocm-5.1.3
clang rocm-5.2.3
clang rocm-5.3.2
clang rocm-5.7.0
clang rocm-6.0.2
clang rocm-6.1.2
clang rocm-6.2.4
clang rocm-6.3.3
clang rocm-6.4.0
clang staging rocm-6.1.2
clang staging rocm-6.2.4
clang staging rocm-6.3.3
clang staging rocm-6.4.0
clang trunk rocm-6.1.2
clang trunk rocm-6.2.4
clang trunk rocm-6.3.3
clang trunk rocm-6.4.0
trunk sm_100a CUDA-12.8.1
Options
Source code
#include <cassert> #include <cmath> #include <cstdio> #include <cuda_fp16.h> #include <cuda.h> template <typename T> struct Afrag_16x16 { static constexpr size_t ne = 8; T x[ne]; static __device__ size_t get_row(int tid, int l) { int group_id = tid >> 2; // same as /4 return group_id + 8 * ((l / 2) % 2); } static __device__ size_t get_col(int tid, int l) { return 2 * (tid % 4) + (l % 2) + 8 * (l / 4); } }; // col major? template <typename T> struct Bfrag_16x8 { static constexpr size_t ne = 4; T x[ne] = {}; static __device__ size_t get_row(int tid, int l) { return (tid % 4) * 2 + (l % 2) + 8 * (l / 2); } static __device__ size_t get_col(int tid, int l) { return tid >> 2; } }; template <typename T> struct CFrag_16x8 { static constexpr size_t ne = 4; T x[ne] = {}; static __device__ size_t get_row(int tid, int l) { return (tid >> 2) + 8 * (l / 2); } static __device__ size_t get_col(int tid, int l) { assert(l < ne); return 2 * (tid % 4) + (l % 2); } }; __global__ void mmaKernel(const half *A, const half *B, float *C, int M, int N, int K) { Afrag_16x16<half> a_tile; Bfrag_16x8<half> b_tile; CFrag_16x8<float> c_tile; const int tid = threadIdx.x; __shared__ alignas(16) half A_shared[16][16]; __shared__ alignas(16) half B_shared[16][8]; const int lane = tid & 31; if (lane < 16) { int row = lane; for(int idx = 0; idx < 8; ++idx) { A_shared[row][idx] = A[row*K + idx]; } for(int idx = 0; idx < 8; ++idx) { A_shared[row][idx + 8] = A[row*K + 8 + idx]; } for(int idx = 0 ; idx < 8; ++idx) { B_shared[row][idx] = B[row*N + idx]; } } int *a_regs = (int *)a_tile.x; int *b_regs = (int *)b_tile.x; int lane_id = tid; uint32_t a_addr = __cvta_generic_to_shared( &A_shared[(lane_id % 16)][(lane_id / 16) * 8]); uint32_t b_addr = __cvta_generic_to_shared( &B_shared[(lane_id % 16)]); asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 " "{%0, %1, %2, %3}, [%4];" : "=r"(a_regs[0]), "=r"(a_regs[1]), "=r"(a_regs[2]), "=r"(a_regs[3]) : "r"(a_addr)); asm volatile("ldmatrix.sync.aligned.m8n8.x2.shared.trans.b16 " "{%0, %1}, [%2];" : "=r"(b_regs[0]), "=r"(b_regs[1]) : "r"(b_addr)); asm volatile("mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 " "{%0, %1, %2, %3}, " "{%4, %5, %6, %7}, " "{%8, %9}, " "{%0, %1, %2, %3};\n" : "+f"(c_tile.x[0]), "+f"(c_tile.x[1]), "+f"(c_tile.x[2]), "+f"(c_tile.x[3]) : "r"(a_regs[0]), "r"(a_regs[1]), "r"(a_regs[2]), "r"(a_regs[3]), "r"(b_regs[0]), "r"(b_regs[1])); for (int i = 0; i < c_tile.ne; ++i) { int row = c_tile.get_row(tid, i); int col = c_tile.get_col(tid, i); C[row * N + col] = c_tile.x[i]; } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree