/usr/lib/R/site-library/BiocParallel/doc/Errors_Logs_And_Debugging.Rnw is in r-bioc-biocparallel 1.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | %\VignetteIndexEntry{Errors, Logs and Debugging}
%\VignetteKeywords{parallel, Infrastructure}
%\VignettePackage{BiocParallel}
%\VignetteEngine{knitr::knitr}
\documentclass{article}
<<style, eval=TRUE, echo=FALSE, results="asis">>=
BiocStyle::latex()
@
\newcommand{\BiocParallel}{\Biocpkg{BiocParallel}}
\title{Errors, Logs and Debugging in \BiocParallel}
\author{Valerie Obenchain and Martin Morgan}
\date{Edited: May 13, 2015; Compiled: \today}
\begin{document}
\maketitle
\tableofcontents
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
This vignette is part of the \BiocParallel{} package and focuses on error
handling and logging. A section at the end demonstrates how the two can be used
together as part of an effective debugging routine.
\BiocParallel{} provides a unified interface to the parallel infrastructure in
several packages including \CRANpkg{snow}, \CRANpkg{parallel},
\CRANpkg{BatchJobs} and \CRANpkg{foreach}. When implementing error handling in
\BiocParallel{} the primary goals were to enable the return of partial results
when an error is thrown (vs just the error) and to establish logging on the
workers. In cases where error handling existed, such as \CRANpkg{BatchJobs} and
\CRANpkg{foreach}, those behaviors were preserved. Clusters created with
\CRANpkg{snow} and \CRANpkg{parallel} now have flexible error handling and
logging available through \Rcode{SnowParam} and \Rcode{MulticoreParam} objects.
In this document the term ``job'' is used to describe a single call to a
bp*apply function (e.g., the \Rcode{X} in \Rcode{bplapply}). A ``job'' consists
of one or more ``tasks'', where each ``task'' is run separately on a worker.
The \Rpackage{BiocParallel} package is available at bioconductor.org
and can be downloaded via \Rcode{biocLite}:
<<biocLite, eval=FALSE>>=
source("http://bioconductor.org/biocLite.R")
biocLite("BiocParallel")
@
Load the package:
<<load>>=
library(BiocParallel)
@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Error Handling}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Catching errors}
By default, \BiocParallel{} attempts all computations and returns any warnings
and errors along with successful results. The \Rcode{stop.on.error} field
controls if the job is terminated as soon as one task throws an error. This is
useful when debugging or when running large jobs (many tasks) and you want to
be notified of an error before all runs complete.
\Rcode{stop.on.error} is \Rcode{FALSE} by default.
<<errors_constructor>>=
param <- SnowParam()
param
@
The field can be set when constructing the param or modified with the
\Rcode{bpstopOnError} accessor.
<<errors_stopOnError>>=
param <- SnowParam(2, stop.on.error = TRUE)
param
bpstopOnError(param) <- FALSE
@
In this example \Rcode{X} is length 6. By default, the elements of \Rcode{X}
are divided as evenly as possible over the number of workers and run in chunks.
To more clearly demonstrate the affect of \Rcode{stop.on.error} the number of
tasks is set equal to the length of \Rcode{X}. This forces each element of
\Rcode{X} to be executed separately (6 tasks) vs chunked.
<<errors_6tasksA_stopOnError>>=
X <- list(1, 2, "3", 4, 5, 6)
param <- SnowParam(3, tasks = length(X), stop.on.error = TRUE)
@
The output list contains results for tasks 1 and 2 and an error for task 3.
Tasks 4, 5, and 6 are not attempted.
\begin{verbatim}
bplapply(X, sqrt, BPPARAM = param)
## starting worker for localhost:11441
## starting worker for localhost:11441
## starting worker for localhost:11441
## ERROR [2015-08-21 14:08:52] non-numeric argument to mathematical function
## error in task 3
## [[1]]
## [1] 1
##
## [[2]]
## [1] 1.414214
##
## [[3]]
## <remote-error in FUN(...): non-numeric argument to mathematical function>
## traceback() available as 'attr(x, "traceback")'
\end{verbatim}
Next we look at an example where the elements of \Rcode{X} are grouped instead
of run separately. The default value for \Rcode{tasks} is 0 which means 'X' is
split as evenly as possible across the number of workers. There are 3
workers so the first task consists of list(1, 2), the second is list("3", 4)
and the third is list(5, 6).
<<errors_3tasksA_stopOnError>>=
X <- list(1, 2, "3", 4, 5, 6)
param <- SnowParam(3, stop.on.error = TRUE)
@
To simulate a longer running computation sleep time is added to 'FUN'. The
sleep forces task 2 to finish before task 3.
<<errors_3tasks_fun_stopOnError>>=
FUN <- function(i) { Sys.sleep(i); sqrt(i) }
@
The output shows an error in task 2 (vs 3 in the previous example) and a result
for '4' is included because it was part of the second task.
\begin{verbatim}
bplapply(X, FUN, BPPARAM = param)
## starting worker for localhost:11441
## starting worker for localhost:11441
## starting worker for localhost:11441
## ERROR [2015-08-21 14:10:28] non-numeric argument to mathematical function
## error in task 2
## [[1]]
## [1] 1
##
## [[2]]
## [1] 1.414214
##
## [[3]]
## <remote-error in sqrt(i): non-numeric argument to mathematical function>
## traceback() available as 'attr(x, "traceback")'
##
## [[4]]
## [1] 2
\end{verbatim}
Side Note: Results are collected from workers as they finish which is not
necessarily the same order in which they were loaded. Depending on how tasks
are divided among workers it is possible that the task with an error completes
after all others. In that situation the output will includes all results along
with the error message and it may appear that \Rcode{stop.on.error} is not
doing much good. This is simply a heads up that the usefulness of
\Rcode{stop.on.error} may vary with run time and distribution of tasks over
workers.
\subsection{Identify failures with \Rcode{bpok()}}
The \Rcode{bpok()} function is a quick way to determine which (if any)
tasks failed. In this example the second element fails.
<<errors_bpok_bplapply>>=
res <- bplapply(list(1, "2", 3), sqrt)
res
@
\Rcode{bpok} returns TRUE if the task was successful.
<<errors_bpok>>=
bpok(res)
@
Once errors are identified with \Rcode{bpok} the traceback can be retrieved
with the \Rcode{attr} function. This is possible because errors are returned
as \Rcode{condition} objects with the traceback as an attribute.
<<errors_traceback>>=
tail(attr(res[[which(!bpok(res))]], "traceback"))
@
\subsection{Rerun failed tasks with \Rcode{BPREDO}}
Tasks can fail due to hardware problems or bugs in the input data. The
\BiocParallel{} functions support a \Rcode{BPREDO} (re-do) argument for
recomputing only the tasks that failed. A list of partial results and errors is
supplied to \Rcode{BPREDO} in a second call to the function. The failed
elements are identified, recomputed and inserted into the original results.
The bug in this example is the second element of 'X' which is a character when
it should be numeric.
\begin{verbatim}
## X <- list(1, "2", 3)
## res <- bplapply(X, sqrt)
## res
##
## [[1]]
## [1] 1
##
## [[2]]
## <remote-error in FUN(...): non-numeric argument to mathematical function>
## traceback() available as 'attr(x, "traceback")'
##
## [[3]]
## [1] 1.732051
\end{verbatim}
First fix the input data.
<<errors_BPREDO_input>>=
X.redo <- list(1, 2, 3)
@
Repeat the call to \Rcode{bplapply} this time supplying the partial results
as \Rcode{BPREDO}.
\begin{verbatim}
## bplapply(X.redo, sqrt, BPREDO = res)
##
## Resuming previous calculation ...
## [[1]]
## [1] 1
##
## [[2]]
## [1] 1.414214
##
## [[3]]
## [1] 1.732051
\end{verbatim}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Logging}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NOTE: Logging as described in this section is supported for SnowParam,
MulticoreParam and SerialParam.
\subsection{Parameters}
Logging in \BiocParallel{} is controlled by 3 fields in the
\Rcode{BiocParallelParam}:
\begin{verbatim}
log: TRUE or FALSE
logdir: location to write log file
threshold: one of "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
\end{verbatim}
When \Rcode{log = TRUE} the \CRANpkg{futile.logger} package is loaded on
each worker. \BiocParallel{} uses a custom script on the workers to
collect log messages as well as additional statistics such as gc, runtime
and node information. Output to stderr and stdout is also captured.
By default \Rcode{log} is FALSE and \Rcode{threshold} is {\it INFO}.
<<logs_constructor>>=
param <- SnowParam()
param
@
Turn logging on and set the threshold to {\it TRACE}.
<<logs_accessors>>=
bplog(param) <- TRUE
bpthreshold(param) <- "TRACE"
param
@
\subsection{Setting a threshold}
All thresholds defined in \CRANpkg{futile.logger} are supported: {\it
FATAL}, {\it ERROR}, {\it WARN}, {\it INFO}, {\it DEBUG} and {\it TRACE}. All
messages greater than or equal to the severity of the threshold are shown. For
example, a threshold of {\it INFO} will print all messages tagged as {\it
FATAL}, {\it ERROR}, {\it WARN} and {\it INFO}.
Because the default threshold is {\it INFO} it catches the {\it ERROR}-level
message thrown when attempting the square root of a character ("2").
<<logs_bplapply>>=
bplapply(list(1, "2", 3), sqrt, BPPARAM = param)
@
All user-supplied messages written in the \CRANpkg{futile.logger} syntax
are also captured. This function performs argument checking and includes
a couple of {\it WARN} and {\it DEBUG}-level messages.
<<logs_FUN>>=
FUN <- function(i) {
flog.debug(paste0("value of 'i' = ", i))
if (!length(i)) {
flog.warn("'i' is missing")
NA
} else if (!is(i, "numeric")) {
flog.debug("coercing 'i' to numeric")
as.numeric(i)
} else {
i
}
}
@
Turn logging on and set the threshold to {\it WARN}.
\begin{verbatim}
param <- SnowParam(2, log = TRUE, threshold = "WARN")
bplapply(list(1, "2", integer()), FUN, BPPARAM = param)
## starting worker for localhost:11441
## starting worker for localhost:11441
## loading futile.logger on workers
## ############### LOG OUTPUT ###############
## Task: 1
## Node: 1
## Timestamp: 2015-08-21 14:20:41
## Success: TRUE
## Task duration:
## user system elapsed
## 0.002 0.001 0.004
## Memory used:
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 324081 17.4 592000 31.7 341266 18.3
## Vcells 431677 3.3 1023718 7.9 497048 3.8
## Log messages:
##
## stderr and stdout:
## character(0)
## ############### LOG OUTPUT ###############
## Task: 2
## Node: 2
## Timestamp: 2015-08-21 14:20:41
## Success: TRUE
## Task duration:
## user system elapsed
## 0.007 0.000 0.007
## Memory used:
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 325664 17.4 592000 31.7 367737 19.7
## Vcells 434331 3.4 1023718 7.9 519642 4.0
## Log messages:
## WARN [2015-08-21 14:20:41] 'i' is missing
##
## stderr and stdout:
## character(0)
## [[1]]
## [1] 1
##
## [[2]]
## [1] 2
##
## [[3]]
## [1] NA
\end{verbatim}
Changing the threshold to {\it DEBUG} catches both {\it WARN} and
{\it DEBUG} messages.
\begin{verbatim}
param <- SnowParam(2, log = TRUE, threshold = "DEBUG")
bplapply(list(1, "2", integer()), FUN, BPPARAM = param)
## starting worker for localhost:11441
## starting worker for localhost:11441
## loading futile.logger on workers
## ############### LOG OUTPUT ###############
## Task: 1
## Node: 1
## Timestamp: 2015-08-21 14:21:40
## Success: TRUE
## Task duration:
## user system elapsed
## 0.005 0.000 0.005
## Memory used:
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 325633 17.4 592000 31.7 352929 18.9
## Vcells 434309 3.4 1023718 7.9 515223 4.0
## Log messages:
## DEBUG [2015-08-21 14:21:40] value of 'i' = 1
##
## stderr and stdout:
## character(0)
## ############### LOG OUTPUT ###############
## Task: 2
## Node: 2
## Timestamp: 2015-08-21 14:21:40
## Success: TRUE
## Task duration:
## user system elapsed
## 0.01 0.00 0.01
## Memory used:
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 325668 17.4 592000 31.7 394333 21.1
## Vcells 434358 3.4 1023718 7.9 524369 4.1
## Log messages:
## DEBUG [2015-08-21 14:21:40] value of 'i' = 2
## DEBUG [2015-08-21 14:21:40] coercing 'i' to numeric
## DEBUG [2015-08-21 14:21:40] value of 'i' =
## WARN [2015-08-21 14:21:40] 'i' is missing
##
## stderr and stdout:
## character(0)
## [[1]]
## [1] 1
##
## [[2]]
## [1] 2
##
## [[3]]
## [1] NA
\end{verbatim}
\subsection{Log files}
When \Rcode{log == TRUE}, log messages are written to the console by default.
If \Rcode{logdir} is given the output is written out to files, one per task.
File names are prefixed with the name in \Rcode{bpjobname(BPPARAM)}; default
is 'BPJOB'.
\begin{verbatim}
param <- SnowParam(2, log = TRUE, threshold = "DEBUG", logdir = tempdir())
res <- bplapply(list(1, "2", integer()), FUN, BPPARAM = param)
## loading futile.logger on workers
list.files(bplogdir(param))
## [1] "BPJOB.task1.log" "BPJOB.task2.log"
\end{verbatim}
Read in BPJOB.task2.log:
\begin{verbatim}
readLines(paste0(bplogdir(param), "/BPJOB.task2.log"))
## [1] "############### LOG OUTPUT ###############"
## [2] "Task: 2"
## [3] "Node: 2"
## [4] "Timestamp: 2015-07-08 09:03:59"
## [5] "Success: TRUE"
## [6] "Task duration: "
## [7] " user system elapsed "
## [8] " 0.009 0.000 0.011 "
## [9] "Memory use (gc): "
## [10] " used (Mb) gc trigger (Mb) max used (Mb)"
## [11] "Ncells 325664 17.4 592000 31.7 393522 21.1"
## [12] "Vcells 436181 3.4 1023718 7.9 530425 4.1"
## [13] "Log messages:"
## [14] "DEBUG [2015-07-08 09:03:59] value of 'i': 2"
## [15] "INFO [2015-07-08 09:03:59] coercing to numeric"
## [16] "DEBUG [2015-07-08 09:03:59] value of 'i': "
## [17] "WARN [2015-07-08 09:03:59] 'i' is missing"
## [18] ""
## [19] "stderr and stdout:"
## [20] "character(0)"
\end{verbatim}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Worker timeout}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NOTE: \Rcode{timeout} is supported for SnowParam and MulticoreParam.
For long running jobs or untested code it can be useful to set a time limit.
The \Rcode{timeout} field is the time, in seconds, allowed for each worker to
complete a task; default is \Rcode{Inf}. If the task takes longer than
\Rcode{timeout} a timeout error is returned.
Time can be changed during param construction with the \Rcode{timeout} arg,
<<timeout_constructor>>=
param <- SnowParam(timeout = 20)
param
@
or with the \Rcode{bptimeout} setter:
<<timeout_setter>>=
bptimeout(param) <- 2
param
@
\begin{verbatim}
fun <- function(i) {
Sys.sleep(i)
i
}
bplapply(1:3, fun, BPPARAM = param)
## starting worker for localhost:11747
## starting worker for localhost:11747
## starting worker for localhost:11747
## [[1]]
## [1] 1
##
## [[2]]
## <remote-error in Sys.sleep(i): reached elapsed time limit>
## traceback() available as 'attr(x, "traceback")'
##
## [[3]]
## <remote-error in Sys.sleep(i): reached elapsed time limit>
## traceback() available as 'attr(x, "traceback")'
\end{verbatim}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Debugging}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Effective debugging strategies vary by problem and often involve a combination
of error handling and logging techniques. In general, when debugging
\R{}-generated errors the traceback is often the best place to start followed
by adding debug messages to the worker function. When trouble shooting
unexpected behavior (i.e., not a formal error or warning) adding debug messages
or switching to \Rcode{SerialParam} are good approaches. Below is an overview
of these different strategies.
\subsection{Accessing the traceback}
The traceback is a good place to start when tracking down \R{}-generated
errors. Because the function is executed on the workers it's not accessible for
interactive debugging with functions such as \Rcode{trace} or \Rcode{debug}.
The traceback provides a snapshot of the state of the worker at the time
the error was thrown.
This function takes the square root of the absolute value of a vector.
<<debug_sqrtabs>>=
fun1 <- function(x) {
v <- abs(x)
sapply(1:length(v), function(i) sqrt(v[i]))
}
@
Calling ``fun1'' with a character throws an error:
\begin{verbatim}
res <- bplapply(list(c(1,3), 5, "6"), fun1)
res
## [[1]]
## [1] 1.000000 1.732051
##
## [[2]]
## [1] 2.236068
##
## [[3]]
## <remote-error in abs(x): non-numeric argument to mathematical function>
## traceback() available as 'attr(x, "traceback")'
\end{vebatim}
Identify which elements failed with \Rcode{bpok}:
\begin{verbatim}
bpok(res)
## [1] TRUE TRUE FALSE
\end{verbatim}
The error (i.e., third element of ``res'') is a \Rcode{condition} object:
\begin{verbatim}
is(res[[3]], "condition")
## [1] TRUE
\end{verbatim}
The traceback is an attribute of the \Rcode{condition} and can be accessed with
the \Rcode{attr} function.
\begin{verbatim}
noquote(tail(attr(res[[3]], "traceback")))
## [1] call <- sapply(sys.calls(), deparse)
## [2] e <- structure(e, class = c("remote-error", "condition"),
## [3] traceback = capture.output(traceback(call)))
## [4] invokeRestart("abort", e)
## [5] }, "non-numeric argument to mathematical function", quote(abs(x)))
## [6] 1: h(simpleError(msg, call))
\end{verbatim}
\subsection{Adding debug messages}
When a \Rcode{numeric()} is passed to ``fun1'' no formal error is thrown
but the length of the second list element is 2 when it should be 1.
\begin{verbatim}
bplapply(list(c(1,3), numeric(), 6), fun1)
## [[1]]
## [1] 1.000000 1.732051
##
## [[2]]
## [[2]][[1]]
## [1] NA
##
## [[2]][[2]]
## numeric(0)
##
## [[3]]
## [1] 2.44949
\end{verbatim}
Without a formal error we have no traceback so we'll add a few debug
messages. The \CRANpkg{futile.logger} syntax tags messages with
different levels of severity. A message created with \Rcode{flog.debug} will
only print if the threshold is {\it DEBUG} or lower. So in this case it will
catch both INFO and DEBUG messages.
``fun2'' has debug statements that show the value of `x', length of `v' and
the index `i'.
<<debug_fun1_debug>>=
fun2 <- function(x) {
v <- abs(x)
flog.debug(
paste0("'x' = ", paste(x, collapse=","), ": length(v) = ", length(v))
)
sapply(1:length(v), function(i) {
flog.info(paste0("'i' = ", i))
sqrt(v[i])
})
}
@
Create a param that logs at a threshold level of {\it DEBUG}.
<<debug_param_debug>>=
param <- SnowParam(3, log = TRUE, threshold = "DEBUG")
@
The debug messages reveal the problem occurs when `x' is \Rcode{numeric()}.
The index for \Rcode{sapply} is along `v' which in this case has length 0.
This forces `i' to take values of `1' and `0' giving an output of length 2 for
the second element (i.e., \Rcode{NA} and \Rcode{numeric(0)}).
<<debug_DEBUG>>=
res <- bplapply(list(c(1,3), numeric(), 6), fun2, BPPARAM = param)
res
@
``fun2'' can be fixed by using \Rcode{seq\_along(v)} to create the index
instead of \Rcode{1:length(v)}.
\subsection{Local debugging with \Rcode{SerialParam}}
Errors that occur on parallel workers can be difficult to debug. Often the
traceback sent back from the workers is too much to parse or not informative.
We are also limited in that our interactive strategies of
\Rcode{browser} and \Rcode{trace} are not available.
One option for further debugging is to run the code in serial with \Rcode{SerialParam}.
This removes the ``parallel'' component and is the same as running a
straight \Rcode{*apply} function. This approach may not help if the problem
was hardware related but can be very useful when the bug is in the \R{} code.
We use the now familiar square root example with a bug in the second element
of \Rcode{X}.
<<debug_sqrt>>=
res <- bplapply(list(1, "2", 3), sqrt, BPPARAM = SnowParam(3))
res
@
\Rcode{sqrt} is an internal function. The problem is likely with our data
going into the function and not the \Rcode{sqrt} function itself. We can
write a small wrapper around \Rcode{sqrt} so we can see the input.
<<debug_sqrt_wrap>>=
fun3 <- function(i) sqrt(i)
@
Debug the new function:
\begin{verbatim}
debug(fun3)
\end{verbatim}
We want to recompute only elements that failed and for that we use
the \Rcode{BPREDO} argument. The BPPARAM has been changed to
\Rcode{SerialParam} so the job is run in the local workspace in serial.
\begin{verbatim}
> bplapply(list(1, "2", 3), fun3, BPREDO = res, BPPARAM = SerialParam())
Resuming previous calculation ...
debugging in: FUN(...)
debug: sqrt(i)
Browse[2]> objects()
[1] "i"
Browse[2]> i
[1] "2"
Browse[2]>
\end{verbatim}
The local browsing allowed us to see the problem input was the
character "2".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{\Rcode{sessionInfo()}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
<<sessionInfo>>=
toLatex(sessionInfo())
@
\end{document}
|