[R] multicore - shared memory

Rune Schjellerup Philosof rphilosof at health.sdu.dk
Wed Mar 18 09:57:03 CET 2009

I am trying to optimize some calculations using the package multicore.
I have a function that returns three variables of interest in a list.
Normally I would do a for loop over (i in 1:n) but the calculations are
independent so I could use mclapply(1:n, loopbody(i)).

  loopbody <- function(i) { waveletsmooth(resampled[, i]) }
  mclapply(1:n, loopbody, mc.preschedule=FALSE)

Except that I am really not interested in returning a list of n lists
each containing the three variables.
I would like to have three variables each containing the data in n columns.

  base = smooth = matrix(0, l, n)
  loopbody <- function(i) {
    tmp <- waveletsmooth(resampled[, i]);
    base[, i] <<- tmp[["basecorrect"]];
    smooth[, i] <<- tmp[["nobasecorrect"]];
  mclapply(1:n, loopbody, mc.preschedule=FALSE);

Frustratingly, this does not work, as is obvious from help(multicore),
since the child threads use copy-on-write on the memory inherited from
the master.

So my question is:
How do I allow the child threads to work on the variables base and
smooth as shared memory, so that I don't have to use sendMaster or
similar functions that copy the results?

Med venlig hilsen
Rune Schjellerup Philosof

More information about the R-help mailing list