La paralelización del bucle externo funciona en REvolution, pero no en R normal

I am trying to parallelize an outer loop, while running the inner loop sequentially. The following code works in Revolution when using the doSMP package, but it does not work in base R when using the foreach y doParallel packages (both R versions on a Windows machine). The error message is: could not find function "%do%". Any ideas how to resolve this issue?

foreach(j = X, .combine = c) %dopar% { 
    Z=1
    foreach(i = Y, .combine = c) %do% { 
        paste(j, i, Z, sep = "") 
    } 
}

preguntado el 03 de mayo de 12 a las 17:05

1 Respuestas

In general, if you need to use a package inside a foreach loop, you should specify it with the ".packages" option. That's even true for the foreach package itself:

library(doParallel)
cl <- makePSOCKcluster(3)
registerDoParallel(cl)
foreach(j = X, .combine = c, .packages='foreach') %dopar% { 
    Z=1
    foreach(i = Y, .combine = c) %do% { 
        paste(j, i, Z, sep = "") 
    } 
}

This makes some sense when you realize that the PSOCK cluster doesn't even know that it's being used by foreach. It's just a normal PSOCK cluster.

Note that it may not be necessary to specify all required packages with some parallel backends. For example, with doMC the workers inherit all currently loaded packages. However, it is good practice to specify them so that the code is more portable.

Respondido el 17 de enero de 14 a las 14:01

Thanks Steve, this help a lot. - user1372987

This only applies if the foreach package is not using parallel as its backend, right? I didn't have this problem. - Lucero

@TrevorAlexander It isn't needed in this case with doMC. But this is a common source of problems on Windows because doMC examples won't work, or when running on a cluster using doSNOW or doMPI. - steve weston

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.