2 #------------------------------------------------------------------------------
4 # \\ / F ield | foam-extend: Open Source CFD
5 # \\ / O peration | Version: 3.2
6 # \\ / A nd | Web: http://www.foam-extend.org
7 # \\/ M anipulation | For copyright notice see file Copyright
8 #------------------------------------------------------------------------------
10 # This file is part of foam-extend.
12 # foam-extend is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by the
14 # Free Software Foundation, either version 3 of the License, or (at your
15 # option) any later version.
17 # foam-extend is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
29 # Scheduler for network distributed compilations using wmake.
30 # - WM_HOSTS contains a list of hosts and number of concurrent processes
32 # export WM_HOSTS="hostA:1 hostB:2 hostC:1"
33 # - WM_COLOURS contains a list of colours to cycle through
34 # export WM_COLOURS="black blue green cyan red magenta yellow"
36 # Sources the relevant cshrc/bashrc if not set.
38 # WM_PROJECT_DIR, WM_PROJECT and WM_PROJECT_VERSION will have been set
39 # before calling this routine.
40 # FOAM_INST_DIR may possibly have been set (to find installation)
43 # wmakeScheduler COMMAND
44 # run 'COMMAND' on one of the slots listed in WM_HOSTS
46 # wmakeScheduler -count
47 # count the total number of slots available in WM_HOSTS
48 # eg, export WM_NCOMPPROCS=$(wmakeScheduler -count)
50 #------------------------------------------------------------------------------
51 lockDir
=$HOME/.wmakeScheduler
53 # fallback - 1 core on current host
54 : ${WM_HOSTS:=$HOST:1}
56 # count the total number of slots available and exit
57 if [ "$1" = "-count" ]
60 for slotGroup
in $WM_HOSTS
63 [ "$n" = "${slotGroup%%:*}" ] && n
=1 # missing ':'
70 # where to source WM_PROJECT settings in a remote shell
71 # This code tries to figure out which cshrc or bashrc to execute.
72 # !! Assumes remote computer running same shell and startup files
75 sourceFoam
=false
# fallback command
77 */csh |
*/tcsh
) # [t]csh vs bash|ksh|sh
85 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
86 # check ~/.$WM_PROJECT/
87 # check <installedProject>/etc/
91 $HOME/.
$WM_PROJECT/$WM_PROJECT_VERSION \
96 if [ -f "$i/$shellRc" ]
98 sourceFoam
="$i/$shellRc"
104 # Construct test string for remote execution.
105 # Source WM_PROJECT settings if WM_PROJECT environment not set.
106 # attempt to preserve the installation directory 'FOAM_INST_DIR'
109 if [ "$FOAM_INST_DIR" ]
111 sourceFoam
='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam"
113 sourceFoam
='[ "$WM_PROJECT" ] || '". $sourceFoam"
118 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
119 sourceFoam
='if ( ! $?WM_PROJECT ) source '"$sourceFoam"
123 # quote double-quotes for remote command line
124 rcmd
=$
(echo $
* |
sed -e s
/\"/\'\"\'/g
)
125 ## the same, without forking (not ksh, maybe not /bin/sh either)
126 # rcmd=$(while [ "$#" -gt 0 ]; do echo "${1//\"/'\"'}"; shift; done)
129 # Convert WM_COLOURS into an array
132 for col in $WM_COLOURS
134 colourList
[$nColours]=$col
135 ((nColours
= $nColours + 1))
138 # Bashism: make pipe fail early.
139 # This ensures the return value of the command is returned and not of the
140 # colouring pipe etc.
145 # colour output by argument 1
154 setterm
-foreground $1
157 setterm
-foreground default
169 for slotGroup
in $WM_HOSTS
171 # split 'host:N', but catch 'host:' and 'host' too
172 host=${slotGroup%%:*}
174 [ "$n" = "$host" ] && n
=1 # missing ':'
178 while [ "$i" -lt "$n" ]
180 lockFile
="$lockDir/$host:$i"
181 if lockfile
-r0 "$lockFile" 2>/dev
/null
183 if [ "$nColours" -gt 0 ]
186 colour
="${colourList[$colourIndex]}"
188 if [ "$host" = "$HOST" ]; then
189 eval $
* 2>&1 | colourPipe
"$colour"
191 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe
"$colour"
195 if [ "$host" = "$HOST" ]; then
198 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1
204 rm -f "$lockFile" 2>/dev
/null
209 # Cycle through colours. Note: outside lock clause!
210 colourIndex
=$
(expr $colourIndex + 1)
211 [ "$colourIndex" -lt "$nColours" ] || colourIndex
=0
215 # Did not find any free slots. Rest a bit.
219 if [ "$nColours" -gt 0 ]
221 setterm
-foreground default
224 #------------------------------------------------------------------------------