/[escript]/release/3.1/bin/run-escript
ViewVC logotype

Annotation of /release/3.1/bin/run-escript

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2986 - (hide annotations)
Fri Mar 12 04:51:44 2010 UTC (9 years ago) by jfenwick
File size: 14594 byte(s)
Allow run-escript to be called without the buildvars being there

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

Properties

Name Value
svn:executable *
svn:mergeinfo /branches/lapack2681/bin/escript:2682-2741 /branches/restext/bin/escript:2610-2624 /branches/stage3.0/bin/escript:2569-2590 /release/3.0/bin/escript:2591-2601 /trunk/bin/run-escript:2924-2944

  ViewVC Help
Powered by ViewVC 1.1.26