xref: /petsc/config/petsc_harness.sh (revision 3f7878b3ed00daeee6ae8d2839a0d41a4d3e79ca)
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  -E <args> ......... Add final arguments to default
39  -f ................ force attempt to run test that would otherwise be skipped
40  -h ................ help: print this message
41  -n <integer> ...... Override the number of processors to use
42  -j ................ Pass -j to petscdiff (just use diff)
43  -J <arg> .......... Pass -J to petscdiff (just use diff with arg)
44  -m ................ Update results using petscdiff
45  -M ................ Update alt files using petscdiff
46  -o <arg> .......... Output format: 'interactive', 'err_only'
47  -p ................ Print command:  Print first command and exit
48  -t ................ Override the default timeout (default=$TIMEOUT sec)
49  -U ................ run cUda-memcheck
50  -V ................ run Valgrind
51  -v ................ Verbose: Print commands
52EOF
53
54  if declare -f extrausage > /dev/null; then extrausage; fi
55  exit $1
56}
57###
58##  Arguments for overriding things
59#
60output_fmt="interactive"
61verbose=false
62cleanup=false
63compile=false
64debugger=false
65printcmd=false
66mpiexec_function=false
67force=false
68diff_flags=""
69while getopts "a:cCde:E:fhjJ:mMn:o:pt:UvV" arg
70do
71  case $arg in
72    a ) args="$OPTARG"       ;;
73    c ) cleanup=true         ;;
74    C ) compile=true         ;;
75    d ) debugger=true        ;;
76    e ) extra_args="$OPTARG" ;;
77    E ) final_args="$OPTARG" ;;
78    f ) force=true           ;;
79    h ) print_usage; exit    ;;
80    n ) nsize="$OPTARG"      ;;
81    j ) diff_flags=$diff_flags" -j"      ;;
82    J ) diff_flags=$diff_flags" -J $OPTARG" ;;
83    m ) diff_flags=$diff_flags" -m"      ;;
84    M ) diff_flags=$diff_flags" -M"      ;;
85    o ) output_fmt=$OPTARG   ;;
86    p ) printcmd=true        ;;
87    t ) TIMEOUT=$OPTARG      ;;
88    U ) mpiexec="petsc_mpiexec_cudamemcheck $mpiexec"
89        mpiexec_function=true
90        ;;
91    V ) mpiexec="petsc_mpiexec_valgrind $mpiexec"
92        mpiexec_function=true
93        ;;
94    v ) verbose=true         ;;
95    *)  # To take care of any extra args
96      if test -n "$OPTARG"; then
97        eval $arg=\"$OPTARG\"
98      else
99        eval $arg=found
100      fi
101      ;;
102  esac
103done
104shift $(( $OPTIND - 1 ))
105
106# Individual tests can extend the default
107export MPIEXEC_TIMEOUT=$((TIMEOUT*timeoutfactor))
108STARTTIME=`date +%s`
109
110if test -n "$extra_args"; then
111  args="$extra_args $args"
112fi
113if test -n "$final_args"; then
114  args="$args $final_args"
115fi
116if $debugger; then
117  args="-start_in_debugger $args"
118fi
119if test -n "$filter"; then
120  diff_flags=$diff_flags" -F \$'$filter'"
121fi
122if test -n "$filter_output"; then
123  diff_flags=$diff_flags" -f \$'$filter_output'"
124fi
125
126
127# Init
128success=0; failed=0; failures=""; rmfiles=""
129total=0
130todo=-1; skip=-1
131job_level=0
132
133if $compile; then
134   curexec=`basename ${exec}`
135   fullexec=${abspath_scriptdir}/${curexec}
136   maketarget=`echo ${fullexec} | sed "s#${petsc_dir}/*##"`
137   (cd $petsc_dir && make -f gmakefile.test ${maketarget})
138fi
139
140###
141##   Rest of code is functions
142#
143function petsc_report_tapoutput() {
144  notornot=$1
145  test_label=$2
146  comment=$3
147  if test -n "$comment"; then
148    comment=" # ${comment}"
149  fi
150
151  tap_message="${notornot} ok ${test_label}${comment}"
152
153  # Log messages
154  printf "${tap_message}\n" >> ${testlogtapfile}
155
156  if test ${output_fmt} == "err_only"; then
157     if test -n "${notornot}"; then
158        printf "${tap_message}\n" | tee -a ${testlogerrfile}
159     fi
160  else
161     printf "${tap_message}\n"
162  fi
163}
164
165function printcmd() {
166  # Print command that can be run from PETSC_DIR
167  cmd="$1"
168  basedir=`dirname ${PWD} | sed "s#${petsc_dir}/##"`
169  modcmd=`echo ${cmd} | sed -e "s#\.\.#${basedir}#" | sed s#\>.*## | sed s#\%#\%\%#`
170  if $mpiexec_function; then
171     # Have to expand valgrind/cudamemcheck
172     modcmd=`eval "$modcmd"`
173  fi
174  printf "${modcmd}\n"
175  exit
176}
177
178function petsc_testrun() {
179  # First arg = Basic command
180  # Second arg = stdout file
181  # Third arg = stderr file
182  # Fourth arg = label for reporting
183  rmfiles="${rmfiles} $2 $3"
184  tlabel=$4
185  error=$5
186  cmd="$1 > $2 2> $3"
187  if test -n "$error"; then
188    cmd="$1 1> $2  2>&1"
189  fi
190  echo "$cmd" > ${tlabel}.sh; chmod 755 ${tlabel}.sh
191  if $printcmd; then
192     printcmd "$cmd"
193  fi
194
195  eval "{ time -p $cmd ; } 2>> timing.out"
196  cmd_res=$?
197  # If testing the error output then we don't test the error code itself
198  if test -n "$error"; then
199     cmd_res=0
200  fi
201  #  If it is a lack of GPU resources or MPI failure (Intel) then try once more
202  #  See: src/sys/error/err.c
203  #  Error #134 added to handle problems with the Radeon card for hip testing
204  if [ $cmd_res -eq 96 -o $cmd_res -eq 97 -o $cmd_res -eq 98 -o $cmd_res -eq 134 ]; then
205    printf "# retrying ${tlabel}\n" | tee -a ${testlogerrfile}
206    sleep 3
207    eval "{ time -p $cmd ; } 2>> timing.out"
208    cmd_res=$?
209  fi
210  touch "$2" "$3"
211  # It appears current MPICH and Open MPI just shut down the job execution and do not return an error code to the executable
212  # ETIMEDOUT=110 was used by Open MPI 3.0.  MPICH used 255
213  # Earlier Open MPI versions returned 1 and the error string
214  if [ $cmd_res -eq 110 -o $cmd_res -eq 255 ] || \
215        grep -F -q -s 'APPLICATION TIMED OUT' "$2" "$3" || \
216        grep -F -q -s MPIEXEC_TIMEOUT "$2" "$3" || \
217        grep -F -q -s 'APPLICATION TERMINATED WITH THE EXIT STRING: job ending due to timeout' "$2" "$3" || \
218        grep -q -s "Timeout after [0-9]* seconds. Terminating job" "$2" "$3"; then
219    timed_out=1
220    # If timed out, then ensure non-zero error code
221    if [ $cmd_res -eq 0 ]; then
222      cmd_res=1
223    fi
224  fi
225
226  # Report errors
227  comment=""
228  if test $cmd_res == 0; then
229     if "${verbose}"; then
230        comment="${cmd}"
231     fi
232    petsc_report_tapoutput "" "$tlabel" "$comment"
233    let success=$success+1
234  else
235    if [ -n "$timed_out" ]; then
236      comment="Exceeded timeout limit of $MPIEXEC_TIMEOUT s"
237    else
238      comment="Error code: ${cmd_res}"
239    fi
240    petsc_report_tapoutput "not" "$tlabel" "$comment"
241
242    # Report errors in detail
243    if [ -z "$timed_out" ]; then
244      # We've had tests fail but stderr->stdout, as well as having
245      # mpi_abort go to stderr which throws this test off.  Show both
246      # with stdout first
247      awk '{print "#\t" $0}' < $2 | tee -a ${testlogerrfile}
248      # if statement is for diff tests
249      if test "$2" != "$3"; then
250        awk '{print "#\t" $0}' < $3 | tee -a ${testlogerrfile}
251      fi
252    fi
253    let failed=$failed+1
254    failures="$failures $tlabel"
255  fi
256  let total=$success+$failed
257  return $cmd_res
258}
259
260function petsc_testend() {
261  logfile=$1/counts/${label}.counts
262  logdir=`dirname $logfile`
263  if ! test -d "$logdir"; then
264    mkdir -p $logdir
265  fi
266  if ! test -e "$logfile"; then
267    touch $logfile
268  fi
269  printf "total $total\n" > $logfile
270  printf "success $success\n" >> $logfile
271  printf "failed $failed\n" >> $logfile
272  printf "failures $failures\n" >> $logfile
273  if test ${todo} -gt 0; then
274    printf "todo $todo\n" >> $logfile
275  fi
276  if test ${skip} -gt 0; then
277    printf "skip $skip\n" >> $logfile
278  fi
279  ENDTIME=`date +%s`
280  timing=`touch timing.out && grep -E '(user|sys)' timing.out | awk '{if( sum1 == "" || $2 > sum1 ) { sum1=sprintf("%.2f",$2) } ; sum2 += sprintf("%.2f",$2)} END {printf "%.2f %.2f\n",sum1,sum2}'`
281  printf "time $timing\n" >> $logfile
282  if $cleanup; then
283    echo "Cleaning up"
284    /bin/rm -f $rmfiles
285  fi
286}
287
288function petsc_mpiexec_cudamemcheck() {
289  # loops over the argument list to find the call to the test executable and insert the
290  # cuda memcheck command before it.
291  # first check if compute-sanitizer exists, since cuda-memcheck is deprecated from CUDA
292  # 11-ish onwards
293  if command -v compute-sanitizer &> /dev/null; then
294    memcheck_cmd="${PETSC_CUDAMEMCHECK_COMMAND:-compute-sanitizer}"
295    declare -a default_args_to_check=('--target-processes all' '--track-stream-ordered-races all')
296  else
297    memcheck_cmd="${PETSC_CUDAMEMCHECK_COMMAND:-cuda-memcheck}"
298    declare -a default_args_to_check=('--flush-to-disk yes')
299  fi
300  if [[ -z ${PETSC_CUDAMEMCHECK_ARGS} ]]; then
301    # if user has not set the memcheck args themselves loop over the predefined default
302    # arguments and check if they can be used
303    memcheck_args='--leak-check full --report-api-errors no '
304    for option in "${default_args_to_check[@]}"; do
305      ${memcheck_cmd} ${memcheck_args} ${option} &> /dev/null
306      if [ $? -eq 0 ]; then
307        memcheck_args+="${option} "
308      fi
309    done
310  else
311    memcheck_args="${PETSC_CUDAMEMCHECK_ARGS}"
312  fi
313  pre_args=()
314  # regex to detect where the test lives in the command line. This
315  # marks the end of the options to mpiexec, and hence where we should insert the
316  # cuda-memcheck command
317  re="${executable}"
318  for i in "$@"; do
319    # first occurrence of the presence of petsc_arch is the executable,
320    # except when we install MPI ourselves
321    if [[ $i =~ ${re} ]]; then
322      # found it, put cuda memcheck command in
323      pre_args+=("${memcheck_cmd} ${memcheck_args}")
324      break
325    fi
326    pre_args+=("$i")
327    shift
328  done
329  # run command, but filter out
330  # ===== CUDA-MEMCHECK or ==== COMPUTE-SANITIZER
331  # and
332  # ===== ERROR SUMMARY: 0 errors
333  if ${printcmd}; then
334    echo ${pre_args[@]} "$@"
335  else
336    ${pre_args[@]} "$@" \
337      | grep -v 'CUDA-MEMCHECK' \
338      | grep -v 'COMPUTE-SANITIZER' \
339      | grep -v 'LEAK SUMMARY: 0 bytes leaked in 0 allocations' \
340      | grep -v 'ERROR SUMMARY: 0 errors' || [[ $? == 1 ]]
341  fi
342  # last or is needed to suppress grep exiting with error code 1 if it doesn't find a
343  # match
344}
345
346function petsc_mpiexec_valgrind() {
347  valgrind_cmd="valgrind -q --tool=memcheck --leak-check=yes --num-callers=20 --track-origins=yes --keep-debuginfo=yes --suppressions=${PETSC_DIR}/share/petsc/suppressions/valgrind --error-exitcode=10"
348  pre_args=()
349  re="${executable}"
350  for i in "$@"; do
351    if [[ $i =~ ${re} ]]; then
352      pre_args+=("${valgrind_cmd}")
353      break
354    fi
355    pre_args+=("$i")
356    shift
357  done
358  if ${printcmd}; then
359    echo ${pre_args[@]} "$@"
360  else
361    ${pre_args[@]} "$@"
362  fi
363}
364export LC_ALL=C
365