-
Notifications
You must be signed in to change notification settings - Fork 6
/
parallelly_disable_parallel_setup_if_needed.R
158 lines (131 loc) · 4.47 KB
/
parallelly_disable_parallel_setup_if_needed.R
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
## Bug #18119 (https://bugs.r-project.org/bugzilla/show_bug.cgi?id=18119)
## has been fixed in R-devel r80472 (2021-06-10) and in R-4.1-branch in
## r80532 (2021-06-19). It does not apply to R (< 4.0.0).
r_version_has_bug18119 <- local({
res <- NA
get_r_info <- function() {
## R version
version <- Sys.getenv("R_PARALLELLY_R_VERSION", NA_character_)
if (is.na(version)) {
version <- getRversion()
} else {
version <- numeric_version(version)
}
## SVN revision
revision <- Sys.getenv("R_PARALLELLY_R_REVISION", NA_character_)
if (is.na(revision)) {
revision <- R.version[["svn rev"]]
if (length(revision) != 1) revision <- -1L
}
revision <- as.integer(revision)
if (!is.finite(revision)) revision <- -1L
list(version = version, revision = revision)
}
function(force = FALSE) {
if (force) res <<- NA
if (!is.na(res)) return(res)
r <- get_r_info()
## Too old version of R?
if (r$version < "4.0.0") {
res <<- FALSE
return(FALSE)
}
## All R 4.0.* versions have the bug
if (r$version < "4.1.0") {
res <<- TRUE
return(TRUE)
}
if (r$version == "4.1.0") {
if (r$revision >= 80532) {
## Bug has been fixed in R 4.1.0 patched r80532
res <<- FALSE
return(FALSE)
}
} else if (r$version == "4.2.0") {
if (r$revision >= 80472) {
## Bug has been fixed in R 4.2.0 devel r80472
res <<- FALSE
return(FALSE)
}
} else if (r$version >= "4.1.1") {
## Bug has been fixed in R 4.1.1 (to be released Aug 2021)
res <<- FALSE
return(FALSE)
}
## In all other cases, we'll assume the running R version has the bug
res <<- TRUE
TRUE
}
})
## Check if the current R session is affected by bug 18119 or not.
## Return NA, if we're not 100% sure
affected_by_bug18119 <- local({
res <- NA
function(force = FALSE) {
if (force) res <<- NA
if (!is.na(res)) return(res)
## Nothing to do: Has R bug 18119 been fixed?
if (!r_version_has_bug18119(force = force)) {
res <<- FALSE
return(FALSE)
}
## Running RStudio Console?
if (Sys.getenv("RSTUDIO") == "1" && !nzchar(Sys.getenv("RSTUDIO_TERM"))) {
res <<- TRUE
return(TRUE)
}
## Is 'tcltk' loaded?
if ("tcltk" %in% loadedNamespaces()) {
res <<- TRUE ## Remember this, in case 'tcltk' is unloaded
return(TRUE)
}
## Otherwise, we don't know
NA
}
})
## The RStudio Console does not support setup_strategy = "parallel"
## https://github.com/rstudio/rstudio/issues/6692#issuecomment-785346223
## Unless our R option is already set explicitly (or via the env var),
## be agile to how RStudio handles it for the 'parallel' package
## This bug (https://bugs.r-project.org/bugzilla/show_bug.cgi?id=18119)
## has been fixed in R-devel r80472 (2021-06-10) and in R-4.1-branch in
## r80532 (2021-06-19).
##
## UPDATE 2021-07-15: It turns out that this bug also affects macOS if
## the 'tcltk' package is loaded, cf.
## https://github.com/rstudio/rstudio/issues/6692#issuecomment-880647623
parallelly_disable_parallel_setup_if_needed <- function(liberal = TRUE) {
## Nothing to do: Has R bug 18119 been fixed?
if (!r_version_has_bug18119()) return(FALSE)
## Always respect users settings
if (!is.null(getOption("parallelly.makeNodePSOCK.setup_strategy"))) {
return(FALSE)
}
if (liberal) {
## Assume it'll work, unless we know it won't
if (is.na(affected_by_bug18119())) return(FALSE)
}
## Force 'parallelly' to use the "sequential" setup strategy
options(parallelly.makeNodePSOCK.setup_strategy = "sequential")
## Force 'parallel' to use the "sequential" setup strategy
parallel_set_setup_strategy("sequential")
TRUE
}
parallel_set_setup_strategy <- function(value) {
ns <- getNamespace("parallel")
if (!exists("defaultClusterOptions", mode = "environment", envir = ns)) {
return()
}
defaultClusterOptions <- get("defaultClusterOptions",
mode = "environment", envir = ns)
## Nothing to do?
current <- defaultClusterOptions$setup_strategy
if (identical(current, value)) return()
## Cannot set?
if (!exists("setDefaultClusterOptions", mode = "function", envir = ns)) {
return()
}
setDefaultClusterOptions <- get("setDefaultClusterOptions",
mode = "function", envir = ns)
setDefaultClusterOptions(setup_strategy = value)
}