Repository navigation
Expand file tree
/
Copy pathlcrun
More file actions
executable file
·98 lines (85 loc) · 2.07 KB
/
Copy pathlcrun
File metadata and controls
executable file
·98 lines (85 loc) · 2.07 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
#!/bin/bash
set -e
if [ "$1" != "-n" ]; then
echo "Usage: $0 -n <nprocs> <command>"
exit 1
fi
nprocs=$2
executable=$3
# Use LCT PMI File to bootstrap
export LCT_PMI_BACKEND=file,local
export LCT_PMI_FILE_NRANKS=${nprocs}
export LCT_PMI_FILE_DIR=$(mktemp -d "${TMPDIR:-/tmp}/lct-pmi-file.XXXXXX")
pids=()
completed=()
clean_up() {
echo "Cleaning up"
echo "Killing all children ${pids[*]}"
for pid in "${pids[@]}"; do
kill "${pid}" 2>/dev/null || true
done
}
remove_rendezvous_dir() {
rm -rf "${LCT_PMI_FILE_DIR}"
}
# Kill all children on interrupt or error
trap clean_up INT TERM
trap remove_rendezvous_dir EXIT
# launch the command nprocs times nonblockingly
shift 2
for i in $(seq 1 ${nprocs}); do
"$@" &
pids+=($!)
completed+=(0)
done
# Poll the jobs so a failure from any child terminates its peers promptly. This
# avoids wait -n, which is unavailable in the Bash shipped with older macOS.
exit_code=0
remaining=${#pids[@]}
while [ "${remaining}" -gt 0 ]; do
jobs -pr >"${LCT_PMI_FILE_DIR}/running-pids"
running_pids=" $(tr '\n' ' ' <"${LCT_PMI_FILE_DIR}/running-pids")"
for i in "${!pids[@]}"; do
if [ "${completed[$i]}" -eq 1 ]; then
continue
fi
pid=${pids[$i]}
if [[ " ${running_pids}" == *" ${pid} "* ]]; then
continue
fi
status=0
wait "${pid}" || status=$?
completed[$i]=1
remaining=$((remaining - 1))
if [ "${status}" -ne 0 ]; then
exit_code=${status}
clean_up
for other_pid in "${pids[@]}"; do
wait "${other_pid}" 2>/dev/null || true
done
remaining=0
break
fi
done
if [ "${remaining}" -gt 0 ]; then
sleep 0.1
fi
done
exit $exit_code
# clean_up() {
# echo "Cleaning up"
# for pid in "${pids[@]}"; do
# if kill -0 "$pid" 2>/dev/null; then
# echo "Killing child $pid"
# kill "$pid"
# fi
# done
# }
# # Kill all children on interrupt or error
# trap clean_up INT TERM
# # launch the command nprocs times nonblockingly
# shift 2
# for i in $(seq 1 ${nprocs}); do
# "$@" &
# done
# wait # wait for all background jobs to finish