# benchmark/python -- the Python comparison: a ctypes binding of this tree's library and its
# comparison with scipy.special.kv and sklearn.gaussian_process.kernels.Matern, measured with
# this tree's own benchmark code (README.md).  Called as `make -C benchmark/python <target>` or
# through the top-level `make python-<target>`.
#
#   make venv       build/python/venv with the pinned requirements.txt (+ the binding, editable)
#   make shim       build/python/libpyshim.so: the benchmark's measurement code for Python
#   make matrix     build/python/libmatern_matrix.so: fused covariance matrices
#   make data       data/python/: the corpus (linked) + the constant-order sets, and data/gp/:
#                   the covariance-matrix sets, generated once (needs FLINT/arb: ARB_CFLAGS,
#                   ARB_LIBS); with the runs asset unpacked in the root, only links the corpus
#                   and checks the shipped sets (no FLINT/arb)
#   make data-check the generated files against gen/data.sha256
#   make check      binding tests, the gp/gpfit control and its K-matrix counter (no data
#                   needed) + shim-check (the CSV formats against benchmark/suppliers.c)
#   make gate       the Python path's accuracy rows against the published AMD rows, string
#                   for string (the published runs: make published); the timed libmatern
#                   callable (Workspace per call) against the scored call's bits; mpmath spot
#                   check of the generated references (needs the data)
#   make smoke      reduced run (REPS=3, two laws, ladder to 2^14) into runs/smoke-<stamp>
#   make run        the full run (harness/run_all.sh) into runs/<stamp>; the paper's figure
#                   fig-paper-scipy reads its accuracy.csv (make figures, --python-runs)
SHELL       := /bin/bash
HERE        := $(abspath .)
REPO        := $(abspath ../..)
BUILD       := $(REPO)/build/python
DATA        ?= $(REPO)/data
VENV        ?= $(BUILD)/venv
PY          := $(VENV)/bin/python
# The interpreter that creates the venv (Python >= 3.11 for the pinned packages).
PYTHON_BASE ?= $(shell for p in python3.13 python3.12 python3.11 python3; do \
                   command -v $$p >/dev/null 2>&1 && $$p -c 'import sys; sys.exit(sys.version_info < (3, 11))' \
                   && { command -v $$p; break; }; done)
CC          ?= cc
SHIM_CFLAGS := -std=c11 -Wall -Wextra -fno-math-errno -O2 -ffp-contract=fast -fPIC
SHIM_INC    := -I$(REPO)/benchmark -I$(REPO)/benchmark/common -I$(REPO)/include
# No __pycache__ in the source tree (make audit: build products live under build/).
export PYTHONDONTWRITEBYTECODE := 1
export LIBMATERN_LIBRARY ?= $(REPO)/build/libmatern.so.0
export LIBMATERN_PY_BUILD := $(BUILD)
export LIBMATERN_PY_DATA := $(DATA)

.PHONY: library venv shim matrix data data-check check shim-check test gate smoke run clean

library: $(REPO)/build/libmatern.so.0 $(REPO)/build/benchmark_cpu
$(REPO)/build/libmatern.so.0 $(REPO)/build/benchmark_cpu:
	$(MAKE) -C $(REPO) shared benchmark-build

venv: $(VENV)/.stamp
$(VENV)/.stamp: requirements.txt python/pyproject.toml
	@test -n "$(PYTHON_BASE)" || { echo "benchmark/python: no Python >= 3.11 found (PYTHON_BASE=<interpreter>)"; exit 1; }
	test -x $(PY) || $(PYTHON_BASE) -m venv $(VENV)
	$(PY) -m pip install -q -r requirements.txt
	$(PY) -m pip install -q --no-deps -e python
	touch $@

shim: $(BUILD)/libpyshim.so
$(BUILD)/libpyshim.so: harness/pyshim.c $(REPO)/benchmark/corpus_dataset.c
	@mkdir -p $(BUILD)
	$(CC) $(SHIM_CFLAGS) $(SHIM_INC) -shared -o $@ harness/pyshim.c \
	    $(REPO)/benchmark/corpus_dataset.c -lm

matrix: $(BUILD)/libmatern_matrix.so
# pow_tile.c alone is built with -ffast-math (so z^(2 nu) of the nu < 1 gradient calls glibc's
# libmvec SIMD pow); the rest keeps contraction off.  Built to a temporary name and renamed,
# so a process that has the library loaded keeps its copy.
$(BUILD)/libmatern_matrix.so: csrc/matern_matrix.c csrc/pow_tile.c $(REPO)/build/libmatern.so.0
	@mkdir -p $(BUILD)
	$(CC) -std=gnu11 -O3 -ffast-math -fopenmp -fPIC -c -o $(BUILD)/pow_tile.o csrc/pow_tile.c
	$(CC) -std=gnu11 -O3 -fno-math-errno -ffp-contract=off -fopenmp -fPIC -shared -Wall -Wextra \
	    -I$(REPO)/include -o $@.tmp csrc/matern_matrix.c $(BUILD)/pow_tile.o \
	    -L$(REPO)/build -l:libmatern.so.0 -Wl,-rpath,$(REPO)/build -lmvec -lm
	mv -f $@.tmp $@

data: $(DATA)/python/MANIFEST.sha256
$(DATA)/python/MANIFEST.sha256: gen/gen_extra.c gen/make_data.sh
	OMP_NUM_THREADS=$${OMP_NUM_THREADS:-32} REPO=$(REPO) DATA=$(DATA) BUILD=$(BUILD) bash gen/make_data.sh

# The generated files (or those of the release asset) against gen/data.sha256.
data-check:
	cd $(DATA)/python && sha256sum -c $(HERE)/gen/data.sha256

shim-check: venv
	$(PY) tools/shim_check.py $(REPO)/benchmark/suppliers.c harness/pyshim.c

test: library venv shim matrix
	$(PY) tests/test_binding.py
	$(PY) tests/test_matrix.py
	$(PY) tests/test_sklearn_kernel.py
	$(PY) tests/test_control.py

check: test shim-check

gate: library venv shim
	$(PY) tests/test_gate.py
	$(PY) tests/test_workspace.py
	$(PY) tests/test_references.py

smoke: library venv shim matrix
	SMOKE=1 PY=$(PY) REPO=$(REPO) bash harness/run_all.sh

run: library venv shim matrix
	PY=$(PY) REPO=$(REPO) bash harness/run_all.sh

clean:
	rm -rf $(BUILD)/libpyshim.so $(BUILD)/libmatern_matrix.so $(BUILD)/pow_tile.o
