2 #---------------------------------*- sh -*-------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
8 #------------------------------------------------------------------------------
10 # This file is part of OpenFOAM.
12 # OpenFOAM is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
18 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 # You should have received a copy of the GNU General Public License
23 # along with OpenFOAM. 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 # csh sets HOST, bash sets HOSTNAME
56 # fallback - 1 core on current host
57 : ${WM_HOSTS:=$HOST:1}
59 # count the total number of slots available and exit
60 if [ "$1" = "-count" ]
63 for slotGroup
in $WM_HOSTS
66 [ "$n" = "${slotGroup%%:*}" ] && n
=1 # missing ':'
73 # where to source WM_PROJECT settings in a remote shell
74 # This code tries to figure out which cshrc or bashrc to execute.
75 # !! Assumes remote computer running same shell and startup files
78 sourceFoam
=false
# fallback command
80 */csh |
*/tcsh
) # [t]csh vs bash|ksh|sh
88 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
89 # check ~/.$WM_PROJECT/
90 # check <installedProject>/etc/
94 $HOME/.
$WM_PROJECT/$WM_PROJECT_VERSION \
99 if [ -f "$i/$shellRc" ]
101 sourceFoam
="$i/$shellRc"
107 # Construct test string for remote execution.
108 # Source WM_PROJECT settings if WM_PROJECT environment not set.
109 # attempt to preserve the installation directory 'FOAM_INST_DIR'
112 if [ "$FOAM_INST_DIR" ]
114 sourceFoam
='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam"
116 sourceFoam
='[ "$WM_PROJECT" ] || '". $sourceFoam"
121 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
122 sourceFoam
='if ( ! $?WM_PROJECT ) source '"$sourceFoam"
126 # quote double-quotes for remote command line
127 rcmd
=$
(echo $
* |
sed -e s
/\"/\'\"\'/g
)
128 ## the same, without forking (not ksh, maybe not /bin/sh either)
129 # rcmd=$(while [ "$#" -gt 0 ]; do echo "${1//\"/'\"'}"; shift; done)
132 # Convert WM_COLOURS into an array
135 for col in $WM_COLOURS
137 colourList
[$nColours]=$col
138 ((nColours
= $nColours + 1))
141 # Bashism: make pipe fail early.
142 # This ensures the return value of the command is returned and not of the
143 # colouring pipe etc.
148 # colour output by argument 1
157 setterm
-foreground $1
160 setterm
-foreground default
172 for slotGroup
in $WM_HOSTS
174 # split 'host:N', but catch 'host:' and 'host' too
175 host=${slotGroup%%:*}
177 [ "$n" = "$host" ] && n
=1 # missing ':'
181 while [ "$i" -lt "$n" ]
183 lockFile
="$lockDir/$host:$i"
184 if lockfile
-r0 "$lockFile" 2>/dev
/null
186 if [ "$nColours" -gt 0 ]
189 colour
="${colourList[$colourIndex]}"
191 if [ "$host" = "$HOST" ]; then
192 eval $
* 2>&1 | colourPipe
"$colour"
194 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe
"$colour"
198 if [ "$host" = "$HOST" ]; then
201 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1
207 rm -f "$lockFile" 2>/dev
/null
212 # Cycle through colours. Note: outside lock clause!
213 colourIndex
=$
(expr $colourIndex + 1)
214 [ "$colourIndex" -lt "$nColours" ] || colourIndex
=0
218 # Did not find any free slots. Rest a bit.
222 if [ "$nColours" -gt 0 ]
224 setterm
-foreground default
227 #------------------------------------------------------------------------------