Simulate the thread hierarchy in NumPy
CPU · your Macscript60 minrequiredGoal · A pure-Python "fake CUDA" that runs a kernel function over a grid of blocks and threads, including warp grouping and a divergence counter.
- 1Write launch(kernel, grid_dim, block_dim, *args) that loops over every (blockIdx, threadIdx) pair and calls kernel with those indices.
- 2Implement a vector-add kernel using idx = blockIdx * blockDim + threadIdx with a bounds guard; verify against numpy.
- 3Group the threads of each block into warps of 32 and record, per warp, whether all threads took the same branch in an if statement.
- 4Write a kernel with if idx % 2 == 0 and another with if idx < n/2; count divergent warps for each and explain the difference.
- 5Implement a 2D launch and a matrix transpose kernel; verify against numpy .T.
- 6Add a grid-stride loop variant and show it gives the same result with gridDim smaller than n / blockDim.
- Deliverable
- fake_cuda.py with the launcher, three kernels and printed divergence statistics.
- Check
- Vector add and transpose match numpy exactly; the idx % 2 kernel reports 100 percent divergent warps and the idx < n/2 kernel reports at most one divergent warp.