/usr/share/doc/r-cran-future/tests/multisession.R is in r-cran-future 1.7.0-1ubuntu1.
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 | source("incl/start.R")
library("listenv")
plan(multisession)
message("*** multisession() ...")
for (cores in 1:availCores) {
message(sprintf("Testing with %d cores ...", cores))
options(mc.cores = cores)
## No global variables
f <- multisession({
42L
})
print(f)
stopifnot(inherits(f, "ClusterFuture") || (inherits(f, "SequentialFuture") && f$lazy))
print(resolved(f))
y <- value(f)
print(y)
stopifnot(y == 42L)
## A global variable
a <- 0
f <- multisession({
b <- 3
c <- 2
a * b * c
})
print(f)
## A multisession future is evaluated in a separate
## R session process. Changing the value of a global
## variable should not affect the result of the
## future.
a <- 7 ## Make sure globals are frozen
v <- value(f)
print(v)
stopifnot(v == 0)
message("*** multisession() with globals and blocking")
x <- listenv()
for (ii in 1:4) {
message(sprintf(" - Creating multisession future #%d ...", ii))
x[[ii]] <- multisession({ ii })
}
message(sprintf(" - Resolving %d multisession futures", length(x)))
v <- sapply(x, FUN = value)
stopifnot(all(v == 1:4))
message("*** multisession() and errors")
f <- multisession({
stop("Whoops!")
1
})
print(f)
v <- value(f, signal = FALSE)
print(v)
stopifnot(inherits(v, "simpleError"))
res <- try(value(f), silent = TRUE)
print(res)
stopifnot(inherits(res, "try-error"))
## Error is repeated
res <- try(value(f), silent = TRUE)
print(res)
stopifnot(inherits(res, "try-error"))
message("*** multisession() - too large globals ...")
ooptsT <- options(future.globals.maxSize = object.size(1:1014))
limit <- getOption("future.globals.maxSize")
cat(sprintf("Max total size of globals: %g bytes\n", limit))
for (workers in unique(c(1L, availableCores()))) {
message("Max number of sessions: ", workers)
## A large object
a <- 1:1014
yTruth <- sum(a)
size <- object.size(a)
cat(sprintf("a: %g bytes\n", size))
f <- multisession({ sum(a) }, workers = workers)
print(f)
rm(list = "a")
v <- value(f)
print(v)
stopifnot(v == yTruth)
## A too large object
a <- 1:1015
yTruth <- sum(a)
size <- object.size(a)
cat(sprintf("a: %g bytes\n", size))
res <- try(f <- multisession({ sum(a) }, workers = workers), silent = TRUE)
rm(list = "a")
stopifnot(inherits(res, "try-error"))
}
## Undo options changed in this test
options(ooptsT)
message("*** multisession() - too large globals ... DONE")
message("*** multisession(..., workers = 1L) ...")
a <- 2
b <- 3
yTruth <- a * b
f <- multisession({ a * b }, workers = 1L)
rm(list = c("a", "b"))
v <- value(f)
print(v)
stopifnot(v == yTruth)
message("*** multisession(..., workers = 1L) ... DONE")
message("*** multisession(..., gc = TRUE) ...")
plan(multisession, workers = 2L)
f <- future({ gc() })
v <- value(f)
print(v)
f <- future({ integer(10e6) })
v <- value(f)
str(v)
f <- future({ gc() })
v <- value(f)
print(v)
f <- future({ integer(10e6) }, gc = TRUE)
v <- value(f)
str(v)
f <- future({ gc() })
v <- value(f)
print(v)
message("*** multisession(..., gc = TRUE) ... TRUE")
message("*** multisession(...) - stopping with plan() change ...")
plan(multisession, workers = 2L)
f <- future(1L)
cl <- ClusterRegistry("get")
stopifnot(inherits(cl, "cluster"), length(cl) >= 1L)
plan(sequential)
cl <- ClusterRegistry("get")
stopifnot(is.null(cl), length(cl) == 0L)
message("*** multisession(...) - stopping with plan() change ... DONE")
message(sprintf("Testing with %d cores ... DONE", cores))
} ## for (cores ...)
message("*** multisession() ... DONE")
source("incl/end.R")
|