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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2905 - (show annotations)
Tue Feb 2 04:26:42 2010 UTC (8 years, 9 months ago) by gross
Original Path: trunk/bin/escript
File size: 14320 byte(s)
some missing files.
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 ../../pkg 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 -ne 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 if [ ! -r $ESCRIPT_ROOT/lib/buildvars ]; then
62 echo "Error! Unable to read escript build information. Exiting."
63 exit 1
64 fi
65
66 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 if [ $STANDALONE -eq 1 ]
79 then
80 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 EXTRA_LD_LIBRARY_PATH=$EXTRA_LD_LIBRARY_PATH
83 EXTRA_LD_LIBRARY_PATH=$ESCRIPT_PARENT/pkg/python/lib:$EXTRA_LD_LIBRARY_PATH
84 EXTRA_PYTHONPATH=$ESCRIPT_PARENT/pkg/numpy/lib/python2.6/site-packages:$ESCRIPT_PARENT/pkg/matplotlib/lib/python2.6/site-packages:$EXTRA_PYTHONPATH
85 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 "V") echo "escript-development(build "`grep svn_revision $ESCRIPT_ROOT/lib/buildvars |cut -d= -f2`")"
135 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 if [ "`uname`" = "Darwin" ]
146 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 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 #
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 if [ "`uname`" = "Darwin" ]
184 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 if [ ! -z $ESCRIPT_VERBOSE ]
189 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 if [ "$MPI_FLAVOUR" = "none" ]
200 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 if [ ! -z $ESCRIPT_VERBOSE ]
251 then
252 echo "ESCRIPT_NUM_NODES = $ESCRIPT_NUM_NODES "
253 echo "ESCRIPT_NUM_PROCS = $ESCRIPT_NUM_PROCS "
254 fi
255 fi
256
257 if [ "$WITH_OPENMP" = "y" ]
258 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 if [ "$ESCRIPT_CREATESTDFILES" = "yes" ]
300 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 if [ -z $@ ]
319 then
320 if [ ! -z $DOBINARY ]
321 then
322 echo "Program must be given."
323 exit 1
324 else
325 DOINTERACTIVE="yes"
326 fi
327 fi
328
329 #=========================================================================================================
330 if [ ! -z $DOBINARY ]
331 then
332 EXEC_CMD="$@"
333 else
334 if [ "$MPI_FLAVOUR" = "none" ]
335 then
336 # Check to see if the python version we were compiled with matches the one of EXEC_CMD
337 # We should only do this check if they are running a python script
338 if [ -f $ESCRIPT_ROOT/lib/pyversion ]
339 then
340 compversion=`cat $ESCRIPT_ROOT/lib/pyversion`
341 intversion=`$PYTHON_CMD -V 2>&1`
342 if [ "$compversion" != "$intversion" ]
343 then
344 echo "Python versions do not match. Escript was compiled for "$compversion"."
345 echo "Current version of Python appears to be "$intversion"."
346 exit 1
347 fi
348 fi
349 if [ ! -z $DOINTERACTIVE ]
350 then
351 EXEC_CMD="$PYTHON_CMD -i $@"
352 else
353 EXEC_CMD="$PYTHON_CMD $@"
354 fi
355 else
356 if [ ! -z $DOINTERACTIVE ]
357 then
358 EXEC_CMD="$PYTHON_MPI -i $@"
359 else
360 EXEC_CMD="$PYTHON_MPI $@"
361 fi
362 fi
363 fi
364 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "Command to be executed is \"$EXEC_CMD\"."; fi
365 #===============================================================================================
366 #
367 # now we start to spwan things:
368 #
369 if [ "$WITH_OPENMP" = "y" ]
370 then
371 export OMP_NUM_THREADS=$ESCRIPT_NUM_THREADS
372 EXPORT_ENV="$EXPORT_ENV,OMP_NUM_THREADS"
373 fi
374 EXIT_CODE=1
375 #=============== no MPI ===================================
376 if [ "$MPI_FLAVOUR" = "none" ]
377 then
378 $EXEC_CMD
379 EXIT_CODE=$?
380 #=============== OpenMPI ===================================
381 elif [ "$MPI_FLAVOUR" = "OPENMPI" ]
382 then
383 if [ ! -z $HOSTFILE ]
384 then
385 HOST_LIST=`awk 'BEGIN{S=""}{if (S == "") { S = $0 } else {S = S "," $0}}END{print S}' $HOSTFILE`
386 CMD="mpirun --gmca mpi_warn_on_fork 0 -x ${EXPORT_ENV//,/ -x } --bynode -np $TOTPROC --host $HOST_LIST $EXEC_CMD"
387 else
388 CMD="mpirun --gmca mpi_warn_on_fork 0 -x ${EXPORT_ENV//,/ -x } -np $TOTPROC $EXEC_CMD"
389 fi
390 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
391 $CMD
392 EXIT_CODE=$?
393
394 #=============== Intel MPI ===================================
395 elif [ "$MPI_FLAVOUR" = "INTELMPI" ]
396 then
397
398 if [ "$WITH_OPENMP" = "y" ]
399 then
400 export I_MPI_PIN_DOMAIN=omp
401 EXPORT_ENV="$EXPORT_ENV, I_MPI_PIN_DOMAIN"
402 fi
403
404 if [ ! -z $HOSTFILE ]
405 then
406 mpdboot -n $ESCRIPT_NUM_NODES -r ssh -f $HOSTFILE
407 if [ $? -ne 0 ]
408 then
409 echo "mpdboot with host file $ESCRIPT_HOSTFILE for $ESCRIPT_NUM_NODES nodes failed."
410 exit 1
411 else
412 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "mpdboot was started with host file $ESCRIPT_HOSTFILE for $ESCRIPT_NUM_NODES nodes."; fi
413 fi
414 else
415 mpdboot -n 1 -r ssh
416 if [ $? -ne 0 ]
417 then
418 echo "mpdboot failed."
419 exit 1
420 else
421 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "mpdboot was started."; fi
422 fi
423 fi
424 CMD="mpiexec -perhost $ESCRIPT_NUM_PROCS -envall -n $TOTPROC $EXEC_CMD"
425 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
426 $CMD
427 EXIT_CODE=$?
428 mpdallexit
429 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "mpdallexit executed."; fi
430 #=============== SGI's MPIMPT ===================================
431 elif [ "$MPI_FLAVOUR" = "MPT" ]
432 then
433 export MPI_NUM_MEMORY_REGIONS=0
434 EXPORT_ENV="$EXPORT_ENV,MPI_NUM_MEMORY_REGIONS"
435 if [ ! -z $HOSTFILE ]
436 then
437 HOST_LIST=`awk 'BEGIN{S=""}{if (S == "") { S = $0 } else {S = S "," $0}}END{print S}' $HOSTFILE`
438 CMD="mpirun $HOST_LIST -np $ESCRIPT_NUM_PROCS $EXEC_CMD"
439 else
440 CMD="mpirun -np $TOTPROC $EXEC_CMD"
441 fi
442 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
443 $CMD
444 EXIT_CODE=$?
445 #=============== MPICH ===================================
446 elif [ "$MPI_FLAVOUR" = "MPICH" ]
447 then
448 if [ ! -z $HOSTFILE ]
449 then
450 touch $HOSTFILE2
451 for (( i=1;i<=$ESCRIPT_NUM_PROCS;i+=1 )) ; do cat $HOSTFILE >> $HOSTFILE2 ; done
452 CMD="mpirun -machinefile $HOSTFILE2 -np $TOTPROC $EXEC_CMD"
453 else
454 CMD="mpirun -np $TOTPROC $EXEC_CMD"
455 fi
456 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
457 $CMD
458 EXIT_CODE=$?
459 #=============== MPICH2 ===================================
460 elif [ "$MPI_FLAVOUR" = "MPICH2" ]
461 then
462 if [ ! -z $HOSTFILE ]
463 then
464 mpdboot -n $ESCRIPT_NUM_NODES -r ssh -f $HOSTFILE
465 if [ $? -ne 0 ]
466 then
467 echo "mpdboot with host file $ESCRIPT_HOSTFILE for $ESCRIPT_NUM_NODES nodes failed."
468 exit 1
469 else
470 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "mpdboot was started with host file $ESCRIPT_HOSTFILE for $ESCRIPT_NUM_NODES nodes."; fi
471 fi
472 else
473 mpdboot -n 1 -r ssh
474 if [ $? -ne 0 ]
475 then
476 echo "mpdboot failed."
477 exit 1
478 else
479 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "mpdboot was started."; fi
480 fi
481 fi
482 CMD="mpiexec -genvlist $EXPORT_ENV -np $TOTPROC $EXEC_CMD"
483 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "MPI command is \"$CMD\"."; fi
484 $CMD
485 EXIT_CODE=$?
486 mpdallexit
487 if [ ! -z $ESCRIPT_VERBOSE ]; then echo "mpdallexit executed."; fi
488 else
489 echo "unknown MPI flavour '$MPI_FLAVOUR'."
490 fi
491 exit $EXIT_CODE

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26