blob: 526dc5e65f0b82274ee396eadedd822acf288b69 (
plain)
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
|
#!/usr/bin/env bash
usage () {
cat << EOF
Usage: run_parallel.sh {command} {num_iter} {num_parallel}
Where:
{command} is a string containing the command, including parameters, to run
{num_iter} is an positive integer indicating how many iterations to execute
[num_parallel] is an (optional) positive integer indicating how many parallel commands should be executed
in each iteration. If not provided, the default is half the number of available CPU cores.
EOF
}
if [ "$#" -lt 2 ]; then
echo "Illegal number of parameters."
usage
exit 1
fi
# Determine the number of CPU cores. This code is Linux specific at the moment.
NCORES=$(grep -c ^processor /proc/cpuinfo)
command=$1
num_iter=$2
if [ "$#" -eq 3 ]; then
num_parallel=$3
else
# Use half the number of processor cores
num_parallel="$(($NCORES / 2))"
fi
echo "run_parallel:"
echo " number of cores: $NCORES"
echo " command: $command"
echo " num_parallel: $num_parallel"
echo " num_iter: $num_iter"
outf=./outfile.txt
for i in $(seq $num_iter); do
echo "Starting iteration $i" >> $outf
echo "Starting iteration $i"
# start the commands in parallel
for((t=1; t<=num_parallel; t++)); do
echo "Starting parallel command $t (of $num_parallel) in iteration $i (of $num_iter)" >> nohup.out.$t
eval nohup $command >> nohup.out.$t 2>&1 &
done
# Wait for the commands to all complete
for((t=1; t<=num_parallel; t++)); do
wait || exit $?
done
done
|