2 #------------------------------------------------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 1991-2008 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 the
14 # Free Software Foundation; either version 2 of the License, or (at your
15 # 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, write to the Free Software Foundation,
24 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 # Scheduler for network distributed compilations using wmake.
31 # - WM_HOSTS contains a list of hosts and number of concurrent processes
33 # export WM_HOSTS="hostA:1 hostB:2 hostC:1"
34 # - WM_COLOURS contains a list of colours to cycle through
35 # export WM_COLOURS="black blue green cyan red magenta yellow"
37 # Sources the relevant cshrc/bashrc if not set.
39 # WM_PROJECT_DIR, WM_PROJECT and WM_PROJECT_VERSION will have been set
40 # before calling this routine.
41 # FOAM_INST_DIR may possibly have been set (to find installation)
44 # wmakeScheduler COMMAND
45 # run 'COMMAND' on one of the slots listed in WM_HOSTS
47 # wmakeScheduler -count
48 # count the total number of slots available in WM_HOSTS
49 # eg, export WM_NCOMPPROCS=$(wmakeScheduler -count)
51 #-------------------------------------------------------------------------------
52 lockDir
=$HOME/.wmakeScheduler
54 # fallback - 1 core on current host
55 : ${WM_HOSTS:=$HOST:1}
57 # count the total number of slots available and exit
58 if [ "$1" = "-count" ]
61 for slotGroup
in $WM_HOSTS
64 [ "$n" = "${slotGroup%%:*}" ] && n
=1 # missing ':'
71 # where to source WM_PROJECT settings in a remote shell
72 # This code tries to figure out which cshrc or bashrc to execute.
73 # !! Assumes remote computer running same shell and startup files
76 sourceFoam
=false
# fallback command
78 */csh |
*/tcsh
) # [t]csh vs bash|ksh|sh
86 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
87 # check ~/.$WM_PROJECT/
88 # check <installedProject>/etc/
92 $HOME/.
$WM_PROJECT/$WM_PROJECT_VERSION \
97 if [ -f "$i/$shellRc" ]
99 sourceFoam
="$i/$shellRc"
105 # Construct test string for remote execution.
106 # Source WM_PROJECT settings if WM_PROJECT environment not set.
107 # attempt to preserve the installation directory 'FOAM_INST_DIR'
110 if [ "$FOAM_INST_DIR" ]
112 sourceFoam
='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam"
114 sourceFoam
='[ "$WM_PROJECT" ] || '". $sourceFoam"
119 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
120 sourceFoam
='if ( ! $?WM_PROJECT ) source '"$sourceFoam"
124 # quote double-quotes for remote command line
125 rcmd
=$
(echo $
* |
sed -e s
/\"/\'\"\'/g
)
126 ## the same, without forking (not ksh, maybe not /bin/sh either)
127 # rcmd=$(while [ "$#" -gt 0 ]; do echo "${1//\"/'\"'}"; shift; done)
130 # Convert WM_COLOURS into an array
133 for col in $WM_COLOURS
135 colourList
[$nColours]=$col
136 ((nColours
= $nColours + 1))
139 # Bashism: make pipe fail early.
140 # This ensures the return value of the command is returned and not of the
141 # colouring pipe etc.
146 # colour output by argument 1
155 setterm
-foreground $1
158 setterm
-foreground default
170 for slotGroup
in $WM_HOSTS
172 # split 'host:N', but catch 'host:' and 'host' too
173 host=${slotGroup%%:*}
175 [ "$n" = "$host" ] && n
=1 # missing ':'
179 while [ "$i" -lt "$n" ]
181 lockFile
="$lockDir/$host:$i"
182 if lockfile
-r0 "$lockFile" 2>/dev
/null
184 if [ "$nColours" -gt 0 ]
187 colour
="${colourList[$colourIndex]}"
189 if [ "$host" = "$HOST" ]; then
190 eval $
* 2>&1 | colourPipe
"$colour"
192 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe
"$colour"
196 if [ "$host" = "$HOST" ]; then
199 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1
205 rm -f "$lockFile" 2>/dev
/null
210 # Cycle through colours. Note: outside lock clause!
211 colourIndex
=$
(expr $colourIndex + 1)
212 [ "$colourIndex" -lt "$nColours" ] || colourIndex
=0
216 # Did not find any free slots. Rest a bit.
220 if [ "$nColours" -gt 0 ]
222 setterm
-foreground default
225 #------------------------------------------------------------------------------