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
|
message("Testing bplapply")
quiet <- suppressWarnings
test_bplapply_Params <- function()
{
cl <- parallel::makeCluster(2)
doParallel::registerDoParallel(cl)
params <- list(
serial=SerialParam(),
snow=SnowParam(2),
dopar=DoparParam()
)
if (.Platform$OS.type != "windows")
params$mc <- MulticoreParam(2)
x <- 1:10
expected <- lapply(x, sqrt)
for (param in names(params)) {
current <- quiet(bplapply(x, sqrt, BPPARAM=params[[param]]))
checkIdentical(expected, current)
}
# test empty input
for (param in names(params)) {
current <- quiet(bplapply(list(), identity, BPPARAM=params[[param]]))
checkIdentical(list(), current)
}
## clean up
foreach::registerDoSEQ()
parallel::stopCluster(cl)
closeAllConnections()
TRUE
}
test_bplapply_symbols <- function()
{
cl <- parallel::makeCluster(2)
doParallel::registerDoParallel(cl)
params <- list(
serial=SerialParam(),
snow=SnowParam(2),
dopar=DoparParam()
)
if (.Platform$OS.type != "windows")
params$mc <- MulticoreParam(2)
x <- list(as.symbol(".XYZ"))
expected <- lapply(x, as.character)
for (param in names(params)) {
current <- quiet(bplapply(x, as.character, BPPARAM=params[[param]]))
checkIdentical(expected, current)
}
## clean up
foreach::registerDoSEQ()
parallel::stopCluster(cl)
closeAllConnections()
TRUE
}
test_bplapply_named_list <- function()
{
X <- list()
Y <- character()
checkIdentical(X, bplapply(X, identity))
checkIdentical(X, bplapply(Y, identity))
names(X) <- names(Y) <- character()
checkIdentical(X, bplapply(X, identity))
checkIdentical(X, bplapply(Y, identity))
X <- list(a = 1:2)
checkIdentical(X, bplapply(X, identity))
X <- list(c(a = 1))
checkIdentical(X, bplapply(X, identity))
X <- list(A = c(a = 1:2, b = 1:3), B = c(b = 1:2))
checkIdentical(X, bplapply(X, identity))
X <- list(a = 1:2, b = 3:4)
checkIdentical(X, bplapply(X, identity))
X <- list(c(a = 1))
checkIdentical(X, bplapply(X, identity))
X <- list(A = c(a = 1, b=2), B = c(c = 1, d = 2))
checkIdentical(X, bplapply(X, identity))
}
test_bplapply_named_list_with_REDO <- function(){
X = setNames(1:3, letters[1:3])
param = SerialParam(stop.on.error = FALSE)
FUN1 = function(i) if (i == 2) stop() else i
result <- bptry(bplapply(X, FUN1, BPPARAM = param))
checkIdentical(names(result), names(X))
FUN2 = function(i) 0
redo <- bplapply(X, FUN2, BPREDO = result, BPPARAM = param)
checkIdentical(names(redo), names(X))
}
test_bplapply_custom_subsetting <- function(){
## We have a class A in the previous unit test
.B <- setClass("B", slots = c(b = "integer"))
setMethod("[", "B", function(x, i, j, ...) initialize(x, b = x@b[i]))
setMethod("length", "B", function(x) length(x@b))
as.list.B <<- function(x, ...) lapply(seq_along(x), function(i) x[i])
x <- .B(b = 1:3)
expected <- lapply(x, function(elt) elt@b)
current <- quiet(bplapply(x, function(elt) elt@b, BPPARAM=SerialParam()))
checkIdentical(expected, current)
## Remote worker does not have the definition of the class B
res <- tryCatch(
bplapply(x, function(elt) elt@b, BPPARAM=SnowParam(workers = 2)),
error = identity
)
checkTrue(is(res, "bplist_error"))
rm(as.list.B, inherits = TRUE)
}
test_bplapply_auto_export <- function(){
p <- SnowParam(2, exportglobals = FALSE)
## user defined symbols
assign("y", 10, envir = .GlobalEnv)
on.exit(rm(y, envir = .GlobalEnv))
fun <- function(x) y
environment(fun) <- .GlobalEnv
bpexportvariables(p) <- TRUE
res <- bplapply(1:2, fun, BPPARAM = p)
checkIdentical(res, rep(list(10), 2))
bpexportvariables(p) <- FALSE
checkException(bplapply(1:2, fun, BPPARAM = p), silent = TRUE)
## symbols defined in a package
fun2 <- function(x) SerialParam()
environment(fun2) <- .GlobalEnv
bpexportvariables(p) <- TRUE
res <- bplapply(1:2, fun2, BPPARAM = p)
checkTrue(is(res[[1]], "SerialParam"))
bpexportvariables(p) <- FALSE
checkException(bplapply(1:2, fun2, BPPARAM = p), silent = TRUE)
}
|