Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Deepak-Sangle/TornadoVM/llms.txt
Use this file to discover all available pages before exploring further.
KernelContext gives you direct access to the GPU thread programming model from ordinary Java methods. Where the @Parallel annotation lets TornadoVM automatically infer parallelism from loop bounds, KernelContext puts you in full control: you read global and local thread IDs, allocate shared/local memory, issue barrier synchronizations, perform atomic operations, and invoke Tensor Core MMA intrinsics — all from Java source that the TornadoVM JIT transparently compiles to OpenCL C or PTX. Tasks that use KernelContext must be dispatched through a GridScheduler so the runtime knows the thread grid dimensions ahead of compilation.
KernelContext and @Parallel/@Reduce annotations are mutually exclusive programming styles within a single task. A method that uses KernelContext to read thread indices should not also annotate its loops with @Parallel — doing so produces undefined behaviour because the two parallelism models make conflicting assumptions about how the iteration space is decomposed.Instantiation
AKernelContext object is constructed by your application code and passed as an ordinary argument to any task() method:
0 and all methods execute their sequential Java fallback bodies. This makes your kernel testable without a GPU.
Thread Index Fields
All fields arepublic final Integer and are re-bound by the TornadoVM JIT to the appropriate hardware intrinsic when compiling for a GPU device. Their JVM-side value is always 0.
Global Thread IDs
The unique global thread index in the X dimension.
OpenCL:
OpenCL:
get_global_id(0) · CUDA: blockIdx.x * blockDim.x + threadIdx.xThe unique global thread index in the Y dimension.
OpenCL:
OpenCL:
get_global_id(1) · CUDA: blockIdx.y * blockDim.y + threadIdx.yThe unique global thread index in the Z dimension.
OpenCL:
OpenCL:
get_global_id(2) · CUDA: blockIdx.z * blockDim.z + threadIdx.zLocal (Workgroup) Thread IDs
Thread index within the current workgroup (CUDA block), X dimension.
OpenCL:
OpenCL:
get_local_id(0) · CUDA: threadIdx.xThread index within the current workgroup, Y dimension.
OpenCL:
OpenCL:
get_local_id(1) · CUDA: threadIdx.yThread index within the current workgroup, Z dimension.
OpenCL:
OpenCL:
get_local_id(2) · CUDA: threadIdx.zWorkgroup (Block) IDs
Index of the current workgroup in the X dimension.
OpenCL:
OpenCL:
get_group_id(0) · CUDA: blockIdx.xIndex of the current workgroup in the Y dimension.
OpenCL:
OpenCL:
get_group_id(1) · CUDA: blockIdx.yIndex of the current workgroup in the Z dimension.
OpenCL:
OpenCL:
get_group_id(2) · CUDA: blockIdx.zGlobal Grid Sizes
Total number of threads across all workgroups in X.
OpenCL:
OpenCL:
get_global_size(0) · CUDA: gridDim.x * blockDim.xTotal number of threads across all workgroups in Y.
OpenCL:
OpenCL:
get_global_size(1) · CUDA: gridDim.y * blockDim.yTotal number of threads across all workgroups in Z.
OpenCL:
OpenCL:
get_global_size(2) · CUDA: gridDim.z * blockDim.zLocal Workgroup Sizes
Number of threads per workgroup in X.
OpenCL:
OpenCL:
get_local_size(0) · CUDA: blockDim.xNumber of threads per workgroup in Y.
OpenCL:
OpenCL:
get_local_size(1) · CUDA: blockDim.yNumber of threads per workgroup in Z.
OpenCL:
OpenCL:
get_local_size(2) · CUDA: blockDim.zLocal Memory Allocation
Local memory (OpenCL terminology) or shared memory (CUDA terminology) lives on-chip and is shared among all threads within a workgroup. It is much faster than global device memory but limited in size (typically 48 KB per block on modern GPUs). Call these methods inside your kernel method to allocate a local array; do not store the returned reference in a field or escape it from the kernel method.Allocates a shared-memory
int array of size elements.Allocates a shared-memory
float array of size elements.Allocates a shared-memory
double array of size elements.Allocates a shared-memory
long array of size elements.Allocates a shared-memory
byte array of size elements.Allocates a shared-memory
HalfFloat array of size elements. Useful for mixed-precision workloads.Allocates a shared-memory array of packed
Half2 pairs. On backends with native packed-half2 support each element maps to a single 32-bit __half2.Synchronization
Synchronizes all threads within the current workgroup on local (shared) memory. All local-memory writes issued before the barrier are guaranteed to be visible to all other threads in the workgroup after it.
OpenCL:
OpenCL:
barrier(CLK_LOCAL_MEM_FENCE) · CUDA: __syncthreads()Synchronizes all threads within the current workgroup on global memory. The barrier scope is the workgroup (CUDA block), not the entire device grid.
OpenCL:
OpenCL:
barrier(CLK_GLOBAL_MEM_FENCE) · CUDA: __syncthreads()Atomic Operations
Atomic operations are essential for concurrent accumulation and histogram computation. TornadoVM provides atomics for both global (IntArray, LongArray, FloatArray, DoubleArray) and local/shared-memory (int[]) targets.
Atomically adds
CUDA:
val to array[index] in global device memory.CUDA:
atomicAdd(int* address, int val)Atomically adds
val to array[index] in local (shared) memory.Atomically adds
CUDA:
val to array[index] in global memory (64-bit).CUDA:
atomicAdd(long long* address, long long val)Atomically adds
CUDA:
val to array[index] in global device memory (32-bit float).CUDA:
atomicAdd(float* address, float val)Atomically adds
CUDA:
val to array[index] in global device memory (64-bit double).CUDA:
atomicAdd(double* address, double val)Compare-and-swap on local/shared memory. If
CUDA:
array[index] == expected, atomically stores value and returns the previous value. Success if return == expected.CUDA:
atomicCAS(&array[index], expected, value)Atomically stores
CUDA:
value into array[index] (local memory) and returns the previous value.CUDA:
atomicExch(&array[index], value)Atomically replaces
array[index] with min(array[index], value) (local memory) and returns the previous value.Atomically replaces
array[index] with max(array[index], value) (local memory) and returns the previous value.SIMD / Warp Intrinsics
These intrinsics operate at the SIMD-group (warp) level and require all lanes to converge at the call site.Tensor Core MMA Intrinsics
TornadoVM exposes matrix multiply-accumulate (MMA) primitives for Tensor Core workloads. These operations are warp-collective: all 32 lanes of the warp must converge at the call site.Matrix-Vector Multiplication Example
The following example demonstratesKernelContext usage for a 2D matrix-vector multiplication kernel with local memory tiling.
Worker Grid
Configure global and local work dimensions that KernelContext tasks require.
Task Graph
Register KernelContext tasks within a TaskGraph.