1 |
#!/bin/sh |
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 |
|
7 |
EXTRAPATH="" |
8 |
EXTRALDLIBRARYPATH="" |
9 |
EXTRADYLDLIBRARYPATH="" |
10 |
EXTRAPYTHONPATH="" |
11 |
|
12 |
#@@SET_ADDITIONAL_REQUIRED_VARS_HERE@@ |
13 |
|
14 |
|
15 |
HELP_TEXT=" |
16 |
Usage: escript [options] script.py [arguments...] |
17 |
-O N Use N OpenMP threads (OMP_NUM_THREADS=N) |
18 |
-M K Use K MPI processes |
19 |
-l 'launcher' MPI launcher, usually -l 'mpirun -np' or -l 'mpiexec -n' |
20 |
-L /path Prepend /path to library search path |
21 |
-P /path Prepend /path to python module search path |
22 |
-e Print export statements for environment and exit |
23 |
script.py Your python script |
24 |
arguments... The optional command-line arguments to your python script |
25 |
" |
26 |
|
27 |
if [ "$1" = "--help" ]; then |
28 |
echo "$HELP_TEXT" |
29 |
exit 0 |
30 |
fi |
31 |
|
32 |
#Now we find the location of this script |
33 |
#Note that this location should be absolute but does not need to be unique |
34 |
scriptdir="" |
35 |
CURDIR=`pwd` |
36 |
|
37 |
#Need to match if the name contains / |
38 |
if [[ $0 =~ / ]] |
39 |
then |
40 |
# We are not using the PATH to find the script |
41 |
cd `dirname $0` |
42 |
scriptdir=`pwd` |
43 |
cd $CURDIR |
44 |
else |
45 |
# name does not contain / therefore we are using |
46 |
tscriptdir=`which $0` |
47 |
if [ $? != 0 ] |
48 |
then |
49 |
echo "Error unable to determine script directory. Exiting." |
50 |
exit 1 |
51 |
fi |
52 |
scriptdir=`dirname $tscriptdir` |
53 |
fi |
54 |
|
55 |
cd $scriptdir/.. |
56 |
ESCRIPT_ROOT=`pwd` |
57 |
cd $CURDIR |
58 |
|
59 |
# PYTHON_CMD="@@PYTHON_CMD@@" |
60 |
# For stand-alone builds this will need to be changed |
61 |
PYTHON_CMD=python |
62 |
|
63 |
# Check to see if the python version we were compiled with matches the one of PYTHON_CMD |
64 |
if [ -f $ESCRIPT_ROOT/lib/pyversion ] |
65 |
then |
66 |
compversion=`cat $ESCRIPT_ROOT/lib/pyversion` |
67 |
intversion=`python --version 2>&1` |
68 |
if [ "$compversion" != "$intversion" ] |
69 |
then |
70 |
echo "Python versions do not match. Escript was compiled for "$compversion"." |
71 |
echo "Current version of Python appears to be "$intversion"." |
72 |
exit 1 |
73 |
fi |
74 |
fi |
75 |
|
76 |
#ESCRIPT_ROOT="@@ESCRIPT_ROOT@@" |
77 |
|
78 |
export PATH="@@PATH@@" |
79 |
|
80 |
export LD_LIBRARY_PATH="@@LD_LIBRARY_PATH@@" |
81 |
|
82 |
if [ `uname` == Darwin ] |
83 |
then |
84 |
export DYLD_LIBRARY_PATH="@@LD_LIBRARY_PATH@@" |
85 |
fi |
86 |
|
87 |
export PYTHONPATH="@@PYTHONPATH@@" |
88 |
|
89 |
# Avoid bug in hybrid runs with MPT MPI |
90 |
export MPI_NUM_MEMORY_REGIONS=0 |
91 |
|
92 |
# Try to guess the MPI launcher (mpirun unless in PBS batch job in which case mpiexec) |
93 |
mpi_launcher='mpirun -np' |
94 |
if [ $?PBS_ENVIRONMENT ]; then |
95 |
if [ "X_$PBS_ENVIRONMENT" = "X_PBS_BATCH" ]; then |
96 |
mpi_launcher='mpiexec -n' |
97 |
fi |
98 |
fi |
99 |
|
100 |
PYTHON_MPI="$ESCRIPT_ROOT/lib/pythonMPI" |
101 |
OMP_NUM_THREADS=1 |
102 |
MPI_NUM_PROCS=1 |
103 |
|
104 |
# Parse the command-line options |
105 |
# option e should not be followed by a : |
106 |
while getopts 'L:P:O:M:l:e' option |
107 |
do |
108 |
case "$option" in |
109 |
"L") export LD_LIBRARY_PATH="$OPTARG:$LD_LIBRARY_PATH" |
110 |
;; |
111 |
"P") export PYTHONPATH="$OPTARG:$PYTHONPATH" |
112 |
;; |
113 |
"O") OMP_NUM_THREADS="$OPTARG" |
114 |
;; |
115 |
"M") MPI_NUM_PROCS="$OPTARG" |
116 |
;; |
117 |
"l") mpi_launcher="$OPTARG" |
118 |
;; |
119 |
"e") echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" |
120 |
echo "export PYTHONPATH=$PYTHONPATH" |
121 |
echo "export PATH=$PATH" |
122 |
if [ `uname` == Darwin ] |
123 |
then |
124 |
echo "export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH" |
125 |
fi |
126 |
exit 0 |
127 |
;; |
128 |
?) echo "$HELP_TEXT" |
129 |
exit 1 |
130 |
;; |
131 |
esac |
132 |
done |
133 |
shift `expr $OPTIND - 1` |
134 |
|
135 |
# Must have at least one command-line arg: the python script |
136 |
if [ $# -eq 0 ]; then |
137 |
echo "No python script specified. Starting python interpreter." |
138 |
# echo "Missing python script" |
139 |
# echo "$HELP_TEXT" |
140 |
# exit 1 |
141 |
fi |
142 |
|
143 |
# Using OpenMP? |
144 |
OMP_OPTIONS='' |
145 |
if [ -f "$ESCRIPT_ROOT/lib/Compiled.with.openmp" ]; then |
146 |
# PYTHON_CMD="$mpi_launcher $MPI_NUM_PROCS $PYTHON_MPI" |
147 |
OMP_OPTIONS="env OMP_NUM_THREADS=$OMP_NUM_THREADS" |
148 |
fi |
149 |
|
150 |
# Using MPI? |
151 |
if [ -f "$ESCRIPT_ROOT/lib/Compiled.with.mpi" ]; then |
152 |
PYTHON_CMD="$mpi_launcher $MPI_NUM_PROCS $PYTHON_MPI" |
153 |
else |
154 |
if [ "$MPI_NUM_PROCS" -ne 1 ]; then |
155 |
echo "Escript/Finley was not compiled for MPI so you must use -M 1" |
156 |
exit 1 |
157 |
fi |
158 |
fi |
159 |
|
160 |
set -x |
161 |
$OMP_OPTIONS $PYTHON_CMD "$@" |
162 |
|