# Publication rerun. One target rebuilds the tables and the scaling figure
# from the raw logs written by scale_r6.py.

rule publication:
    input:
        "bench/h2h/scaling/r8/rows.json",
        "bench/h2h/scaling/r8/hardware.json",
        "bench/h2h/scaling/r8/h2h.json",
        "bench/h2h/scaling/r8/langs.json",
        "bench/h2h/scaling/r8/scaling.pdf",


rule measure:
    output:
        "bench/h2h/scaling/r8/rows.json",
        "bench/h2h/scaling/r8/hardware.json",
        "bench/h2h/scaling/r8/h2h.json",
        "bench/h2h/scaling/r8/langs.json",
    shell:
        "python3 bench/h2h/scale_r6.py"


rule figure:
    input:
        "bench/h2h/scaling/r8/rows.json",
    output:
        "bench/h2h/scaling/r8/scaling.pdf",
    shell:
        "python3 bench/h2h/plot_r6.py"


rule mem_r1:
    output:
        "bench/h2h/scaling/mem-r1/rows.json",
        "bench/h2h/scaling/mem-r1/hardware.json",
    shell:
        "python3 bench/h2h/mem_r1.py"


# One rerun of the paper campaigns on this commit. Raw logs stay under
# bench/h2h/scaling/rel1.
rule release:
    output:
        "bench/h2h/scaling/rel1/rows.json",
        "bench/h2h/scaling/rel1/hardware.json",
        "bench/h2h/scaling/rel1/h2h.json",
        "bench/h2h/scaling/rel1/langs.json",
        "bench/h2h/scaling/rel1/compressed.json",
        "bench/h2h/scaling/rel1/table2-h2h.tsv",
        "bench/h2h/scaling/rel1/table3-bindings.tsv",
        "bench/h2h/scaling/rel1/s61-strong.tsv",
        "bench/h2h/scaling/rel1/s-memory.tsv",
        "bench/h2h/scaling/rel1/memory.json",
        "bench/h2h/scaling/rel1/fig7-threads.pdf",
        "bench/h2h/scaling/rel1/fig8-mpi.pdf",
        "bench/h2h/scaling/rel1/fidelity.log",
        "bench/h2h/scaling/rel1/db.log",
        "bench/h2h/scaling/rel1/DONE",
    shell:
        "python3 bench/h2h/release_campaign.py"


# The release rerun: extxyz head-to-head, bindings, compressed CON,
# strong and weak scaling, fidelity, and readcon-db.
rule campaign:
    input:
        "bench/h2h/scaling/rel1/DONE",


# Final paper campaign. One target rebuilds the bindings from this
# checkout and reruns every campaign. Default output is
# bench/h2h/scaling/rel1 with n=6. A smoke run does not touch that
# directory:
#   snakemake -s bench/h2h/Snakefile --cores 1 \
#     --config out=/tmp/readcon-final-smoke repeats=1 final_campaign
# Each rule: one warmup, serial samples, type-7 median and IQR, raw
# samples in the JSON, unlink+create on writes, STREAM / fdatasync /
# raw pwrite ceilings, and the HEAD sha stamped into every output.
# final_compressed also records 1/2/4/8 thread ladders for 8-member
# gzip and zstd, with a per-count codec ceiling (inflate for reads,
# deflate for writes) and strong-scaling efficiency.
# final_check fails if any file under the output directory was produced
# on a commit other than HEAD. On the default path that directory is
# bench/h2h/scaling/rel1.
FINAL_OUT = config.get("out", "bench/h2h/scaling/rel1")
FINAL_REPEATS = int(config.get("repeats", 6))

# A stale PYTHONPATH must not shadow the wheel this campaign installs.
shell.prefix("unset PYTHONPATH; ")


rule final_bindings:
    output:
        f"{FINAL_OUT}/bindings.json",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py bindings --out {params.out} --repeats {params.repeats}"


rule final_ceilings:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/hardware.json",
        f"{FINAL_OUT}/lscpu.txt",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py ceilings --out {params.out} --repeats {params.repeats}"


