appendix-b

appendix B  Running the code examples

 

All code examples live in the public book.cu repository, organized by chapter. Clone it once, and the per-chapter commands that follow will run from inside that directory:

git clone https://github.com/infatoshi/book.cu
cd book.cu

Each chapter directory is self-contained with build scripts and README files.

B.1 Chapter 2: Vector addition (0_vecadd/)

What you’ll learn: Basic CUDA kernel launches, memory management, and thread-indexing patterns.

Build and run the following:

cd book.cu/0_vecadd

# Using Makefile (recommended)
make all
make run

# Or compile individually
nvcc vecadd.cu -o vecadd
./vecadd

nvcc vecadd_scalable.cu -o vecadd_scalable
./vecadd_scalable

nvcc tensor_add_3d.cu -o tensor_add_3d
./tensor_add_3d

Expected output:

Vector addition successful!
All 8 elements match.

Learning path:

  1. Start with vecadd.cu, the simplest CUDA program (eight elements, eight threads).
  2. Move to vecadd_scalable.cu to handle large arrays (1 million elements).
  3. Explore tensor_add_3d.cu for multidimensional indexing in deep learning.

B.2 Chapter 3: Naive neural network operations (1_naive/)

What you’ll learn: CPU-first development, fundamental neural network operations, correctness verification.

Build and run the following:

cd book.cu/1_naive

# Build all examples
make all

# Run all with verification
make run

# Or build/run individual operations
make vector_add
./vector_add

make gemm
./gemm

make softmax
./softmax

make conv2d
./conv2d

Expected output:

B.3 Chapter 4: MLP training on MNIST (2_mnist/)

B.4 Chapter 5: Transformer architecture (3_transformer/)

B.5 Chapter 6: CUDA core optimizations (4_optim/)

B.6 Chapter 7: Tensor core programming (5_tensor_cores/)

B.7 Chapter 8: Flash attention (6_flash/)

B.8 Chapter 9: Quantization (7_quant/)

B.9 Chapter 10: Distributed computing (8_distributed/)

B.10 Chapter 11: CUTLASS (9_cutlass/)