Skip to content
Prev 2055 / 2152 Next

speed up a function containing resampling

Dear John,


there are two things I recognized:

a) do not use do.call within mclapply: in this case, tii.calc is run once, and the result returned to mclapply, which is not a function; use the ... in mclapply instead to provide your config
b) in tii.calc you will need an argument for the iteration integer, can also be ..., see below

Put in a simple repruducible example:

library(parallel)
config <- list(a=2, b=2)
f <- function (a = 1, b = 1, ...) a + b
iter <- 1:3
mclapply(iter, do.call(f, config), mc.cores = 3) # this breaks, because f is called and returns a value, which is not a function
mclapply(iter, f, a = 2, b = 2, mc.cores = 3) # this breaks, if f() has no argument for each value in iter, otherwise runs; a=2 and b=2 get forwarded to f for each iter

So this needs to change:
a) mclapply(iters, tii.calc, treelist=miltrees, taxnames=nom, full.trees=fulltree, outgroup=outgroup, burnin=burnin, mc.cores=ncores)
b) tii.calc <- function(treelist, taxnames, full.trees, outgroup=NULL, burnin=NULL, ...) {...}


Hope this helps,

Sven