# =============================================================================
# Makefile for Stanford CS149 Lecture 6 demos (locality, communication, contention)
# =============================================================================
#
# [What this builds]
#   Six C++ programs (message passing, locality, histogram contention,
#   roofline, high watermarks, scaling) and runs two Python demos
#   (communication counting, contention models).
#
# [How to use]
#   make            # build the C++ demos
#   make run        # build, then run all demos in lecture order (~40 s in total)
#   make clean      # remove built files
#
# [Why these flags] -std=c++20 for std::barrier; -O2 as in every lecture. On
#   x86-64 the roofline needs AVX2 + FMA (the demo has NEON and AVX2 paths).
# -----------------------------------------------------------------------------

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

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

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

BINS := 01_message_passing 03_locality 05_histogram_contention 06_roofline \
        07_high_watermarks 08_scaling

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

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

06_roofline: ../lecture02/sinx.h

run: all
	@echo; ./01_message_passing
	@echo; $(PYTHON) 02_communication.py
	@echo; ./03_locality
	@echo; $(PYTHON) 04_contention.py
	@echo; ./05_histogram_contention
	@echo; ./06_roofline
	@echo; ./07_high_watermarks
	@echo; ./08_scaling

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