⌂ Modules nupp nupp.gpu nupp.gpu
Resident compute buffers over the selected GPU provider.
The context owns every buffer and compiled kernel created from it. Buffers and kernels borrow that context, so checked code cannot close the device while one is live. Uploads, dispatches, and downloads only enqueue commands; synchronize() is the explicit CPU boundary.
local gpu = nupp . gpu
local kernels = require ( "kernels" )
local ffi = require ( "ffi" )
local context = gpu . open ( )
local input = context : buffer ( ffi . typeof < float > ( ) , 1024 )
local output = context : buffer ( ffi . typeof < float > ( ) , 1024 )
local scale = kernels . scale : compile ( context ) : bind ( output , input )
context : upload ( input , values : read ( ) )
scale : dispatch ( 2.0 )
context : enqueueDownload ( output )
context : synchronize ( )
context : readDownloaded ( output , result : write ( ) )
Here kernels.scale is generated from an @aot(target = "gpu") function by a target with aot = "require". Shader source, entrypoint identity, binding order and uniform packing remain compiler details. Ordinary spans are the CPU boundary; a chain of kernels keeps its intermediate buffers resident and pays upload/download only where the program asks for them.
context:tensor(element, shape) allocates dense row-major storage. A checked gpu.Layout can be sliced, transposed, broadcast, or explicitly strided, then applied with gpu.view(buffer, layout) without allocating. Transfers remain dense; layout-aware kernels consume the explicit metadata.
Module contents Types Type Kind Description Bufferinterface Typed resident device storage or a view borrowing its owning context. Contextinterface Device operations shared by application and provider contexts. ContextTokeninterface Canonical non-suspending teardown obligation for a device context. Layoutrecord Phasesinterface Sharedinterface
Functions Values
Types
Bufferinterface
interface Buffer < T >
readonly count : integer
readonly dimensions : function ( borrows self : Buffer < T > ) : { integer }
readonly strides : function ( borrows self : Buffer < T > ) : { integer }
readonly subview : function (
borrows self : Buffer < T > ,
origin : { integer } ,
shape : { integer }
) : Buffer < T > borrows ( self )
readonly layout : function ( borrows self : Buffer < T > ) : TensorLayout
readonly view : function ( borrows self : Buffer < T > , layout : TensorLayout ) : Buffer < T > borrows ( self )
end
Typed resident device storage or a view borrowing its owning context.
Type parameters
Methods
dimensions
Returns the logical shape in elements.
Arguments Name Type Description borrows selfgpu . Buffer < T >
Returns
strides
Returns the element strides for the logical view.
Arguments Name Type Description borrows selfgpu . Buffer < T >
Returns
subview
Creates a context-borrowed view at a zero-based origin with the requested shape.
Arguments Name Type Description borrows selfgpu . Buffer < T > origin{ integer } shape{ integer }
Returns Type Description gpu . Buffer < T > borrows ( self )
layout
layout : function ( borrows self : gpu . Buffer < T > ) : TensorLayout
Returns the canonical tensor layout for this view.
Arguments Name Type Description borrows selfgpu . Buffer < T >
Returns Type Description TensorLayout
view
view : function ( borrows self : gpu . Buffer < T > , layout : TensorLayout ) : gpu . Buffer < T > borrows ( self )
Creates a view using the supplied checked layout without copying storage.
Arguments Name Type Description borrows selfgpu . Buffer < T > layoutTensorLayout
Returns Type Description gpu . Buffer < T > borrows ( self )
Fields
count
Number of elements, kept with the allocation.
Contextinterface
interface Context is gpu . ContextToken
readonly drop : nosuspend function ( takes self : Context ) : nil
readonly driver : function ( borrows self : Context ) : string
readonly buffer : function < T > (
borrows self : Context ,
element : ctype < T > ,
count : integer
) : gpu . Buffer < T > borrows ( self )
readonly tensor : function < T > (
borrows self : Context ,
element : ctype < T > ,
shape : { integer }
) : gpu . Buffer < T > borrows ( self )
readonly releaseBuffer : function < T > ( borrows self : Context , borrows buffer : gpu . Buffer < T > ) : nil
readonly upload : function < T > ( borrows self : Context , borrows buffer : gpu . Buffer < T > , borrows source : Span < T > ) : nil
readonly enqueueDownload : function < T > ( borrows self : Context , borrows buffer : gpu . Buffer < T > ) : nil
readonly synchronize : function ( borrows self : Context ) : nil
readonly readDownloaded : function < T > (
borrows self : Context ,
borrows buffer : gpu . Buffer < T > ,
exclusive destination : WriteSpan < T >
) : nil
readonly download : function < T > (
borrows self : Context ,
borrows buffer : gpu . Buffer < T > ,
exclusive destination : WriteSpan < T >
) : nil
end
Device operations shared by application and provider contexts.
Methods
drop
drop : nosuspend function ( takes self : gpu . Context ) : nil
Consumes the context and releases all device resources without suspending.
Arguments Name Type Description takes selfgpu . Context
Returns
driver
driver : function ( borrows self : gpu . Context ) : string
Returns a diagnostic name for the selected device implementation.
Arguments Name Type Description borrows selfgpu . Context
Returns
buffer
Allocates a typed resident device buffer.
Arguments Name Type Description borrows selfgpu . Context elementctype < T > countinteger
Returns Type Description gpu . Buffer < T > borrows ( self )
tensor
tensor : function < T > (
borrows self : gpu . Context ,
element : ctype < T > ,
shape : { integer }
) : gpu . Buffer < T > borrows ( self )
Allocates typed storage with the requested logical shape.
Arguments Name Type Description borrows selfgpu . Context elementctype < T > shape{ integer }
Returns Type Description gpu . Buffer < T > borrows ( self )
releaseBuffer
Releases the device allocation represented by buffer. The caller must not use it or its dependent views afterward.
Arguments Name Type Description borrows selfgpu . Context borrows buffergpu . Buffer < T >
Returns
upload
upload : function < T > ( borrows self : gpu . Context , borrows buffer : gpu . Buffer < T > , borrows source : Span < T > ) : nil
Copies borrowed host-span data into the resident buffer.
Arguments Name Type Description borrows selfgpu . Context borrows buffergpu . Buffer < T > borrows sourceSpan < T >
Returns
enqueueDownload
Queues a buffer readback for subsequent synchronization and reading.
Arguments Name Type Description borrows selfgpu . Context borrows buffergpu . Buffer < T >
Returns
synchronize
synchronize : function ( borrows self : gpu . Context ) : nil
Waits for queued device work and readbacks to complete.
Arguments Name Type Description borrows selfgpu . Context
Returns
readDownloaded
readDownloaded : function < T > (
borrows self : gpu . Context ,
borrows buffer : gpu . Buffer < T > ,
exclusive destination : WriteSpan < T >
) : nil
Copies a completed readback into an exclusively borrowed destination span.
Arguments Name Type Description borrows selfgpu . Context borrows buffergpu . Buffer < T > exclusive destinationWriteSpan < T >
Returns
download
download : function < T > (
borrows self : gpu . Context ,
borrows buffer : gpu . Buffer < T > ,
exclusive destination : WriteSpan < T >
) : nil
Performs readback, synchronization, and copying into the exclusive destination.
Arguments Name Type Description borrows selfgpu . Context borrows buffergpu . Buffer < T > exclusive destinationWriteSpan < T >
Returns
ContextTokeninterface
interface ContextToken
readonly drop : nosuspend function ( takes self : ContextToken ) : nil
end
Canonical non-suspending teardown obligation for a device context.
Methods
drop
Consumes and releases the context without suspending.
Arguments
Returns
Layoutrecord
record Layout
readonly count : integer
end
Fields
Phasesinterface
Methods
scratch
scratch : function < T > ( borrows self : gpu . Phases , initial : T , count : integer ) : gpu . Shared < T >
Allocates fresh zero-based scratch for this workgroup.
Arguments Name Type Description borrows selfgpu . Phases initialT countinteger
Returns
run
run : function ( borrows self : gpu . Phases , scoped stage : function ( uint32 ) : nil ) : nil
Runs one stage to completion in ascending local-index order.
Arguments Name Type Description borrows selfgpu . Phases scoped stagefunction ( uint32 ) : nil
Returns
reduceSumF32
reduceSumF32 : function ( borrows self : gpu . Phases , exclusive values : gpu . Shared < float > ) : nil
Reduces one power-of-two f32 workgroup in a fixed left-before-right tree. The sum is left in element zero.
Arguments
Returns
inclusiveScanU32
inclusiveScanU32 : function (
borrows self : gpu . Phases ,
exclusive values : gpu . Shared < uint32 > ,
exclusive temporary : gpu . Shared < uint32 >
) : nil
Computes one deterministic inclusive u32 prefix sum. temporary is a same-sized scratch array used for disjoint ping-pong stages; the result is left in values.
Arguments Name Type Description borrows selfgpu . Phases exclusive valuesgpu . Shared < uint32 > exclusive temporarygpu . Shared < uint32 >
Returns
Sharedinterface
interface Shared < T >
readonly count : integer
metamethod __len : function ( borrows self : Shared < T > ) : integer
metamethod __index : function ( borrows self : Shared < T > , index : integer ) : T
metamethod __newindex : function ( exclusive self : Shared < T > , index : integer , value : T ) : nil
end
Type parameters
Methods
__len
__len : function ( borrows self : gpu . Shared < T > ) : integer
Arguments Name Type Description borrows selfgpu . Shared < T >
Returns
__index
__index : function ( borrows self : gpu . Shared < T > , index : integer ) : T
Arguments Name Type Description borrows selfgpu . Shared < T > indexinteger
Returns
__newindex
__newindex : function ( exclusive self : gpu . Shared < T > , index : integer , value : T ) : nil
Arguments Name Type Description exclusive selfgpu . Shared < T > indexinteger valueT
Returns
Fields
Functions
bufferIsDensefunction
function bufferIsDense < T > ( borrows buffer : api . Buffer < T > ) : boolean
Type parameters
Arguments Name Type Description borrows bufferapi . Buffer < T >
Returns
bufferIsInjectivefunction
function bufferIsInjective < T > ( borrows buffer : api . Buffer < T > ) : boolean
Type parameters
Arguments Name Type Description borrows bufferapi . Buffer < T >
Returns
bufferLayoutfunction
Type parameters
Arguments Name Type Description borrows bufferapi . Buffer < T >
Returns Type Description tensorlayout . Layout
openfunction
Opens a compute device through the selected provider.
Returns
viewfunction
Type parameters
Arguments Name Type Description borrows bufferapi . Buffer < T > layouttensorlayout . Layout
Returns
workgroupsfunction
function workgroups ( groups : uint32 , size : uint32 , scoped controller : function ( uint32 , gpu . Phases ) : nil ) : nil
Executes uniformly ordered workgroup phases on the CPU.
A GPU AOT body recognizes this call and its immediate callbacks as structure: the controller does not escape, each run is a device barrier, and each stage invocation becomes one local lane.
Arguments Name Type Description groupsuint32 sizeuint32 scoped controllerfunction ( uint32 , gpu . Phases ) : nil
Returns
Raises
Values
PORTABLE_SCRATCH_BYTESvariable
PORTABLE_WORKGROUP_THREADSvariable
← Previous nupp.events Next → nupp.gpu.layout