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.

Submodules

ModuleDescription
nupp.gpu.layout

Module contents

Types

TypeKindDescription
BufferinterfaceTyped resident device storage or a view borrowing its owning context.
ContextinterfaceDevice operations shared by application and provider contexts.
ContextTokeninterfaceCanonical non-suspending teardown obligation for a device context.
Layoutrecord
Phasesinterface
Sharedinterface

Functions

FunctionKindDescription
bufferIsDensefunction
bufferIsInjectivefunction
bufferLayoutfunction
openfunctionOpens a compute device through the selected provider.
viewfunction
workgroupsfunctionExecutes uniformly ordered workgroup phases on the CPU.

Values

ValueKindDescription
destroyContextvariable
PORTABLE_SCRATCH_BYTESvariable
PORTABLE_WORKGROUP_THREADSvariable

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

NameDescription
T

Methods

dimensions#
dimensions: function(borrows self: gpu.Buffer<T>): {integer}

Returns the logical shape in elements.

Arguments
NameTypeDescription
borrows selfgpu.Buffer<T>
Returns
TypeDescription
{integer}
strides#
strides: function(borrows self: gpu.Buffer<T>): {integer}

Returns the element strides for the logical view.

Arguments
NameTypeDescription
borrows selfgpu.Buffer<T>
Returns
TypeDescription
{integer}
subview#
subview: function(
    borrows self: gpu.Buffer<T>,
    origin: {integer},
    shape: {integer}
): gpu.Buffer<T> borrows (self)

Creates a context-borrowed view at a zero-based origin with the requested shape.

Arguments
NameTypeDescription
borrows selfgpu.Buffer<T>
origin{integer}
shape{integer}
Returns
TypeDescription
gpu.Buffer<T> borrows (self)
layout#
layout: function(borrows self: gpu.Buffer<T>): TensorLayout

Returns the canonical tensor layout for this view.

Arguments
NameTypeDescription
borrows selfgpu.Buffer<T>
Returns
TypeDescription
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
NameTypeDescription
borrows selfgpu.Buffer<T>
layoutTensorLayout
Returns
TypeDescription
gpu.Buffer<T> borrows (self)

Fields

count#
count: integer

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
NameTypeDescription
takes selfgpu.Context
Returns
TypeDescription
nil
driver#
driver: function(borrows self: gpu.Context): string

Returns a diagnostic name for the selected device implementation.

Arguments
NameTypeDescription
borrows selfgpu.Context
Returns
TypeDescription
string
buffer#
buffer: function<T>(
    borrows self: gpu.Context,
    element: ctype<T>,
    count: integer
): gpu.Buffer<T> borrows (self)

Allocates a typed resident device buffer.

Arguments
NameTypeDescription
borrows selfgpu.Context
elementctype<T>
countinteger
Returns
TypeDescription
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
NameTypeDescription
borrows selfgpu.Context
elementctype<T>
shape{integer}
Returns
TypeDescription
gpu.Buffer<T> borrows (self)
releaseBuffer#
releaseBuffer: function<T>(borrows self: gpu.Context, borrows buffer: gpu.Buffer<T>): nil

Releases the device allocation represented by buffer. The caller must not use it or its dependent views afterward.

Arguments
NameTypeDescription
borrows selfgpu.Context
borrows buffergpu.Buffer<T>
Returns
TypeDescription
nil
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
NameTypeDescription
borrows selfgpu.Context
borrows buffergpu.Buffer<T>
borrows sourceSpan<T>
Returns
TypeDescription
nil
enqueueDownload#
enqueueDownload: function<T>(borrows self: gpu.Context, borrows buffer: gpu.Buffer<T>): nil

Queues a buffer readback for subsequent synchronization and reading.

Arguments
NameTypeDescription
borrows selfgpu.Context
borrows buffergpu.Buffer<T>
Returns
TypeDescription
nil
synchronize#
synchronize: function(borrows self: gpu.Context): nil

Waits for queued device work and readbacks to complete.

Arguments
NameTypeDescription
borrows selfgpu.Context
Returns
TypeDescription
nil
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
NameTypeDescription
borrows selfgpu.Context
borrows buffergpu.Buffer<T>
exclusive destinationWriteSpan<T>
Returns
TypeDescription
nil
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
NameTypeDescription
borrows selfgpu.Context
borrows buffergpu.Buffer<T>
exclusive destinationWriteSpan<T>
Returns
TypeDescription
nil