rule final_strong_threads:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/strong-threads.json",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py strong-threads --out {params.out} --repeats {params.repeats}"


rule final_strong_mpi:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/strong-mpi.json",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py strong-mpi --out {params.out} --repeats {params.repeats}"


rule final_weak:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/weak-threads.json",
        f"{FINAL_OUT}/weak-ranks-1.json",
        f"{FINAL_OUT}/weak-ranks-2.json",
        f"{FINAL_OUT}/weak-ranks-4.json",
        f"{FINAL_OUT}/weak-ranks-8.json",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py weak --out {params.out} --repeats {params.repeats}"


# Concurrent pwrite of N frame-sized payloads at disjoint offsets, plus
# one thread pwriting those same bytes. Samples stay inside one process.
# The asv class WeakWrite times one call of the same helpers.
rule sink_ceiling:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/sink-ceiling.json",
        f"{FINAL_OUT}/samples.log",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/weak_write.py sink --out {params.out} --repeats {params.repeats}"


# Weak writes beside that ceiling. The table has two efficiency columns:
# ideal T(1)/T(N), and max(format alone, sink(N)) / T(N).
rule weak_write:
    input:
        f"{FINAL_OUT}/sink-ceiling.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/weak-rows.json",
        f"{FINAL_OUT}/weak-write.json",
        f"{FINAL_OUT}/weak-write-table.tsv",
        f"{FINAL_OUT}/NOTES.txt",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/weak_write.py weak --out {params.out} --repeats {params.repeats}"


rule final_langs:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/langs.json",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py langs --out {params.out} --repeats {params.repeats}"


# 8-member gzip and zstd, threads 1/2/4/8, plus the codec ceiling.
rule final_compressed:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/compressed.json",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py compressed --out {params.out} --repeats {params.repeats}"


rule final_table2:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/h2h.json",
        f"{FINAL_OUT}/h2h.log",
        f"{FINAL_OUT}/table2-h2h.tsv",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py table2 --out {params.out} --repeats {params.repeats}"


# Same compressed .con/.extxyz files, readcon vs ASE vs eOn.
# Same structures in each library's own format. readcon is CON
# (plain, gz, zst). Chemfiles has no CON reader; the other arms are
# extxyz, XYZ, PDB, DCD, XTC, and NetCDF where that library imports.
rule cross_format:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/cross-format.json",
        f"{FINAL_OUT}/cross-format.log",
        f"{FINAL_OUT}/cross-format.tsv",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/cross_format.py --out {params.out} --repeats {params.repeats}"


rule compressed_h2h:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/compressed-h2h.json",
        f"{FINAL_OUT}/compressed-h2h.log",
        f"{FINAL_OUT}/compressed-h2h-lscpu.txt",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/compressed_h2h.py --out {params.out} --repeats {params.repeats}"


rule final_con_h2h:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/con-h2h.json",
        f"{FINAL_OUT}/con-h2h.log",
        f"{FINAL_OUT}/con-h2h.tsv",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py con-h2h --out {params.out} --repeats {params.repeats}"


rule final_db:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/db.log",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py db --out {params.out} --repeats {params.repeats}"


rule final_fidelity:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
    output:
        f"{FINAL_OUT}/fidelity.json",
        f"{FINAL_OUT}/fidelity-timing.json",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/final_campaign.py fidelity --out {params.out} --repeats {params.repeats}"


rule compressed_write_memory:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/compressed-write-memory.json",
        f"{FINAL_OUT}/compressed-write-memory.txt",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/paper_rows.py compressed-memory --out {params.out} --repeats {params.repeats}"


rule plugin_rss:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/plugin-rss.json",
        f"{FINAL_OUT}/plugin-rss.txt",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/paper_rows.py plugin-rss --out {params.out} --repeats {params.repeats}"


rule mw_persistence:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/mw-persistence.json",
        f"{FINAL_OUT}/mw-persistence.txt",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/paper_rows.py mw-persistence --out {params.out} --repeats {params.repeats}"


rule hop_barrier:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/hop-barrier.json",
        f"{FINAL_OUT}/hop-barrier.txt",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/paper_rows.py hop-barrier --out {params.out} --repeats {params.repeats}"


