Merge branch 'upstream/OpenFOAM-1.7.x' into upstream/OpenFOAM
[freefoam.git] / wmake / wmakeScheduler
bloba97d53990d48eab52e254a12d94daa60ae209c5d
1 #!/bin/bash
2 #---------------------------------*- sh -*-------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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
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
20 # for more details.
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/>.
25 # Script
26 # wmakeScheduler
28 # Description
29 # Scheduler for network distributed compilations using wmake.
30 # - WM_HOSTS contains a list of hosts and number of concurrent processes
31 # eg,
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)
42 # Usage
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
54 : ${HOST:=$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" ]
61 then
62 expr $(
63 for slotGroup in $WM_HOSTS
65 n=${slotGroup##*:}
66 [ "$n" = "${slotGroup%%:*}" ] && n=1 # missing ':'
67 echo "+ ${n:-1}"
68 done
70 exit 0
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
76 # in same location
78 sourceFoam=false # fallback command
79 case $SHELL in
80 */csh | */tcsh ) # [t]csh vs bash|ksh|sh
81 shellRc=cshrc
84 shellRc=bashrc
86 esac
88 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
89 # check ~/.$WM_PROJECT/
90 # check <installedProject>/etc/
91 if [ "$WM_PROJECT" ]
92 then
93 for i in \
94 $HOME/.$WM_PROJECT/$WM_PROJECT_VERSION \
95 $HOME/.$WM_PROJECT \
96 $WM_PROJECT_DIR/etc \
99 if [ -f "$i/$shellRc" ]
100 then
101 sourceFoam="$i/$shellRc"
102 break
104 done
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'
110 case $sourceFoam in
111 */bashrc)
112 if [ "$FOAM_INST_DIR" ]
113 then
114 sourceFoam='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam"
115 else
116 sourceFoam='[ "$WM_PROJECT" ] || '". $sourceFoam"
120 */cshrc)
121 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
122 sourceFoam='if ( ! $?WM_PROJECT ) source '"$sourceFoam"
124 esac
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
133 declare colourList
134 nColours=0
135 for col in $WM_COLOURS
137 colourList[$nColours]=$col
138 ((nColours = $nColours + 1))
139 done
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.
144 set -o pipefail
148 # colour output by argument 1
150 colourPipe()
152 if [ "$1" ]
153 then
155 while read line
157 setterm -foreground $1
158 echo "$line"
159 done
160 setterm -foreground default
162 else
168 colourIndex=0
170 while :
172 for slotGroup in $WM_HOSTS
174 # split 'host:N', but catch 'host:' and 'host' too
175 host=${slotGroup%%:*}
176 n=${slotGroup##*:}
177 [ "$n" = "$host" ] && n=1 # missing ':'
178 : ${n:=1}
181 while [ "$i" -lt "$n" ]
183 lockFile="$lockDir/$host:$i"
184 if lockfile -r0 "$lockFile" 2>/dev/null
185 then
186 if [ "$nColours" -gt 0 ]
187 then
188 # Set colour
189 colour="${colourList[$colourIndex]}"
191 if [ "$host" = "$HOST" ]; then
192 eval $* 2>&1 | colourPipe "$colour"
193 else
194 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe "$colour"
196 retval=$?
197 else
198 if [ "$host" = "$HOST" ]; then
199 eval $* 2>&1
200 else
201 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1
203 retval=$?
206 # Release lock
207 rm -f "$lockFile" 2>/dev/null
208 exit $retval
210 i=$(expr $i + 1)
212 # Cycle through colours. Note: outside lock clause!
213 colourIndex=$(expr $colourIndex + 1)
214 [ "$colourIndex" -lt "$nColours" ] || colourIndex=0
216 done
217 done
218 # Did not find any free slots. Rest a bit.
219 sleep 1
220 done
222 if [ "$nColours" -gt 0 ]
223 then
224 setterm -foreground default
227 #------------------------------------------------------------------------------