Skip to content

unigpu API reference

Document generated by docs/generate_api.py from the docstrings.

Kernel language (to use inside a kernel)

threadIdx

threadIdx, blockIdx, blockDim or gridDim (components x, y, z).

Members: threadIdx.kind, threadIdx.x, threadIdx.y, threadIdx.z

blockIdx

threadIdx, blockIdx, blockDim or gridDim (components x, y, z).

Members: blockIdx.kind, blockIdx.x, blockIdx.y, blockIdx.z

blockDim

threadIdx, blockIdx, blockDim or gridDim (components x, y, z).

Members: blockDim.kind, blockDim.x, blockDim.y, blockDim.z

gridDim

threadIdx, blockIdx, blockDim or gridDim (components x, y, z).

Members: gridDim.kind, gridDim.x, gridDim.y, gridDim.z

grid(ndim)

Global index of the thread: blockIdx * blockDim + threadIdx.

grid(1) returns an integer; grid(2) and grid(3) a tuple (x, y[, z]).

gridsize(ndim)

Total number of threads of the grid per axis: gridDim * blockDim.

Used by "grid-stride" loops: for i in range(grid(1), n, gridsize(1)).

syncthreads()

Barrier: each thread waits until every thread of the block reaches it.

Makes the writes to shared memory visible to the whole block. It must be reached by every thread of the block (never in a divergent branch).

syncthreads_count(predicate)

Barrier that returns the number of threads of the block whose predicate is non-zero.

As in numba-cuda, predicate is an integer: syncthreads_count(int(x > 0)).

syncthreads_and(predicate)

Barrier that returns 1 if the (integer) predicate is non-zero for every thread.

syncthreads_or(predicate)

Barrier that returns 1 if the (integer) predicate is non-zero for at least one thread.

syncwarp()

Barrier: each thread waits until every thread of its warp reaches it.

