Skip to content

Instantly share code, notes, and snippets.

@ruderphilipp
Created June 3, 2012 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruderphilipp/2863086 to your computer and use it in GitHub Desktop.
Save ruderphilipp/2863086 to your computer and use it in GitHub Desktop.
disabling of mc.preschedule in mcapply not possible with plyr
# file: <multicore>/R/mcapply.R
mclapply <- function(X, FUN, ..., mc.preschedule=TRUE, mc.set.seed=TRUE, mc.silent=FALSE, mc.cores=getOption("cores"), mc.cleanup=TRUE) {
# skip code...
if (!mc.preschedule) { # sequential (non-scheduled)
# => reaching this code is my goal!
# skip code...
}
# skip code...
}
# file: <doMC>/R/doMC.R
doMC <- function(obj, expr, envir, data) {
# set the default mclapply options
preschedule <- TRUE
set.seed <- TRUE
silent <- FALSE
cores <- workers(data)
# skip code...
# check for multicore-specific options
options <- obj$options$multicore
if (!is.null(options)) {
nms <- names(options)
recog <- nms %in% c('preschedule', 'set.seed', 'silent', 'cores')
if (any(!recog))
warning(sprintf('ignoring unrecognized multicore option(s): %s',
paste(nms[!recog], collapse=', ')), call.=FALSE)
if (!is.null(options$preschedule)) {
if (!is.logical(options$preschedule) || length(options$preschedule) != 1) {
warning('preschedule must be logical value', call.=FALSE)
} else {
if (obj$verbose)
cat(sprintf('setting mc.preschedule option to %d\n', options$preschedule))
preschedule <- options$preschedule
}
}
# skip code...
}
# skip code...
}
# file: <foreach>/R/foreach.R
foreach <- function(..., .combine, .init, .final=NULL, .inorder=TRUE,
.multicombine=FALSE,
.maxcombine=if (.multicombine) 100 else 2,
.errorhandling=c('stop', 'remove', 'pass'),
.packages=NULL, .export=NULL, .noexport=NULL,
.verbose=FALSE) {
# skip code...
args <- substitute(list(...))[-1]
if (length(args) == 0)
stop('no iteration arguments specified')
argnames <- names(args)
if (is.null(argnames))
argnames <- rep('', length(args))
# check for backend-specific options
options <- list()
opts <- grep('^\\.options\\.[A-Za-z][A-Za-z]*$', argnames)
if (length(opts) > 0) {
# put the specified options objects into the options list
for (i in opts) {
bname <- substr(argnames[i], 10, 100)
options[[bname]] <- list(...)[[i]]
}
# remove the specified options objects from args and argnames
args <- args[-opts]
argnames <- argnames[-opts]
}
# skip code...
}
# file: <plyr>/R/ply-list.r
llply <- function(.data, .fun = NULL, ..., .progress = "none", .inform = FALSE, .parallel = FALSE) {
# skip code...
if (.parallel) {
# skip code...
result <- foreach(i = seq_len(n)) %dopar% do.ply(i) # => no possibility to add optional code to foreach() call!
} else {
result <- loop_apply(n, do.ply)
}
# skip code...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment