/[escript]/branches/numpy/scripts/finley_wrapper_template
ViewVC logotype

Annotation of /branches/numpy/scripts/finley_wrapper_template

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2396 - (hide annotations)
Thu Apr 23 23:58:29 2009 UTC (9 years, 11 months ago) by jfenwick
File size: 13442 byte(s)
Branching to port escript to use numpy rather than numarray

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

  ViewVC Help
Powered by ViewVC 1.1.26