# =============================================================================
# Makefile for Stanford CS149 Lecture 12 demos (mapping AI to the datacenter)
# =============================================================================
#
# [What this builds]
#   One C++ program: all-reduce algorithms on real threads. Demo 01 needs
#   numpy; demos 03-05 need only the standard library (demo 03 imports
#   ../lecture11/pipesim.py).
#
# [How to use]
#   make            # build
#   make run        # run all demos in lecture order (~10 s)
#   make selftest   # self-checks of collectives.py and dram_sim.py
#   make clean
# -----------------------------------------------------------------------------

CXX      ?= c++
CXXFLAGS := -std=c++20 -O2 -g -Wall -Wextra -pthread
PYTHON   ?= python3
HEADERS  := ../lecture01/cs149_timer.h ../lecture01/machine_info.h
BINS     := 02_allreduce_threads

ifeq ($(shell uname -m),x86_64)
CXXFLAGS += -mavx2 -mfma
endif

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

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

run: all
	@echo; $(PYTHON) 01_collectives.py
	@echo; ./02_allreduce_threads
	@echo; $(PYTHON) 03_parallelism.py
	@echo; $(PYTHON) 04_pipeline_schedules.py
	@echo; $(PYTHON) 05_dram.py

selftest:
	$(PYTHON) collectives.py
	$(PYTHON) dram_sim.py

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