#!/usr/bin/env bash # parallel_collect.sh # Usage: parallel_collect.sh \ # -- # # collect_cmd_template must accept: # --out DIR --seeds A-B --max-kept N (plus any extra args before --) # SHARD_OUT, SHARD_SEEDS, SHARD_TARGET are substituted per GPU # # Runs n_gpus parallel shards, each on its own GPU, merges results into final_meta dir. N_GPUS=$1; TOTAL_SEEDS=$2; TARGET=$3; TIMEOUT=$4; FINAL_DIR=$5; MERGE_PY=$6 shift 6; [ "$1" = "--" ] && shift CMD=("$@") RUN=/lustre/fsw/portfolios/nvr/users/jtremblay/yu/1027_run log() { echo "== $(date '+%F %T') [parallel_collect] $*"; } # Split seeds across GPUs PER=$((TOTAL_SEEDS / N_GPUS)) PER_TARGET=$((TARGET / N_GPUS + 2)) # slight over-collect per shard PIDS=() SHARD_DIRS=() for gpu in $(seq 0 $((N_GPUS-1))); do SSTART=$((gpu * PER)) SEND=$(( (gpu == N_GPUS-1) ? TOTAL_SEEDS-1 : (gpu+1)*PER - 1 )) SHARD_DIR="${FINAL_DIR}_gpu${gpu}" mkdir -p "$SHARD_DIR" SHARD_DIRS+=("$SHARD_DIR") log "GPU $gpu: seeds ${SSTART}-${SEND} -> $SHARD_DIR" ( export CUDA_VISIBLE_DEVICES=$gpu export OMNIGIBSON_GPU_ID=$gpu bash $RUN/collect_with_skip.sh \ "$SHARD_DIR/collect_meta.json" \ "$PER_TARGET" \ "$TIMEOUT" -- \ "${CMD[@]}" \ --out "$SHARD_DIR" \ --seeds "${SSTART}-${SEND}" \ --max-kept "$PER_TARGET" \ 2>&1 | tee "${RUN}/logs/$(basename $FINAL_DIR)_gpu${gpu}.log" ) & PIDS+=($!) # Stagger startup by 30s to avoid Isaac Sim init collisions sleep 30 done log "Waiting for ${#PIDS[@]} shards (PIDs: ${PIDS[*]})" for pid in "${PIDS[@]}"; do wait "$pid" log "Shard PID $pid done" done log "All shards done. Merging into $FINAL_DIR" python3 "$MERGE_PY" "${SHARD_DIRS[@]}" "$FINAL_DIR" "$TARGET" log "Merge done. kept=$(python3 -c "import json; d=json.load(open('$FINAL_DIR/collect_meta.json')); print(d.get('n_kept',0))" 2>/dev/null)"