152 lines
7.1 KiB
Bash
152 lines
7.1 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
# Usage: bench_mphf.sh LABEL
|
||
|
|
#
|
||
|
|
# Measures the obikmer commands that sit on MPHF lookups (see
|
||
|
|
# DevDocMD/implementation/mphf_batch_queries.md for the site inventory), on the
|
||
|
|
# 20-genome benchmark data already built under run/ (`gmake all` first).
|
||
|
|
# Nothing under run/ other than run/bench_mphf/ is written.
|
||
|
|
#
|
||
|
|
# Env:
|
||
|
|
# BINARY obikmer binary to measure (default ../src/target/release/obikmer)
|
||
|
|
# REPS repetitions per step (default 3)
|
||
|
|
# CPU_MAX passed as --cpu-max when set (default: all cores)
|
||
|
|
#
|
||
|
|
# Output: run/bench_mphf/LABEL/results.csv (label,step,rep,wall_s,maxrss_b)
|
||
|
|
# run/bench_mphf/LABEL/checksums.txt (order-independent output digests,
|
||
|
|
# to be diffed across LABELs: a speed-up must not change any of them)
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
LABEL="$1"
|
||
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
|
|
RUN_DIR="${SCRIPT_DIR}/run"
|
||
|
|
BINARY="${BINARY:-${SCRIPT_DIR}/../src/target/release/obikmer}"
|
||
|
|
REPS="${REPS:-3}"
|
||
|
|
OUT="${RUN_DIR}/bench_mphf/${LABEL}"
|
||
|
|
CPU=()
|
||
|
|
[[ -n "${CPU_MAX:-}" ]] && CPU=(--cpu-max "${CPU_MAX}")
|
||
|
|
|
||
|
|
rm -rf "${OUT}"; mkdir -p "${OUT}"
|
||
|
|
RESULTS="${OUT}/results.csv"
|
||
|
|
SUMS="${OUT}/checksums.txt"
|
||
|
|
echo "label,step,rep,wall_s,maxrss_b" >"${RESULTS}"
|
||
|
|
: >"${SUMS}"
|
||
|
|
|
||
|
|
# timed STEP REP CMD... — wall time + peak RSS via BSD time(1); stdout of CMD
|
||
|
|
# goes to $OUT/last.stdout, stderr to $OUT/last.stderr.
|
||
|
|
timed() {
|
||
|
|
local step="$1" rep="$2"; shift 2
|
||
|
|
local tf="${OUT}/time.tmp"
|
||
|
|
/usr/bin/time -l -o "${tf}" "$@" >"${OUT}/last.stdout" 2>"${OUT}/last.stderr" \
|
||
|
|
|| { echo "FAILED: $step: $*" >&2; cat "${OUT}/last.stderr" >&2; exit 1; }
|
||
|
|
local wall rss
|
||
|
|
wall=$(awk '/ real /{print $1}' "${tf}")
|
||
|
|
rss=$(awk '/maximum resident set size/{print $1}' "${tf}")
|
||
|
|
echo "${LABEL},${step},${rep},${wall},${rss}" >>"${RESULTS}"
|
||
|
|
# per-stage wall times from the Reporter log lines ("done stage=X wall_secs=Y")
|
||
|
|
sed $'s/\x1b\\[[0-9;]*m//g' "${OUT}/last.stderr" \
|
||
|
|
| awk -v L="${LABEL}" -v S="${step}" -v R="${rep}" \
|
||
|
|
'/ done stage=/{ for(i=1;i<=NF;i++){ if($i ~ /^stage=/) st=substr($i,7); if($i ~ /^wall_secs=/) w=substr($i,11) } print L","S":"st","R","w",0" }' \
|
||
|
|
>>"${RESULTS}"
|
||
|
|
printf ' %-28s rep %s %8s s\n' "${step}" "${rep}" "${wall}" >&2
|
||
|
|
}
|
||
|
|
|
||
|
|
digest() { LC_ALL=C sort "$1" | md5 -q; }
|
||
|
|
|
||
|
|
# ── 0. data: the indexes under run/ may have been built by an older
|
||
|
|
# MPHF generation (ptr_hash 1.x, unreadable by current binaries), so the
|
||
|
|
# bench builds its own, once, from the simulated reads. Shared by all LABELs;
|
||
|
|
# delete run/bench_mphf/data to force a rebuild.
|
||
|
|
DATA="${RUN_DIR}/bench_mphf/data"
|
||
|
|
if [[ ! -f "${DATA}/.ready" ]]; then
|
||
|
|
echo "preparing data with ${BINARY}" >&2
|
||
|
|
mkdir -p "${DATA}/presence" "${DATA}/count"
|
||
|
|
for d in "${RUN_DIR}"/simulated_data/*/*/; do
|
||
|
|
strain="$(basename "$d")"; species="$(basename "$(dirname "$d")")"
|
||
|
|
sp="${species}--${strain}"
|
||
|
|
for kind in presence count; do
|
||
|
|
extra=(); [[ $kind == count ]] && extra=(--with-counts)
|
||
|
|
"${BINARY}" "${CPU[@]}" index --output "${DATA}/${kind}/${sp}" --force \
|
||
|
|
--theta 0 "${extra[@]}" --label "${sp}" --meta "species=${species}" \
|
||
|
|
"$d/reads_R1.fastq.gz" "$d/reads_R2.fastq.gz" 2>/dev/null
|
||
|
|
done
|
||
|
|
done
|
||
|
|
"${BINARY}" "${CPU[@]}" merge --output "${DATA}/global_presence" --force \
|
||
|
|
--force-presence "${DATA}"/presence/*/ 2>/dev/null
|
||
|
|
"${BINARY}" "${CPU[@]}" merge --output "${DATA}/global_count" --force \
|
||
|
|
"${DATA}"/count/*/ 2>/dev/null
|
||
|
|
touch "${DATA}/.ready"
|
||
|
|
fi
|
||
|
|
sources=("${DATA}"/presence/*/)
|
||
|
|
|
||
|
|
# ── 1. index: build pass 2 (mphf.index per kmer + evidence writes) ──────────
|
||
|
|
for sp in Escherichia_coli--K-12_MG1655 Candidozyma_auris--GCF_003013715.1_ASM301371v2; do
|
||
|
|
species="${sp%%--*}"; strain="${sp#*--}"
|
||
|
|
reads="${RUN_DIR}/simulated_data/${species}/${strain}"
|
||
|
|
for rep in $(seq "${REPS}"); do
|
||
|
|
timed "index_${species}" "${rep}" "${BINARY}" "${CPU[@]}" index \
|
||
|
|
--output "${OUT}/idx_${species}" --force --theta 0 --label "${sp}" \
|
||
|
|
"${reads}/reads_R1.fastq.gz" "${reads}/reads_R2.fastq.gz"
|
||
|
|
done
|
||
|
|
done
|
||
|
|
|
||
|
|
# ── 2. merge: find_batch_grouped, build_graph filter, hash_batch, index_batch ─
|
||
|
|
for rep in $(seq "${REPS}"); do
|
||
|
|
timed "merge_20_presence" "${rep}" "${BINARY}" "${CPU[@]}" merge \
|
||
|
|
--output "${OUT}/merged" --force --force-presence "${sources[@]}"
|
||
|
|
done
|
||
|
|
"${BINARY}" "${CPU[@]}" dump "${OUT}/merged" >"${OUT}/merged.dump"
|
||
|
|
echo "merged_dump $(digest "${OUT}/merged.dump")" >>"${SUMS}"
|
||
|
|
rm -f "${OUT}/merged.dump"
|
||
|
|
|
||
|
|
# ── 3. query: query stage 1 (find per kmer, layer by layer) ─────────────────
|
||
|
|
for kind in presence count; do
|
||
|
|
for sp in Escherichia_coli/K-12_MG1655 Saccharolobus_islandicus/M.16.4; do
|
||
|
|
reads="${RUN_DIR}/query_data/${sp}"
|
||
|
|
for rep in $(seq "${REPS}"); do
|
||
|
|
timed "query_${kind}_${sp%%/*}" "${rep}" "${BINARY}" "${CPU[@]}" query \
|
||
|
|
--count-missing "${DATA}/global_${kind}" \
|
||
|
|
"${reads}/reads_R1.fastq.gz" "${reads}/reads_R2.fastq.gz"
|
||
|
|
done
|
||
|
|
echo "query_${kind}_${sp%%/*} $(digest "${OUT}/last.stdout")" >>"${SUMS}"
|
||
|
|
done
|
||
|
|
done
|
||
|
|
|
||
|
|
# ── 4. filter: iter_layer_kmers → hash_batch + fill_sub_matrix ──────────────
|
||
|
|
for rep in $(seq "${REPS}"); do
|
||
|
|
timed "filter_species" "${rep}" "${BINARY}" "${CPU[@]}" filter \
|
||
|
|
--output "${OUT}/filtered" --force --ingroup "species=Escherichia_coli" \
|
||
|
|
--outgroup all --min-frac 0.5 --max-frac 1.0 --max-outgroup-count 0 \
|
||
|
|
"${DATA}/global_presence"
|
||
|
|
done
|
||
|
|
"${BINARY}" "${CPU[@]}" dump "${OUT}/filtered" >"${OUT}/filtered.dump"
|
||
|
|
echo "filtered_dump $(digest "${OUT}/filtered.dump")" >>"${SUMS}"
|
||
|
|
rm -f "${OUT}/filtered.dump"
|
||
|
|
|
||
|
|
# ── 5. convert: build_exact_evidence / build_approx_evidence ────────────────
|
||
|
|
# On an APFS clone (cp -c) so run/global_index_presence is untouched.
|
||
|
|
for rep in $(seq "${REPS}"); do
|
||
|
|
rm -rf "${OUT}/conv"; cp -cR "${DATA}/global_presence" "${OUT}/conv"
|
||
|
|
timed "convert_to_approx8" "${rep}" "${BINARY}" "${CPU[@]}" convert \
|
||
|
|
--approx-evidence 8 "${OUT}/conv"
|
||
|
|
timed "convert_to_exact" "${rep}" "${BINARY}" "${CPU[@]}" convert \
|
||
|
|
--exact-evidence "${OUT}/conv"
|
||
|
|
done
|
||
|
|
"${BINARY}" "${CPU[@]}" dump "${OUT}/conv" >"${OUT}/conv.dump"
|
||
|
|
echo "convert_roundtrip_dump $(digest "${OUT}/conv.dump")" >>"${SUMS}"
|
||
|
|
rm -rf "${OUT}/conv" "${OUT}/conv.dump"
|
||
|
|
|
||
|
|
# ── 6. phylo: sibling annex (annex.rs: find_in_partition per variant) and
|
||
|
|
# its consumer (family_scan.rs: hash_batch / find_slot) ──────────────────
|
||
|
|
for rep in $(seq "${REPS}"); do
|
||
|
|
rm -rf "${OUT}/phy"; cp -cR "${DATA}/global_presence" "${OUT}/phy"
|
||
|
|
timed "phylo_sibling_annex" "${rep}" "${BINARY}" "${CPU[@]}" phylo \
|
||
|
|
--sibling-annex "${OUT}/phy"
|
||
|
|
timed "phylo_snp_distance" "${rep}" "${BINARY}" "${CPU[@]}" phylo \
|
||
|
|
--output "${OUT}/snp" --distance snp-jc --csv "${OUT}/phy"
|
||
|
|
done
|
||
|
|
echo "phylo_snp_dist $(digest "${OUT}/snp_dist.csv")" >>"${SUMS}"
|
||
|
|
rm -rf "${OUT}/phy" "${OUT}/idx_"* "${OUT}/merged" "${OUT}/filtered" \
|
||
|
|
"${OUT}/last.stdout" "${OUT}/time.tmp"
|
||
|
|
|
||
|
|
echo "results: ${RESULTS}" >&2
|
||
|
|
echo "checksums: ${SUMS}" >&2
|