comparison PLIDflow/scripts/parallel_launch.sh @ 0:6fcfa4756040 draft

Uploaded
author bitlab
date Tue, 14 Jan 2020 06:09:42 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:6fcfa4756040
1 #!/bin/bash
2
3 # PIDs are not re-used until PID_MAX_DEFAULT is reached.
4
5 #requirement for the workflow
6 #source /home/galaxy-bitlab/galaxy/tools/proteindocking/scripts/ADT_VENV/bin/activate
7
8
9 # Reads a text file with commands to execute
10 if [ $# -lt 1 ]; then
11 echo " ==== ERROR ... ====."
12 echo ""
13 echo " usage: $0 <execution file> [max cores]"
14 echo ""
15 echo " MANDATORY <execution file>: a text file containing one command per line"
16 echo " OPTIONAL [max cores] : number of cores to use. Default is available cores minus one"
17 echo ""
18 exit -1
19 fi
20
21 input=$1
22 cores=$(grep -c ^processor /proc/cpuinfo)
23 cores=`expr $cores - 1`
24 current_jobs=0
25 pidArray=()
26 jobsArray=()
27 totalJobs=0
28 executedJobs=0
29 finishedJobs=0
30
31 if [ $# -eq 2 ]; then
32 cores=$2
33 fi
34
35 echo "Using $cores cores"
36
37
38 #initialize
39 for ((i=0 ; i < $cores ; i++))
40 do
41 pidArray[$i]=-1
42 done
43
44 # read execution guide and launch jobs
45 while IFS= read -r var
46 do
47
48 jobsArray[$totalJobs]=$var
49 #echo "${jobsArray[$totalJobs]}"
50 totalJobs=`expr $totalJobs + 1`
51
52 done < "$input"
53
54
55 # control them and only launch as many as specified
56 while [[ $executedJobs -lt $totalJobs || $finishedJobs -lt $totalJobs ]]; do
57
58 # Execute job
59
60 if [[ $current_jobs -lt $cores ]]; then
61
62 ${jobsArray[$executedJobs]} &
63 pid=$!
64 for ((i=0 ; i < $cores ; i++))
65 do
66
67
68 if [[ ${pidArray[$i]} -eq -1 && $executedJobs -lt $totalJobs ]]; then
69 pidArray[$i]=$pid
70 current_jobs=`expr $current_jobs + 1`
71 echo "PID: $pid JOBS: $current_jobs LAUNCHING: ${jobsArray[$executedJobs]}"
72 executedJobs=`expr $executedJobs + 1`
73 break
74 fi
75 done
76
77
78 fi
79
80 for ((i=0 ; i < $cores ; i++))
81 do
82 pid=${pidArray[$i]}
83 if [[ $pid -ne -1 ]]; then
84
85 ps -p $pid > /dev/null
86 if [[ $? == 1 ]]; then
87 current_jobs=`expr $current_jobs - 1`
88 finishedJobs=`expr $finishedJobs + 1`
89 echo "PID: $pid finished! Completed: $finishedJobs from total: $totalJobs"
90 pidArray[$i]=-1
91 fi
92
93 fi
94 done
95
96 sleep .01
97
98 done
99
100
101 #deactivate
102
103
104 #for job in `jobs -p`
105 #do
106 #echo $job
107 # wait $job
108 #done