# =============================================================================
# Makefile for Stanford CS149 Lecture 7 demos (GPU architecture and CUDA)
# =============================================================================
#
# [What this builds]
#   One Swift program that runs Metal compute kernels on an Apple GPU
#   (05_metal_convolution). The four Python demos need no build.
#   convolve.cu is for readers with an NVIDIA GPU (nvcc); it is not built here.
#
# [How to use]
#   make            # build the Metal demo (macOS with swiftc only)
#   make run        # run all demos in lecture order (~5 s)
#   make selftest   # self-check of the SIMT simulator
#   make clean
#
# On a machine without swiftc (Linux), `make run` runs the Python demos and
# skips the Metal one.
# -----------------------------------------------------------------------------

PYTHON ?= python3
SWIFTC := $(shell command -v swiftc 2>/dev/null)

METAL_BINS := 05_metal_convolution

.PHONY: all run clean selftest
ifeq ($(SWIFTC),)
all:
	@echo "swiftc not found: skipping the Metal demo (Python demos need no build)"
else
all: $(METAL_BINS)
endif

05_metal_convolution: 05_metal_convolution.swift metal_util.swift
	swiftc -O -parse-as-library metal_util.swift 05_metal_convolution.swift -o $@

selftest:
	$(PYTHON) simt.py

run: all
	@echo; $(PYTHON) 01_cuda_basics.py
	@echo; $(PYTHON) 02_convolution.py
	@echo; $(PYTHON) 03_block_scheduling.py
	@echo; $(PYTHON) 04_memory_and_divergence.py
	@if [ -x ./05_metal_convolution ]; then echo; ./05_metal_convolution; fi

clean:
	rm -f $(METAL_BINS)
	rm -rf __pycache__
