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

"""Run a CUTLASS copy kernel twice with direct or transpose load/store."""

import argparse
import os
import tempfile

# Import the kernel DSL and its cooperative primitives.
# isort: off
import numpy as np
import cutlass
from cutlass import cute
from cutlass.cute.runtime import make_ptr

# First breakpoint: inspect compiler validation.
import cuda.coop.cutlass as coop
from cuda.bindings import driver
# isort: on


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 main(*, algorithm="direct", items_per_thread=2):
    """Compile one copy kernel, then verify two launches of the same callable.

    Reset the destination before each launch so an earlier result cannot hide
    a missing write. The algorithm selects whether compilation must also plan
    shared scratch. Driver allocations are freed even if compilation or a
    comparison fails.
    """

    # docs: start cutlass-debug-kernel
    @cute.kernel
    def copy_tile(
        source: cute.Pointer,
        destination: cute.Pointer,
        items_per_thread: cutlass.Constexpr,
    ):
        block = coop.this_block()
        scratch = coop.TempStorage(auto_sync=True)
        items = coop.load(
            block,
            source,
            items_per_thread=items_per_thread,
            algorithm=algorithm,
            temp_storage=scratch,
        )
        coop.store(
            block, destination, items, algorithm=algorithm, temp_storage=scratch
        )

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

    # docs: end cutlass-debug-kernel

    cutlass.cuda.initialize_cuda_context()
    source = np.arange(128 * items_per_thread, dtype=np.int32)
    destination = np.full_like(source, -1)
    source_device = _check(driver.cuMemAlloc(source.nbytes))
    try:
        destination_device = _check(driver.cuMemAlloc(destination.nbytes))
        try:
            _check(
                driver.cuMemcpyHtoD(
                    source_device, source.ctypes.data, source.nbytes
                )
            )
            source_pointer = make_ptr(
                cutlass.Int32,
                int(source_device),
                cute.AddressSpace.gmem,
                assumed_align=16,
            )
            destination_pointer = make_ptr(
                cutlass.Int32,
                int(destination_device),
                cute.AddressSpace.gmem,
                assumed_align=16,
            )

            # docs: start cutlass-debug-launches
            compiled = cute.compile(
                launch, source_pointer, destination_pointer, items_per_thread
            )
            for iteration in range(2):
                destination.fill(-1)
                _check(
                    driver.cuMemcpyHtoD(
                        destination_device,
                        destination.ctypes.data,
                        destination.nbytes,
                    )
                )
                compiled(source_pointer, destination_pointer)
                _check(driver.cuCtxSynchronize())
                _check(
                    driver.cuMemcpyDtoH(
                        destination.ctypes.data,
                        destination_device,
                        destination.nbytes,
                    )
                )
                np.testing.assert_array_equal(destination, source)
                print(f"Launch {iteration + 1} ({algorithm}): copy verified")
            # docs: end cutlass-debug-launches
        finally:
            _check(driver.cuMemFree(destination_device))
    finally:
        _check(driver.cuMemFree(source_device))
    return destination


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--algorithm", choices=("direct", "transpose"), default="direct"
    )
    parser.add_argument("--items-per-thread", type=int, default=2)
    args = parser.parse_args()
    # Each debug session reaches NVRTC; launch two reuses the compiled kernel.
    with tempfile.TemporaryDirectory(
        prefix="cuda-coop-cutlass-debug-"
    ) as cache_dir:
        os.environ["CUDA_COOP_CUTLASS_PROVIDER_CACHE_DIR"] = cache_dir
        main(algorithm=args.algorithm, items_per_thread=args.items_per_thread)
