2 #------------------------------------------------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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 #-------------------------------------------------------------------------------
52 # csh sets HOST, bash sets HOSTNAME
56 lockDir
=$HOME/.
$WM_PROJECT/.wmake
57 # fallback - 1 core on current host
58 : ${WM_HOSTS:=$HOST:1}
60 # count the total number of slots available and exit
61 if [ "$1" = "-count" ]
64 for slotGroup
in $WM_HOSTS
67 [ "$n" = "${slotGroup%%:*}" ] && n
=1 # missing ':'
74 # where to source WM_PROJECT settings in a remote shell
75 # This code tries to figure out which cshrc or bashrc to execute.
76 # !! Assumes remote computer running same shell and startup files
79 sourceFoam
=false
# fallback command
81 */csh |
*/tcsh
) # [t]csh vs bash|ksh|sh
89 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
90 # check ~/.$WM_PROJECT/
91 # check <installedProject>/etc/
95 $HOME/.
$WM_PROJECT/$WM_PROJECT_VERSION \
100 if [ -f "$i/$shellRc" ]
102 sourceFoam
="$i/$shellRc"
108 # Construct test string for remote execution.
109 # Source WM_PROJECT settings if WM_PROJECT environment not set.
110 # attempt to preserve the installation directory 'FOAM_INST_DIR'
111 # use FOAM_SETTINGS to pass command-line settings
114 if [ "$FOAM_INST_DIR" ]
116 sourceFoam
='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam $FOAM_SETTINGS"
118 sourceFoam
='[ "$WM_PROJECT" ] || '". $sourceFoam $FOAM_SETTINGS"
123 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
124 sourceFoam
='if ( ! $?WM_PROJECT ) source '"$sourceFoam $FOAM_SETTINGS"
128 # quote double-quotes for remote command line
129 rcmd
=$
(echo $
* |
sed -e s
/\"/\'\"\'/g
)
130 ## the same, without forking (not ksh, maybe not /bin/sh either)
131 # rcmd=$(while [ "$#" -gt 0 ]; do echo "${1//\"/'\"'}"; shift; done)
134 # Convert WM_COLOURS into an array
137 for col in $WM_COLOURS
139 colourList
[$nColours]=$col
140 ((nColours
= $nColours + 1))
143 # Bashism: make pipe fail early.
144 # This ensures the return value of the command is returned and not of the
145 # colouring pipe etc.
150 # colour output by argument 1
159 setterm
-foreground $1
162 setterm
-foreground default
174 for slotGroup
in $WM_HOSTS
176 # split 'host:N', but catch 'host:' and 'host' too
177 host=${slotGroup%%:*}
179 [ "$n" = "$host" ] && n
=1 # missing ':'
183 while [ "$i" -lt "$n" ]
185 lockFile
="$lockDir/$host:$i"
186 if lockfile
-r0 "$lockFile" 2>/dev
/null
188 if [ "$nColours" -gt 0 ]
191 colour
="${colourList[$colourIndex]}"
193 if [ "$host" = "$HOST" ]; then
194 eval $
* 2>&1 | colourPipe
"$colour"
196 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe
"$colour"
200 if [ "$host" = "$HOST" ]; then
203 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1
209 rm -f "$lockFile" 2>/dev
/null
214 # Cycle through colours. Note: outside lock clause!
215 colourIndex
=$
(expr $colourIndex + 1)
216 [ "$colourIndex" -lt "$nColours" ] || colourIndex
=0
220 # Did not find any free slots. Rest a bit.
224 if [ "$nColours" -gt 0 ]
226 setterm
-foreground default
229 #------------------------------------------------------------------------------