xref: /petsc/config/petsc_harness.sh (revision f5ff9c666c0d37e8a7ec3aa2f8e2aa9e44449bdb)
1
2
3scriptname=`basename $0`
4rundir=${scriptname%.sh}
5TIMEOUT=60
6
7if test "$PWD"!=`dirname $0`; then
8  cd `dirname $0`
9  abspath_scriptdir=$PWD
10fi
11if test -d "${rundir}" && test -n "${rundir}"; then
12  rm -f ${rundir}/*.tmp ${rundir}/*.err ${rundir}/*.out
13fi
14mkdir -p ${rundir}
15if test -n "${runfiles}"; then
16  for runfile in ${runfiles}; do
17      subdir=`dirname ${runfile}`
18      mkdir -p ${rundir}/${subdir}
19      cp -r ${runfile} ${rundir}/${subdir}
20  done
21fi
22cd ${rundir}
23
24#
25# Method to print out general and script specific options
26#
27print_usage() {
28
29cat >&2 <<EOF
30Usage: $0 [options]
31
32OPTIONS
33  -a <args> ......... Override default arguments
34  -c ................ Cleanup (remove generated files)
35  -C ................ Compile
36  -d ................ Launch in debugger
37  -e <args> ......... Add extra arguments to default
38  -f ................ force attempt to run test that would otherwise be skipped
39  -h ................ help: print this message
40  -n <integer> ...... Override the number of processors to use
41  -j ................ Pass -j to petscdiff (just use diff)
42  -J <arg> .......... Pass -J to petscdiff (just use diff with arg)
43  -m ................ Update results using petscdiff
44  -M ................ Update alt files using petscdiff
45  -o <arg> .......... Output format: 'interactive', 'err_only'
46  -p ................ Print command:  Print first command and exit
47  -t ................ Override the default timeout (default=$TIMEOUT sec)
48  -U ................ run cUda-memcheck
49  -V ................ run Valgrind
50  -v ................ Verbose: Print commands
51EOF
52
53  if declare -f extrausage > /dev/null; then extrausage; fi
54  exit $1
55}
56###
57##  Arguments for overriding things
58#
59output_fmt="interactive"
60verbose=false
61cleanup=false
62compile=false
63debugger=false
64printcmd=false
65mpiexec_function=false
66force=false
67diff_flags=""
68while getopts "a:cCde:fhjJ:mMn:o:pt:UvV" arg
69do
70  case $arg in
71    a ) args="$OPTARG"       ;;
72    c ) cleanup=true         ;;
73    C ) compile=true         ;;
74    d ) debugger=true        ;;
75    e ) extra_args="$OPTARG" ;;
76    f ) force=true           ;;
77    h ) print_usage; exit    ;;
78    n ) nsize="$OPTARG"      ;;
79    j ) diff_flags=$diff_flags" -j"      ;;
80    J ) diff_flags=$diff_flags" -J $OPTARG" ;;
81    m ) diff_flags=$diff_flags" -m"      ;;
82    M ) diff_flags=$diff_flags" -M"      ;;
83    o ) output_fmt=$OPTARG   ;;
84    p ) printcmd=true        ;;
85    t ) TIMEOUT=$OPTARG      ;;
86    U ) mpiexec="petsc_mpiexec_cudamemcheck $mpiexec"
87        mpiexec_function=true
88        ;;
89    V ) mpiexec="petsc_mpiexec_valgrind $mpiexec"
90        mpiexec_function=true
91        ;;
92    v ) verbose=true         ;;
93    *)  # To take care of any extra args
94      if test -n "$OPTARG"; then
95        eval $arg=\"$OPTARG\"
96      else
97        eval $arg=found
98      fi
99      ;;
100  esac
101done
102shift $(( $OPTIND - 1 ))
103
104# Individual tests can extend the default
105export MPIEXEC_TIMEOUT=$((TIMEOUT*timeoutfactor))
106STARTTIME=`date +%s`
107
108if test -n "$extra_args"; then
109  args="$args $extra_args"
110fi
111if $debugger; then
112  args="-start_in_debugger $args"
113fi
114if test -n "$filter"; then
115  diff_flags=$diff_flags" -F \$'$filter'"
116fi
117if test -n "$filter_output"; then
118  diff_flags=$diff_flags" -f \$'$filter_output'"
119fi
120
121
122# Init
123success=0; failed=0; failures=""; rmfiles=""
124total=0
125todo=-1; skip=-1
126job_level=0
127
128if $compile; then
129   curexec=`basename ${exec}`
130   fullexec=${abspath_scriptdir}/${curexec}
131   maketarget=`echo ${fullexec} | sed "s#${petsc_dir}/*##"`
132   (cd $petsc_dir && make -f gmakefile.test ${maketarget})
133fi
134
135###
136##   Rest of code is functions
137#
138function petsc_report_tapoutput() {
139  notornot=$1
140  test_label=$2
141  comment=$3
142  if test -n "$comment"; then
143    comment=" # ${comment}"
144  fi
145
146  tap_message="${notornot} ok ${test_label}${comment}"
147
148  # Log messages
149  printf "${tap_message}\n" >> ${testlogtapfile}
150
151  if test ${output_fmt} == "err_only"; then
152     if test -n "${notornot}"; then
153        printf "${tap_message}\n" | tee -a ${testlogerrfile}
154     fi
155  else
156     printf "${tap_message}\n"
157  fi
158}
159
160function printcmd() {
161  # Print command that can be run from PETSC_DIR
162  cmd="$1"
163  basedir=`dirname ${PWD} | sed "s#${petsc_dir}/##"`
164  modcmd=`echo ${cmd} | sed -e "s#\.\.#${basedir}#" | sed s#\>.*##`
165  if $mpiexec_function; then
166     # Have to expand valgrind/cudamemchk
167     modcmd=`eval "$modcmd"`
168  fi
169  printf "${modcmd}\n"
170  exit
171}
172
173function petsc_testrun() {
174  # First arg = Basic command
175  # Second arg = stdout file
176  # Third arg = stderr file
177  # Fourth arg = label for reporting
178  rmfiles="${rmfiles} $2 $3"
179  tlabel=$4
180  error=$5
181  cmd="$1 > $2 2> $3"
182  if test -n "$error"; then
183    cmd="$1 2>&1 | cat > $2"
184  fi
185  echo "$cmd" > ${tlabel}.sh; chmod 755 ${tlabel}.sh
186  if $printcmd; then
187     printcmd "$cmd"
188  fi
189
190  eval "{ time -p $cmd ; } 2>> timing.out"
191  cmd_res=$?
192  #  If it is a lack of GPU resources or MPI failure (Intel) then try once more
193  #  See: src/sys/error/err.c
194  if [ $cmd_res -eq 96 -o $cmd_res -eq 97 -o $cmd_res -eq 98 ]; then
195    printf "# retrying ${tlabel}\n" | tee -a ${testlogerrfile}
196    sleep 3
197    eval "{ time -p $cmd ; } 2>> timing.out"
198    cmd_res=$?
199  fi
200  touch "$2" "$3"
201  # It appears current MPICH and OpenMPI just shut down the job executation and do not return an error code to the executable
202  # ETIMEDOUT=110 was used by OpenMPI 3.0.  MPICH used 255
203  # Earlier OpenMPI versions returned 1 and the error string
204  if [ $cmd_res -eq 110 -o $cmd_res -eq 255 ] || \
205        fgrep -q -s 'APPLICATION TIMED OUT' "$2" "$3" || \
206        fgrep -q -s MPIEXEC_TIMEOUT "$2" "$3" || \
207        fgrep -q -s 'APPLICATION TERMINATED WITH THE EXIT STRING: job ending due to timeout' "$2" "$3" || \
208        grep -q -s "Timeout after [0-9]* seconds. Terminating job" "$2" "$3"; then
209    timed_out=1
210    # If timed out, then ensure non-zero error code
211    if [ $cmd_res -eq 0 ]; then
212      cmd_res=1
213    fi
214  fi
215
216  # Report errors
217  comment=""
218  if test $cmd_res == 0; then
219     if "${verbose}"; then
220        comment="${cmd}"
221     fi
222    petsc_report_tapoutput "" "$tlabel" "$comment"
223    let success=$success+1
224  else
225    if [ -n "$timed_out" ]; then
226      comment="Exceeded timeout limit of $MPIEXEC_TIMEOUT s"
227    else
228      comment="Error code: ${cmd_res}"
229    fi
230    petsc_report_tapoutput "not" "$tlabel" "$comment"
231
232    # Report errors in detail
233    if [ -z "$timed_out" ]; then
234      # We've had tests fail but stderr->stdout, as well as having
235      # mpi_abort go to stderr which throws this test off.  Show both
236      # with stdout first
237      awk '{print "#\t" $0}' < $2 | tee -a ${testlogerrfile}
238      # if statement is for diff tests
239      if test "$2" != "$3"; then
240        awk '{print "#\t" $0}' < $3 | tee -a ${testlogerrfile}
241      fi
242    fi
243    let failed=$failed+1
244    failures="$failures $tlabel"
245  fi
246  let total=$success+$failed
247  return $cmd_res
248}
249
250function petsc_testend() {
251  logfile=$1/counts/${label}.counts
252  logdir=`dirname $logfile`
253  if ! test -d "$logdir"; then
254    mkdir -p $logdir
255  fi
256  if ! test -e "$logfile"; then
257    touch $logfile
258  fi
259  printf "total $total\n" > $logfile
260  printf "success $success\n" >> $logfile
261  printf "failed $failed\n" >> $logfile
262  printf "failures $failures\n" >> $logfile
263  if test ${todo} -gt 0; then
264    printf "todo $todo\n" >> $logfile
265  fi
266  if test ${skip} -gt 0; then
267    printf "skip $skip\n" >> $logfile
268  fi
269  ENDTIME=`date +%s`
270  timing=`touch timing.out && egrep '(user|sys)' timing.out | awk '{if( sum1 == "" || $2 > sum1 ) { sum1=sprintf("%.2f",$2) } ; sum2 += sprintf("%.2f",$2)} END {printf "%.2f %.2f\n",sum1,sum2}'`
271  printf "time $timing\n" >> $logfile
272  if $cleanup; then
273    echo "Cleaning up"
274    /bin/rm -f $rmfiles
275  fi
276}
277
278function petsc_mpiexec_cudamemcheck() {
279  _mpiexec=$1;shift
280  npopt=$1;shift
281  np=$1;shift
282
283  cudamemchk="cuda-memcheck"
284
285  $_mpiexec $npopt $np $cudamemchk $*
286}
287
288function petsc_mpiexec_valgrind() {
289  # some systems set $1 to be the function name
290  if [[ $1 == 'petsc_mpiexec_valgrind' ]]; then
291    shift
292  fi
293  _mpiexec=$1;shift
294  npopt=$1;shift
295  np=$1;shift
296
297  valgrind="valgrind -q --tool=memcheck --leak-check=yes --num-callers=20 --track-origins=yes --suppressions=$petsc_bindir/maint/petsc-val.supp --error-exitcode=10"
298
299  if $printcmd; then
300     echo $_mpiexec $npopt $np $valgrind "$@"
301  else
302     $_mpiexec $npopt $np $valgrind "$@"
303  fi
304}
305export LC_ALL=C
306