# Rebuild the corpora, the reader exchange, and the tables the paper inputs.
# Run from paper/cpc: snakemake -c1 all

rule cargo_version:
    output:
        "src/cargo_version.tex",
    shell:
        "python3 scripts/cargo_version_tex.py"

rule all:
    input:
        "data/corpus31/manifest.toml",
        "logs/reader-exchange.log",
        "src/figures/generated/host_macros.tex",
        "src/figures/generated/host_table.tex",
        "src/figures/generated/format_context.tex",
        "src/figures/generated/timing.pdf",
        "src/figures/generated/ratios.pdf",
        "src/figures/generated/memory.pdf",

rule corpus31:
    output:
        "data/corpus31/manifest.toml",
    shell:
        "python3 scripts/make_corpus31.py --out data/corpus31"

rule reader_exchange:
    output:
        "logs/reader-exchange.log",
    shell:
        "python3 scripts/bench_readers.py"

rule host_tables:
    input:
        "logs/reader-exchange.log",
    output:
        "src/figures/generated/host_macros.tex",
        "src/figures/generated/host_table.tex",
        "src/figures/generated/format_context.tex",
    shell:
        "python3 scripts/host_from_log.py"

rule db_log:
    output:
        "logs/db.log",
    shell:
        "python3 bench/db_v2.py"

rule db_macros:
    input:
        "logs/db.log",
    output:
        "src/figures/generated/db_macros.tex",
    shell:
        "python3 scripts/db_from_log.py"

rule mpi_log:
    output:
        "logs/mpi.log",
    shell:
        "python3 bench/run_mpi_v2.py"

rule mpi_products:
    input:
        "logs/mpi.log",
    output:
        "src/figures/generated/mpi_macros.tex",
        "src/figures/generated/mpi_host_table.tex",
        "src/figures/generated/mpi_host.pdf",
    shell:
        "python3 scripts/mpi_from_log.py"

rule mpi_fallback:
    output:
        "logs/mpi-fallback.log",
    shell:
        "python3 scripts/mpi_fallback.py"

rule dlpack_log:
    output:
        "logs/dlpack-exchange.log",
        "src/figures/generated/dlpack_macros.tex",
        "src/figures/generated/dlpack_table.tex",
        "src/figures/generated/dlpack.pdf",
    shell:
        "taskset -c 0 python3 scripts/bench_dlpack.py"

rule eon_build_log:
    output:
        "logs/eon-build.log",
    shell:
        "python3 scripts/eon_build_log.py"

rule manuscript:
    input:
        "src/figures/generated/dlpack_macros.tex",
        "src/figures/generated/host_macros.tex",
        "src/figures/generated/host_table.tex",
        "src/figures/generated/mpi_macros.tex",
        "src/figures/generated/db_macros.tex",
    output:
        "src/rg_main.pdf",
    shell:
        "cd src && ../.texcache/tectonic rg_main.tex"

rule stamp_host:
    shell:
        "python3 scripts/stamp_host.py"

rule install_log:
    output:
        "logs/install.log",
    shell:
        "python3 scripts/logged_cmd.py paper/cpc/logs/install.log python3 -m pip install ."

rule build_log:
    output:
        "logs/build.log",
    shell:
        "python3 scripts/logged_cmd.py paper/cpc/logs/build.log python3 -m pip install ."

rule eon_driver_log:
    output:
        "logs/eon-driver.log",
    shell:
        "python3 scripts/logged_cmd.py paper/cpc/logs/eon-driver.log ninja -C paper/cpc/bench/work/peers/eon/build-h2h client/python/_core.abi3.so"

rule regen_figures_log:
    output:
        "logs/regen-figures.log",
    shell:
        "python3 scripts/regen_figures.py"

rule reader_figures:
    input:
        "logs/reader-exchange.log",
    output:
        "src/figures/generated/timing.pdf",
        "src/figures/generated/ratios.pdf",
        "src/figures/generated/memory.pdf",
    shell:
        "cd scripts && python3 -c 'import plot_union_extra as u; u.reader_figures()'"
