xref: /petsc/config/petsc_harness.sh (revision b2ccae6bdc8edea944f1c160ca3b2eb32c69ecb2)
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  # Here we only grep for error strings in output
215  #if [ $cmd_res -eq 110 -o $cmd_res -eq 255 ] || \
216  if \
217        grep -F -q -s 'I_MPI_JOB_TIMEOUT' "$2" "$3" || \
218        grep -F -q -s 'APPLICATION TIMED OUT' "$2" "$3" || \
219        grep -F -q -s MPIEXEC_TIMEOUT "$2" "$3" || \
220        grep -F -q -s 'APPLICATION TERMINATED WITH THE EXIT STRING: job ending due to timeout' "$2" "$3" || \
221        grep -q -s "Timeout after [0-9]* seconds. Terminating job" "$2" "$3"; then
222    timed_out=1
223    # If timed out, then ensure non-zero error code
224    if [ $cmd_res -eq 0 ]; then
225      cmd_res=1
226    fi
227  fi
228
229  # Report errors
230  comment=""
231  if test $cmd_res == 0; then
232     if "${verbose}"; then
233        comment="${cmd}"
234     fi
235    petsc_report_tapoutput "" "$tlabel" "$comment"
236    let success=$success+1
237  else
238    if [ -n "$timed_out" ]; then
239      comment="Exceeded timeout limit of $MPIEXEC_TIMEOUT s"
240    else
241      comment="Error code: ${cmd_res}"
242    fi
243    petsc_report_tapoutput "not" "$tlabel" "$comment"
244
245    # Report errors in detail
246    if [ -z "$timed_out" ]; then
247      # We've had tests fail but stderr->stdout, as well as having
248      # mpi_abort go to stderr which throws this test off.  Show both
249      # with stdout first
250      awk '{print "#\t" $0}' < $2 | tee -a ${testlogerrfile}
251      # if statement is for diff tests
252      if test "$2" != "$3"; then
253        awk '{print "#\t" $0}' < $3 | tee -a ${testlogerrfile}
254      fi
255    fi
256    let failed=$failed+1
257    failures="$failures $tlabel"
258  fi
259  let total=$success+$failed
260  return $cmd_res
261}
262
263function petsc_testend() {
264  logfile=$1/counts/${label}.counts
265  logdir=`dirname $logfile`
266  if ! test -d "$logdir"; then
267    mkdir -p $logdir
268  fi
269  if ! test -e "$logfile"; then
270    touch $logfile
271  fi
272  printf "total $total\n" > $logfile
273  printf "success $success\n" >> $logfile
274  printf "failed $failed\n" >> $logfile
275  printf "failures $failures\n" >> $logfile
276  if test ${todo} -gt 0; then
277    printf "todo $todo\n" >> $logfile
278  fi
279  if test ${skip} -gt 0; then
280    printf "skip $skip\n" >> $logfile
281  fi
282  ENDTIME=`date +%s`
283  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}'`
284  printf "time $timing\n" >> $logfile
285  if $cleanup; then
286    echo "Cleaning up"
287    /bin/rm -f $rmfiles
288  fi
289}
290
291function petsc_mpiexec_cudamemcheck() {
292  # loops over the argument list to find the call to the test executable and insert the
293  # cuda memcheck command before it.
294  # first check if compute-sanitizer exists, since cuda-memcheck is deprecated from CUDA
295  # 11-ish onwards
296  if command -v compute-sanitizer &> /dev/null; then
297    memcheck_cmd="${PETSC_CUDAMEMCHECK_COMMAND:-compute-sanitizer}"
298    declare -a default_args_to_check=('--target-processes all' '--track-stream-ordered-races all')
299  else
300    memcheck_cmd="${PETSC_CUDAMEMCHECK_COMMAND:-cuda-memcheck}"
301    declare -a default_args_to_check=('--flush-to-disk yes')
302  fi
303  if [[ -z ${PETSC_CUDAMEMCHECK_ARGS} ]]; then
304    # if user has not set the memcheck args themselves loop over the predefined default
305    # arguments and check if they can be used
306    memcheck_args='--leak-check full --report-api-errors no '
307    for option in "${default_args_to_check[@]}"; do
308      ${memcheck_cmd} ${memcheck_args} ${option} &> /dev/null
309      if [ $? -eq 0 ]; then
310        memcheck_args+="${option} "
311      fi
312    done
313  else
314    memcheck_args="${PETSC_CUDAMEMCHECK_ARGS}"
315  fi
316  pre_args=()
317  # regex to detect where the test lives in the command line. This
318  # marks the end of the options to mpiexec, and hence where we should insert the
319  # cuda-memcheck command
320  re="${executable}"
321  for i in "$@"; do
322    # first occurrence of the presence of petsc_arch is the executable,
323    # except when we install MPI ourselves
324    if [[ $i =~ ${re} ]]; then
325      # found it, put cuda memcheck command in
326      pre_args+=("${memcheck_cmd} ${memcheck_args}")
327      break
328    fi
329    pre_args+=("$i")
330    shift
331  done
332  # run command, but filter out
333  # ===== CUDA-MEMCHECK or ==== COMPUTE-SANITIZER
334  # and
335  # ===== ERROR SUMMARY: 0 errors
336  if ${printcmd}; then
337    echo ${pre_args[@]} "$@"
338  else
339    ${pre_args[@]} "$@" \
340      | grep -v 'CUDA-MEMCHECK' \
341      | grep -v 'COMPUTE-SANITIZER' \
342      | grep -v 'LEAK SUMMARY: 0 bytes leaked in 0 allocations' \
343      | grep -v 'ERROR SUMMARY: 0 errors' || [[ $? == 1 ]]
344  fi
345  # last or is needed to suppress grep exiting with error code 1 if it doesn't find a
346  # match
347}
348
349function petsc_mpiexec_valgrind() {
350  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"
351  pre_args=()
352  re="${executable}"
353  for i in "$@"; do
354    if [[ $i =~ ${re} ]]; then
355      pre_args+=("${valgrind_cmd}")
356      break
357    fi
358    pre_args+=("$i")
359    shift
360  done
361  if ${printcmd}; then
362    echo ${pre_args[@]} "$@"
363  else
364    ${pre_args[@]} "$@"
365  fi
366}
367export LC_ALL=C
368