BUGFIX: 552fd5fd requires calculation of deflated geometry
[foam-extend-3.2.git] / wmake / wmakeScheduler
blobdd86d588155ef67fab7d4f4a766b6076b1e0baef
1 #!/bin/bash
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright held by original author
7 # \\/ M anipulation |
8 #------------------------------------------------------------------------------
9 # License
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
20 # for more details.
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
26 # Script
27 # wmakeScheduler
29 # Description
30 # Scheduler for network distributed compilations using wmake.
31 # - WM_HOSTS contains a list of hosts and number of concurrent processes
32 # eg,
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)
43 # Usage
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" ]
59 then
60 expr $(
61 for slotGroup in $WM_HOSTS
63 n=${slotGroup##*:}
64 [ "$n" = "${slotGroup%%:*}" ] && n=1 # missing ':'
65 echo "+ ${n:-1}"
66 done
68 exit 0
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
74 # in same location
76 sourceFoam=false # fallback command
77 case $SHELL in
78 */csh | */tcsh ) # [t]csh vs bash|ksh|sh
79 shellRc=cshrc
82 shellRc=bashrc
84 esac
86 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
87 # check ~/.$WM_PROJECT/
88 # check <installedProject>/etc/
89 if [ "$WM_PROJECT" ]
90 then
91 for i in \
92 $HOME/.$WM_PROJECT/$WM_PROJECT_VERSION \
93 $HOME/.$WM_PROJECT \
94 $WM_PROJECT_DIR/etc \
97 if [ -f "$i/$shellRc" ]
98 then
99 sourceFoam="$i/$shellRc"
100 break
102 done
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'
108 case $sourceFoam in
109 */bashrc)
110 if [ "$FOAM_INST_DIR" ]
111 then
112 sourceFoam='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam"
113 else
114 sourceFoam='[ "$WM_PROJECT" ] || '". $sourceFoam"
118 */cshrc)
119 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
120 sourceFoam='if ( ! $?WM_PROJECT ) source '"$sourceFoam"
122 esac
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
131 declare colourList
132 nColours=0
133 for col in $WM_COLOURS
135 colourList[$nColours]=$col
136 ((nColours = $nColours + 1))
137 done
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.
142 set -o pipefail
146 # colour output by argument 1
148 colourPipe()
150 if [ "$1" ]
151 then
153 while read line
155 setterm -foreground $1
156 echo "$line"
157 done
158 setterm -foreground default
160 else
166 colourIndex=0
168 while :
170 for slotGroup in $WM_HOSTS
172 # split 'host:N', but catch 'host:' and 'host' too
173 host=${slotGroup%%:*}
174 n=${slotGroup##*:}
175 [ "$n" = "$host" ] && n=1 # missing ':'
176 : ${n:=1}
179 while [ "$i" -lt "$n" ]
181 lockFile="$lockDir/$host:$i"
182 if lockfile -r0 "$lockFile" 2>/dev/null
183 then
184 if [ "$nColours" -gt 0 ]
185 then
186 # Set colour
187 colour="${colourList[$colourIndex]}"
189 if [ "$host" = "$HOST" ]; then
190 eval $* 2>&1 | colourPipe "$colour"
191 else
192 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe "$colour"
194 retval=$?
195 else
196 if [ "$host" = "$HOST" ]; then
197 eval $* 2>&1
198 else
199 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1
201 retval=$?
204 # Release lock
205 rm -f "$lockFile" 2>/dev/null
206 exit $retval
208 i=$(expr $i + 1)
210 # Cycle through colours. Note: outside lock clause!
211 colourIndex=$(expr $colourIndex + 1)
212 [ "$colourIndex" -lt "$nColours" ] || colourIndex=0
214 done
215 done
216 # Did not find any free slots. Rest a bit.
217 sleep 1
218 done
220 if [ "$nColours" -gt 0 ]
221 then
222 setterm -foreground default
225 #------------------------------------------------------------------------------