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 if [ "$n" = "${slotGroup%%:*}" ]; then n
=1; fi # 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/
88 if [ "$WM_PROJECT" ]; then
91 $HOME/.
$WM_PROJECT/$WM_PROJECT_VERSION \
96 if [ -f "$i/$shellRc" ]; then
97 sourceFoam
="$i/$shellRc"
103 # Construct test string for remote execution.
104 # Source WM_PROJECT settings if WM_PROJECT environment not set.
105 # attempt to preserve the installation directory 'FOAM_INST_DIR'
108 if [ "$FOAM_INST_DIR" ]; then
109 sourceFoam
='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam"
111 sourceFoam
='[ "$WM_PROJECT" ] || '". $sourceFoam"
116 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
117 sourceFoam
='if ( ! $?WM_PROJECT ) source '"$sourceFoam"
121 # quote double-quotes for remote command line
122 rcmd
=$
(echo $
* |
sed -e s
/\"/\'\"\'/g
)
123 ## the same, without forking (not ksh, maybe not /bin/sh either)
124 # rcmd=$(while [ "$#" -gt 0 ]; do echo "${1//\"/'\"'}"; shift; done)
127 # Convert WM_COLOURS into an array
130 for col in $WM_COLOURS
132 colours
[$nColours]=$col
133 ((nColours
= $nColours + 1))
136 # Bashism: make pipe fail early. This make sure return value of compilation
137 # is returned and not of colouring pipe.
140 # Define function to colour output by argument 1
143 (while read line
; do setterm
-foreground $1; echo "$line" ; done; setterm
-foreground default
)
154 for slotGroup
in $WM_HOSTS
156 # split 'host:N', but catch 'host:' and 'host' too
157 host=${slotGroup%%:*}
159 if [ "$n" = "$host" ]; then n
=1; fi # missing ':'
163 while [ "$i" -lt "$n" ]
165 lockFile
="$lockDir/$host:$i"
166 if lockfile
-r0 "$lockFile" 2>/dev
/null
; then
167 if [ "$WM_COLOURS" ]; then
169 colour
="${colours[$colourIndex]}"
171 if [ "$host" = "$HOST" ]; then
172 eval $
* 2>&1 | colourPipe
"$colour"
173 elif [ -n "$JOB_ID" ]; then
174 qrsh
-inherit -v PWD
$host "$rcmd"
176 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe
"$colour"
180 if [ "$host" = "$HOST" ]; then
182 elif [ -n "$JOB_ID" ]; then
183 qrsh
-inherit -v PWD
$host "$rcmd"
185 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd"
191 rm -f "$lockFile" 2>/dev
/null
196 # Cycle through colours
197 colourIndex
=$
(expr $colourIndex + 1)
198 if (( $colourIndex >= $nColours )); then
203 # Did not find any free slots. Rest a bit.
207 if [ "$WM_COLOURS" ]; then
208 setterm
-foreground default
211 #------------------------------------------------------------------------------