Merge branch 'master' of github.com:OpenFOAM/OpenFOAM-2.0.x
[OpenFOAM-2.0.x.git] / wmake / wmakeScheduler
blobaa6fb1ec81bd963412192fff74fcbda92d50a46e
1 #!/bin/bash
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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 #-------------------------------------------------------------------------------
52 # csh sets HOST, bash sets HOSTNAME
53 : ${HOST:=$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" ]
62 then
63 expr $(
64 for slotGroup in $WM_HOSTS
66 n=${slotGroup##*:}
67 [ "$n" = "${slotGroup%%:*}" ] && n=1 # missing ':'
68 echo "+ ${n:-1}"
69 done
71 exit 0
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
77 # in same location
79 sourceFoam=false # fallback command
80 case $SHELL in
81 */csh | */tcsh ) # [t]csh vs bash|ksh|sh
82 shellRc=cshrc
85 shellRc=bashrc
87 esac
89 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
90 # check ~/.$WM_PROJECT/
91 # check <installedProject>/etc/
92 if [ "$WM_PROJECT" ]
93 then
94 for i in \
95 $HOME/.$WM_PROJECT/$WM_PROJECT_VERSION \
96 $HOME/.$WM_PROJECT \
97 $WM_PROJECT_DIR/etc \
100 if [ -f "$i/$shellRc" ]
101 then
102 sourceFoam="$i/$shellRc"
103 break
105 done
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
112 case $sourceFoam in
113 */bashrc)
114 if [ "$FOAM_INST_DIR" ]
115 then
116 sourceFoam='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam $FOAM_SETTINGS"
117 else
118 sourceFoam='[ "$WM_PROJECT" ] || '". $sourceFoam $FOAM_SETTINGS"
122 */cshrc)
123 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
124 sourceFoam='if ( ! $?WM_PROJECT ) source '"$sourceFoam $FOAM_SETTINGS"
126 esac
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
135 declare colourList
136 nColours=0
137 for col in $WM_COLOURS
139 colourList[$nColours]=$col
140 ((nColours = $nColours + 1))
141 done
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.
146 set -o pipefail
150 # colour output by argument 1
152 colourPipe()
154 if [ "$1" ]
155 then
157 while read line
159 setterm -foreground $1
160 echo "$line"
161 done
162 setterm -foreground default
164 else
170 colourIndex=0
172 while :
174 for slotGroup in $WM_HOSTS
176 # split 'host:N', but catch 'host:' and 'host' too
177 host=${slotGroup%%:*}
178 n=${slotGroup##*:}
179 [ "$n" = "$host" ] && n=1 # missing ':'
180 : ${n:=1}
183 while [ "$i" -lt "$n" ]
185 lockFile="$lockDir/$host:$i"
186 if lockfile -r0 "$lockFile" 2>/dev/null
187 then
188 if [ "$nColours" -gt 0 ]
189 then
190 # Set colour
191 colour="${colourList[$colourIndex]}"
193 if [ "$host" = "$HOST" ]; then
194 eval $* 2>&1 | colourPipe "$colour"
195 else
196 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe "$colour"
198 retval=$?
199 else
200 if [ "$host" = "$HOST" ]; then
201 eval $* 2>&1
202 else
203 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1
205 retval=$?
208 # Release lock
209 rm -f "$lockFile" 2>/dev/null
210 exit $retval
212 i=$(expr $i + 1)
214 # Cycle through colours. Note: outside lock clause!
215 colourIndex=$(expr $colourIndex + 1)
216 [ "$colourIndex" -lt "$nColours" ] || colourIndex=0
218 done
219 done
220 # Did not find any free slots. Rest a bit.
221 sleep 1
222 done
224 if [ "$nColours" -gt 0 ]
225 then
226 setterm -foreground default
229 #------------------------------------------------------------------------------