Introduction
PTX, the parallel thread execution instruction set, is the assembly language that sits between CUDA source and the machine code of an NVIDIA graphics card. NVIDIA describes it as a low-level virtual machine and instruction set architecture that exposes the GPU as a data-parallel computing device. It is virtual: it does not name the instructions of one chip, so the same PTX can be turned into code for different GPUs.
A .ptx file is the text form. NVIDIA’s nvcc compiler writes
one when it is given the -ptx option, and it can compile a
.ptx file onward to a .cubin or a fat
binary. The same text can also be embedded in a fat binary, so that the CUDA driver
can finish the job for a GPU that did not exist when the program was built.
Structure
A PTX file is plain text with one instruction or directive per line. Each module must
begin with a .version directive, which gives the PTX language version,
followed by a .target directive, which gives the architecture assumed,
such as sm_80. Then .address_size sets 32-bit or 64-bit
addresses. Kernels are declared with .entry, and
.visible makes one visible to other modules. Inside a function,
.reg declares typed virtual registers such as %r1, and
instructions have the form opcode.type destination, source, source;. Memory
spaces are part of the instruction name, as in ld.param,
ld.global and st.global. A line can be guarded by a predicate
register, written @%p1.
.version 8.0
.target sm_80
.address_size 64
.visible .entry scale(
.param .u64 data,
.param .u32 n,
.param .f32 factor
)
{
.reg .pred %p<2>;
.reg .b32 %r<6>;
.reg .b64 %rd<5>;
.reg .f32 %f<4>;
ld.param.u64 %rd1, [data];
ld.param.u32 %r1, [n];
ld.param.f32 %f1, [factor];
mov.u32 %r2, %ctaid.x;
mov.u32 %r3, %ntid.x;
mov.u32 %r4, %tid.x;
mad.lo.s32 %r5, %r2, %r3, %r4;
setp.ge.s32 %p1, %r5, %r1;
@%p1 bra DONE;
cvta.to.global.u64 %rd2, %rd1;
mul.wide.s32 %rd3, %r5, 4;
add.s64 %rd4, %rd2, %rd3;
ld.global.f32 %f2, [%rd4];
mul.f32 %f3, %f2, %f1;
st.global.f32 [%rd4], %f3;
DONE:
ret;
}
Not described
Known, not yet covered
- Reference: this overview is the only page. The instruction set has no reference pages: the types and state spaces, the full instruction list (integer and floating-point arithmetic, comparison, data movement, texture, atomic, barrier and warp-level instructions), the directives, the special registers, the ABI for functions and the PTX versions and targets. NVIDIA’s PTX ISA document describes them.
- Reference: how PTX is turned into machine code (the SASS of each GPU) and how it is stored inside a fat binary is not described, and neither is inline PTX in CUDA C++ (
asm). - Viewer: there is no viewer for this format yet, so a PTX file is not coloured, and nothing is assembled or checked.
Also used for
- Leica PTX point cloud – A text export of gridded laser scans, each with its scanner position and registration matrix.
- Pro Tools session – An Avid Pro Tools session: an obfuscated binary file beside an Audio Files folder.
Links
Software
- CUDA Toolkit – NVIDIA’s
nvcc, which writes PTX with-ptxand builds it onward (Windows, Linux) - LLVM NVPTX back-end – the LLVM code generator that produces PTX for GPU kernels (Windows, macOS, Linux)
Reference
- PTX ISA – NVIDIA’s reference for the instruction set and the PTX language
- NVCC compiler driver – NVIDIA’s guide, with the file suffixes
nvccaccepts - User guide for the NVPTX back-end – how LLVM marks and compiles kernels