CC      := gcc
CFLAGS  := -Wall -Wextra -std=c17
SRC     := mandel_mpi.c
TARGET  := $(SRC:.c=) # filename without .c extension
OUTDIR  := out
OUT     := $(OUTDIR)/$(TARGET)
ARGS    := 1 # yes/no save image
NBENCH  := 3 # how many times to run the command for benchmark
NPROC   := 3 # how many mpi processes

# usage:
# you can run `make parallel-time` or `make time SRC=mandel_cpu.c` to benchmark.
# using the provided python tool.

.PHONY: all clean run parallel-build parallel-run parallel-time time show

all: clean parallel-build parallel-run show

clean:
	rm -rf $(OUTDIR)/*

show: $(OUTDIR)/mandel.bmp
	feh $<

pdf: report.md
	pandoc report.md -o $(OUTDIR)/report.pdf --pdf-engine=typst

zip: $(OUTDIR)/report.pdf Makefile mandel_mpi.c bench.py
	zip $(OUTDIR)/handin.zip $^

unzip: $(OUTDIR)/handin.zip
	unzip $< -d $(OUTDIR)/handin

# --- cpu-based ---
$(OUT): $(SRC)
	$(CC) $(CFLAGS) -o $(OUT) $<

run: $(OUT)
	cd $(OUTDIR) && ./$(TARGET) $(ARGS)

# use with mandel_cpu.c as SRC to benchmark non-parallel
time: $(OUT)
	python3 bench.py './$(OUT) 0' $(NBENCH)

# --- parallel ---
parallel-build: $(SRC)
	mpicc -o $(OUT) $(SRC)

parallel-run: $(SRC)
	cd $(OUTDIR) && mpirun -np $(NPROC) $(TARGET) $(ARGS)

# use with mandel_mpi.c as SRC to benchmark non-parallel
parallel-time: parallel-build
	python3 bench.py 'cd $(OUTDIR) && mpirun -np $(NPROC) $(TARGET) 0' $(NBENCH)
