/[escript]/branches/inv2jf/run-escript.in
ViewVC logotype

Contents of /branches/inv2jf/run-escript.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5267 - (show annotations)
Mon Nov 17 06:48:27 2014 UTC (4 years, 5 months ago) by caltinay
Original Path: trunk/run-escript.in
File size: 14659 byte(s)
template is not executable...

1 #!/usr/bin/env sh
2
3 ##############################################################################
4 #
5 # Copyright (c) 2003-2014 by University of Queensland
6 # http://www.uq.edu.au
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
13 # Development 2012-2013 by School of Earth Sciences
14 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
15 #
16 ##############################################################################
17
18 # Escript wrapper for python
19 # Sets LD_LIBRARY_PATH and PYTHONPATH and then runs either python or the MPI
20 # launcher.
21
22 # Extra paths can be configured about a page further down
23 # Search for EXTRA_PATH=""
24
25 # set to 1 if this is a standalone build and ../../pkg contains the relevant
26 # tools
27 STANDALONE=0
28
29 # set to 1 if this is part of a packaged build (.deb) and files will be
30 # installed in standard locations rather than everything in a single directory
31 # Do not use this together with STANDALONE
32 STDLOCATION=0
33
34 # Now we find the location of this script
35 # Note that this location should be absolute but does not need to be unique
36 scriptdir=""
37 CURDIR=$(pwd)
38
39 # Environment vars which control operations:
40 # ESCRIPT_NUM_NODES, ESCRIPT_NUM_PROCS, ESCRIPT_NUM_THREADS, ESCRIPT_HOSTFILE, ESCRIPT_CREATESTDFILES
41
42 HOSTFILE=/tmp/escript.$USER.$$
43
44 die () {
45 echo "Error: $@" 1>&2
46 exit 1
47 }
48
49 #Begin finding ESCRIPT_ROOT
50 if [ $STDLOCATION -ne 0 ]
51 then
52 ESCRIPT_ROOT=/usr/lib/python-escript
53 else
54 # We don't know the escript root so we need to work it out from the invocation
55 # Need to match if the name contains /
56 if $(echo $0|grep -q /)
57 then
58 # We are not using the PATH to find the script
59 cd "$(dirname $0)"
60 scriptdir=$(pwd)
61 cd "$CURDIR"
62 else
63 # name does not contain / therefore we are using
64 tscriptdir=$(which $0)
65 if [ $? -ne 0 ]
66 then
67 die "Unable to determine script directory!"
68 fi
69 scriptdir=$(dirname $tscriptdir)
70 fi
71
72 cd "$scriptdir/.."
73 ESCRIPT_ROOT=$(pwd)
74 cd ..
75 ESCRIPT_PARENT=$(pwd)
76 cd "$CURDIR"
77
78 fi
79 ##### End finding ESCRIPT_ROOT ########
80
81 PYTHON_MPI_NULL="$ESCRIPT_ROOT/lib/pythonMPI"
82 PYTHON_MPI_REDIRECT="$ESCRIPT_ROOT/lib/pythonMPIredirect"
83 PYTHON_CMD=python
84
85 # if possible please express paths relative to $ESCRIPT_ROOT unless
86 # they are in an unrelated location
87
88 EXTRA_DYLD_LIBRARY_PATH=""
89 EXTRA_PATH=$ESCRIPT_ROOT/bin
90 EXTRA_PYTHONPATH=$ESCRIPT_ROOT
91
92 if [ $STDLOCATION -eq 1 ]
93 then
94 EXTRA_LD_LIBRARY_PATH=/usr/lib/python-escript
95 else
96 EXTRA_LD_LIBRARY_PATH=$ESCRIPT_ROOT/lib
97 fi
98
99
100 if [ $STANDALONE -eq 1 ]
101 then
102 EXTRA_PATH=$ESCRIPT_PARENT/pkg/python/bin:$ESCRIPT_PARENT/pkg/scons/bin:$EXTRA_PATH
103 EXTRA_LD_LIBRARY_PATH=$ESCRIPT_PARENT/pkg/boost/lib:$ESCRIPT_PARENT/pkg/netcdf/lib/:$EXTRA_LD_LIBRARY_PATH
104 EXTRA_LD_LIBRARY_PATH=$EXTRA_LD_LIBRARY_PATH
105 EXTRA_LD_LIBRARY_PATH=$ESCRIPT_PARENT/pkg/python/lib:$EXTRA_LD_LIBRARY_PATH
106 EXTRA_PYTHONPATH=$ESCRIPT_PARENT/pkg/numpy/lib/python2.6/site-packages:$ESCRIPT_PARENT/pkg/matplotlib/lib/python2.6/site-packages:$EXTRA_PYTHONPATH
107 fi
108
109
110 BUILDINFO_FILE="$ESCRIPT_ROOT/lib/buildvars"
111 if [ "$STDLOCATION" = "1" ]
112 then
113 BUILDINFO_FILE=/usr/lib/python-escript/buildvars
114 fi
115 if [ ! -r "$BUILDINFO_FILE" ]; then
116 if [ "$1" = "-e" ]; then
117 echo "export LD_LIBRARY_PATH=$EXTRA_LD_LIBRARY_PATH:\$LD_LIBRARY_PATH"
118 echo "export PYTHONPATH=$EXTRA_PYTHONPATH:\$PYTHONPATH"
119 echo "export PATH=$EXTRA_PATH:\$PATH"
120 if [ "$(uname)" = "Darwin" ]
121 then
122 echo "export DYLD_LIBRARY_PATH=$EXTRA_DYLD_LIBRARY_PATH:$EXTRA_LD_LIBRARY_PATH:\$DYLD_LIBRARY_PATH"
123 fi
124 exit 0
125 fi
126 die "Unable to read escript build information!"
127 fi
128
129 get_buildvar () {
130 echo $(grep "^$1=" "$BUILDINFO_FILE" |cut -d= -f2)
131 }
132
133 HELP_TEXT="
134 Usage: run-escript [options] script.py [arguments...]
135 -n nn number of nodes to use
136 -p np number of MPI processes to spawn per node
137 -t nt number of OpenMP threads to use
138 -f file name of MPI hostfile
139 -c print compile information for escript and exit
140 -V print escript version and exit
141 -i interactive mode
142 -b do not invoke python (run non-python programs)
143 -e print export statements for environment and exit
144 -o redirect output from MPI to files
145 -v print diagnostics
146 -x run in new xterm instance
147 -m tool run with valgrind {tool=m[emcheck]/c[allgrind]/[cac]h[egrind]}
148 script.py Your python script
149 arguments... The optional command-line arguments to your script
150 "
151
152 if [ "$1" = "--help" ]; then
153 echo "$HELP_TEXT"
154 exit 0
155 fi
156 #==============================================================================
157
158 # Parse the command-line options
159 while getopts 'bn:p:t:f:echim:oVvx' option
160 do
161 case "$option" in
162 "b") DO_BINARY=y
163 ;;
164 "m") DO_VALGRIND=$OPTARG
165 ;;
166 "n") ESCRIPT_NUM_NODES=$OPTARG
167 ;;
168 "p") ESCRIPT_NUM_PROCS=$OPTARG
169 ;;
170 "t") ESCRIPT_NUM_THREADS=$OPTARG
171 ;;
172 "f") ESCRIPT_HOSTFILE=$OPTARG
173 ;;
174 "c") cat "$BUILDINFO_FILE"
175 exit 0
176 ;;
177 "V") echo "escript-development(build "$(get_buildvar svn_revision)")"
178 exit 0
179 ;;
180 "h") echo "$HELP_TEXT"
181 exit 0
182 ;;
183 "i") DO_INTERACTIVE=y
184 ;;
185 "e") echo "export LD_LIBRARY_PATH=$EXTRA_LD_LIBRARY_PATH:\$LD_LIBRARY_PATH"
186 echo "export PYTHONPATH=$EXTRA_PYTHONPATH:\$PYTHONPATH"
187 echo "export PATH=$EXTRA_PATH:\$PATH"
188 if [ "$(uname)" = "Darwin" ]
189 then
190 echo "export DYLD_LIBRARY_PATH=$EXTRA_DYLD_LIBRARY_PATH:$EXTRA_LD_LIBRARY_PATH:\$DYLD_LIBRARY_PATH"
191 fi
192 exit 0
193 ;;
194 "o") ESCRIPT_CREATESTDFILES=y
195 ;;
196 "v") ESCRIPT_VERBOSE=y
197 ;;
198 "x") DO_XTERM=y
199 ;;
200 ?) echo "$HELP_TEXT"
201 exit 1
202 ;;
203 esac
204 done
205 shift $(($OPTIND - 1))
206
207 vlog () {
208 if [ ! -z $ESCRIPT_VERBOSE ]; then
209 echo "$@"
210 fi
211 }
212
213 #==============================================
214 #
215 # Read MPI_FLAVOUR and WITH_OPENMP from the buildvars
216 #
217 MPI_FLAVOUR=$(get_buildvar mpi)
218 WITH_OPENMP=$(get_buildvar openmp)
219
220 vlog "MPI flavour is $MPI_FLAVOUR."
221 if [ "$WITH_OPENMP" = "1" ]; then
222 vlog "OpenMP enabled."
223 else
224 vlog "OpenMP disabled."
225 fi
226
227 #
228 # Add VisIt paths if required
229 #
230 WITH_VISIT=$(get_buildvar visit)
231 if [ "$WITH_VISIT" = "1" ]; then
232 VISIT_BIN=$(which visit 2>/dev/null)
233 if [ $? -eq 0 ]; then
234 VISIT_PY_PATH=$($VISIT_BIN -env | grep LIBPATH | cut -d= -f2)
235 EXTRA_PYTHONPATH=$EXTRA_PYTHONPATH:$VISIT_PY_PATH
236 EXTRA_LD_LIBRARY_PATH=$EXTRA_LD_LIBRARY_PATH:$VISIT_PY_PATH
237 else
238 vlog "Warning: VisIt module enabled but VisIt not in path!"
239 fi
240 fi
241
242 #
243 # extend path variables
244 #
245 export PATH=$EXTRA_PATH:$PATH
246 export LD_LIBRARY_PATH=$EXTRA_LD_LIBRARY_PATH:$LD_LIBRARY_PATH
247 export PYTHONPATH=$EXTRA_PYTHONPATH:$PYTHONPATH
248 EXPORT_ENV="PATH,LD_LIBRARY_PATH,PYTHONPATH"
249 if [ "$(uname)" = "Darwin" ]
250 then
251 export DYLD_LIBRARY_PATH=$EXTRA_DYLD_LIBRARY_PATH:$EXTRA_LD_LIBRARY_PATH:$DYLD_LIBRARY_PATH
252 EXPORT_ENV="$EXPORT_ENV,DYLD_LIBRARY_PATH"
253 fi
254 vlog "PATH = $PATH
255 LD_LIBRARY_PATH = $LD_LIBRARY_PATH
256 PYTHONPATH = $PYTHONPATH"
257 if [ ! -z $DYLD_LIBRARY_PATH ]; then
258 vlog "DYLD_LIBRARY_PATH = $DYLD_LIBRARY_PATH"
259 fi
260
261 #==============================================
262 #
263 # Ensure the variables have sensible values
264 #
265 if [ "$MPI_FLAVOUR" = "none" ]
266 then
267 if [ ! -z "$ESCRIPT_NUM_NODES" ]; then
268 if [ $ESCRIPT_NUM_NODES -gt 1 ]; then
269 echo "Warning: MPI disabled but number of nodes set. Option ignored."
270 fi
271 fi
272 if [ ! -z "$ESCRIPT_NUM_PROCS" ]; then
273 if [ $ESCRIPT_NUM_PROCS -gt 1 ]; then
274 echo "Warning: MPI disabled but number of processors per node set. Option ignored."
275 fi
276 fi
277 if [ ! -z "$ESCRIPT_HOSTFILE" ]
278 then
279 echo "Warning: MPI disabled but host file is given. Option ignored."
280 fi
281 ESCRIPT_NUM_NODES=1
282 ESCRIPT_NUM_PROCS=1
283 else
284 # use the PBS_NODEFILE if not otherwise specified
285 if [ ! -z "$PBS_NODEFILE" ] && [ -z "$ESCRIPT_HOSTFILE" ]
286 then
287 ESCRIPT_HOSTFILE=$PBS_NODEFILE
288 fi
289
290 if [ ! -z "$ESCRIPT_HOSTFILE" ]
291 then
292 if [ -f "$ESCRIPT_HOSTFILE" ]
293 then
294 sort -u "${ESCRIPT_HOSTFILE}" > $HOSTFILE
295 HOSTLIST=$(awk 'BEGIN{S=""}{if (S == "") { S = $0 } else {S = S "," $0}}END{print S}' "$HOSTFILE")
296
297 NUM_HOSTS=$(wc -l "$HOSTFILE")
298 if [ ! -z $ESCRIPT_NUM_NODES ]
299 then
300 if [ $NUM_HOSTS -ne $ESCRIPT_NUM_NODES ]
301 then
302 die "Number of hosts selected in the host file $ESCRIPT_HOSTFILE needs to match the requested number of nodes $ESCRIPT_NUM_NODES!"
303 fi
304 else
305 ESCRIPT_NUM_NODES=$NUM_HOSTS
306 fi
307 else
308 die "Cannot find hostfile $ESCRIPT_HOSTFILE!"
309 fi
310 else
311 echo "localhost" > $HOSTFILE
312 HOSTLIST="localhost"
313 fi
314
315 if [ -z $ESCRIPT_NUM_NODES ]
316 then
317 ESCRIPT_NUM_NODES=1
318 fi
319
320 if [ -z $ESCRIPT_NUM_PROCS ]
321 then
322 ESCRIPT_NUM_PROCS=1
323 fi
324
325 vlog "ESCRIPT_NUM_NODES = $ESCRIPT_NUM_NODES\nESCRIPT_NUM_PROCS = $ESCRIPT_NUM_PROCS"
326 fi
327
328 if [ "$WITH_OPENMP" = "1" ]
329 then
330 if [ -z $ESCRIPT_NUM_THREADS ]
331 then
332 ESCRIPT_NUM_THREADS=$OMP_NUM_THREADS
333 if [ -z $ESCRIPT_NUM_THREADS ]
334 then
335 ESCRIPT_NUM_THREADS=1
336 fi
337 fi
338 vlog "ESCRIPT_NUM_THREADS = $ESCRIPT_NUM_THREADS"
339 else
340 if [ ! -z $ESCRIPT_NUM_THREADS ] && [ $ESCRIPT_NUM_THREADS != 1 ]
341 then
342 echo "Warning: OpenMP is disabled but number of threads requested is $ESCRIPT_NUM_THREADS!=1. Option ignored."
343 fi
344 ESCRIPT_NUM_THREADS=1
345 fi
346
347 #
348 # Now we compute total number of Processes
349 #
350 TOTPROC=$((ESCRIPT_NUM_NODES * ESCRIPT_NUM_PROCS))
351 if [ $? -ne 0 ] #Some compute error
352 then #This could happen if the args were not a number
353 die "Expression of total number of processors = $ESCRIPT_NUM_NODES * $ESCRIPT_NUM_PROCS is not numerical!"
354 fi
355
356 # set up thread binding if unset
357 if [ "$OMP_PROC_BIND" = "" ]; then
358 #Force OpenMP binding for Intel (and GCC, though GCC is on by default)
359 export OMP_PROC_BIND=true
360 fi
361 if [ "$KMP_AFFINITY" = "" ]; then
362 #Set the style of binding (overrides OMP_PROC_BIND in many cases)
363 export KMP_AFFINITY=verbose,compact
364 fi
365
366 #
367 # Test to ensure people aren't trying to combine interactive and multi-process
368 #
369 if ([ ! -z $DO_INTERACTIVE ] || [ $# -eq 0 ]) && ([ $TOTPROC -gt 1 ])
370 then
371 die "Interactive mode cannot be used with more than one process!"
372 fi
373
374 if [ $TOTPROC -gt 1 ]
375 then
376 if [ "$ESCRIPT_CREATESTDFILES" = "y" ]
377 then
378 PYTHON_MPI=$PYTHON_MPI_REDIRECT
379 else
380 PYTHON_MPI=$PYTHON_MPI_NULL
381 fi
382 else
383 PYTHON_MPI=$PYTHON_MPI_NULL
384 fi
385 #==============================================================================
386 # Must have at least one command-line arg: the python script
387 if [ $# -eq 0 ]
388 then
389 if [ ! -z $DO_BINARY ]
390 then
391 die "No program to run was specified!"
392 else
393 DO_INTERACTIVE=y
394 fi
395 fi
396
397 #==============================================================================
398
399 if [ ! -z $DO_XTERM ]
400 then
401 EXEC_CMD="xterm -e"
402 else
403 EXEC_CMD=""
404 fi
405
406 if [ ! -z "$DO_VALGRIND" ]
407 then
408 VALGRIND_BIN=$(which valgrind 2>/dev/null)
409 if [ $? -eq 0 ]; then
410 LOGDIR=$ESCRIPT_ROOT/valgrind_logs
411 [ -d $LOGDIR ] || mkdir $LOGDIR
412 VG_TOOL=$(echo $DO_VALGRIND | awk '{ s=substr($0,1,1);print s;}')
413 if [ $VG_TOOL = "c" ];
414 then
415 # run callgrind
416 LOGFILE=${LOGDIR}/callgrind.%p.xml
417 VALGRIND="valgrind --tool=callgrind --callgrind-out-file=$LOGFILE"
418 EXEC_CMD="$EXEC_CMD $VALGRIND"
419 elif [ $VG_TOOL = "h" ];
420 then
421 # run cachegrind
422 LOGFILE=${LOGDIR}/cachegrind.%p.xml
423 VALGRIND="valgrind --tool=cachegrind --cachegrind-out-file=$LOGFILE"
424 EXEC_CMD="$EXEC_CMD $VALGRIND"
425 else
426 # run memcheck by default
427 LAST_N=$(ls -1 $LOGDIR|grep "^memcheck"|tail -1|cut -d. -f2)
428 NEW_N=$(printf "%04d" $((LAST_N + 1)))
429 LOGFILE=${LOGDIR}/memcheck.${NEW_N}.xml
430 VALGRIND="valgrind --tool=memcheck --xml=yes --show-reachable=yes --error-limit=no --gen-suppressions=all --suppressions=$ESCRIPT_ROOT/scripts/escript.supp --leak-check=full --xml-file=$LOGFILE"
431 EXEC_CMD="$EXEC_CMD $VALGRIND"
432 fi
433 else
434 die "Execution with valgrind requested but valgrind not in path!"
435 fi
436 fi
437
438 if [ ! -z $DO_BINARY ]
439 then
440 EXEC_CMD="$EXEC_CMD $@"
441 else
442 # Check to see if the python version we were compiled with matches the
443 # one of PYTHON_CMD.
444 compfull=$(get_buildvar python_version)
445 compversion=$(echo $compfull | cut -d. -f1,2)
446 compmajor=$(echo $compfull | cut -d. -f1)
447 if [ "$PYTHON_CMD" = "python" ] # if people have customised the command they
448 then # might not want us changing it
449 if [ "$compmajor" = "3" ]
450 then
451 PYTHON_CMD=python3
452 fi
453 fi
454 intversion=$($PYTHON_CMD -c 'from __future__ import print_function;import sys;print("%d.%d"%(sys.version_info[0], sys.version_info[1]))')
455 if [ "$compversion" != "$intversion" ]
456 then
457 die "Python versions do not match. Escript was compiled for '$compversion'.
458 Current version of Python appears to be '$intversion'."
459 fi
460 if [ "$MPI_FLAVOUR" = "none" ]
461 then
462 if [ ! -z $DO_INTERACTIVE ]
463 then
464 EXEC_CMD="$EXEC_CMD $PYTHON_CMD -i $@"
465 else
466 EXEC_CMD="$EXEC_CMD $PYTHON_CMD $@"
467 fi
468 else
469 if [ ! -z $DO_INTERACTIVE ]
470 then
471 EXEC_CMD="$EXEC_CMD $PYTHON_MPI -i $@"
472 else
473 EXEC_CMD="$EXEC_CMD $PYTHON_MPI $@"
474 fi
475 fi
476 fi
477 vlog "Command to be executed is \"$EXEC_CMD\""
478 #==============================================================================
479 #
480 # now we start to spawn things:
481 #
482 if [ "$WITH_OPENMP" = "1" ]
483 then
484 export OMP_NUM_THREADS=$ESCRIPT_NUM_THREADS
485 EXPORT_ENV="$EXPORT_ENV,OMP_NUM_THREADS"
486 fi
487
488 vlog "Pre-launch command: \"@@PRELAUNCH\""
489 @@PRELAUNCH
490 vlog "Launch command: \"@@LAUNCH\""
491 @@LAUNCH
492 EXIT_CODE=$?
493 vlog "Post-launch command: \"@@POSTLAUNCH\""
494 @@POSTLAUNCH
495
496 if [ ! -z "$DO_VALGRIND" ]; then
497 echo "Valgrind log file written to ${LOGFILE}"
498 fi
499
500 rm -f $HOSTFILE
501
502 exit $EXIT_CODE
503

Properties

Name Value
svn:mergeinfo /branches/3.4/bin/run-escript:4453-4490 /branches/diaplayground/bin/run-escript:4940-5147 /branches/lapack2681/bin/escript:2682-2741 /branches/pasowrap/bin/run-escript:3661-3674 /branches/py3_attempt2/bin/run-escript:3871-3891 /branches/release3.4/bin/run-escript:4452 /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