/[escript]/trunk/scripts/finley_wrapper_template
ViewVC logotype

Contents of /trunk/scripts/finley_wrapper_template

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2368 - (show annotations)
Mon Apr 6 05:05:35 2009 UTC (10 years, 6 months ago) by gross
File size: 12616 byte(s)
some fixes for MKL and Mpisize>1
1 #!/bin/bash
2
3 # Escript/Finley wrapper for python
4 # Sets LD_LIBRARY_PATH and PYTHONPATH and then runs either python or the MPI launcher
5
6 #Extra paths can be configured about a page further down
7 #Search for EXTRA_PATH=""
8
9 #set to 1 if performing this is a standalone build and /packages contains the relevant tools
10 STANDALONE=0
11
12 #set to 1 if this is part of a packaged build (.deb) and files will be installed in standard locations
13 #rather than everything in a single directory
14 #Do not use this together with $STANDALONE
15 STDLOCATION=0
16
17 #Now we find the location of this script
18 #Note that this location should be absolute but does not need to be unique
19 scriptdir=""
20 CURDIR=`pwd`
21
22 #Environment vars which control operations:
23 # ESCRIPT_NUM_NODES, ESCRIPT_NUM_PROCS, ESCRIPT_NUM_THREADS, ESCRIPT_HOSTFILE, ESCRIPT_CREATESTDFILES
24
25 HOSTFILE=/tmp/escript.$$
26 HOSTFILE2=/tmp/escript2.$$
27
28 #Begin finding ESCRIPT_ROOT
29 if [ $STDLOCATION != 0 ]
30 then
31 ESCRIPT_ROOT=/usr/lib/escript
32 else
33 #We don't know the escript root so we need to work it out from the invocation
34 #Need to match if the name contains /
35 if [[ $0 =~ / ]]
36 then
37 # We are not using the PATH to find the script
38 cd `dirname $0`
39 scriptdir=`pwd`
40 cd $CURDIR
41 else
42 # name does not contain / therefore we are using
43 tscriptdir=`which $0`
44 if [ $? != 0 ]
45 then
46 echo "Error unable to determine script directory. Exiting."
47 exit 1
48 fi
49 scriptdir=`dirname $tscriptdir`
50 fi
51
52 cd $scriptdir/..
53 ESCRIPT_ROOT=`pwd`
54 cd ..
55 ESCRIPT_PARENT=`pwd`
56 cd $CURDIR
57
58 fi
59 ##### End finding ESCRIPT_ROOT ########
60
61 PYTHON_MPI="$ESCRIPT_ROOT/lib/pythonMPI"
62 PYTHON_CMD=python
63
64 # if possible please express paths relative to $ESCRIPT_ROOT unless
65 # they are in an unrelated location
66
67 EXTRA_DYLD_LIBRARY_PATH=""
68 EXTRA_PATH=$ESCRIPT_ROOT/bin
69 EXTRA_PYTHONPATH=$ESCRIPT_ROOT
70 EXTRA_LD_LIBRARY_PATH=$ESCRIPT_ROOT/lib
71
72 if [ $STANDALONE == 1 ]
73 then
74 EXTRA_PATH=$ESCRIPT_PARENT/packages/python/bin:$EXTRA_PATH
75 EXTRA_LD_LIBRARY_PATH=$ESCRIPT_PARENT/packages/boost/lib:$ESCRIPT_PARENT/packages/netcdf/lib/:$EXTRA_LD_LIBRARY_PATH
76 EXTRA_LD_LIBRARY_PATH=$ESCRIPT_PARENT/packages/vtk/lib/vtk-5.2:$ESCRIPT_PARENT/packages/mesa/lib:$EXTRA_LD_LIBRARY_PATH
77 EXTRA_LD_LIBRARY_PATH=$ESCRIPT_PARENT/packages/python/lib:$EXTRA_LD_LIBRARY_PATH
78 EXTRA_PYTHONPATH=$ESCRIPT_PARENT/packages/numarray/lib:$ESCRIPT_PARENT/packages/vtk/lib/python2.6/site-packages:$EXTRA_PYTHONPATH
79 fi
80
81
82
83 HELP_TEXT="
84 Usage: escript [options] script.py [arguments...]
85 -n nn number of nodes to use
86 -p np number of MPI processes to spawn
87 -t nt number of OpenMP threads to use
88 -f file name of MPI hostfile
89 -c print compile information for escript and exit
90 -V print escript version and exit
91 -i interactive mode
92 -b do not invoke python (run non-python programs)
93 -e print export statements for environment and exit
94 -o redirect output from MPI to files
95 -v print diagnostics
96 -x ..reserved for future use ..
97 script.py Your python script
98 arguments... The optional command-line arguments to your script
99 "
100
101 if [ "$1" = "--help" ]; then
102 echo "$HELP_TEXT"
103 exit 0
104 fi
105 #=======================================================================================
106
107 # Avoid bug in hybrid runs with MPT MPI
108
109
110 # Parse the command-line options
111 # option e should not be followed by a :
112 while getopts 'bn:p:t:f:h:ecVviox' option
113 do
114 case "$option" in
115 "b") DOBINARY=yes
116 ;;
117 "n") ESCRIPT_NUM_NODES=$OPTARG
118 ;;
119 "p") ESCRIPT_NUM_PROCS=$OPTARG
120 ;;
121 "t") ESCRIPT_NUM_THREADS=$OPTARG
122 ;;
123 "f") ESCRIPT_HOSTFILE=$OPTARG
124 ;;
125 "c") cat $ESCRIPT_ROOT/lib/buildvars
126 exit 0
127 ;;
128 "V") echo "escript-pre2.0(build "`grep svn_revision $ESCRIPT_ROOT/lib/buildvars |cut -d= -f2`")"
129 exit 0
130 ;;
131 "h") echo "$HELPTEXT"
132 exit 0
133 ;;
134 "i") DOINTERACTIVE=yes
135 ;;
136 "e") echo "export LD_LIBRARY_PATH=$EXTRA_LD_LIBRARY_PATH:\$LD_LIBRARY_PATH"
137 echo "export PYTHONPATH=$EXTRA_PYTHONPATH:\$PYTHONPATH"
138 echo "export PATH=$EXTRA_PATH:\$PATH"
139 if [ `uname` == Darwin ]
140 then
141 echo "export DYLD_LIBRARY_PATH=$EXTRA_DYLD_LIBRARY_PATH:$EXTRA_LD_LIBRARY_PATH:\$DYLD_LIBRARY_PATH"
142 fi
143 exit 0
144 ;;
145 "o") ESCRIPT_CREATESTDFILES="yes"
146 ;;
147 "v") ESCRIPT_VERBOSE="yes"
148 ;;
149 "x") echo "-x not implemented yet"
150 exit 1
151 ;;
152 ?) echo "$HELP_TEXT"
153 exit 1
154 ;;
155 esac
156 done
157 shift `expr $OPTIND - 1`
158 #==============================================
159 #
160 # Read the MPI_FLAVOUR from the buildvars
161 #
162 MPI_FLAVOUR=`grep mpi_flavour $ESCRIPT_ROOT/lib/buildvars |cut -d= -f2`
163 WITH_OPENMP=`grep useopenmp $ESCRIPT_ROOT/lib/buildvars |cut -d= -f2`
164
165 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI flavor is $MPI_FLAVOUR."; fi
166 if [ ! -z $ESCRIPT_VERBOSE ] && [ $WITH_OPENMP == "y" ]; then echo "OpenMP enabled."; fi
167 #
168 # extend path variables
169 #
170 export PATH=$EXTRA_PATH:$PATH
171 export LD_LIBRARY_PATH=$EXTRA_LD_LIBRARY_PATH:$LD_LIBRARY_PATH
172 export PYTHONPATH=$EXTRA_PYTHONPATH:$PYTHONPATH
173 EXPORT_ENV="PATH,LD_LIBRARY_PATH,PYTHONPATH"
174 if [ `uname` == Darwin ]
175 then
176 export DYLD_LIBRARY_PATH=$EXTRA_DYLD_LIBRARY_PATH:$EXTRA_LD_LIBRARY_PATH:$DYLD_LIBRARY_PATH
177 EXPORT_ENV="$EXPORT_ENV,DYLD_LIBRARY_PATH"
178 fi
179 if [ ! -z $ESCRIPT_VERBOSE ]
180 then
181 echo "PATH = $PATH "
182 echo "LD_LIBRARY_PATH = $LD_LIBRARY_PATH "
183 echo "PYTHONPATH = $PYTHONPATH "
184 if [ ! -z $DYLD_LIBRARY_PATH ]; then echo "DYLD_LIBRARY_PATH = $DYLD_LIBRARY_PATH "; fi
185 fi
186 #==============================================
187 #
188 # Ensure the variables have sensible values
189 #
190 if [ $MPI_FLAVOUR == "none" ]
191 then
192 if [ ! -z $ESCRIPT_NUM_NODES ]
193 then
194 echo "Warning: MPI disabled but number of nodes set. Option ignored."
195 fi
196 if [ ! -z $ESCRIPT_NUM_PROCS ]
197 then
198 echo "Warning: MPI disabled but number of processors per node set. Option ignored."
199 fi
200 if [ ! -z $ESCRIPT_HOSTFILE ]
201 then
202 echo "Warning: MPI disabled but host file is given. Option ignored."
203 fi
204 ESCRIPT_NUM_NODES=1
205 ESCRIPT_NUM_PROCS=1
206 else
207 if [ ! -z $ESCRIPT_HOSTFILE ]
208 then
209 if [ -f $ESCRIPT_HOSTFILE ]
210 then
211 cat $ESCRIPT_HOSTFILE | sort -u > $HOSTFILE
212 NUM_HOSTS=`cat $HOSTFILE | wc -l`
213 if [ ! -z $ESCRIPT_NUM_NODES ]
214 then
215 if [ $NUM_HOSTS -ne $ESCRIPT_NUM_NODES ]
216 then
217 echo "number of hosts selected in the host file $ESCRIPT_HOSTFILE needs to match the requested number of nodes $ESCRIPT_NUM_NODES."
218 exit 1
219 fi
220 else
221 ESCRIPT_NUM_NODES=$NUM_HOSTS
222 fi
223 else
224 echo "cannot find hostfile $ESCRIPT_HOSTFILE."
225 exit 1
226 fi
227 else
228 HOSTFILE=''
229 fi
230
231 if [ -z $ESCRIPT_NUM_NODES ]
232 then
233 ESCRIPT_NUM_NODES=1
234 fi
235
236 if [ -z $ESCRIPT_NUM_PROCS ]
237 then
238 ESCRIPT_NUM_PROCS=1
239 fi
240
241 if [ ! -z $ESCRIPT_VERBOSE ]
242 then
243 echo "ESCRIPT_NUM_NODES = $ESCRIPT_NUM_NODES "
244 echo "ESCRIPT_NUM_PROCS = $ESCRIPT_NUM_PROCS "
245 fi
246 fi
247
248 if [ $WITH_OPENMP == "y" ]
249 then
250 if [ -z $ESCRIPT_NUM_THREADS ]
251 then
252 ESCRIPT_NUM_THREADS=$OMP_NUM_THREADS
253 if [ -z $ESCRIPT_NUM_THREADS ]
254 then
255 ESCRIPT_NUM_THREADS=1
256 fi
257 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "ESCRIPT_NUM_THREADS is $ESCRIPT_NUM_THREADS."; fi
258 fi
259 else
260 ESCRIPT_NUM_THREADS=1
261 fi
262 #
263 # Now we compute total number of Processes
264 #
265 (( TOTPROC=$ESCRIPT_NUM_NODES * $ESCRIPT_NUM_PROCS))
266 if [ $? -ne 0 ] #Some compute error
267 then #This could happen if the args were not a number
268 echo "expression of total number of processors = $ESCRIPT_NUM_NODES * $ESCRIPT_NUM_PROCS is not numerical."
269 exit 1
270 fi
271 #
272 # Test to ensure people aren't trying to combine interactive and multi-process
273 #
274 if [[ ( ( ! -z $DOINTERACTIVE ) || ( $# -eq 0 ) ) && ( $TOTPROC -gt 1) ]]
275 then
276 echo "Interactive mode cannot be used with more than one process"
277 exit 1
278 fi
279 #=========================================================================================================
280 # Must have at least one command-line arg: the python script
281 if [ $# -eq 0 ]
282 then
283 if [ ! -z $DOBINARY ]
284 then
285 echo "No program to run was specified. Exiting."
286 exit 1
287 fi
288 fi
289
290 #=========================================================================================================
291 if [ ! -z $DOBINARY ]
292 then
293 EXEC_CMD="$@"
294 else
295 if [ $MPI_FLAVOUR == "none" ]
296 then
297 # Check to see if the python version we were compiled with matches the one of EXEC_CMD
298 # We should only do this check if they are running a python script
299 if [ -f $ESCRIPT_ROOT/lib/pyversion ]
300 then
301 compversion=`cat $ESCRIPT_ROOT/lib/pyversion`
302 intversion=`$PYTHON_CMD -V 2>&1`
303 if [ "$compversion" != "$intversion" ]
304 then
305 echo "Python versions do not match. Escript was compiled for "$compversion"."
306 echo "Current version of Python appears to be "$intversion"."
307 exit 1
308 fi
309 fi
310 if [ ! -z $DOINTERACTIVE ]
311 then
312 EXEC_CMD="$PYTHON_CMD -i $@"
313 else
314 EXEC_CMD="$PYTHON_CMD $@"
315 fi
316 else
317 EXEC_CMD="$PYTHON_MPI $@"
318 fi
319 fi
320 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "Command to be executed is \"$EXEC_CMD\"."; fi
321 #===============================================================================================
322 #
323 # now we start to spwan things:
324 #
325 if [ $WITH_OPENMP == "y" ]
326 then
327 export OMP_NUM_THREADS=$ESCRIPT_NUM_THREADS
328 EXPORT_ENV="$EXPORT_ENV,OMP_NUM_THREADS"
329 fi
330 EXIT_CODE=1
331 #=============== no MPI ===================================
332 if [ $MPI_FLAVOUR == "none" ]
333 then
334 $EXEC_CMD
335 EXIT_CODE=$?
336 #=============== OpenMPI ===================================
337 elif [ $MPI_FLAVOUR == "OPENMPI" ]
338 then
339 if [ ! -z $HOSTFILE ]
340 then
341 HOST_LIST=`awk 'BEGIN{S=""}{if (S == "") { S = $0 } else {S = S "," $0}}END{print S}' $HOSTFILE`
342 CMD="mpirun --bynode -np $TOTPROC --host $HOST_LIST $EXEC_CMD"
343 else
344 CMD="mpirun -np $TOTPROC $EXEC_CMD"
345 fi
346 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
347 $CMD
348 EXIT_CODE=$?
349
350 #=============== Intel MPI ===================================
351 elif [ $MPI_FLAVOUR == "INTELMPI" ]
352 then
353 if [ ! -z $HOSTFILE ]
354 then
355 mpdboot -n $ESCRIPT_NUM_NODES -r ssh -f $HOSTFILE
356 if [ $? -ne 0 ]
357 then
358 echo "mpdboot with host file $ESCRIPT_HOSTFILE for $ESCRIPT_NUM_NODES nodes failed."
359 exit 1
360 else
361 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "mpdboot was started with host file $ESCRIPT_HOSTFILE for $ESCRIPT_NUM_NODES nodes."; fi
362 fi
363 fi
364 CMD="mpiexec -perhost $ESCRIPT_NUM_PROCS -envall -n $TOTPROC $EXEC_CMD"
365 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
366 $CMD
367 EXIT_CODE=$?
368 if [ ! -z $HOSTFILE ]
369 then
370 mpdallexit
371 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "mpdallexit executed."; fi
372 fi
373 #=============== SGI's MPIMPT ===================================
374 elif [ $MPI_FLAVOUR == "MPT" ]
375 then
376 export MPI_NUM_MEMORY_REGIONS=0
377 EXPORT_ENV="$EXPORT_ENV,MPI_NUM_MEMORY_REGIONS"
378 if [ ! -z $HOSTFILE ]
379 then
380 HOST_LIST=`awk 'BEGIN{S=""}{if (S == "") { S = $0 } else {S = S "," $0}}END{print S}' $HOSTFILE`
381 CMD="mpirun $HOST_LIST $ESCRIPT_NUM_PROCS $EXEC_CMD"
382 else
383 CMD="mpirun -np $TOTPROC $EXEC_CMD"
384 fi
385 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
386 $CMD
387 EXIT_CODE=$?
388 #=============== MPICH ===================================
389 elif [ $MPI_FLAVOUR == "MPICH" ]
390 then
391 if [ ! -z $HOSTFILE ]
392 then
393 touch $HOSTFILE2
394 for (( i=1;i<=$ESCRIPT_NUM_PROCS;i+=1 )) ; do cat $HOSTFILE >> $HOSTFILE2 ; done
395 CMD="mpirun -machinefile $HOSTFILE2 -np $TOTPROC $EXEC_CMD"
396 else
397 CMD="mpirun -np $TOTPROC $EXEC_CMD"
398 fi
399 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
400 $CMD
401 EXIT_CODE=$?
402 #=============== MPICH2 ===================================
403 elif [ $MPI_FLAVOUR == "MPICH2" ]
404 then
405 if [ ! -z $HOSTFILE ]
406 then
407 HOST_LIST=`awk 'BEGIN{S=""; N='$ESCRIPT_NUM_PROCS'}{ S = S " " $0 " " N }END{print S}' $HOSTFILE`
408 CMD="mpiexec -hosts $ESCRIPT_NUM_NODES $HOST_LIST -genvlist $EXPORT_ENV $EXEC_CMD"
409 else
410 CMD="mpiexec -np $TOTPROC -genvlist EXPORT_ENV $EXEC_CMD"
411 fi
412 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
413 $CMD
414 EXIT_CODE=$?
415 else
416 echo "unknown MPI falvour $MPI_FLAVOUR."
417 fi
418 exit $EXIT_CODE

  ViewVC Help
Powered by ViewVC 1.1.26