/[escript]/branches/py3_attempt2/bin/run-escript
ViewVC logotype

Annotation of /branches/py3_attempt2/bin/run-escript

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2806 - (hide annotations)
Fri Dec 4 06:46:13 2009 UTC (9 years, 4 months ago) by caltinay
Original Path: trunk/bin/escript
File size: 13672 byte(s)
bin/escript:
 - bail out early if buildvars is not around
 - quote variables to avoid syntax errors
 - some work towards POSIX compliance (s/==/=/ in string comparisons)
 - escript -V was still spitting out 'pre2.0' -> changed to pre4.0
 - fixed minor typos

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26