"""CUDA moat toy. NO GPU IS USED. This is a CPU illustration of an indexing
pattern and of a data-parallel call, not a CUDA benchmark. Timings are this
machine's and say nothing about GPU speed.

(1) A grid/block/thread index, one output element per index, run in two loops.
(2) The same SAXPY as a Python loop and as a NumPy expression.
(3) A fake dispatch table. The strings name libraries that show up in docs
    this article read. The function does not import PyTorch and does not call them.
"""
import time
import numpy as np

N = 1_000_000
a = 2.0
x = np.arange(N, dtype=np.float32)
y = np.ones(N, dtype=np.float32)


def saxpy_kernel(block_idx, block_dim, thread_idx, out, x, y, a, n):
    i = block_idx * block_dim + thread_idx
    if i < n:
        out[i] = a * x[i] + y[i]


small_n, block_dim = 10, 4
grid = (small_n + block_dim - 1) // block_dim
out_small = np.zeros(small_n, dtype=np.float32)
for b in range(grid):
    for t in range(block_dim):
        saxpy_kernel(b, block_dim, t, out_small, x, y, a, small_n)
print(f"(1) grid={grid} blocks x {block_dim} threads = {grid * block_dim} 'threads' for n={small_n}")
print("(1) out:", out_small.tolist())

t0 = time.perf_counter()
out_loop = [a * x[i] + y[i] for i in range(N)]
t1 = time.perf_counter()
out_vec = a * x + y
t2 = time.perf_counter()
assert np.allclose(out_loop, out_vec)
ratio = (t1 - t0) / (t2 - t1)
print(f"(2) python loop: {t1 - t0:.3f}s | numpy vectorized: {t2 - t1:.4f}s | ~{ratio:.0f}x on this CPU")

# Names only. Not a running framework.
BACKENDS = {
    "cuda": "cuBLAS/cuDNN, the names in the PyTorch CUDA notes",
    "rocm": "HIP build; the notes still require device 'cuda'",
    "cpu": "no GPU library is called",
}


def matmul(device):
    return f"user code identical -> device='{device}' -> {BACKENDS[device]}"


for d in ("cuda", "rocm", "cpu"):
    print("(3)", matmul(d))
