#!/bin/bash
# run_all.sh.txt (a bash script; the archive accepts only certain file types, hence .txt): rerun every certificate of the supplement
# at a SMALL resolution and compare the outputs with expected_small/ (compare.py).
#
# These are consistency runs, not certificates: at these resolutions several margins are negative. They check that the code and inputs
# as shipped reproduce the stored small-resolution computations exactly. The theorems cite the full-resolution outputs in full/, whose
# runs take from minutes to about 18 hours (README.md lists the full commands, run times and peak memory).
#
# Usage:   PY=<python with numpy and python-flint> bash run_all.sh.txt [OUTDIR]
# Default: PY = python3; OUTDIR = ./out. Single-threaded; each script runs at nice 10 if `nice` is available.
# The code and inputs are first copied into OUTDIR, the input pickles are decoded there from their base64 text files, and every script
# runs from its copy there (the scripts write their outputs into their working directory).
set -u
HERE="$(cd "$(dirname "$0")" && pwd)"
OUT="$(mkdir -p "${1:-$HERE/out}" && cd "${1:-$HERE/out}" && pwd)"
PY="${PY:-python3}"
"$PY" -c 'import numpy, flint' 2>/dev/null || { echo "run_all.sh.txt: $PY cannot import numpy and python-flint; set PY=<python with both>" >&2; exit 2; }
export OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 MKL_NUM_THREADS=1 FLINT_NUM_THREADS=1 PYTHONDONTWRITEBYTECODE=1
NICE=""; command -v nice >/dev/null && NICE="nice -n 10"
for d in engine2 exact1 sep_p4 sep_p5 inputs full audit; do cp -rp "$HERE/$d" "$OUT/"; done
for f in "$OUT"/inputs/*.pkl.b64.txt; do  # decode each input pickle; compare.py checks the hashes
  "$PY" -c 'import base64, sys; open(sys.argv[2], "wb").write(base64.b64decode(open(sys.argv[1]).read()))' "$f" "${f%.b64.txt}"
done
echo "# run_all.sh.txt started $(date -u '+%Y-%m-%d %H:%M:%S UTC'); $($PY -c 'import sys, numpy, flint; print("python", sys.version.split()[0], "numpy", numpy.__version__, "python-flint", flint.__version__)')"
now() {  # a monotonic clock in seconds (the wall clock can step on some virtual machines)
  if [ -r /proc/uptime ]; then cut -d' ' -f1 /proc/uptime; else date +%s; fi
}
run() {  # run LOGFILE CMD...: stdout and stderr to LOGFILE, report exit status and elapsed time
  local name=$1; shift; local t0; t0=$(now)
  $NICE "$@" > "$name" 2>&1; local rc=$?
  echo "${PWD#$OUT/}/$name: exit $rc, $(awk -v a="$t0" -v b="$(now)" 'BEGIN { printf "%.1f", b - a }') s"
}
# 1. exact1/: the exact-integer first-order certificates of record for (1/11, 1/10), (1/14, 1/8) and (1/18, 1/6)
cd "$OUT/exact1"
run runb_e11_M13200.log      "$PY" -I cert_p3_binned.py 13200 ../inputs/cont_h1_sol_e11a10b3_reg0.10.pkl \
    "0,3,3;0,4,2;0,4,3;0,5,1;0,5,2;0,5,3;0,6,1;0,6,2;0,6,3;1,2,3;2,3,3" 0,3,3 e11a10b3
run runb_e14_19c_M53760.log  "$PY" -I cert_p3_binned.py 53760 ../inputs/cont_h1_sol_e14a8_reg0.10.pkl \
    "0,3,1;0,4,1;1,2,1;1,3,1;1,4,1;2,2,1;2,3,1;2,4,1;3,2,1;3,3,1;3,4,1;4,2,1;5,5,1;8,3,1;10,2,1;11,1,1;12,0,0;12,1,1;13,0,0" 0,3,1 e14a8_19c
run run4v3_M7200.log         "$PY" -I cert_p4_exact_v3.py 7200 50 50 ../inputs/cont_h2b_sol_e18a6nb_reg0.065.pkl v2main
# 2. engine2/: the second-order engine, (1/20, 1/5) with its caps pass, (1/30, 1/4) and (1/30, 4/15), with the full runs' settings at
#    small M
cd "$OUT/engine2"
run v2_p5frozen_M3600.log    env KPROF=3 "$PY" -I cert2_v2.py 3600 40 ../inputs/cont_hmk_sol_e20a5k3_reg0.05.pkl p5frozen
run caps_p5frozen_M3600.log  env KPROF=3 GBIN=4 "$PY" -I cert2_caps.py 3600 40 ../inputs/cont_hmk_sol_e20a5k3_reg0.05.pkl p5frozen
run dev_g93adjQ32_M960.log   env KW=2:1,3:-1 GVEC=2:1,3:1 T0ADJ=1 NBQ=32 NBQLO=64 \
    "$PY" -I cert2_5c779c07.py 960 80 ../inputs/kw23m_binsJ12_reg1040glo93.pkl g93adjQ32
run dev_p7r_M960.log         env KW=2:1,3:-1 GVEC=2:1,3:1 T0ADJ=1 F1ADJ=1 NBQ=32 NBQLO=64 QOUTER=1 QADAPT=2,1:-1.0147:8:2 \
    "$PY" -I cert2_f2.py 960 80 ../inputs/kw23m_e30a415_reg1030glo93.pkl p7r
# 3. sep_p4/ and sep_p5/: the separately written implementations for (1/18, 1/6) and (1/20, 1/5); they read their constants from TSV files
#    and import vconv.py from their own directory (so they run without -I)
cd "$OUT/sep_p4"
run cert3_M144000.out        "$PY" p4rig3.py 144000 cert 4
cd "$OUT/sep_p5"
run rig_M36000.out           "$PY" p5rig.py 36000 cert 1 36000 16
# 4. audit/: the independent audits' separately written recomputations (ball or exact arithmetic); they read the stored outputs in
#    full/ (copied above) and the replay outputs shipped beside them, and write their results next to themselves
cd "$OUT/audit/p3a"
run spot_least_cell.log      "$PY" -I spot_least_cell.py
cd "$OUT/audit/p5"
run p5_independent.log       "$PY" -I p5_independent.py
cd "$OUT/audit/p6"
run p6_independent.log       "$PY" -I p6_independent.py
cd "$OUT/audit/p7"
run p7_independent.log       "$PY" -I p7_independent.py
# 5. compare with expected_small/; check the stored full outputs and the input exports
cd "$OUT"
"$PY" -I "$HERE/compare.py" "$OUT" "$HERE/expected_small"
