Quick question regarding multicore versions of mapply. Package
'multicore'
provides a parallelized version of 'lapply', called 'mclapply'.
I haven't
found any parallelized versions of 'mapply', however (although one can
use
the lower level function 'parallel', it becomes harder to control the
number
of spawned processes etc).
Is anyone aware of a parallelized version of mapply?
In case anyone finds it interesting, I have coded my pedestrian attempt to a
parallelized 'mapply', which basically sets up a series of calls and
then
uses mclapply. It's clearly sub-optimal, as setting up the series of calls
is done with a 'for' which can have a non-negligible cost. Could this be
useful for the multicore package? Any suggestions?
mcmapply <- function(FUN, ..., mc.preschedule = TRUE, mc.set.seed = TRUE,
mc.silent = FALSE, mc.cores=1) {
FUN <- match.fun(FUN)
dots <- list(...)
if (length(dots)==1) {
ans <- mclapply(dots[[1]], FUN, mc.preschedule=mc.preschedule,
mc.set.seed=mc.set.seed, mc.silent=mc.silent, mc.cores=mc.cores)
} else {
l <- length(dots[[1]])
if (any(sapply(dots,length)!=l)) stop("Arguments must be lists of the
same length")
elem <- mycall <- vector("list", l)
for (i in 1:length(elem)) {
elem[[i]] <- lapply(dots, "[[", i)
mycall[[i]] <- paste("do.call(FUN,
elem[[",i,"]])",sep="")
}
ans <- mclapply(mycall, function(z) eval(parse(text=z)),
mc.preschedule=mc.preschedule, mc.set.seed=mc.set.seed, mc.silent=mc.silent,
mc.cores=mc.cores)
}
return(ans)
}
Here's a couple test examples to compute (1:5)^2.
> mcmapply(function(x) x^2, as.list(1:5), mc.cores=2) #basically the same
as mclapply
[[1]]
[1] 1
[[2]]
[1] 4
[[3]]
[1] 9
[[4]]
[1] 16
[[5]]
[1] 25
> mcmapply(function(x,y) x*y, as.list(1:5), as.list(1:5), mc.cores=2)
[[1]]
[1] 1
[[2]]
[1] 4
[[3]]
[1] 9
[[4]]
[1] 16
[[5]]
[1] 25
Best,
David
[[alternative HTML version deleted]]