From 1aa94ffc28332368aa3899590b39fa6cddb20438 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 27 Aug 2025 19:50:33 +0000 Subject: [PATCH 1/3] Add batch processing script for gfan tropical prevariety computation Co-authored-by: anton.leykin --- batch_processor.sh | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 batch_processor.sh diff --git a/batch_processor.sh b/batch_processor.sh new file mode 100755 index 00000000000..3bdc493d8f5 --- /dev/null +++ b/batch_processor.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +# Script to find an available BATCH directory and launch gfan command +# Usage: ./batch_processor.sh [nBatch] + +# Default number of batches if not provided +nBatch=${1:-10} + +echo "Searching through BATCH-0 to BATCH-$((nBatch-1)) for available directory..." + +# Search through BATCH directories +for i in $(seq 0 $((nBatch-1))); do + batch_dir="BATCH-$i" + + # Check if directory exists + if [ ! -d "$batch_dir" ]; then + echo "Directory $batch_dir does not exist, skipping..." + continue + fi + + # Check if "done" or "processing" files exist + if [ -f "$batch_dir/done" ] || [ -f "$batch_dir/processing" ]; then + echo "Directory $batch_dir is busy (has 'done' or 'processing' file), skipping..." + continue + fi + + # Found an available directory + echo "Found available directory: $batch_dir" + echo "Creating 'processing' file to mark as busy..." + touch "$batch_dir/processing" + + # Change to the batch directory + cd "$batch_dir" + + # Check if input file exists + if [ ! -f "_tropicalprevariety.input" ]; then + echo "Error: _tropicalprevariety.input file not found in $batch_dir" + rm -f "processing" + cd .. + continue + fi + + echo "Launching gfan command in $batch_dir..." + + # Launch the gfan command + gfan _tropicalprevariety -j32 --log1 --usevaluation --bits64 --halfopenrestrictions --matrixoutput < _tropicalprevariety.input > _tropicalprevariety.output 2> gfan.log + + # Check if command completed successfully + if [ $? -eq 0 ]; then + echo "gfan command completed successfully in $batch_dir" + # Remove processing file and create done file + rm -f "processing" + touch "done" + echo "Marked $batch_dir as completed" + else + echo "gfan command failed in $batch_dir" + # Remove processing file on failure + rm -f "processing" + fi + + # Return to parent directory + cd .. + + # Exit after processing one batch + exit 0 +done + +echo "No available BATCH directories found" +exit 1 \ No newline at end of file From 74d81aa3faed76938551636ff4dc4620d0b09496 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 27 Aug 2025 19:55:35 +0000 Subject: [PATCH 2/3] Implement concurrent batch processing with configurable max processes Co-authored-by: anton.leykin --- batch_processor.sh | 116 ++++++++++++++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 33 deletions(-) diff --git a/batch_processor.sh b/batch_processor.sh index 3bdc493d8f5..7a7339c2237 100755 --- a/batch_processor.sh +++ b/batch_processor.sh @@ -1,59 +1,47 @@ #!/bin/bash -# Script to find an available BATCH directory and launch gfan command -# Usage: ./batch_processor.sh [nBatch] +# Script to find available BATCH directories and launch gfan commands concurrently +# Usage: ./batch_processor.sh [nBatch] [max_concurrent] -# Default number of batches if not provided +# Default number of batches and max concurrent processes if not provided nBatch=${1:-10} +max_concurrent=${2:-3} -echo "Searching through BATCH-0 to BATCH-$((nBatch-1)) for available directory..." +echo "Searching through BATCH-0 to BATCH-$((nBatch-1)) for available directories..." +echo "Processing up to $max_concurrent batches concurrently..." -# Search through BATCH directories -for i in $(seq 0 $((nBatch-1))); do - batch_dir="BATCH-$i" - - # Check if directory exists - if [ ! -d "$batch_dir" ]; then - echo "Directory $batch_dir does not exist, skipping..." - continue - fi - - # Check if "done" or "processing" files exist - if [ -f "$batch_dir/done" ] || [ -f "$batch_dir/processing" ]; then - echo "Directory $batch_dir is busy (has 'done' or 'processing' file), skipping..." - continue - fi +# Function to process a single batch +process_batch() { + local batch_dir="$1" + local batch_num="$2" - # Found an available directory - echo "Found available directory: $batch_dir" - echo "Creating 'processing' file to mark as busy..." - touch "$batch_dir/processing" + echo "[BATCH-$batch_num] Starting processing..." # Change to the batch directory cd "$batch_dir" # Check if input file exists if [ ! -f "_tropicalprevariety.input" ]; then - echo "Error: _tropicalprevariety.input file not found in $batch_dir" + echo "[BATCH-$batch_num] Error: _tropicalprevariety.input file not found" rm -f "processing" cd .. - continue + return 1 fi - echo "Launching gfan command in $batch_dir..." + echo "[BATCH-$batch_num] Launching gfan command..." # Launch the gfan command gfan _tropicalprevariety -j32 --log1 --usevaluation --bits64 --halfopenrestrictions --matrixoutput < _tropicalprevariety.input > _tropicalprevariety.output 2> gfan.log # Check if command completed successfully if [ $? -eq 0 ]; then - echo "gfan command completed successfully in $batch_dir" + echo "[BATCH-$batch_num] gfan command completed successfully" # Remove processing file and create done file rm -f "processing" touch "done" - echo "Marked $batch_dir as completed" + echo "[BATCH-$batch_num] Marked as completed" else - echo "gfan command failed in $batch_dir" + echo "[BATCH-$batch_num] gfan command failed" # Remove processing file on failure rm -f "processing" fi @@ -61,9 +49,71 @@ for i in $(seq 0 $((nBatch-1))); do # Return to parent directory cd .. - # Exit after processing one batch - exit 0 + echo "[BATCH-$batch_num] Finished processing" +} + +# Array to store background process PIDs +declare -a pids=() +declare -a batch_nums=() + +# Search through BATCH directories +for i in $(seq 0 $((nBatch-1))); do + batch_dir="BATCH-$i" + + # Check if directory exists + if [ ! -d "$batch_dir" ]; then + echo "Directory $batch_dir does not exist, skipping..." + continue + fi + + # Check if "done" or "processing" files exist + if [ -f "$batch_dir/done" ] || [ -f "$batch_dir/processing" ]; then + echo "Directory $batch_dir is busy (has 'done' or 'processing' file), skipping..." + continue + fi + + # Found an available directory + echo "Found available directory: $batch_dir" + echo "Creating 'processing' file to mark as busy..." + touch "$batch_dir/processing" + + # Start processing this batch in background + process_batch "$batch_dir" "$i" & + pid=$! + pids+=($pid) + batch_nums+=($i) + + echo "Started BATCH-$i with PID $pid" + + # Check if we've reached max concurrent processes + if [ ${#pids[@]} -ge $max_concurrent ]; then + echo "Reached maximum concurrent processes ($max_concurrent), waiting for one to complete..." + + # Wait for any background process to complete + wait -n + + # Clean up completed processes from arrays + for j in "${!pids[@]}"; do + if ! kill -0 "${pids[j]}" 2>/dev/null; then + echo "BATCH-${batch_nums[j]} (PID ${pids[j]}) has completed" + unset pids[j] + unset batch_nums[j] + fi + done + + # Reindex arrays + pids=("${pids[@]}") + batch_nums=("${batch_nums[@]}") + fi done -echo "No available BATCH directories found" -exit 1 \ No newline at end of file +# Wait for all remaining background processes to complete +if [ ${#pids[@]} -gt 0 ]; then + echo "Waiting for remaining ${#pids[@]} processes to complete..." + for i in "${!pids[@]}"; do + wait "${pids[i]}" + echo "BATCH-${batch_nums[i]} (PID ${pids[i]}) has completed" + done +fi + +echo "All batch processing completed" \ No newline at end of file From 32e71e1927003e4ca82228ee4de00f92398df62d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 27 Aug 2025 20:21:05 +0000 Subject: [PATCH 3/3] Fix batch process wait logic for better concurrency and array management Co-authored-by: anton.leykin --- batch_processor.sh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/batch_processor.sh b/batch_processor.sh index 7a7339c2237..575c87f3189 100755 --- a/batch_processor.sh +++ b/batch_processor.sh @@ -89,21 +89,21 @@ for i in $(seq 0 $((nBatch-1))); do if [ ${#pids[@]} -ge $max_concurrent ]; then echo "Reached maximum concurrent processes ($max_concurrent), waiting for one to complete..." - # Wait for any background process to complete - wait -n - - # Clean up completed processes from arrays - for j in "${!pids[@]}"; do - if ! kill -0 "${pids[j]}" 2>/dev/null; then - echo "BATCH-${batch_nums[j]} (PID ${pids[j]}) has completed" - unset pids[j] - unset batch_nums[j] - fi + # Wait for any background process to complete (compatible approach) + while true; do + for j in "${!pids[@]}"; do + if ! kill -0 "${pids[j]}" 2>/dev/null; then + echo "BATCH-${batch_nums[j]} (PID ${pids[j]}) has completed" + unset pids[j] + unset batch_nums[j] + # Reindex arrays + pids=("${pids[@]}") + batch_nums=("${batch_nums[@]}") + break 2 # Break out of both loops + fi + done + sleep 1 # Wait a second before checking again done - - # Reindex arrays - pids=("${pids[@]}") - batch_nums=("${batch_nums[@]}") fi done