The other warps do not need to reach it. Their size, warpsize, depends on the device. Where this barrier does not exist (OpenCL devices without subgroups, numba's simulator), compiling the kernel is an error: syncthreads() never replaces it.

warpsize

Number of threads of a warp, which depends on the device (an integer, not a function).

On an OpenCL device without subgroups, compiling a kernel that uses it is an error.

laneid

Index of the thread in its warp, less than warpsize (an integer, not a function).

On an OpenCL device without subgroups, compiling a kernel that uses it is an error.

shared

Group primitives under a name (shared.array, atomic.add...).

The attributes are ordinary functions: their identity is stable, which lets the compiler recognize them.

Members: shared.array

local

Group primitives under a name (shared.array, atomic.add...).

The attributes are ordinary functions: their identity is stable, which lets the compiler recognize them.

Members: local.array

const

Group primitives under a name (shared.array, atomic.add...).

The attributes are ordinary functions: their identity is stable, which lets the compiler recognize them.

Members: const.array_like

atomic

Group primitives under a name (shared.array, atomic.add...).

The attributes are ordinary functions: their identity is stable, which lets the compiler recognize them.

Members: atomic.add, atomic.and_, atomic.cas, atomic.dec, atomic.exch, atomic.inc, atomic.max, atomic.min, atomic.nanmax, atomic.nanmin, atomic.or_, atomic.sub, atomic.xor

Compilation

jit(func_or_sig=None, device=False, **options)

Compile a Python function into a GPU kernel (or a device function).

Used like numba.cuda.jit ::

@unigpu.jit
def kernel(a): ...

@unigpu.jit("void(float32[:], float32)")
def typed_kernel(a, x): ...

@unigpu.jit(device=True)
def square(x): return x * x

Kernel

GPU kernel (__global__ in CUDA).

Launched with a configuration, exactly as with numba-cuda ::

kernel[griddim, blockdim](args)
kernel[griddim, blockdim, stream](args)
kernel[griddim, blockdim, stream, sharedmem](args)
kernel.forall(ntasks)(args)
  • configure(griddim, blockdim, stream=0, sharedmem=0) — Configured kernel, ready to be called.
  • forall(ntasks, tpb=0, stream=0, sharedmem=0) — 1D configuration covering ntasks tasks.
  • inspect_code(*args, device=None) — Code generated for these arguments on the current device (or device).

ConfiguredKernel

Kernel bound to a launch configuration.

DeviceFunction

Function callable from a kernel (__device__ in CUDA).

Called from Python, it simply runs on the CPU (handy to test it).

translate(kernel, *args, target='opencl')

OpenCL C or Metal code of a kernel, without any device.

The arguments only serve to determine the types (NumPy arrays or scalars); without arguments, the first declared signature is used ::

print(unigpu.translate(addition, a, b, c, target="metal"))

Memory

to_device(obj, stream=0, copy=True, to=None)

Copy a NumPy array to device memory (cudaMemcpy host → GPU).

device_array(shape, dtype=np.float64, strides=None, order='C', stream=0)

Allocate an uninitialized array on the device (cudaMalloc).

As in numba, the default type is float64; only C order is supported.

device_array_like(ary, stream=0)

Allocate an array with the same shape and type as ary.

DeviceArray

Array in device memory (numba's DeviceNDArray), with the properties of NumPy.

  • driver — Memory manager that allocated the buffer.
  • buffer — Device memory holding the elements (possibly shared with other arrays).
  • layout — Shape and type of the elements.
  • shape — Extent of each dimension.
  • dtype — Type of the elements.
  • ndim — Number of dimensions.
  • size — Number of elements.
  • itemsize — Size of an element, in bytes.
  • nbytes — Size of the elements, in bytes.
  • strides — Bytes between two consecutive elements of each dimension.
  • device — Owning device.
  • is_c_contiguous() — Are the elements contiguous, in C order?
  • copy_to_host(ary=None, stream=0) — Device → host copy.
  • copy_to_device(ary, stream=0) — Host → device copy (NumPy array) or device → device copy.
  • reshape(*newshape) — Same data, another shape (a -1 dimension is inferred).
  • ravel() — One-dimensional view.
  • view(dtype) — Reinterpret the bytes with another type (the last dimension adapts).

synchronize()

Wait until every command of the current device has completed.

Streams and events

stream()

Create a new stream (command queue) on the current device.

default_stream()

Default stream of the current device.

event(timing=True)

Create an event (to synchronize or to time).

event_elapsed_time(start, end)

Duration in milliseconds between two events (cuda.event_elapsed_time).

Stream

Command queue of a device.

  • synchronize() — Wait until every command of the stream has completed.
  • auto_synchronize() — Context manager that synchronizes the stream on exit (as numba does).

Event

Marker in a stream.

  • record(stream=0) — Place the event after the current commands of stream.
  • elapsed_time(end) — Duration in milliseconds between this event and end (waits for end).
  • synchronize() — Wait until the event is reached.
  • query() — True if the event is reached (without waiting).
  • wait(stream=0) — Make stream wait until the event is reached.

Platforms and devices

get_platforms()

Platforms available on this machine (clGetPlatformIDs).

list_devices(device_type=DeviceType.ALL)

Available devices, across all platforms.

select_device(spec)

Choose the current device.

spec: a :class:Device, its rank in :func:list_devices, a "opencl:0:1", "sim"...

get_current_device()

Current device (chosen automatically on first use).

gpus

List of devices (cuda.gpus); gpus.current is the current device.

Each element is a :class:DeviceScope: with gpus[1]: temporarily makes device 1 the current one, gpus[1].name is the name of the device.

Members: gpus.count, gpus.current, gpus.index

device_scope(device)

with device_scope(device): makes device current inside the block.

detect()

Print the detected platforms and devices; true if there is at least one.

is_available()

True if a real device (other than the simulator) is available.

close()

Close the open drivers and forget the chosen device.

Platform

A vendor's implementation, grouping devices.

  • get_devices(device_type=DeviceType.ALL) — Devices of the platform, filtered by type (like clGetDeviceIDs).

Device

Compute device (with gpus[1]: makes it current: :class:DeviceScope).

  • id — Rank of the device within its platform (numba's device.id).
  • name
  • device_type
  • backend
  • key — Identifier backend:platform:device (e.g. "opencl:0:1").
  • create_driver() — Create the driver (execution context) of this device.
  • check_allocation(nbytes) — OpenCL drivers often limit a buffer to a quarter of the memory of the GPU (CL_DEVICE_MAX_MEM_ALLOC_SIZE), Metal to maxBufferLength.

DeviceInfo

Characteristics of a device (equivalent of clGetDeviceInfo).

Memory sizes are in bytes; zero means "unknown".

DeviceType(*values)

Device type (can be combined: DeviceType.GPU | DeviceType.CPU).

  • label — Readable name of the type.

Errors

UnigpuError

Base class of every unigpu error.

DeviceNotFoundError

No device matches the request (or the backend is missing).

KernelContextError

A GPU primitive (threadIdx, syncthreads...) is used outside a kernel.

LaunchError

Invalid launch configuration (grid, block, shared memory...).

DeviceMemoryError

The device cannot allocate the array: too large for one buffer, or memory full.

TranslationError(message, location=None)

The kernel uses a Python construct that is not supported on GPUs.

  • with_location(location) — Return the error completed with location (if it had none, and if location is known).

CompilationError(message, source=None)

The driver's compiler (OpenCL, Metal) rejected the generated code.

The generated code, with line numbers, is attached to the message to help diagnose.

DivergentBarrierError

The threads of a block wait at different barriers (simulator).

DeadlockError

The threads of a block wait forever for a change that cannot happen (simulator).

UnigpuPerformanceWarning

Performance warning (equivalent of NumbaPerformanceWarning).