# Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

"""Copy independent partial tiles with two physical warps in one CuTe block."""

import cutlass
import numpy as np
from cutlass import cute
from cutlass.cute.runtime import make_ptr

from cuda.bindings import driver

_BLOCK = (8, 4, 2)
_SOURCE_OFFSET = 3
_DESTINATION_OFFSET = 5


def _check(result):
    """Raise on Driver errors and unwrap a single returned handle or value."""

    if int(result[0]):
        raise RuntimeError(f"CUDA Driver call failed: {result[0]}")
    return result[1] if len(result) == 2 else result[1:]


def run_example(items_per_thread=4):
    """Check two warp tiles with different valid prefixes in one block.

    Each warp loads a different count, then stores its entire tile. The CPU
    comparison therefore checks copied values, fill values and each warp's
    automatic tile origin. Sentinels outside the two stored tiles must remain
    untouched. ``items_per_thread`` sets each thread's payload size.

    Return the verified destination array.
    """

    warp_tile = 32 * items_per_thread
    block_tile = 2 * warp_tile
    import cuda.coop.cutlass as coop

    # docs: start cutlass-warp-load-store
    @cute.kernel
    def copy_warp_tiles(
        source: cute.Pointer,
        destination: cute.Pointer,
        items_per_thread: cutlass.Constexpr,
    ):
        warp_tile = 32 * items_per_thread
        x, y, z = cute.arch.thread_idx()
        thread = x + _BLOCK[0] * (y + _BLOCK[1] * z)
        warp = thread // 32
        # Each warp gets its own consecutive tile automatically. The user
        # offset selects the beginning of the block's collection of tiles.
        payload = coop.load(
            coop.this_warp(),
            source,
            items_per_thread=items_per_thread,
            algorithm="transpose",
            valid_items=cutlass.Int32(warp_tile - 7 - warp * 11),
            oob_default=-1,
            offset=_SOURCE_OFFSET,
        )
        coop.store(
            coop.this_warp(),
            destination,
            payload,
            algorithm="transpose",
            offset=_DESTINATION_OFFSET,
        )

    @cute.jit
    def launch(
        source: cute.Pointer,
        destination: cute.Pointer,
        items_per_thread: cutlass.Constexpr,
    ):
        copy_warp_tiles(source, destination, items_per_thread).launch(
            grid=1, block=_BLOCK
        )

    # docs: end cutlass-warp-load-store

    source = np.arange(block_tile + _SOURCE_OFFSET, dtype=np.int32)
    destination = np.full(
        block_tile + _DESTINATION_OFFSET + 3,
        -101,
        dtype=np.int32,
    )
    cutlass.cuda.initialize_cuda_context()
    src = _check(driver.cuMemAlloc(source.nbytes))
    try:
        dst = _check(driver.cuMemAlloc(destination.nbytes))
        try:
            _check(driver.cuMemcpyHtoD(src, source.ctypes.data, source.nbytes))
            _check(
                driver.cuMemcpyHtoD(
                    dst, destination.ctypes.data, destination.nbytes
                )
            )
            src_pointer = make_ptr(
                cutlass.Int32,
                int(src),
                cute.AddressSpace.gmem,
                assumed_align=16,
            )
            dst_pointer = make_ptr(
                cutlass.Int32,
                int(dst),
                cute.AddressSpace.gmem,
                assumed_align=16,
            )
            launch(src_pointer, dst_pointer, items_per_thread)
            _check(driver.cuCtxSynchronize())
            _check(
                driver.cuMemcpyDtoH(
                    destination.ctypes.data, dst, destination.nbytes
                )
            )
        finally:
            _check(driver.cuMemFree(dst))
    finally:
        _check(driver.cuMemFree(src))
    expected = np.full_like(destination, -101)
    for warp in range(2):
        origin = warp * warp_tile
        count = warp_tile - 7 - warp * 11
        expected[
            _DESTINATION_OFFSET + origin : _DESTINATION_OFFSET
            + origin
            + warp_tile
        ] = -1
        expected[
            _DESTINATION_OFFSET + origin : _DESTINATION_OFFSET + origin + count
        ] = source[_SOURCE_OFFSET + origin : _SOURCE_OFFSET + origin + count]
    np.testing.assert_array_equal(destination, expected)
    return destination


if __name__ == "__main__":
    run_example()
