| 1 |
library(parallelly) |
| 2 |
|
| 3 |
message("*** availableCores() ...") |
| 4 |
|
| 5 |
## detectCores() may return NA_integer_ |
| 6 |
n <- parallel::detectCores() |
| 7 |
message(sprintf("detectCores() = %d", n)) |
| 8 |
stopifnot(length(n) == 1, is.numeric(n)) |
| 9 |
|
| 10 |
## Default |
| 11 |
n <- availableCores() |
| 12 |
message(sprintf("availableCores() = %d", n)) |
| 13 |
stopifnot(length(n) == 1, is.integer(n), n >= 1) |
| 14 |
n0 <- n |
| 15 |
|
| 16 |
n <- availableCores(omit = 1L) |
| 17 |
stopifnot(length(n) == 1, is.integer(n), n >= 1, n == max(1L, n0 - 1L)) |
| 18 |
|
| 19 |
n <- availableCores(max = 1L) |
| 20 |
stopifnot(length(n) == 1, is.integer(n), n == 1L) |
| 21 |
|
| 22 |
## Minimium of all known settings (default) |
| 23 |
print(availableCores(which = "min")) |
| 24 |
|
| 25 |
## Maximum of all known settings (should never be used) |
| 26 |
print(availableCores(which = "max")) |
| 27 |
|
| 28 |
## All known settings |
| 29 |
ns <- availableCores(na.rm = FALSE, which = "all") |
| 30 |
stopifnot(length(ns) >= 1, is.integer(ns), all(is.na(ns) | ns >= 0L)) |
| 31 |
|
| 32 |
## System settings |
| 33 |
n <- availableCores(methods = "system") |
| 34 |
print(n) |
| 35 |
stopifnot(length(n) == 1, is.integer(n), n >= 1) |
| 36 |
|
| 37 |
## Contrain by number of available connections |
| 38 |
opts <- options(mc.cores = availableConnections()) |
| 39 |
n <- availableCores(constraints = "connections", method = "mc.cores") |
| 40 |
print(n) |
| 41 |
stopifnot(length(n) == 1, is.integer(n), n >= 1) |
| 42 |
n0 <- n |
| 43 |
n <- availableCores(constraints = "connections-1", method = "mc.cores") |
| 44 |
print(n) |
| 45 |
stopifnot(length(n) == 1, is.integer(n), n >= 1, n == n0 - 1L) |
| 46 |
n <- availableCores(constraints = "connections-2", method = "mc.cores") |
| 47 |
print(n) |
| 48 |
stopifnot(length(n) == 1, is.integer(n), n >= 1, n == n0 - 2L) |
| 49 |
n <- availableCores(method = "connections") |
| 50 |
print(n) |
| 51 |
stopifnot(length(n) == 1, is.integer(n), n >= 1, n == n0) |
| 52 |
n <- availableCores(method = "connections-1") |
| 53 |
print(n) |
| 54 |
stopifnot(length(n) == 1, is.integer(n), n >= 1, n == n0 - 1L) |
| 55 |
n <- availableCores(method = "connections-2") |
| 56 |
print(n) |
| 57 |
stopifnot(length(n) == 1, is.integer(n), n >= 1, n == n0 - 2L) |
| 58 |
options(opts) |
| 59 |
|
| 60 |
## Contrain by support for multicore |
| 61 |
n <- availableCores(constraints = "multicore") |
| 62 |
print(n) |
| 63 |
stopifnot(length(n) == 1, is.integer(n), n >= 1) |
| 64 |
|
| 65 |
## Special case: mc.cores = 0L |
| 66 |
opts <- options(mc.cores = 0L) |
| 67 |
n <- availableCores(method = "mc.cores") |
| 68 |
print(n) |
| 69 |
stopifnot(length(n) == 1, is.integer(n), n == 1L) |
| 70 |
n <- availableCores(method = "mc.cores+1") |
| 71 |
print(n) |
| 72 |
stopifnot(length(n) == 1, is.integer(n), n == 1L) |
| 73 |
options(opts) |
| 74 |
|
| 75 |
## Predefined ones for known cluster schedulers |
| 76 |
print(availableCores(methods = "PBS")) |
| 77 |
print(availableCores(methods = "SGE")) |
| 78 |
print(availableCores(methods = "Slurm")) |
| 79 |
print(availableCores(methods = "LSF")) |
| 80 |
|
| 81 |
## Any R options and system environment variable |
| 82 |
print(availableCores(methods = c("width", "FOO_BAR_ENV"), |
| 83 |
na.rm = FALSE, which = "all")) |
| 84 |
|
| 85 |
## Exception handling |
| 86 |
Sys.setenv("FOO_BAR_ENV" = "0") |
| 87 |
res <- try(availableCores(methods = "FOO_BAR_ENV"), silent = TRUE) |
| 88 |
stopifnot(inherits(res, "try-error")) |
| 89 |
|
| 90 |
|
| 91 |
ncores0 <- 42L |
| 92 |
|
| 93 |
message("*** LSF ...") |
| 94 |
message(" - LSB_DJOB_NUMPROC") |
| 95 |
Sys.setenv(LSB_DJOB_NUMPROC = as.character(ncores0)) |
| 96 |
env <- environment(parallelly:::availableCoresLSF) |
| 97 |
env$n <- NULL |
| 98 |
ncores <- availableCores(methods = "LSF") |
| 99 |
print(ncores) |
| 100 |
stopifnot(ncores == ncores0) |
| 101 |
message("*** LSF ... done") |
| 102 |
|
| 103 |
message("*** PJM (Fujitsu Technical Computing Suite) ...") |
| 104 |
message(" - PJM_VNODE_CORE") |
| 105 |
Sys.setenv(PJM_VNODE_CORE = as.character(ncores0)) |
| 106 |
env <- environment(parallelly:::availableCoresPJM) |
| 107 |
env$n <- NULL |
| 108 |
ncores <- availableCores(methods = "PJM") |
| 109 |
print(ncores) |
| 110 |
stopifnot(ncores == ncores0) |
| 111 |
Sys.unsetenv("PJM_VNODE_CORE") |
| 112 |
|
| 113 |
message(" - PJM_PROC_BY_NODE") |
| 114 |
Sys.setenv(PJM_PROC_BY_NODE = as.character(ncores0)) |
| 115 |
env <- environment(parallelly:::availableCoresPJM) |
| 116 |
env$n <- NULL |
| 117 |
ncores <- availableCores(methods = "PJM") |
| 118 |
print(ncores) |
| 119 |
stopifnot(ncores == ncores0) |
| 120 |
Sys.unsetenv("PJM_PROC_BY_NODE") |
| 121 |
message("*** PJM (Fujitsu Technical Computing Suite) ... done") |
| 122 |
|
| 123 |
|
| 124 |
message("*** Internal detectCores() ...") |
| 125 |
|
| 126 |
## Option 'parallelly.availableCores.system' |
| 127 |
|
| 128 |
## Reset internal cache |
| 129 |
env <- environment(parallelly:::detectCores) |
| 130 |
env$cache <- list(NULL, NULL) |
| 131 |
|
| 132 |
options(parallelly.availableCores.system = 2L) |
| 133 |
n <- detectCores() |
| 134 |
print(n) |
| 135 |
stopifnot(is.integer(n), is.finite(n), n >= 1, n == 2L) |
| 136 |
options(parallelly.availableCores.system = NULL) |
| 137 |
|
| 138 |
## Reset |
| 139 |
env <- environment(parallelly:::detectCores) |
| 140 |
env$cache <- list(NULL, NULL) |
| 141 |
|
| 142 |
n <- detectCores() |
| 143 |
print(n) |
| 144 |
stopifnot(is.integer(n), is.finite(n), n >= 1) |
| 145 |
|
| 146 |
message("*** Internal detectCores() ... DONE") |
| 147 |
|
| 148 |
|
| 149 |
message("*** Slurm multi-node scenarios ...") |
| 150 |
|
| 151 |
## Reset Slurm cache |
| 152 |
env <- environment(parallelly:::availableCoresSlurm) |
| 153 |
|
| 154 |
## Single-node with SLURM_CPUS_ON_NODE |
| 155 |
Sys.unsetenv("SLURM_CPUS_PER_TASK") |
| 156 |
Sys.setenv(SLURM_JOB_NUM_NODES = "1", SLURM_CPUS_ON_NODE = "8") |
| 157 |
env$n <- NULL |
| 158 |
ncores <- availableCores(methods = "Slurm") |
| 159 |
print(ncores) |
| 160 |
stopifnot(ncores == 8L) |
| 161 |
|
| 162 |
## SLURM_NNODES fallback when SLURM_JOB_NUM_NODES is not set |
| 163 |
Sys.unsetenv("SLURM_CPUS_PER_TASK") |
| 164 |
Sys.unsetenv("SLURM_JOB_NUM_NODES") |
| 165 |
Sys.setenv(SLURM_NNODES = "1", SLURM_CPUS_ON_NODE = "16") |
| 166 |
env$n <- NULL |
| 167 |
ncores <- availableCores(methods = "Slurm") |
| 168 |
print(ncores) |
| 169 |
stopifnot(ncores == 16L) |
| 170 |
|
| 171 |
## Multi-node with SLURM_TASKS_PER_NODE (simple format) |
| 172 |
Sys.unsetenv("SLURM_CPUS_PER_TASK") |
| 173 |
Sys.setenv(SLURM_JOB_NUM_NODES = "2", SLURM_TASKS_PER_NODE = "5,2") |
| 174 |
env$n <- NULL |
| 175 |
ncores <- availableCores(methods = "Slurm") |
| 176 |
print(ncores) |
| 177 |
stopifnot(ncores == 5L) ## Uses first node's count |
| 178 |
|
| 179 |
## Multi-node with SLURM_TASKS_PER_NODE (expanded format) |
| 180 |
Sys.unsetenv("SLURM_CPUS_PER_TASK") |
| 181 |
Sys.setenv(SLURM_JOB_NUM_NODES = "5", SLURM_TASKS_PER_NODE = "2(x2),1(x3)") |
| 182 |
env$n <- NULL |
| 183 |
ncores <- availableCores(methods = "Slurm") |
| 184 |
print(ncores) |
| 185 |
stopifnot(ncores == 2L) ## Uses first node's count |
| 186 |
|
| 187 |
## Cleanup |
| 188 |
Sys.unsetenv(c("SLURM_CPUS_PER_TASK", "SLURM_JOB_NUM_NODES", "SLURM_NNODES", |
| 189 |
"SLURM_CPUS_ON_NODE", "SLURM_TASKS_PER_NODE")) |
| 190 |
env$n <- NULL |
| 191 |
|
| 192 |
message("*** Slurm multi-node scenarios ... done") |
| 193 |
|
| 194 |
|
| 195 |
message("*** PBS NCPUS fallback ...") |
| 196 |
|
| 197 |
## Reset PBS cache |
| 198 |
env <- environment(parallelly:::availableCoresPBS) |
| 199 |
|
| 200 |
## Test NCPUS fallback when PBS_NUM_PPN is not set |
| 201 |
Sys.unsetenv("PBS_NUM_PPN") |
| 202 |
Sys.setenv(NCPUS = "16") |
| 203 |
env$n <- NULL |
| 204 |
ncores <- availableCores(methods = "PBS") |
| 205 |
print(ncores) |
| 206 |
stopifnot(ncores == 16L) |
| 207 |
|
| 208 |
## Cleanup |
| 209 |
Sys.unsetenv("NCPUS") |
| 210 |
env$n <- NULL |
| 211 |
|
| 212 |
message("*** PBS NCPUS fallback ... done") |
| 213 |
|
| 214 |
|
| 215 |
message("*** BiocParallel method ...") |
| 216 |
|
| 217 |
Sys.setenv(BIOCPARALLEL_WORKER_NUMBER = "4") |
| 218 |
ncores <- availableCores(methods = "BiocParallel") |
| 219 |
print(ncores) |
| 220 |
stopifnot(ncores == 4L) |
| 221 |
Sys.unsetenv("BIOCPARALLEL_WORKER_NUMBER") |
| 222 |
|
| 223 |
message("*** BiocParallel method ... done") |
| 224 |
|
| 225 |
|
| 226 |
message("*** Bioconductor build machine ...") |
| 227 |
|
| 228 |
## IS_BIOC_BUILD_MACHINE (modern, >= 3.16) |
| 229 |
Sys.setenv(IS_BIOC_BUILD_MACHINE = "TRUE") |
| 230 |
ncores <- availableCores(methods = "Bioconductor") |
| 231 |
print(ncores) |
| 232 |
stopifnot(ncores == 4L) |
| 233 |
Sys.unsetenv("IS_BIOC_BUILD_MACHINE") |
| 234 |
|
| 235 |
## BBS_HOME fallback (legacy, <= 3.15) |
| 236 |
Sys.setenv(BBS_HOME = "TRUE") |
| 237 |
ncores <- availableCores(methods = "Bioconductor") |
| 238 |
print(ncores) |
| 239 |
stopifnot(ncores == 4L) |
| 240 |
Sys.unsetenv("BBS_HOME") |
| 241 |
|
| 242 |
message("*** Bioconductor build machine ... done") |
| 243 |
|
| 244 |
|
| 245 |
message("*** Custom function method ...") |
| 246 |
|
| 247 |
## Valid custom function |
| 248 |
opts <- options(parallelly.availableCores.custom = function() 3L) |
| 249 |
ncores <- availableCores(methods = "custom") |
| 250 |
print(ncores) |
| 251 |
stopifnot(ncores == 3L) |
| 252 |
options(opts) |
| 253 |
|
| 254 |
## Custom function returning NA (should be ignored) |
| 255 |
opts <- options(parallelly.availableCores.custom = function() NA_integer_) |
| 256 |
ncores <- availableCores(methods = c("system", "custom"), which = "all", na.rm = FALSE) |
| 257 |
print(ncores) |
| 258 |
stopifnot("custom" %in% names(ncores), is.na(ncores["custom"])) |
| 259 |
options(opts) |
| 260 |
|
| 261 |
## Custom function calling availableCores() (recursion protection) |
| 262 |
opts <- options(parallelly.availableCores.custom = function() { |
| 263 |
availableCores(methods = "system") - 1L |
| 264 |
}) |
| 265 |
ncores <- availableCores(methods = "custom") |
| 266 |
print(ncores) |
| 267 |
stopifnot(ncores >= 1L) |
| 268 |
options(opts) |
| 269 |
|
| 270 |
## Error: custom function returns non-single value |
| 271 |
opts <- options(parallelly.availableCores.custom = function() c(1L, 2L)) |
| 272 |
res <- tryCatch(availableCores(methods = "custom"), error = identity) |
| 273 |
stopifnot(inherits(res, "error")) |
| 274 |
options(opts) |
| 275 |
|
| 276 |
## Custom option is not a function (should be skipped) |
| 277 |
opts <- options(parallelly.availableCores.custom = "not a function") |
| 278 |
ncores <- availableCores(methods = c("system", "custom"), which = "all") |
| 279 |
print(ncores) |
| 280 |
stopifnot(!("custom" %in% names(ncores))) |
| 281 |
options(opts) |
| 282 |
|
| 283 |
message("*** Custom function method ... done") |
| 284 |
|
| 285 |
|
| 286 |
message("*** Fallback method ...") |
| 287 |
|
| 288 |
## Test fallback option |
| 289 |
opts <- options(parallelly.availableCores.fallback = 2L) |
| 290 |
ncores <- availableCores(methods = "fallback") |
| 291 |
print(ncores) |
| 292 |
stopifnot(ncores == 2L) |
| 293 |
options(opts) |
| 294 |
|
| 295 |
## Fallback is used when only "special" methods are present |
| 296 |
opts <- options(parallelly.availableCores.fallback = 2L) |
| 297 |
ncores <- availableCores(methods = c("system", "fallback")) |
| 298 |
print(ncores) |
| 299 |
## Fallback should be used if system agrees or is higher |
| 300 |
stopifnot(ncores >= 1L) |
| 301 |
options(opts) |
| 302 |
|
| 303 |
message("*** Fallback method ... done") |
| 304 |
|
| 305 |
|
| 306 |
message("*** parallelly.availableCores.min validation ...") |
| 307 |
|
| 308 |
## Error: non-numeric min |
| 309 |
opts <- options(parallelly.availableCores.min = "two") |
| 310 |
res <- tryCatch(availableCores(), error = identity) |
| 311 |
stopifnot(inherits(res, "error")) |
| 312 |
options(opts) |
| 313 |
|
| 314 |
## Error: min < 1 |
| 315 |
opts <- options(parallelly.availableCores.min = 0L) |
| 316 |
res <- tryCatch(availableCores(), error = identity) |
| 317 |
stopifnot(inherits(res, "error")) |
| 318 |
options(opts) |
| 319 |
|
| 320 |
## Error: min is not finite (Inf) |
| 321 |
opts <- options(parallelly.availableCores.min = Inf) |
| 322 |
res <- tryCatch(availableCores(), error = identity) |
| 323 |
stopifnot(inherits(res, "error")) |
| 324 |
options(opts) |
| 325 |
|
| 326 |
## Error: min > detectCores() |
| 327 |
opts <- options(parallelly.availableCores.min = parallel::detectCores() + 1L) |
| 328 |
res <- tryCatch(availableCores(), error = identity) |
| 329 |
stopifnot(inherits(res, "error")) |
| 330 |
options(opts) |
| 331 |
|
| 332 |
## Error: min has wrong length |
| 333 |
opts <- options(parallelly.availableCores.min = c(1L, 2L)) |
| 334 |
res <- tryCatch(availableCores(), error = identity) |
| 335 |
stopifnot(inherits(res, "error")) |
| 336 |
options(opts) |
| 337 |
|
| 338 |
message("*** parallelly.availableCores.min validation ... done") |
| 339 |
|
| 340 |
|
| 341 |
message("*** Input validation edge cases ...") |
| 342 |
|
| 343 |
## Invalid default (< 1) |
| 344 |
res <- tryCatch(availableCores(default = 0L), error = identity) |
| 345 |
stopifnot(inherits(res, "error")) |
| 346 |
|
| 347 |
## Invalid default (not finite) |
| 348 |
res <- tryCatch(availableCores(default = Inf), error = identity) |
| 349 |
stopifnot(inherits(res, "error")) |
| 350 |
|
| 351 |
## Invalid omit (negative) |
| 352 |
res <- tryCatch(availableCores(omit = -1L), error = identity) |
| 353 |
stopifnot(inherits(res, "error")) |
| 354 |
|
| 355 |
## Invalid omit (not finite) |
| 356 |
res <- tryCatch(availableCores(omit = Inf), error = identity) |
| 357 |
stopifnot(inherits(res, "error")) |
| 358 |
|
| 359 |
## Invalid max (< 1) |
| 360 |
res <- tryCatch(availableCores(max = 0L), error = identity) |
| 361 |
stopifnot(inherits(res, "error")) |
| 362 |
|
| 363 |
## Invalid max (NA) |
| 364 |
res <- tryCatch(availableCores(max = NA), error = identity) |
| 365 |
stopifnot(inherits(res, "error")) |
| 366 |
|
| 367 |
## Invalid constraints (NA values) |
| 368 |
res <- tryCatch(availableCores(constraints = c("multicore", NA)), error = identity) |
| 369 |
stopifnot(inherits(res, "error")) |
| 370 |
|
| 371 |
message("*** Input validation edge cases ... done") |
| 372 |
|
| 373 |
|
| 374 |
message("*** checkNumberOfLocalWorkers() ...") |
| 375 |
|
| 376 |
checkNumberOfLocalWorkers <- parallelly:::checkNumberOfLocalWorkers |
| 377 |
|
| 378 |
## AsIs bypass - should not warn or error |
| 379 |
result <- tryCatch({ |
| 380 |
checkNumberOfLocalWorkers(I(1000L)) |
| 381 |
TRUE |
| 382 |
}, warning = function(w) FALSE, error = function(e) FALSE) |
| 383 |
stopifnot(result) |
| 384 |
|
| 385 |
## Save current options |
| 386 |
opts <- options(parallelly.maxWorkers.localhost = c(1.0, 3.0)) |
| 387 |
|
| 388 |
## Test warning at soft limit (workers/ncores > 1.0) |
| 389 |
ncores <- availableCores() |
| 390 |
if (ncores >= 1L) { |
| 391 |
workers <- ncores + 1L ## Just above soft limit |
| 392 |
res <- tryCatch({ |
| 393 |
checkNumberOfLocalWorkers(workers) |
| 394 |
"none" |
| 395 |
}, warning = function(w) "warning", error = function(e) "error") |
| 396 |
message(sprintf(" workers=%d, ncores=%d -> %s", workers, ncores, res)) |
| 397 |
stopifnot(res == "warning") |
| 398 |
} |
| 399 |
|
| 400 |
## Test error at hard limit (workers/ncores > 3.0) |
| 401 |
if (ncores >= 1L) { |
| 402 |
workers <- ncores * 4L ## Above hard limit |
| 403 |
res <- tryCatch({ |
| 404 |
checkNumberOfLocalWorkers(workers) |
| 405 |
"none" |
| 406 |
}, warning = function(w) "warning", error = function(e) "error") |
| 407 |
message(sprintf(" workers=%d, ncores=%d -> %s", workers, ncores, res)) |
| 408 |
stopifnot(res == "error") |
| 409 |
} |
| 410 |
|
| 411 |
## Restore options |
| 412 |
options(opts) |
| 413 |
|
| 414 |
## Test with empty limits (disabled) |
| 415 |
## Note: NULL returns default, so use numeric(0) to disable |
| 416 |
opts <- options(parallelly.maxWorkers.localhost = numeric(0)) |
| 417 |
result <- tryCatch({ |
| 418 |
checkNumberOfLocalWorkers(1000L) |
| 419 |
TRUE |
| 420 |
}, warning = function(w) FALSE, error = function(e) FALSE) |
| 421 |
stopifnot(result) |
| 422 |
options(opts) |
| 423 |
|
| 424 |
message("*** checkNumberOfLocalWorkers() ... done") |
| 425 |
|
| 426 |
|
| 427 |
message("*** detectCoresHint() ...") |
| 428 |
|
| 429 |
detectCoresHint <- parallelly:::detectCoresHint |
| 430 |
sysCores <- parallel::detectCores() |
| 431 |
|
| 432 |
## Hint for detectCores() pattern |
| 433 |
hint <- detectCoresHint(sysCores) |
| 434 |
message(sprintf(" detectCoresHint(%d): %s", sysCores, hint)) |
| 435 |
stopifnot(!is.null(hint), grepl("detectCores", hint)) |
| 436 |
|
| 437 |
## Hint for detectCores() - 1 pattern |
| 438 |
hint <- detectCoresHint(sysCores - 1L) |
| 439 |
message(sprintf(" detectCoresHint(%d): %s", sysCores - 1L, hint)) |
| 440 |
stopifnot(!is.null(hint), grepl("detectCores", hint), grepl("- 1", hint)) |
| 441 |
|
| 442 |
## Hint for detectCores() - 2 pattern |
| 443 |
hint <- detectCoresHint(sysCores - 2L) |
| 444 |
message(sprintf(" detectCoresHint(%d): %s", sysCores - 2L, hint)) |
| 445 |
stopifnot(!is.null(hint), grepl("detectCores", hint), grepl("- 2", hint)) |
| 446 |
|
| 447 |
## No hint when delta > 2 |
| 448 |
hint <- detectCoresHint(1L) |
| 449 |
message(sprintf(" detectCoresHint(1): %s", if (is.null(hint)) "NULL" else hint)) |
| 450 |
if (sysCores > 3L) { |
| 451 |
stopifnot(is.null(hint)) |
| 452 |
} |
| 453 |
|
| 454 |
message("*** detectCoresHint() ... done") |
| 455 |
|
| 456 |
|
| 457 |
message("*** logical parameter ...") |
| 458 |
|
| 459 |
## Test logical = TRUE (logical/hyperthreaded cores) |
| 460 |
n_logical <- availableCores(methods = "system", logical = TRUE) |
| 461 |
print(n_logical) |
| 462 |
stopifnot(is.integer(n_logical), n_logical >= 1L) |
| 463 |
|
| 464 |
## Test logical = FALSE (physical cores) |
| 465 |
n_physical <- availableCores(methods = "system", logical = FALSE) |
| 466 |
print(n_physical) |
| 467 |
stopifnot(is.integer(n_physical), n_physical >= 1L) |
| 468 |
|
| 469 |
## Physical cores should be <= logical cores |
| 470 |
stopifnot(n_physical <= n_logical) |
| 471 |
|
| 472 |
message("*** logical parameter ... done") |
| 473 |
|
| 474 |
|
| 475 |
message("*** connections-N delta tests ...") |
| 476 |
|
| 477 |
## Get baseline |
| 478 |
n_conn <- freeConnections() |
| 479 |
message(sprintf(" freeConnections() = %d", n_conn)) |
| 480 |
|
| 481 |
## Test various connection deltas |
| 482 |
n0 <- availableCores(methods = "connections") |
| 483 |
print(n0) |
| 484 |
stopifnot(n0 >= 1L) |
| 485 |
|
| 486 |
n5 <- availableCores(methods = "connections-5") |
| 487 |
print(n5) |
| 488 |
stopifnot(n5 >= 1L) |
| 489 |
if (n0 > 5L) { |
| 490 |
stopifnot(n5 == n0 - 5L) |
| 491 |
} |
| 492 |
|
| 493 |
## Large delta should still return at least 1 |
| 494 |
n_large <- availableCores(methods = "connections-1000") |
| 495 |
print(n_large) |
| 496 |
stopifnot(n_large >= 1L) |
| 497 |
|
| 498 |
message("*** connections-N delta tests ... done") |
| 499 |
|
| 500 |
|
| 501 |
message("*** na.rm = FALSE with missing values ...") |
| 502 |
|
| 503 |
## Verify NA values are preserved when na.rm = FALSE |
| 504 |
ns <- availableCores(methods = c("system", "nonexistent_method_xyz123"), |
| 505 |
na.rm = FALSE, which = "all") |
| 506 |
print(ns) |
| 507 |
stopifnot("nonexistent_method_xyz123" %in% names(ns)) |
| 508 |
stopifnot(is.na(ns["nonexistent_method_xyz123"])) |
| 509 |
|
| 510 |
message("*** na.rm = FALSE with missing values ... done") |
| 511 |
|
| 512 |
|
| 513 |
message("*** SGE method ...") |
| 514 |
|
| 515 |
## Reset SGE cache |
| 516 |
env <- environment(parallelly:::availableCoresSGE) |
| 517 |
|
| 518 |
Sys.setenv(NSLOTS = "24") |
| 519 |
env$n <- NULL |
| 520 |
ncores <- availableCores(methods = "SGE") |
| 521 |
print(ncores) |
| 522 |
stopifnot(ncores == 24L) |
| 523 |
Sys.unsetenv("NSLOTS") |
| 524 |
env$n <- NULL |
| 525 |
|
| 526 |
message("*** SGE method ... done") |
| 527 |
|
| 528 |
|
| 529 |
message("*** _R_CHECK_LIMIT_CORES_ method ...") |
| 530 |
|
| 531 |
## When set to true-ish value, should return 2 |
| 532 |
Sys.setenv(`_R_CHECK_LIMIT_CORES_` = "TRUE") |
| 533 |
ncores <- availableCores(methods = "_R_CHECK_LIMIT_CORES_") |
| 534 |
print(ncores) |
| 535 |
stopifnot(ncores == 2L) |
| 536 |
|
| 537 |
## When set to "warn", should also return 2 |
| 538 |
Sys.setenv(`_R_CHECK_LIMIT_CORES_` = "warn") |
| 539 |
ncores <- availableCores(methods = "_R_CHECK_LIMIT_CORES_") |
| 540 |
print(ncores) |
| 541 |
stopifnot(ncores == 2L) |
| 542 |
|
| 543 |
## When set to "false", should return NA |
| 544 |
Sys.setenv(`_R_CHECK_LIMIT_CORES_` = "false") |
| 545 |
ncores <- availableCores(methods = "_R_CHECK_LIMIT_CORES_", na.rm = FALSE) |
| 546 |
print(ncores) |
| 547 |
stopifnot(is.na(ncores)) |
| 548 |
|
| 549 |
## Cleanup |
| 550 |
Sys.unsetenv("_R_CHECK_LIMIT_CORES_") |
| 551 |
|
| 552 |
message("*** _R_CHECK_LIMIT_CORES_ method ... done") |
| 553 |
|
| 554 |
|
| 555 |
message("*** nproc method ...") |
| 556 |
|
| 557 |
## Check if nproc is available |
| 558 |
has_nproc <- nzchar(Sys.which("nproc")) |
| 559 |
message(sprintf(" - nproc available: %s", has_nproc)) |
| 560 |
|
| 561 |
if (has_nproc) { |
| 562 |
## Reset getNproc cache |
| 563 |
env <- environment(parallelly:::getNproc) |
| 564 |
env$res <- NULL |
| 565 |
|
| 566 |
## Test nproc method |
| 567 |
ncores <- availableCores(methods = "nproc") |
| 568 |
print(ncores) |
| 569 |
stopifnot(length(ncores) == 1L, is.integer(ncores), ncores >= 1L) |
| 570 |
|
| 571 |
## Test OMP variable handling |
| 572 |
message(" - Testing OMP variable handling") |
| 573 |
|
| 574 |
## Reset cache |
| 575 |
env$res <- NULL |
| 576 |
|
| 577 |
## Set OMP variables that could influence nproc |
| 578 |
old_omp_threads <- Sys.getenv("OMP_NUM_THREADS", unset = NA) |
| 579 |
old_omp_limit <- Sys.getenv("OMP_THREAD_LIMIT", unset = NA) |
| 580 |
|
| 581 |
Sys.setenv(OMP_NUM_THREADS = "2", OMP_THREAD_LIMIT = "4") |
| 582 |
|
| 583 |
## getNproc should ignore these when querying |
| 584 |
env$res <- NULL |
| 585 |
n <- parallelly:::getNproc() |
| 586 |
message(sprintf(" - getNproc() with OMP vars set: %s", |
| 587 |
if (is.na(n)) "NA" else as.character(n))) |
| 588 |
|
| 589 |
## Verify OMP variables are restored after call |
| 590 |
stopifnot(Sys.getenv("OMP_NUM_THREADS") == "2") |
| 591 |
stopifnot(Sys.getenv("OMP_THREAD_LIMIT") == "4") |
| 592 |
|
| 593 |
## Restore original OMP values |
| 594 |
if (is.na(old_omp_threads)) { |
| 595 |
Sys.unsetenv("OMP_NUM_THREADS") |
| 596 |
} else { |
| 597 |
Sys.setenv(OMP_NUM_THREADS = old_omp_threads) |
| 598 |
} |
| 599 |
if (is.na(old_omp_limit)) { |
| 600 |
Sys.unsetenv("OMP_THREAD_LIMIT") |
| 601 |
} else { |
| 602 |
Sys.setenv(OMP_THREAD_LIMIT = old_omp_limit) |
| 603 |
} |
| 604 |
|
| 605 |
## Reset cache for other tests |
| 606 |
env$res <- NULL |
| 607 |
} |
| 608 |
|
| 609 |
message("*** nproc method ... done") |
| 610 |
|
| 611 |
|
| 612 |
message("*** /proc/self/status method ...") |
| 613 |
|
| 614 |
## Test /proc/self/status method on Linux |
| 615 |
if (file.exists("/proc/self/status")) { |
| 616 |
ncores <- availableCores(methods = "/proc/self/status") |
| 617 |
print(ncores) |
| 618 |
stopifnot(length(ncores) == 1L, is.integer(ncores)) |
| 619 |
if (!is.na(ncores)) { |
| 620 |
stopifnot(ncores >= 1L) |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
message("*** /proc/self/status method ... done") |
| 625 |
|
| 626 |
|
| 627 |
message("*** fraction parameter ...") |
| 628 |
|
| 629 |
## fraction = 1 should be the same as default |
| 630 |
n0 <- availableCores() |
| 631 |
n <- availableCores(fraction = 1.0) |
| 632 |
stopifnot(identical(n, n0)) |
| 633 |
|
| 634 |
## fraction = 0.5 on system cores (which we know) |
| 635 |
n_sys <- availableCores(methods = "system") |
| 636 |
n <- availableCores(methods = "system", fraction = 0.5) |
| 637 |
stopifnot(length(n) == 1, is.integer(n), n >= 1L) |
| 638 |
stopifnot(n == max(1L, as.integer(floor(0.5 * n_sys)))) |
| 639 |
|
| 640 |
## fraction = 0.7 with omit = 1 |
| 641 |
n <- availableCores(methods = "system", fraction = 0.7, omit = 1) |
| 642 |
stopifnot(length(n) == 1, is.integer(n), n >= 1L) |
| 643 |
expected <- max(1L, as.integer(floor(0.7 * n_sys)) - 1L) |
| 644 |
stopifnot(n == expected) |
| 645 |
|
| 646 |
## fraction should always return at least 1 |
| 647 |
n <- availableCores(methods = "system", fraction = 0.01) |
| 648 |
stopifnot(n >= 1L) |
| 649 |
|
| 650 |
## fraction combined with omit should always return at least 1 |
| 651 |
n <- availableCores(methods = "system", fraction = 0.01, omit = 100) |
| 652 |
stopifnot(n >= 1L) |
| 653 |
|
| 654 |
## Input validation |
| 655 |
res <- tryCatch(availableCores(fraction = 0), error = identity) |
| 656 |
stopifnot(inherits(res, "error")) |
| 657 |
|
| 658 |
res <- tryCatch(availableCores(fraction = -0.5), error = identity) |
| 659 |
stopifnot(inherits(res, "error")) |
| 660 |
|
| 661 |
res <- tryCatch(availableCores(fraction = 1.5), error = identity) |
| 662 |
stopifnot(inherits(res, "error")) |
| 663 |
|
| 664 |
res <- tryCatch(availableCores(fraction = NA), error = identity) |
| 665 |
stopifnot(inherits(res, "error")) |
| 666 |
|
| 667 |
res <- tryCatch(availableCores(fraction = "half"), error = identity) |
| 668 |
stopifnot(inherits(res, "error")) |
| 669 |
|
| 670 |
message("*** fraction parameter ... done") |
| 671 |
|
| 672 |
|
| 673 |
message("*** availableCores() ... DONE") |