warp.tile_randi#

warp.tile_randi(
shape: tuple[int, ...],
rng: uint32,
storage: str = 'register',
) Tile[Any, tuple[int, ...]]#
  • Kernel: true
  • Python: false
  • Differentiable: false

Generate a tile of random integers.

Each element is drawn with randi() using rng. Values are deterministic for a fixed kernel, device, and block_dim given the same rng, but are not portable across block_dim values or between CPU and GPU (see CPU Tile Semantics).

The call leaves rng unchanged, so two calls with the same rng in the same thread produce identical tiles. Pass a different rng (for example a rand_init() offset that is unique per block) to get different values.

Parameters:
  • shape – Shape of the output tile. Must be a compile-time constant.

  • rng – Random number generator state, typically from rand_init().

  • storage – The storage location for the tile: "register" for registers or "shared" for shared memory. Must be a compile-time constant.

Returns:

A tile of int32 elements with the requested shape, each in the range [-2^31, 2^31) as for randi().

Example

TILE_M, TILE_N = 2, 2
M, N = 2, 2
seed = 42

@wp.kernel
def generate_random_integers(seed: int, x: wp.array2d[int]):
    i, j = wp.tid()
    # one distinct RNG offset per block
    rng = wp.rand_init(seed, i * N + j)
    t = wp.tile_randi(shape=(TILE_M, TILE_N), rng=rng)
    wp.tile_store(x, t, offset=(i * TILE_M, j * TILE_N))

x = wp.zeros(shape=(M * TILE_M, N * TILE_N), dtype=int)

wp.launch_tiled(generate_random_integers, dim=[M, N], inputs=[seed], outputs=[x], block_dim=8)

print(x.numpy())
[[  798497746  1803297529  -955788638    17806966]
 [ 1788185933  1320194893  2073257406 -2009156320]
 [ -257534450 -1138585923  1145322783  -321794125]
 [-2096177388 -1835610841  1159339128  -652221052]]

The values above are those produced on a CUDA device; a CPU launch of the same kernel generates a different sequence.

warp.tile_randi(
shape: int32,
rng: uint32,
storage: str = 'register',
) Tile[Any, tuple[int, ...]]
  • Kernel: true
  • Python: false
  • Differentiable: false

Generate a tile of random integers.

Overload for 1D tiles: shape is the number of elements, equivalent to passing (shape,). See the overload taking a tuple-valued shape argument for usage details and an example.

warp.tile_randi(
shape: tuple[int, ...],
rng: uint32,
min: int32,
max: int32,
storage: str = 'register',
) Tile[Any, tuple[int, ...]]
  • Kernel: true
  • Python: false
  • Differentiable: false

Generate a tile of random integers in the range [min, max).

See the overload without min and max for reproducibility details and a usage example.

Parameters:
  • shape – Shape of the output tile. Must be a compile-time constant.

  • rng – Random number generator state, typically from rand_init().

  • min – Minimum value (inclusive). Must be a compile-time integer constant representable as int32.

  • max – Maximum value (exclusive). Must be a compile-time integer constant representable as int32 and strictly greater than min.

  • storage – The storage location for the tile: "register" for registers or "shared" for shared memory. Must be a compile-time constant.

Returns:

A tile of int32 elements with the requested shape, each in [min, max).

warp.tile_randi(
shape: int32,
rng: uint32,
min: int32,
max: int32,
storage: str = 'register',
) Tile[Any, tuple[int, ...]]
  • Kernel: true
  • Python: false
  • Differentiable: false

Generate a tile of random integers in the range [min, max).

Overload for 1D tiles: shape is the number of elements, equivalent to passing (shape,). See the overload taking a tuple-valued shape argument with min and max for usage details and an example.