-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparallel.sh
62 lines (52 loc) · 1.42 KB
/
parallel.sh
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
#!/bin/bash
# scripts to reproduce the analysis and figures from Garrido-Oter et al., 2018
#
# originally by Ruben Garrido-Oter
parallel() {
# default parameters
local cmd_list=$1
local n_cores=$2
local sleep_time=2
# initializations
array[$n_cores]=pid
local n=1
local n_total=$(wc -l $cmd_list | sed 's/ .*//g')
# fork first batch of processes
for ((i = 0; i <= $n_cores - 1; i++))
do
if [[ $n -le $n_total ]]
then
$(sed -n "$n"p $cmd_list) &
pid[$i]=$!
echo -e "thread $i started\tpid: ${pid[$i]}"
n=$(($n + 1))
fi
done
# while there is still data
# if there are free cores
# fork new processes
while [[ $n -le $n_total ]]
do
sleep $sleep_time
clear
echo -e "n: $n\n"
for ((i = 0; i <= $n_cores - 1; i++))
do
is_running=$(ps -A | grep "^ *${pid[$i]} " | wc -l)
if [[ $is_running -eq 0 ]]
then
if [[ $n -le $n_total ]]
then
$(sed -n "$n"p $cmd_list) &
pid[$i]=$!
n=$(($n + 1))
echo -e "thread $i started\tpid: ${pid[$i]}"
fi
else
echo -e "thread $i running\tpid: ${pid[$i]}"
fi
done
done
wait
}