Merge branch 'master' of ssh://opencfd@repo.or.cz/srv/git/OpenFOAM-1.5.x
[OpenFOAM-1.5.x.git] / wmake / wmakeScheduler
blob2a2ed0fd765151293a4b1f8979288cffa535d6c8
1 #!/bin/bash
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 1991-2008 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 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 if [ "$n" = "${slotGroup%%:*}" ]; then n=1; fi # missing ':'
65 echo "+ ${n:-1}"
66 done)
67 exit 0
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
73 # in same location
75 sourceFoam=false # fallback command
76 case $SHELL in
77 */csh | */tcsh ) # [t]csh vs bash|ksh|sh
78 shellRc=cshrc
81 shellRc=bashrc
83 esac
85 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
86 # check ~/.$WM_PROJECT/
87 # check <installedProject>/etc/
88 if [ "$WM_PROJECT" ]; then
90 for i in \
91 $HOME/.$WM_PROJECT/$WM_PROJECT_VERSION \
92 $HOME/.$WM_PROJECT \
93 $WM_PROJECT_DIR/etc \
96 if [ -f "$i/$shellRc" ]; then
97 sourceFoam="$i/$shellRc"
98 break
100 done
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'
106 case $sourceFoam in
107 */bashrc)
108 if [ "$FOAM_INST_DIR" ]; then
109 sourceFoam='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam"
110 else
111 sourceFoam='[ "$WM_PROJECT" ] || '". $sourceFoam"
115 */cshrc)
116 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
117 sourceFoam='if ( ! $?WM_PROJECT ) source '"$sourceFoam"
119 esac
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
128 declare colours
129 nColours=0
130 for col in $WM_COLOURS
132 colours[$nColours]=$col
133 ((nColours = $nColours + 1))
134 done
136 # Bashism: make pipe fail early. This make sure return value of compilation
137 # is returned and not of colouring pipe.
138 set -o pipefail
140 # Define function to colour output by argument 1
141 colourPipe(){
142 if [ "$1" ]; then
143 (while read line; do setterm -foreground $1; echo "$line" ; done; setterm -foreground default)
144 else
150 colourIndex=0
152 while :
154 for slotGroup in $WM_HOSTS
156 # split 'host:N', but catch 'host:' and 'host' too
157 host=${slotGroup%%:*}
158 n=${slotGroup##*:}
159 if [ "$n" = "$host" ]; then n=1; fi # missing ':'
160 : ${n:=1}
163 while [ "$i" -lt "$n" ]
165 lockFile="$lockDir/$host:$i"
166 if lockfile -r0 "$lockFile" 2>/dev/null; then
167 if [ "$WM_COLOURS" ]; then
168 # Set colour
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"
175 else
176 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe "$colour"
178 retval=$?
179 else
180 if [ "$host" = "$HOST" ]; then
181 eval $*
182 elif [ -n "$JOB_ID" ]; then
183 qrsh -inherit -v PWD $host "$rcmd"
184 else
185 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd"
187 retval=$?
190 # Release lock
191 rm -f "$lockFile" 2>/dev/null
192 exit $retval
194 i=$(expr $i + 1)
196 # Cycle through colours
197 colourIndex=$(expr $colourIndex + 1)
198 if (( $colourIndex >= $nColours )); then
199 colourIndex=0
201 done
202 done
203 # Did not find any free slots. Rest a bit.
204 sleep 1
205 done
207 if [ "$WM_COLOURS" ]; then
208 setterm -foreground default
211 #------------------------------------------------------------------------------