# =============================================================================
# Makefile for Stanford CS149 Lecture 8 demos (data-parallel thinking)
# =============================================================================
#
# [What this builds]
#   Three C++ programs on the data-parallel library dp.h (primitives on the
#   CPU, sparse matrix-vector multiply, the particle grid) and one Metal
#   program (a large scan on the Apple GPU; macOS + swiftc only). The Python
#   demo uses Lecture 7's SIMT simulator.
#
# [How to use]
#   make            # build
#   make run        # run all demos in lecture order (~15 s)
#   make clean
# -----------------------------------------------------------------------------

CXX      ?= c++
CXXFLAGS := -std=c++20 -O2 -g -Wall -Wextra -pthread
PYTHON   ?= python3
SWIFTC   := $(shell command -v swiftc 2>/dev/null)

HEADERS := dp.h ../lecture01/cs149_timer.h ../lecture01/machine_info.h
BINS    := 02_primitives_cpu 03_sparse_matvec 04_particle_grid

.PHONY: all run clean
ifeq ($(SWIFTC),)
all: $(BINS)
	@echo "swiftc not found: skipping the Metal demo"
else
all: $(BINS) 05_metal_scan
endif

$(BINS): %: %.cpp $(HEADERS)
	$(CXX) $(CXXFLAGS) -o $@ $<

05_metal_scan: 05_metal_scan.swift ../lecture07/metal_util.swift
	swiftc -O -parse-as-library ../lecture07/metal_util.swift 05_metal_scan.swift -o $@

run: all
	@echo; $(PYTHON) 01_scan_algorithms.py
	@echo; ./02_primitives_cpu
	@echo; ./03_sparse_matvec
	@echo; ./04_particle_grid
	@if [ -x ./05_metal_scan ]; then echo; ./05_metal_scan; fi

clean:
	rm -f $(BINS) 05_metal_scan
	rm -rf *.dSYM __pycache__
