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
|
#
# Copyright (c) 2008-2009, REvolution Computing, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
.foreachGlobals <- new.env(parent=emptyenv())
# this is called to register a parallel backend
setDoPar <- function(fun, data=NULL, info=function(data, item) NULL) {
assign('fun', fun, pos=.foreachGlobals, inherits=FALSE)
assign('data', data, pos=.foreachGlobals, inherits=FALSE)
assign('info', info, pos=.foreachGlobals, inherits=FALSE)
}
# this explicitly registers a sequential parallel backend
registerDoSEQ <- function() {
setDoPar(doSEQ, NULL, info)
}
# passed to setDoPar via registerDoSEQ, and called by getDoParWorkers, etc
info <- function(data, item) {
switch(item,
workers=1L,
name='doSEQ',
version=packageDescription('foreach', fields='Version'),
NULL)
}
# this returns a logical value indicating if a parallel backend
# has been registered or not
getDoParRegistered <- function() {
exists('fun', where=.foreachGlobals, inherits=FALSE)
}
# this returns the number of workers used by the currently registered
# parallel backend
getDoParWorkers <- function() {
wc <- if (exists('info', where=.foreachGlobals, inherits=FALSE))
.foreachGlobals$info(.foreachGlobals$data, 'workers')
else
NULL
# interpret a NULL as a single worker, but the backend
# can return NA without interference
if (is.null(wc)) 1L else wc
}
# this returns the name of the currently registered parallel backend
getDoParName <- function() {
if (exists('info', where=.foreachGlobals, inherits=FALSE))
.foreachGlobals$info(.foreachGlobals$data, 'name')
else
NULL
}
# this returns the version of the currently registered parallel backend
getDoParVersion <- function() {
if (exists('info', where=.foreachGlobals, inherits=FALSE))
.foreachGlobals$info(.foreachGlobals$data, 'version')
else
NULL
}
# used internally to get the currently registered parallel backend
getDoPar <- function() {
if (exists('fun', where=.foreachGlobals, inherits=FALSE)) {
list(fun=.foreachGlobals$fun, data=.foreachGlobals$data)
} else {
if (!exists('warningIssued', where=.foreachGlobals, inherits=FALSE)) {
warning('executing %dopar% sequentially: no parallel backend registered',
call.=FALSE)
assign('warningIssued', TRUE, pos=.foreachGlobals, inherits=FALSE)
}
list(fun=doSEQ, data=NULL)
}
}
'%do%' <- function(obj, ex) {
doSEQ(obj, substitute(ex), parent.frame())
}
'%dopar%' <- function(obj, ex) {
e <- getDoPar()
e$fun(obj, substitute(ex), parent.frame(), e$data)
}
doSEQ <- function(obj, expr, envir, data) {
# note that the "data" argument isn't used
if (!inherits(obj, 'foreach'))
stop('obj must be a foreach object')
it <- iter(obj)
accumulator <- makeAccum(it)
for (p in obj$packages)
library(p, character.only=TRUE)
i <- 1
tryCatch({
repeat {
# get the next set of arguments
args <- nextElem(it)
if (obj$verbose) {
cat(sprintf('evaluation # %d:\n', i))
print(args)
}
# assign arguments to local environment
for (a in names(args))
assign(a, args[[a]], pos=envir, inherits=FALSE)
# evaluate the expression
r <- tryCatch(eval(expr, envir=envir), error=function(e) e)
if (obj$verbose) {
cat('result of evaluating expression:\n')
print(r)
}
# process the results
tryCatch(accumulator(list(r), i), error=function(e) {
cat('error calling combine function:\n')
print(e)
NULL
})
i <- i + 1
}
},
error=function(e) {
if (!identical(conditionMessage(e), 'StopIteration'))
stop(simpleError(conditionMessage(e), expr))
})
errorValue <- getErrorValue(it)
errorIndex <- getErrorIndex(it)
if (identical(obj$errorHandling, 'stop') && !is.null(errorValue)) {
msg <- sprintf('task %d failed - "%s"', errorIndex,
conditionMessage(errorValue))
stop(simpleError(msg, call=expr))
} else {
getResult(it)
}
}
|