rocm_kernel_macros
Crate for generating subprojects with kernel source written in rust
Requirements
- rust nightly
- rust target: amdgcn-amd-amdhsa
- rust-src
- ROCM 6.0 or newer
Macro Arguments
Both amdgpu_kernel_init!() and amdgpu_kernel_finalize!() accept optional arguments:
path- name prefix for the kernel (default: "kernel")gfx- target GPU architecture (default: "gfx1103")dir- directory for kernel sources (default: "kernel_sources")binary_name- name of output binary (default: "kernels")
Attribute macros #[amdgpu_global] and #[amdgpu_device] accept:
path- must match the path used inamdgpu_kernel_init!()dir- must match the dir used inamdgpu_kernel_init!()
Examples
- Writing gpu kernels in rust (basic)
// initialize new kernel subproject
amdgpu_kernel_init!;
// mark function that will be copied to kernel src
// compile and get path to kernel binary
const AMDGPU_KERNEL_BINARY_PATH: &str = amdgpu_kernel_finalize!;
- Writing gpu kernels with custom configuration
// initialize with custom settings
amdgpu_kernel_init!;
// use matching path and dir in attributes
// compile with matching settings
const KERNEL_PATH: &str = amdgpu_kernel_finalize!;
- Running kernel on gpu side using
rocm-rs(assuming above kernel)
// aquire device
let device = new?;
device.set_current?;
// load kernel binary
let kernel_path = from;
assert!;
let module = load?;
// search for function in module
let function = unsafe ;
// prepare input and output memory
let mut in_host: = vec!;
let mut out_host: = vec!;
// prepare data
for i in 0..LEN
let mut input = new?;
let output = new?;
input.copy_from_host?;
// prepare kernel arguments
let kernel_args = ;
// setup launch arguments
let grid_dim = Dim3 ;
let block_dim = Dim3 ;
// launch kernel (grid_dim, block_dim, shared_mem_bytes, stream, args)
function.launch?;
- Running kernel on host side (assuming above kernel, highly not recomended)