rule eon_loadcons:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/eon-loadcons.json",
        f"{FINAL_OUT}/eon-loadcons.txt",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/paper_rows.py eon-loadcons --out {params.out} --repeats {params.repeats}"


# The paper's reader-exchange figures: 100000 frames of 32 atoms, six
# repeats, time and the resident-set increase. The JSON is what
# plot_union_extra.py reads.
rule reader_exchange:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/reader-exchange.json",
        f"{FINAL_OUT}/reader-exchange.log",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/reader_exchange.py --out {params.out} --frames 100000 --repeats {params.repeats}"


# The roofline figure reads cells of reads.json. The payload is the
# seeded Langevin trajectory of resources/test/cuh2.con.
rule roofline:
    input:
        f"{FINAL_OUT}/bindings.json",
    output:
        f"{FINAL_OUT}/reads.json",
    params:
        out=FINAL_OUT,
        repeats=FINAL_REPEATS,
    shell:
        "python3 bench/h2h/roofline_reads.py --out {params.out} --frames 10000 --repeats {params.repeats}"


rule final_check:
    input:
        f"{FINAL_OUT}/bindings.json",
        f"{FINAL_OUT}/hardware.json",
        f"{FINAL_OUT}/lscpu.txt",
        f"{FINAL_OUT}/strong-threads.json",
        f"{FINAL_OUT}/strong-mpi.json",
        f"{FINAL_OUT}/weak-threads.json",
        f"{FINAL_OUT}/weak-ranks-1.json",
        f"{FINAL_OUT}/weak-ranks-2.json",
        f"{FINAL_OUT}/weak-ranks-4.json",
        f"{FINAL_OUT}/weak-ranks-8.json",
        f"{FINAL_OUT}/langs.json",
        f"{FINAL_OUT}/compressed.json",
        f"{FINAL_OUT}/h2h.json",
        f"{FINAL_OUT}/table2-h2h.tsv",
        f"{FINAL_OUT}/con-h2h.json",
        f"{FINAL_OUT}/con-h2h.tsv",
        f"{FINAL_OUT}/cross-format.json",
        f"{FINAL_OUT}/cross-format.log",
        f"{FINAL_OUT}/cross-format.tsv",
        f"{FINAL_OUT}/compressed-h2h.json",
        f"{FINAL_OUT}/compressed-h2h.log",
        f"{FINAL_OUT}/compressed-h2h-lscpu.txt",
        f"{FINAL_OUT}/sink-ceiling.json",
        f"{FINAL_OUT}/weak-rows.json",
        f"{FINAL_OUT}/weak-write.json",
        f"{FINAL_OUT}/weak-write-table.tsv",
        f"{FINAL_OUT}/samples.log",
        f"{FINAL_OUT}/NOTES.txt",
        f"{FINAL_OUT}/compressed-write-memory.json",
        f"{FINAL_OUT}/plugin-rss.json",
        f"{FINAL_OUT}/mw-persistence.json",
        f"{FINAL_OUT}/hop-barrier.json",
        f"{FINAL_OUT}/eon-loadcons.json",
        f"{FINAL_OUT}/reader-exchange.json",
        f"{FINAL_OUT}/reader-exchange.log",
        f"{FINAL_OUT}/reads.json",
        f"{FINAL_OUT}/db.log",
        f"{FINAL_OUT}/fidelity.json",
        f"{FINAL_OUT}/fidelity-timing.json",
    output:
        f"{FINAL_OUT}/COMMIT_OK",
    params:
        out=FINAL_OUT,
    shell:
        "python3 bench/h2h/final_campaign.py check --out {params.out}"


rule final_campaign:
    input:
        f"{FINAL_OUT}/COMMIT_OK",


# Fresh-file ranked writes. The spare is a separate table, not the default.
# n=12, one warmup, pinned. Does not edit bench/h2h/scaling/camp16.
rule camp16b:
    output:
        "bench/h2h/scaling/camp16b/DONE",
    shell:
        "mkdir -p bench/h2h/scaling/camp16b/logs && python3 bench/h2h/camp16b_run.py"
