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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5415 - (show annotations)
Tue Dec 23 23:31:47 2014 UTC (3 years, 10 months ago) by jfenwick
File size: 14736 byte(s)
For experiments with the new netcdf

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=$(cat "$HOSTFILE" | wc -l)
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 -- disabled by default because it interfers
357 # with MPI binding
358 #if [ "$OMP_PROC_BIND" = "" ]; then
359 # #Force OpenMP binding for Intel (and GCC, though GCC is on by default)
360 # export OMP_PROC_BIND=true
361 #fi
362 #if [ "$KMP_AFFINITY" = "" ]; then
363 # #Set the style of binding (overrides OMP_PROC_BIND in many cases)
364 # export KMP_AFFINITY=verbose,compact
365 #fi
366
367 #
368 # Test to ensure people aren't trying to combine interactive and multi-process
369 #
370 if ([ ! -z $DO_INTERACTIVE ] || [ $# -eq 0 ]) && ([ $TOTPROC -gt 1 ])
371 then
372 die "Interactive mode cannot be used with more than one process!"
373 fi
374
375 if [ $TOTPROC -gt 1 ]
376 then
377 if [ "$ESCRIPT_CREATESTDFILES" = "y" ]
378 then
379 PYTHON_MPI=$PYTHON_MPI_REDIRECT
380 else
381 PYTHON_MPI=$PYTHON_MPI_NULL
382 fi
383 else
384 PYTHON_MPI=$PYTHON_MPI_NULL
385 fi
386 #==============================================================================
387 # Must have at least one command-line arg: the python script
388 if [ $# -eq 0 ]
389 then
390 if [ ! -z $DO_BINARY ]
391 then
392 die "No program to run was specified!"
393 else
394 DO_INTERACTIVE=y
395 fi
396 fi
397
398 #==============================================================================
399
400 if [ ! -z $DO_XTERM ]
401 then
402 EXEC_CMD="xterm -e"
403 else
404 EXEC_CMD=""
405 fi
406
407 if [ ! -z "$DO_VALGRIND" ]
408 then
409 VALGRIND_BIN=$(which valgrind 2>/dev/null)
410 if [ $? -eq 0 ]; then
411 LOGDIR=$ESCRIPT_ROOT/valgrind_logs
412 [ -d $LOGDIR ] || mkdir $LOGDIR
413 VG_TOOL=$(echo $DO_VALGRIND | awk '{ s=substr($0,1,1);print s;}')
414 if [ $VG_TOOL = "c" ];
415 then
416 # run callgrind
417 LOGFILE=${LOGDIR}/callgrind.%p.xml
418 VALGRIND="valgrind --tool=callgrind --callgrind-out-file=$LOGFILE"
419 EXEC_CMD="$EXEC_CMD $VALGRIND"
420 elif [ $VG_TOOL = "h" ];
421 then
422 # run cachegrind
423 LOGFILE=${LOGDIR}/cachegrind.%p.xml
424 VALGRIND="valgrind --tool=cachegrind --cachegrind-out-file=$LOGFILE"
425 EXEC_CMD="$EXEC_CMD $VALGRIND"
426 else
427 # run memcheck by default
428 LAST_N=$(ls -1 $LOGDIR|grep "^memcheck"|tail -1|cut -d. -f2)
429 NEW_N=$(printf "%04d" $((LAST_N + 1)))
430 LOGFILE=${LOGDIR}/memcheck.${NEW_N}.xml
431 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"
432 EXEC_CMD="$EXEC_CMD $VALGRIND"
433 fi
434 else
435 die "Execution with valgrind requested but valgrind not in path!"
436 fi
437 fi
438
439 if [ ! -z $DO_BINARY ]
440 then
441 EXEC_CMD="$EXEC_CMD $@"
442 else
443 # Check to see if the python version we were compiled with matches the
444 # one of PYTHON_CMD.
445 compfull=$(get_buildvar python_version)
446 compversion=$(echo $compfull | cut -d. -f1,2)
447 compmajor=$(echo $compfull | cut -d. -f1)
448 if [ "$PYTHON_CMD" = "python" ] # if people have customised the command they
449 then # might not want us changing it
450 if [ "$compmajor" = "3" ]
451 then
452 PYTHON_CMD=python3
453 fi
454 fi
455 intversion=$($PYTHON_CMD -c 'from __future__ import print_function;import sys;print("%d.%d"%(sys.version_info[0], sys.version_info[1]))')
456 if [ "$compversion" != "$intversion" ]
457 then
458 die "Python versions do not match. Escript was compiled for '$compversion'.
459 Current version of Python appears to be '$intversion'."
460 fi
461 if [ "$MPI_FLAVOUR" = "none" ]
462 then
463 if [ ! -z $DO_INTERACTIVE ]
464 then
465 EXEC_CMD="$EXEC_CMD $PYTHON_CMD -i $@"
466 else
467 EXEC_CMD="$EXEC_CMD $PYTHON_CMD $@"
468 fi
469 else
470 if [ ! -z $DO_INTERACTIVE ]
471 then
472 EXEC_CMD="$EXEC_CMD $PYTHON_MPI -i $@"
473 else
474 EXEC_CMD="$EXEC_CMD $PYTHON_MPI $@"
475 fi
476 fi
477 fi
478 vlog "Command to be executed is \"$EXEC_CMD\""
479 #==============================================================================
480 #
481 # now we start to spawn things:
482 #
483 if [ "$WITH_OPENMP" = "1" ]
484 then
485 export OMP_NUM_THREADS=$ESCRIPT_NUM_THREADS
486 EXPORT_ENV="$EXPORT_ENV,OMP_NUM_THREADS"
487 fi
488
489 vlog "Pre-launch command: \"@@PRELAUNCH\""
490 @@PRELAUNCH
491 vlog "Launch command: \"@@LAUNCH\""
492 @@LAUNCH
493 EXIT_CODE=$?
494 vlog "Post-launch command: \"@@POSTLAUNCH\""
495 @@POSTLAUNCH
496
497 if [ ! -z "$DO_VALGRIND" ]; then
498 echo "Valgrind log file written to ${LOGFILE}"
499 fi
500
501 rm -f $HOSTFILE
502
503 exit $EXIT_CODE
504

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 /release/4.0/run-escript.in:5380-5406 /trunk/ripley/test/python/bin/run-escript:3480-3515

  ViewVC Help
Powered by ViewVC 1.1.26