ContextTokeninterface#

interface ContextToken
    readonly drop: nosuspend function(takes self: ContextToken): nil
end

Canonical non-suspending teardown obligation for a device context.

Methods

drop#
drop: nosuspend function(takes self: ContextToken): nil

Consumes and releases the context without suspending.

Arguments
NameTypeDescription
takes selfContextToken
Returns
TypeDescription
nil

Layoutrecord#

record Layout

    readonly count: integer
end

Fields

count#
count: integer

Phasesinterface#

interface Phases
    readonly scratch: function<T>(borrows self: Phases, initial: T, count: integer): gpu.Shared<T>
    readonly run: function(borrows self: Phases, scoped stage: function(uint32): nil): nil
    readonly reduceSumF32: function(borrows self: Phases, exclusive values: gpu.Shared<float>): nil
    readonly inclusiveScanU32: function(
        borrows self: Phases,
        exclusive values: gpu.Shared<uint32>,
        exclusive temporary: gpu.Shared<uint32>
    ): nil
end

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
NameTypeDescription
borrows selfgpu.Phases
initialT
countinteger
Returns
TypeDescription
gpu.Shared<T>
run#
run: function(borrows self: gpu.Phases, scoped stage: function(uint32): nil): nil

Runs one stage to completion in ascending local-index order.

Arguments
NameTypeDescription
borrows selfgpu.Phases
scoped stagefunction(uint32): nil
Returns
TypeDescription
nil
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
NameTypeDescription
borrows selfgpu.Phases
exclusive valuesgpu.Shared<float>
Returns
TypeDescription
nil
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
NameTypeDescription
borrows selfgpu.Phases
exclusive valuesgpu.Shared<uint32>
exclusive temporarygpu.Shared<uint32>
Returns
TypeDescription
nil

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

NameDescription
T

Methods

__len#
__len: function(borrows self: gpu.Shared<T>): integer
Arguments
NameTypeDescription
borrows selfgpu.Shared<T>
Returns
TypeDescription
integer
__index#
__index: function(borrows self: gpu.Shared<T>, index: integer): T
Arguments
NameTypeDescription
borrows selfgpu.Shared<T>
indexinteger
Returns
TypeDescription
T
__newindex#
__newindex: function(exclusive self: gpu.Shared<T>, index: integer, value: T): nil
Arguments
NameTypeDescription
exclusive selfgpu.Shared<T>
indexinteger
valueT
Returns
TypeDescription
nil

Fields

count#
count: integer

Functions#

bufferIsDensefunction#

function bufferIsDense<T>(borrows buffer: api.Buffer<T>): boolean

Type parameters

NameDescription
T

Arguments

NameTypeDescription
borrows bufferapi.Buffer<T>

Returns

TypeDescription
boolean

bufferIsInjectivefunction#

function bufferIsInjective<T>(borrows buffer: api.Buffer<T>): boolean

Type parameters

NameDescription
T

Arguments

NameTypeDescription
borrows bufferapi.Buffer<T>

Returns

TypeDescription
boolean

bufferLayoutfunction#

function bufferLayout<T>(borrows buffer: api.Buffer<T>): tensorlayout.Layout

Type parameters

NameDescription
T

Arguments

NameTypeDescription
borrows bufferapi.Buffer<T>

Returns

TypeDescription
tensorlayout.Layout

openfunction#

const open: function(): affine(api.Context, api.destroyContext)

Opens a compute device through the selected provider.

Returns

TypeDescription
affine(api.Context, api.destroyContext)

viewfunction#

function view<T>(borrows buffer: api.Buffer<T>, layout: tensorlayout.Layout): api.Buffer<T>

Type parameters

NameDescription
T

Arguments

NameTypeDescription
borrows bufferapi.Buffer<T>
layouttensorlayout.Layout

Returns

TypeDescription
api.Buffer<T>

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

NameTypeDescription
groupsuint32
sizeuint32
scoped controllerfunction(uint32, gpu.Phases): nil

Returns

TypeDescription
nil

Raises

  • when size is outside the portable workgroup limit

Values#

destroyContextvariable#

PORTABLE_SCRATCH_BYTESvariable#

PORTABLE_WORKGROUP_THREADSvariable#