warp.experimental.cutile.encode_struct#

warp.experimental.cutile.encode_struct(instance)[source]#

Return a Warp struct instance’s bytes as a NumPy int32 array padded to a power-of-two length.

Experimental

The cuTile interop API is experimental and subject to change without a formal deprecation cycle. See export_func() for details.

The result has the layout a struct operand of export_func() expects: upload it as a warp.array and load it as an int32 tile (as is for one struct shared by every invocation, tiled per invocation otherwise).

Parameters:

instance (object) – An instance of a @warp.struct class.

Returns:

A 1-D NumPy int32 array holding the struct’s bytes followed by zero padding up to the next power-of-two word count.

Raises:

TypeError – If instance is not a Warp struct instance.

Return type:

ndarray

Example

Upload the bytes as a warp.array and load them as one int32 tile of the array’s full length to share one struct with every invocation:

import cuda.tile as ct
import numpy as np

import warp as wp
import warp.experimental.cutile as wpct

TILE_SIZE = 128


@warp.struct
class SphereFalloff:
    center: warp.vec3
    radius: float


@warp.func
def falloff_weight(falloff: SphereFalloff, p: warp.vec3) -> float:
    return warp.max(1.0 - warp.length(p - falloff.center) / falloff.radius, 0.0)


wp_falloff_weight = wpct.export_func(falloff_weight)

falloff = SphereFalloff()
falloff.center = warp.vec3(0.5, 0.5, 0.5)
falloff.radius = 0.25
falloff_words = warp.array(wpct.encode_struct(falloff), dtype=warp.int32)
NUM_FALLOFF_WORDS = falloff_words.shape[0]


@ct.kernel
def falloff_weight_cutile_kernel(falloff_words, points, weights):
    falloff_tile = ct.load(falloff_words, (0,), shape=(NUM_FALLOFF_WORDS,))  # one struct for every point
    point_tile = ct.load(points, (0, 0), shape=(TILE_SIZE, 4))
    ct.store(weights, (0,), wp_falloff_weight(falloff_tile, ct.reshape(point_tile, (TILE_SIZE * 4,))))


points = warp.array(np.random.default_rng(42).uniform(0.0, 1.0, (TILE_SIZE, 4)), dtype=warp.float32)
weights = warp.empty(TILE_SIZE, dtype=warp.float32)
ct.launch(
    warp.get_stream().cuda_stream, (1, 1, 1), falloff_weight_cutile_kernel, (falloff_words, points, weights)
)