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 bpiterate")
quiet <- suppressWarnings
.lazyCount <- function(count) {
i <- 0L
function() {
if (i >= count)
return(NULL)
i <<- i + 1L
i
}
}
test_bpiterate_Params <- function()
{
## chunks greater than number of workers
x <- 1:5
expected <- lapply(x, sqrt)
FUN <- function(count, ...) sqrt(count)
params <- list(serial=SerialParam(),
snow=SnowParam(2))
if (.Platform$OS.type != "windows")
params$mc <- MulticoreParam(2)
for (p in params) {
ITER <- .lazyCount(length(x))
quiet(res <- bpiterate(ITER, FUN, BPPARAM=p))
checkIdentical(expected, res)
}
## chunks less than number of workers
x <- 1:2
expected <- lapply(x, sqrt)
FUN <- function(count, ...) sqrt(count)
params <- list(serial=SerialParam(),
snow=SnowParam(3))
if (.Platform$OS.type != "windows")
params$mc <- MulticoreParam(3)
for (p in params) {
ITER <- .lazyCount(length(x))
quiet(res <- bpiterate(ITER, FUN, BPPARAM=p))
checkIdentical(expected, res)
}
cl <- parallel::makeCluster(2)
doParallel::registerDoParallel(cl)
params <- list(dopar=DoparParam())
for (p in params) {
ITER <- .lazyCount(length(x))
checkException(bpiterate(ITER, FUN, BPPARAM=p), silent=TRUE)
}
## clean up
foreach::registerDoSEQ()
parallel::stopCluster(cl)
closeAllConnections()
TRUE
}
test_bpiterate_REDUCE <- function() {
ncount <- 3L
params <- list(snow=SnowParam(ncount))
## On Windows MulticoreParam dispatches to SerialParam where
## 'reduce.in.order' does not apply (always TRUE)
if (.Platform$OS.type != "windows")
params <- c(params, multi=MulticoreParam(ncount))
for (p in params) {
## no REDUCE
FUN <- function(count, ...) rep(count, 10)
ITER <- .lazyCount(ncount)
res <- bpiterate(ITER, FUN, BPPARAM=p)
checkTrue(length(res) == ncount)
expected <- list(rep(1L, 10), rep(2L, 10), rep(3L, 10))
checkIdentical(expected, res)
## REDUCE
FUN <- function(count, ...) rep(count, 10)
ITER <- .lazyCount(ncount)
res <- bpiterate(ITER, FUN, BPPARAM=p, REDUCE=`+`)
checkIdentical(rep(6L, 10), res)
FUN <- function(count, ...) {
Sys.sleep(3 - count)
count
}
## 'reduce.in.order' FALSE
ITER <- .lazyCount(ncount)
res <- bpiterate(ITER, FUN, BPPARAM=p, REDUCE=paste0,
reduce.in.order=FALSE)
checkIdentical("321", res)
ITER <- .lazyCount(ncount)
res <- quiet(bpiterate(ITER, FUN, BPPARAM=p, REDUCE=paste0, init=0,
reduce.in.order=FALSE))
checkIdentical("0321", res)
## 'reduce.in.order' TRUE
ITER <- .lazyCount(ncount)
res <- bpiterate(ITER, FUN, BPPARAM=p, REDUCE=paste0,
reduce.in.order=TRUE)
checkIdentical("123", res)
ITER <- .lazyCount(ncount)
res <- bpiterate(ITER, FUN, BPPARAM=p, REDUCE=paste0,
init=0, reduce.in.order=TRUE)
checkIdentical("0123", res)
}
## clean up
closeAllConnections()
TRUE
}
test_bpiterate_REDUCE_SerialParam <- function() {
p <- SerialParam()
FUN <- identity
## REDUCE missing, concatenate
ITER <- .lazyCount(0)
res <- suppressWarnings({
## warning: first invocation of 'ITER()' returned NULL
bpiterate(ITER, FUN, BPPARAM=p)
})
checkIdentical(list(), res)
ITER <- .lazyCount(1)
res <- bpiterate(ITER, FUN, BPPARAM=p)
checkIdentical(list(1L), res)
ITER <- .lazyCount(5)
res <- bpiterate(ITER, FUN, BPPARAM=p)
checkIdentical(as.list(1:5), res)
## REDUCE == `+`
ITER <- .lazyCount(0)
res <- suppressWarnings({
## warning: first invocation of 'ITER()' returned NULL
res <- bpiterate(ITER, FUN, BPPARAM=p, REDUCE=`+`)
})
checkIdentical(NULL, res)
ITER <- .lazyCount(1)
res <- bpiterate(ITER, FUN, BPPARAM=p, REDUCE=`+`)
checkIdentical(1L, res)
ITER <- .lazyCount(5)
res <- bpiterate(ITER, FUN, BPPARAM=p, REDUCE=`+`)
checkIdentical(15L, res)
}
|