warp.tile\_reduce ================= .. currentmodule:: warp._src.lang .. function:: tile_reduce(op: Callable, a: Tile[Any, tuple[int, ...]]) -> Tile[Any, tuple[Literal[1]]] .. wp-function-tags:: :kernel: true :python: false :differentiable: false Apply a custom reduction operator across a tile. Reduce across all elements using the provided operator. :param op: A callable function that accepts two arguments and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's data type :returns: A single-element tile with the same data type as the input tile. .. rubric:: Example .. code-block:: python @wp.kernel def factorial(): t = wp.tile_arange(1, 10, dtype=int) s = wp.tile_reduce(wp.mul, t) print(s) wp.launch_tiled(factorial, dim=[1], inputs=[], block_dim=16) .. code-block:: text [362880] = tile(shape=(1), storage=register) .. function:: tile_reduce(op: Callable, a: Tile[Scalar, tuple[int, ...]], axis: int32) -> Tile[Scalar, tuple[int, ...]] :noindex: .. wp-function-tags:: :kernel: true :python: false :differentiable: false Apply a custom reduction operator across a tile. Reduce across a tile axis using the provided operator. :param op: A callable function that accepts two arguments and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's data type. Must reside in shared memory. :param axis: The tile axis to perform the reduction across. Must be a compile-time constant. :returns: A tile with the same shape as the input tile less the axis dimension and the same data type as the input tile. On a partial CPU block, a slice with no active values returns the operation's identity for ``wp.add``, ``wp.mul``, ``wp.min``, and ``wp.max``. Other operators have no declared identity, so an empty slice triggers an assertion instead of returning an arbitrary value. See :ref:`CPU Tile Semantics ` for definitions of partial CPU blocks and active lanes. .. rubric:: Example .. code-block:: python TILE_M = wp.constant(4) TILE_N = wp.constant(2) @wp.kernel def compute(x: wp.array2d[float], y: wp.array[float]): a = wp.tile_load(x, shape=(TILE_M, TILE_N)) b = wp.tile_reduce(wp.add, a, axis=1) wp.tile_store(y, b) arr = np.arange(TILE_M * TILE_N).reshape(TILE_M, TILE_N) x = wp.array(arr, dtype=float) y = wp.zeros(TILE_M, dtype=float) wp.launch_tiled(compute, dim=[1], inputs=[x], outputs=[y], block_dim=32) print(x.numpy()) print(y.numpy()) .. code-block:: text [[0. 1.] [2. 3.] [4. 5.] [6. 7.]] [ 1. 5. 9. 13.]