nesi / APSIM-HPC

Deploy APSIM (Agricultural Production Systems sIMulator - https://www.apsim.info/) on high performance computing clusters.
https://nesi.github.io/APSIM-HPC/
MIT License
0 stars 0 forks source link

Running create_apsimx.sl to create .apsimx files and .db place holder files require a progress bar #34

Open DininduSenanayake opened 2 months ago

DininduSenanayake commented 2 months ago

At the moment, standard out coming out from https://github.com/DininduSenanayake/APSIM-eri-mahuika/blob/main/4-create-apsimx-files/create_apsimx.sl doesn't have any verbose levels which means it is rather difficult to keep track of the progress. Particularly when it is being done for thousands of files. Therefore, it will be ideal to have an ASCII level progress bar

DininduSenanayake commented 2 months ago

I think the standard out coming out of #35 can be used to trap the progress . Perhaps we can use tput command to create a basic progress bar ?

#!/bin/bash

#SBATCH --job-name=apsim_models
#SBATCH --output=slurmlogs/%j.out
#SBATCH --cpus-per-task=4
#SBATCH --mem=8G
#SBATCH --time=16:00:00

module load Apptainer
export APPTAINER_BIND="/agr/scratch,/agr/persist"
export APPTAINER_CMD="apptainer exec /agr/persist/projects/2024_apsim_improvements/apsim-simulations/container/apsim-2024.09.7579.0.aimg"

# Create FAILED directory if it doesn't exist
mkdir -p FAILED

consecutive_failures=0
max_consecutive_failures=10

# Count total number of .txt files (excluding ExampleConfig.txt)
total_files=$(find . -maxdepth 1 -name "*.txt" ! -name "ExampleConfig.txt" | wc -l)
processed_files=0

# Function to update progress bar
update_progress() {
    local progress=$((processed_files * 100 / total_files))
    local completed=$((progress / 2))
    local remaining=$((50 - completed))

    printf "\rProgress: [%-${completed}s%-${remaining}s] %d%%" "$(printf '#%.0s' $(seq 1 $completed))" "$(printf ' %.0s' $(seq 1 $remaining))" "$progress"
}

# Function to process a file
process_file() {
    local file="$1"
    if ${APPTAINER_CMD} Models --cpu-count ${SLURM_CPUS_PER_TASK} --apply "$file"; then
        echo "Successfully processed $file"
        return 0
    else
        echo "Failed to process $file"
        return 1
    fi
}

# Run command for all .txt files, excluding ExampleConfig.txt
for file in *.txt; do
    if [ -f "$file" ] && [ "$file" != "ExampleConfig.txt" ]; then
        if process_file "$file"; then
            consecutive_failures=0
        else
            mv "$file" FAILED/
            ((consecutive_failures++))

            if [ $consecutive_failures -ge $max_consecutive_failures ]; then
                echo "Error: $max_consecutive_failures consecutive failures reached. Terminating job." >&2
                exit 1
            fi
        fi

        ((processed_files++))
        update_progress
    fi
done

echo -e "\nProcessing completed."
  1. At the beginning of the script, It counts the total number of .txt files (excluding ExampleConfig.txt) in the current directory:

    total_files=$(find . -maxdepth 1 -name "*.txt" ! -name "ExampleConfig.txt" | wc -l)
    processed_files=0
  2. function update_progress() calculates and displays the progress bar:

    update_progress() {
       local progress=$((processed_files * 100 / total_files))
       local completed=$((progress / 2))
       local remaining=$((50 - completed))
    
       printf "\rProgress: [%-${completed}s%-${remaining}s] %d%%" "$(printf '#%.0s' $(seq 1 $completed))" "$(printf ' %.0s' $(seq 1 $remaining))" "$progress"
    }

    This function creates a 50-character wide progress bar and displays the percentage of completion.

  3. In the main loop, we increment the processed_files counter and call update_progress after each file is processed:

    ((processed_files++))
    update_progress
  4. At the end of the script, we add a newline character to ensure the final output is on a new line:

    echo -e "\nProcessing completed."

This progress bar will update in real-time as each file is processed, A few notes about this implementation:

  1. The progress bar updates on the same line using the \r (carriage return) character, which works well in most terminal environments.

  2. This implementation assumes that processing each file takes roughly the same amount of time. If processing times vary significantly between files, the progress bar might not accurately represent the time remaining.

DininduSenanayake commented 2 months ago

Above Progress bar is working but the output format will require some improvement .

❯ cat slurmlogs/987936.out 
Successfully processed Oaklea_9a1_28426ConfigFile.txt
Progress: [#                                                  ] 0%Successfully processed Oaklea_9a1_28720ConfigFile.txt
Progress: [#                                                  ] 0%Successfully processed Oaklea_9a1_29813ConfigFile.txt
Progress: [#                                                  ] 0%Successfully processed Oaklea_9a1_9938ConfigFile.txt
Progress: [#                                                  ] 1%Successfully processed Otaihanga_3a1_13223ConfigFile.txt
Progress: [#                                                  ] 1%Successfully processed Otaihanga_3a1_19487ConfigFile.txt
Progress: [#                                                  ] 1%Successfully processed Otaihanga_3a1_20661ConfigFile.txt
DininduSenanayake commented 2 months ago

I made a minor modification by leaving a space. Still not the best in-terms of a proper progress bar as this creates a new line for each file.


  Successfully processed Otaihanga_3a1_28720ConfigFile.txt
 Progress: [#                                                  ] 0%  Successfully processed Otaihanga_3a1_29813ConfigFile.txt
 Progress: [#                                                  ] 1%  Successfully processed Otaihanga_3a1_9938ConfigFile.txt
 Progress: [#                                                 ] 2%  Successfully processed Otaraia_3a1_13223ConfigFile.txt
 Progress: [#                                                 ] 3%  Successfully processed Otaraia_3a1_19487ConfigFile.txt
 Progress: [##                                                ] 4%  Successfully processed Otaraia_3a1_20661ConfigFile.txt
 Progress: [##                                                ] 5%  Successfully processed Otaraia_3a1_20993ConfigFile.txt
 Progress: [##                                                ] 5%  Successfully processed Otaraia_3a1_28276ConfigFile.txt
 Progress: [###                                               ] 6%  Successfully processed Otaraia_3a1_28426ConfigFile.txt
 Progress: [###                                               ] 7%  Successfully processed Otaraia_3a1_28720ConfigFile.txt
 Progress: [####                                              ] 8%  Successfully processed Otaraia_3a1_29813ConfigFile.txt
 Progress: [####                                              ] 9%  Successfully processed Otaraia_3a1_9938ConfigFile.txt
 Progress: [#####                                             ] 10%  Successfully processed Oturehua_13a1_13223ConfigFile.txt
 Progress: [#####                                             ] 10%