xref: /petsc/config/petsc_harness.sh (revision 7e1a0bbe36d2be40a00a95404ece00db4857f70d)
1scriptname=`basename $0`
2rundir=${scriptname%.sh}
3TIMEOUT=60
4
5if test "$PWD"!=`dirname $0`; then
6  cd `dirname $0`
7  abspath_scriptdir=$PWD
8fi
9if test -d "${rundir}" && test -n "${rundir}"; then
10  rm -f ${rundir}/*.tmp ${rundir}/*.err ${rundir}/*.out
11fi
12mkdir -p ${rundir}
13if test -n "${runfiles}"; then
14  for runfile in ${runfiles}; do
15      subdir=`dirname ${runfile}`
16      mkdir -p ${rundir}/${subdir}
17      cp -r ${runfile} ${rundir}/${subdir}
18  done
19fi
20cd ${rundir}
21
22#
23# Method to print out general and script specific options
24#
25print_usage() {
26
27cat >&2 <<EOF
28Usage: $0 [options]
29
30OPTIONS
31  -a <args> ......... Override default arguments
32  -c ................ Cleanup (remove generated files)
33  -C ................ Compile
34  -d ................ Launch in debugger
35  -e <args> ......... Add extra arguments to default
36  -E <args> ......... Add final arguments to default
37  -f ................ force attempt to run test that would otherwise be skipped
38  -h ................ help: print this message
39  -n <integer> ...... Override the number of processors to use
40  -j ................ Pass -j to petscdiff (just use diff)
41  -J <arg> .......... Pass -J to petscdiff (just use diff with arg)
42  -m ................ Update results using petscdiff
43  -M ................ Update alt files using petscdiff
44  -o <arg> .......... Output format: 'interactive', 'err_only'
45  -p ................ Print command:  Print first command and exit
46  -t ................ Override the default timeout (default=$TIMEOUT sec)
47  -U ................ run cUda-memcheck
48  -V ................ run Valgrind
49  -v ................ Verbose: Print commands
50EOF
51
52  if declare -f extrausage > /dev/null; then extrausage; fi
53  exit $1
54}
55###
56##  Arguments for overriding things
57#
58output_fmt="interactive"
59verbose=false
60cleanup=false
61compile=false
62debugger=false
63printcmd=false
64mpiexec_function=false
65force=false
66diff_flags=""
67while getopts "a:cCde:E:fhjJ:mMn:o:pt:UvV" arg
68do
69  case $arg in
70    a ) args="$OPTARG"       ;;
71    c ) cleanup=true         ;;
72    C ) compile=true         ;;
73    d ) debugger=true        ;;
74    e ) extra_args="$OPTARG" ;;
75    E ) final_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="$extra_args $args"
110fi
111if test -n "$final_args"; then
112  args="$args $final_args"
113fi
114if $debugger; then
115  args="-start_in_debugger $args"
116fi
117if test -n "$filter"; then
118  diff_flags=$diff_flags" -F \$'$filter'"
119fi
120if test -n "$filter_output"; then
121  diff_flags=$diff_flags" -f \$'$filter_output'"
122fi
123
124# Init
125success=0; failed=0; failures=""; rmfiles=""
126total=0
127todo=-1; skip=-1
128job_level=0
129
130if $compile; then
131   curexec=`basename ${exec}`
132   fullexec=${abspath_scriptdir}/${curexec}
133   maketarget=`echo ${fullexec} | sed "s#${petsc_dir}/*##"`
134   (cd $petsc_dir && make -f gmakefile.test ${maketarget})
135fi
136
137###
138##   Rest of code is functions
139#
140function petsc_report_tapoutput() {
141  notornot=$1
142  test_label=$2
143  comment=$3
144  if test -n "$comment"; then
145    comment=" # ${comment}"
146  fi
147
148  tap_message="${notornot} ok ${test_label}${comment}"
149
150  # Log messages
151  printf "${tap_message}\n" >> ${testlogtapfile}
152
153  if test ${output_fmt} == "err_only"; then
154     if test -n "${notornot}"; then
155        printf "${tap_message}\n" | tee -a ${testlogerrfile}
156     fi
157  else
158     printf "${tap_message}\n"
159  fi
160}
161
162function printcmd() {
163  # Print command that can be run from PETSC_DIR
164  cmd="$1"
165  basedir=`dirname ${PWD} | sed "s#${petsc_dir}/##"`
166  modcmd=`echo ${cmd} | sed -e "s#\.\.#${basedir}#" | sed s#\>.*## | sed s#\%#\%\%#`
167  if $mpiexec_function; then
168     # Have to expand valgrind/cudamemcheck
169     modcmd=`eval "$modcmd"`
170  fi
171  printf "${modcmd}\n"
172  exit
173}
174
175function petsc_testrun() {
176  # First arg = Basic command
177  # Second arg = stdout file
178  # Third arg = stderr file
179  # Fourth arg = label for reporting
180  rmfiles="${rmfiles} $2 $3"
181  tlabel=$4
182  error=$5
183  cmd="$1 > $2 2> $3"
184  if test -n "$error"; then
185    cmd="$1 1> $2  2>&1"
186  fi
187  echo "$cmd" > ${tlabel}.sh; chmod 755 ${tlabel}.sh
188  if $printcmd; then
189     printcmd "$cmd"
190  fi
191
192  eval "{ time -p $cmd ; } 2>> timing.out"
193  cmd_res=$?
194  # If testing the error output then we don't test the error code itself
195  if test -n "$error"; then
196     cmd_res=0
197  fi
198  #  If it is a lack of GPU resources or MPI failure (Intel) then try once more
199  #  See: src/sys/error/err.c
200  #  Error #134 added to handle problems with the Radeon card for hip testing
201  if [ $cmd_res -eq 96 -o $cmd_res -eq 97 -o $cmd_res -eq 98 -o $cmd_res -eq 134 ]; then
202    printf "# retrying ${tlabel}\n" | tee -a ${testlogerrfile}
203    sleep 3
204    eval "{ time -p $cmd ; } 2>> timing.out"
205    cmd_res=$?
206  fi
207  touch "$2" "$3"
208  # It appears current MPICH and Open MPI just shut down the job execution and do not return an error code to the executable
209  # ETIMEDOUT=110 was used by Open MPI 3.0.  MPICH used 255
210  # Earlier Open MPI versions returned 1 and the error string
211  # Here we only grep for error strings in output
212  #if [ $cmd_res -eq 110 -o $cmd_res -eq 255 ] || \
213  if \
214        grep -F -q -s 'I_MPI_JOB_TIMEOUT' "$2" "$3" || \
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