-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendrun.sh
More file actions
100 lines (81 loc) · 3.12 KB
/
Copy pathsendrun.sh
File metadata and controls
100 lines (81 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
# ----------- Argument validation ----------- #
NRUNS=$1
SEED=$2
NCORES=20
if [[ -z "$NRUNS" || ! "$NRUNS" =~ ^[0-9]+$ ]]; then
echo "Usage: $0 <NRUNS> [SEED]"
echo "NRUNS must be a positive integer."
exit 1
fi
# ----------- Generate random seed if not provided ----------- #
if [[ -z "$SEED" ]]; then
SEED=$(date +%s%N | cut -b1-9) # Nano timestamp, first 9 digits
echo "[INFO] No seed provided. Generated random seed: $SEED"
fi
# ----------- Define output folder using timestamp and seed ----------- #
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
OUTPUT_PATH="output/run_${TIMESTAMP}_seed${SEED}/"
echo "[INFO] Creating output directories..."
mkdir -p "$OUTPUT_PATH" "$OUTPUT_PATH/main_files" "$OUTPUT_PATH/inputs_tmp" "$OUTPUT_PATH/logs"
inputs_tmp="$OUTPUT_PATH/inputs_tmp"
main_files="$OUTPUT_PATH/main_files"
logs="$OUTPUT_PATH/logs"
# ----------- Save execution parameters ----------- #
{
echo "NRUNS=$NRUNS"
echo "SEED=$SEED"
echo "TIMESTAMP=$TIMESTAMP"
echo "NCORES=$NCORES"
} > "$OUTPUT_PATH/run_info.txt"
# ----------- Determine max concurrent jobs ----------- #
MAX_JOBS=$(( NRUNS < NCORES ? NRUNS : NCORES ))
echo "[INFO] Running $NRUNS simulations with up to $MAX_JOBS concurrent jobs..."
echo "[INFO] Output will be saved in: $OUTPUT_PATH"
# ----------- Launch simulation jobs ----------- #
for ((nj=0; nj<NRUNS; nj++)); do
SUFFIX=$nj
echo "[INFO] Launching job $nj with suffix $SUFFIX..."
# Create customized GetImage_$SUFFIX.in
awk -v SUFFIX=$SUFFIX -v OUTPUT_PATH=$OUTPUT_PATH '{
if ($2=="GetImage:ResultsFolder") {
printf("/gamos/setParam GetImage:ResultsFolder %s\n", OUTPUT_PATH)
} else if ($2=="GetImage:OutputFilename") {
printf("/gamos/setParam GetImage:OutputFilename image_%s\n", SUFFIX)
} else {
print $0
}
}' inputs/GetImage.in > "$inputs_tmp/GetImage_$SUFFIX.in"
# Compute unique seeds
seed1=$((SEED + 2 * nj))
seed2=$((SEED + 2 * nj + 1))
# Create customized main_$SUFFIX.inn
awk -v SUFFIX=$SUFFIX -v s1=$seed1 -v s2=$seed2 -v TMP_INPUTS=$inputs_tmp '{
if ($2=="inputs/GetImage.in") {
printf("/control/execute %s/GetImage_%s.in\n", TMP_INPUTS, SUFFIX)
} else if ($1=="/gamos/random/setSeeds") {
printf("/gamos/random/setSeeds %s %s\n", s1, s2)
} else {
print $0
}
}' main.in > "$main_files/main_$SUFFIX.inn"
# Run simulation and log output
gamos "$main_files/main_$SUFFIX.inn" 2>&1 | tee "$logs/log_main_$SUFFIX.log" &
# Control number of concurrent jobs
if [[ $(jobs -r -p | wc -l) -ge $MAX_JOBS ]]; then
wait -n
fi
done
wait
echo "[INFO] All simulations have completed."
echo "[INFO] Output directory: $OUTPUT_PATH"
# ----------- Prompt for cleanup ----------- #
echo ""
read -p "Do you want to delete the temporary files ($inputs_tmp and $main_files)? [y/N]: " answer
if [[ "$answer" == "y" || "$answer" == "Y" ]]; then
echo "[INFO] Deleting temporary files..."
rm -rf "$inputs_tmp" "$main_files"
echo "[INFO] Temporary files deleted."
else
echo "[INFO] Temporary files kept."
fi