-
Notifications
You must be signed in to change notification settings - Fork 8
Add error handling plumbing #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c5ed31d
Add error handling plumbing
t-kalinowski 6459cc1
Merge main into error-handling
t-kalinowski 14eba24
Fix OpenMP error cancellation
t-kalinowski f5cb4a0
Cancel OpenMP sapply on error
t-kalinowski aa0a800
Fix OpenMP depth after parallel sapply
t-kalinowski 16bcb7a
add NEWS
t-kalinowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| quickr_error_msg_name <- function() "quickr_err_msg" | ||
|
|
||
| quickr_error_msg_len <- function() 256L | ||
|
|
||
| quickr_error_setter_name <- function() "quickr_set_error_msg" | ||
|
|
||
| quickr_error_arg_names <- function() { | ||
| c(quickr_error_msg_name()) | ||
| } | ||
|
|
||
| is_quickr_error_msg <- function(name) { | ||
| identical(name, quickr_error_msg_name()) | ||
| } | ||
|
|
||
| scope_root_for_errors <- function(scope) { | ||
| if (!inherits(scope, "quickr_scope")) { | ||
| return(scope) | ||
| } | ||
| while ( | ||
| !identical(attr(scope, "kind", exact = TRUE), "subroutine") && | ||
| inherits(parent.env(scope), "quickr_scope") | ||
| ) { | ||
| scope <- parent.env(scope) | ||
| } | ||
| scope | ||
| } | ||
|
|
||
| mark_scope_uses_errors <- function(scope) { | ||
| root <- scope_root_for_errors(scope) | ||
| if (inherits(root, "quickr_scope")) { | ||
| attr(root, "uses_errors") <- TRUE | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
| scope_uses_errors <- function(scope) { | ||
| root <- scope_root_for_errors(scope) | ||
| isTRUE(attr(root, "uses_errors", TRUE)) | ||
| } | ||
|
|
||
| fortran_string_literal <- function(x) { | ||
| stopifnot(is_string(x)) | ||
| escaped <- gsub("\r\n|\r|\n", "\\\\n", x) | ||
| escaped <- gsub("\"", "\"\"", escaped, fixed = TRUE) | ||
| paste0("\"", escaped, "\"") | ||
| } | ||
|
|
||
| check_quickr_error_message_continuable <- function(msg) { | ||
| stopifnot(is_string(msg)) | ||
| if (grepl("[ \t]", msg)) { | ||
| return(invisible(TRUE)) | ||
| } | ||
| msg_literal <- fortran_string_literal(msg) | ||
| line_len <- nchar(glue( | ||
| "&{quickr_error_setter_name()}( {msg_literal} )" | ||
| )) | ||
| if (line_len > 132L) { | ||
| stop( | ||
| "Error message is too long to fit in a single Fortran line without spaces.", | ||
| " Add spaces to allow line continuations.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
| quickr_error_manifest_lines <- function() { | ||
| msg_name <- quickr_error_msg_name() | ||
| len_val <- quickr_error_msg_len() | ||
|
|
||
| glue("character(kind=c_char), intent(inout) :: {msg_name}({len_val})") | ||
| } | ||
|
|
||
| quickr_error_helper_fortran <- function(openmp = FALSE) { | ||
| msg_name <- quickr_error_msg_name() | ||
| setter <- quickr_error_setter_name() | ||
| len_val <- quickr_error_msg_len() | ||
|
|
||
| glue::trim(str_flatten_lines( | ||
| glue("subroutine {setter}(msg)"), | ||
| " character(len=*), intent(in) :: msg", | ||
| " integer :: i", | ||
| " integer :: n", | ||
| if (isTRUE(openmp)) " !$omp critical (quickr_error)", | ||
| glue(" if ({msg_name}(1) == c_null_char) then"), | ||
| glue(" n = min(len(msg), {len_val} - 1)"), | ||
| glue(" {msg_name}(1:n) = [(msg(i:i), i = 1, n)]"), | ||
| glue(" {msg_name}(n + 1) = c_null_char"), | ||
| " end if", | ||
| if (isTRUE(openmp)) " !$omp end critical (quickr_error)", | ||
| glue("end subroutine {setter}") | ||
| )) | ||
| } | ||
|
|
||
| quickr_error_fortran_lines <- function(message = NULL, scope = NULL) { | ||
| msg <- message %||% "quickr error" | ||
| stopifnot(is_string(msg)) | ||
| if (!nzchar(msg)) { | ||
| msg <- "quickr error" | ||
| } | ||
| check_quickr_error_message_continuable(msg) | ||
| msg_literal <- fortran_string_literal(msg) | ||
| lines <- glue("call {quickr_error_setter_name()}({msg_literal})") | ||
| if (isTRUE(scope_in_openmp(scope))) { | ||
| lines <- c(lines, "!$omp cancel do") | ||
| } else { | ||
| lines <- c(lines, "return") | ||
| } | ||
| lines | ||
| } | ||
|
|
||
| quickr_error_return_if_set <- function( | ||
| scope, | ||
| openmp_depth = scope_openmp_depth(scope) | ||
| ) { | ||
| if (!isTRUE(scope_uses_errors(scope))) { | ||
| return("") | ||
| } | ||
| if (is.null(openmp_depth)) { | ||
| openmp_depth <- 0L | ||
| } | ||
| openmp_depth <- max(as.integer(openmp_depth), 0L) | ||
| if (openmp_depth > 0L) { | ||
| return(str_flatten_lines( | ||
| glue("if ({quickr_error_msg_name()}(1) /= c_null_char) then"), | ||
| " !$omp cancel do", | ||
| "end if" | ||
| )) | ||
| } | ||
| glue("if ({quickr_error_msg_name()}(1) /= c_null_char) return") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
parallelis set,error_check_inneris deliberately empty, so the OpenMPsapplyloop never checksquickr_err_msginside thedoregion. Becausecompile_sapply_assignment()never callsenter_openmp_scope(), astop()in the closure only callsquickr_set_error_msgand returns, and the parallel loop continues to run until completion; the error is only surfaced after the loop. This means OpenMPsapply()can keep executing with known-invalid inputs or side effects long after an error is raised, unlike parallelforloops where!$omp cancel dois triggered promptly. Consider inserting a cancellation check inside the loop for the parallel case or marking the OpenMP scope sostop()emits!$omp cancel do.Useful? React with 👍 / 👎.