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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
### =========================================================================
### Write array blocks
### -------------------------------------------------------------------------
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### write_block()
###
### 'sink' must be a **writable** array-like object, typically a
### RealizationSink concrete subclass. We also make write_block() work
### on ordinary arrays or other in-memory array-like objects (e.g.
### sparseMatrix derivatives from the Matrix package) by defining
### the default method below.
### For now dispatch is only on the first argument ('sink') but we
### could change this in the future to also dispatch on the third
### argument ('block') when the need arises.
### Must return 'sink' (possibly modified if it's an in-memory object).
setGeneric("write_block", signature="sink",
function(sink, viewport, block)
{
stopifnot(is(viewport, "ArrayViewport"),
identical(refdim(viewport), dim(sink)),
identical(dim(block), dim(viewport)))
standardGeneric("write_block")
}
)
### Work on any array-like object that supports '[<-'.
setMethod("write_block", "ANY",
function(sink, viewport, block)
{
if (is(block, "SparseArraySeed"))
block <- sparse2dense(block)
Nindex <- makeNindexFromArrayViewport(viewport)
replace_by_Nindex(sink, Nindex, block)
}
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### RealizationSink objects
###
### Virtual class with no slots. Intended to be extended to support specific
### realization backends. Concrete subclasses must implement the "sink
### contract", that is:
### 1) A constructor function where the first 3 arguments are 'dim',
### 'dimnames', and 'type', in that order. Optionally it can have
### the 'as.sparse' argument, in which case this **must** be the 4th
### argument. It can have any additional argument.
### 2) A dim(), dimnames(), and type() method.
### 3) A write_block() method. It must return the modified array-like
### object 'sink'.
### 4) A close() method (optional).
### 5) Coercion to DelayedArray.
###
### Examples of RealizationSink concrete subclasses: arrayRealizationSink
### (see below), RleRealizationSink (see RleArray-class.R),
### HDF5RealizationSink and TENxRealizationSink (see HDF5Array package).
setClass("RealizationSink", representation("VIRTUAL"))
setGeneric("close")
### The default "close" method for RealizationSink objects is a no-op.
setMethod("close", "RealizationSink", function(con) invisible(NULL))
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### arrayRealizationSink objects
###
### The arrayRealizationSink class is a concrete RealizationSink subclass that
### implements an in-memory realization sink.
###
setClass("arrayRealizationSink",
contains="RealizationSink",
representation(
result_envir="environment"
)
)
.get_arrayRealizationSink_result <- function(sink)
{
get("result", envir=sink@result_envir)
}
.set_arrayRealizationSink_result <- function(sink, result)
{
assign("result", result, envir=sink@result_envir)
}
setMethod("dim", "arrayRealizationSink",
function(x) dim(.get_arrayRealizationSink_result(x))
)
arrayRealizationSink <- function(dim, dimnames=NULL, type="double")
{
result <- array(get(type)(0), dim=dim, dimnames=dimnames)
result_envir <- new.env(parent=emptyenv())
sink <- new("arrayRealizationSink", result_envir=result_envir)
.set_arrayRealizationSink_result(sink, result)
sink
}
setMethod("write_block", "arrayRealizationSink",
function(sink, viewport, block)
{
result <- .get_arrayRealizationSink_result(sink)
result <- write_block(result, viewport, block)
.set_arrayRealizationSink_result(sink, result)
}
)
setAs("arrayRealizationSink", "DelayedArray",
function(from) DelayedArray(.get_arrayRealizationSink_result(from))
)
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Get/set the "realization backend" for the current session
###
.auto_realization_backend_envir <- new.env(parent=emptyenv())
getAutoRealizationBackend <- function()
{
get_user_option("auto.realization.backend")
}
getRealizationBackend <- function()
{
.Defunct("getAutoRealizationBackend")
getAutoRealizationBackend()
}
.SUPPORTED_REALIZATION_BACKENDS <- data.frame(
BACKEND=c("RleArray", "HDF5Array", "TENxMatrix"),
package=c("DelayedArray", "HDF5Array", "HDF5Array"),
realization_sink_class=c("RleRealizationSink",
"HDF5RealizationSink",
"TENxRealizationSink"),
stringsAsFactors=FALSE
)
supportedRealizationBackends <- function()
{
ans <- .SUPPORTED_REALIZATION_BACKENDS[ , c("BACKEND", "package")]
backend <- getAutoRealizationBackend()
Lcol <- ifelse(ans[ , "BACKEND"] %in% backend, "->", "")
Rcol <- ifelse(ans[ , "BACKEND"] %in% backend, "<-", "")
cbind(data.frame(` `=Lcol, check.names=FALSE),
ans,
data.frame(` `=Rcol, check.names=FALSE))
}
### NOT exported.
load_BACKEND_package <- function(BACKEND)
{
if (!isSingleString(BACKEND))
stop(wmsg("'BACKEND' must be a single string or NULL"))
backends <- .SUPPORTED_REALIZATION_BACKENDS
m <- match(BACKEND, backends[ , "BACKEND"])
if (is.na(m))
stop(wmsg("\"", BACKEND, "\" is not a supported backend. Please ",
"use supportedRealizationBackends() to get the list of ",
"\"realization backends\" that are currently supported."))
package <- backends[ , "package"][[m]]
class_package <- attr(BACKEND, "package")
if (is.null(class_package)) {
attr(BACKEND, "package") <- package
} else if (!identical(package, class_package)) {
stop(wmsg("\"package\" attribute on supplied 'BACKEND' is ",
"inconsistent with package normally associated with ",
"this backend"))
}
library(package, character.only=TRUE)
stopifnot(getClass(BACKEND)@package == package)
}
.check_realization_sink_constructor <- function(realization_sink_constructor)
{
stopifnot(is.function(realization_sink_constructor))
ok <- identical(head(formalArgs(realization_sink_constructor), n=3L),
c("dim", "dimnames", "type"))
if (!ok)
stop(wmsg("the first 3 arguments of a RealizationSink constructor ",
"function must be 'dim', 'dimnames', and 'type', in ",
"that order"))
## Either 'realization_sink_constructor' has the 'as.sparse' argument,
## in which case it **must** be in 4th position, or it does not have it.
m <- match("as.sparse", formalArgs(realization_sink_constructor))
if (!(m %in% c(4L, NA_integer_)))
stop(wmsg("RealizationSink constructor functions with an 'as.sparse' ",
"argument must have it in 4th position"))
}
.get_realization_sink_constructor <- function(BACKEND)
{
backends <- .SUPPORTED_REALIZATION_BACKENDS
m <- match(BACKEND, backends[ , "BACKEND"])
realization_sink_class <- backends[ , "realization_sink_class"][[m]]
package <- backends[ , "package"][[m]]
realization_sink_constructor <- get(realization_sink_class,
envir=.getNamespace(package),
inherits=FALSE)
.check_realization_sink_constructor(realization_sink_constructor)
realization_sink_constructor
}
setAutoRealizationBackend <- function(BACKEND=NULL)
{
if (is.null(BACKEND)) {
remove(list=ls(envir=.auto_realization_backend_envir),
envir=.auto_realization_backend_envir)
} else {
load_BACKEND_package(BACKEND)
auto_realization_sink_constructor <-
.get_realization_sink_constructor(BACKEND)
assign("AUTO_REALIZATION_SINK_CONSTRUCTOR",
auto_realization_sink_constructor,
envir=.auto_realization_backend_envir)
}
set_user_option("auto.realization.backend", BACKEND)
return(invisible(NULL))
}
setRealizationBackend <- function(...)
{
.Defunct("setAutoRealizationBackend")
setAutoRealizationBackend(...)
}
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Backend-agnostic RealizationSink constructor
###
.get_auto_realization_sink_constructor <- function()
{
if (is.null(getAutoRealizationBackend()))
return(arrayRealizationSink)
auto_realization_sink_constructor <-
try(get("AUTO_REALIZATION_SINK_CONSTRUCTOR",
envir=.auto_realization_backend_envir),
silent=TRUE)
if (is(auto_realization_sink_constructor, "try-error"))
stop(wmsg("This operation requires a \"realization backend\". ",
"Please see '?setAutoRealizationBackend' for how ",
"to set one."))
auto_realization_sink_constructor
}
AutoRealizationSink <- function(dim, dimnames=NULL, type="double",
as.sparse=FALSE)
{
realization_sink_constructor <- .get_auto_realization_sink_constructor()
args <- list(dim, dimnames, type)
formal_args <- formalArgs(realization_sink_constructor)
if (length(formal_args) >= 4L && formal_args[[4L]] == "as.sparse")
args <- c(args, list(as.sparse=as.sparse))
do.call(realization_sink_constructor, args)
}
RealizationSink <- function(...)
{
.Defunct("AutoRealizationSink")
AutoRealizationSink(...)
}
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### sinkApply()
###
### Thin wrapper around gridReduce().
###
### Note that, despite its name, this is actually a convenience wrapper
### around gridReduce(), and **not** around gridApply(). However, we call it
### sinkApply() and make its interface look similar to the gridApply/blockApply
### interface because this seems more user-friendly.
### Finally note that an important difference with gridReduce() is that the
### first two arguments of callback function 'FUN' are expected to be 'sink'
### and 'viewport' (in that order) rather than 'viewport' and 'init'.
sinkApply <- function(sink, FUN, ..., grid=NULL, verbose=NA)
{
if (!is(sink, "RealizationSink"))
stop(wmsg("'sink' must be a RealizationSink derivative"))
FUN <- match.fun(FUN)
grid <- normarg_sink_grid(grid, sink)
verbose <- normarg_verbose(verbose)
## Only purpose of this wrapper is to swap the order of the first
## two arguments.
FUN_WRAPPER <- function(viewport, init, FUN, ...)
{
effective_grid <- effectiveGrid()
current_block_id <- currentBlockId()
set_grid_context(effective_grid, current_block_id)
FUN(init, viewport, ...)
}
gridReduce(FUN_WRAPPER, grid, sink, FUN, ..., verbose=verbose)
}
|