# =============================================================================
# Makefile for Stanford CS149 Lecture 3 demos
# =============================================================================
#
# [What this builds]
#   Two C++ programs (memory bandwidth; SIMD lane assignment) and runs two
#   Python demos (latency/bandwidth model; the ISPC gang emulator).
#
# [How to use]
#   make            # build the C++ demos (NEON on arm64, AVX2+FMA on x86-64)
#   make run        # build, then run all demos in order (~5 s in total)
#   make selftest   # self-check of the SPMD gang emulator
#   make clean      # remove built files
#
# [Why these flags] as in Lecture 2. Headers come from ../lecture01 (timer,
#   machine info) and ../lecture02 (sinx.h: SIMD width, vector helpers).
# -----------------------------------------------------------------------------

CXX      ?= c++
CXXFLAGS := -std=c++20 -O2 -g -Wall -Wextra -pthread
PYTHON   ?= python3

HEADERS := ../lecture01/cs149_timer.h ../lecture01/machine_info.h ../lecture02/sinx.h

BINS := 02_bandwidth_meter 04_gang_assignment

ARCH := $(shell uname -m)
ifeq ($(ARCH),x86_64)
  SIMDFLAGS := -mavx2 -mfma
endif

.PHONY: all run selftest clean
all: $(BINS)

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

run: all
	@echo; $(PYTHON) 01_latency_vs_bandwidth.py
	@echo; ./02_bandwidth_meter
	@echo; $(PYTHON) 03_ispc_semantics.py
	@echo; ./04_gang_assignment

selftest:
	$(PYTHON) spmd.py

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