/[escript]/trunk/bin/run-escript
ViewVC logotype

Contents of /trunk/bin/run-escript

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4031 - (show annotations)
Wed Oct 24 03:36:37 2012 UTC (6 years, 10 months ago) by caltinay
File size: 15889 byte(s)
Added option -x to run xterm to run-escript and replaced a few bashims.

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

Properties

Name Value
svn:executable *
svn:mergeinfo /branches/lapack2681/bin/escript:2682-2741 /branches/pasowrap/bin/run-escript:3661-3674 /branches/py3_attempt2/bin/run-escript:3871-3891 /branches/restext/bin/escript:2610-2624 /branches/ripleygmg_from_3668/bin/run-escript:3669-3791 /branches/stage3.0/bin/escript:2569-2590 /branches/symbolic_from_3470/bin/run-escript:3471-3974 /branches/symbolic_from_3470/ripley/test/python/bin/run-escript:3517-3974 /release/3.0/bin/escript:2591-2601 /trunk/ripley/test/python/bin/run-escript:3480-3515

  ViewVC Help
Powered by ViewVC 1.1.26