[R-sig-hpc] Parallel version of the code below - Dual core or more

Stephen Weston stephen.b.weston at gmail.com
Fri Dec 11 03:02:51 CET 2009

On Thu, Dec 10, 2009 at 6:53 PM, Debabrata Midya
<Debabrata.Midya at services.nsw.gov.au> wrote:

> # Create an iterator that will return "numWorkers" values that sum to
> itermax
>     it <- idiv(itermax, chunks=getDoParWorkers())
> I think itermax is divided into "numWorkers" and a chunk of iterations will
> be allocated to each worker. Am I correct?

I think so.  To put it formally:

    itermax <- 50
    numWorkers <- 4
    it <- idiv(itermax, chunks=numWorkers)
    x <- as.list(it)
    stopifnot(itermax == sum(unlist(x)))
    stopifnot(numWorkers == length(x))

> # Pick a starting solution based on randomness
>       cursol <- pickRandomSolution()
> Does each worker receive random solution [that means initial random
> solutions are different from one worker to another]? Am I correct?

I think you're correct.  The important point is for each worker to start
searching in a different place than all of the other workers.  And if
you're doing that with a random number generator, it's important to
use clusterSetupRNG.

> # Pick the best solution that the cluster workers find
>     bestsol <- foreach(i=it, .combine='bestSolution') %dopar% {
>       lookForBestSolution(i)
>     }
> At the end, .combine='bestSolution' provides best solution as per the
> defined function bestSolution. That means this is a solution combined from
> different workers? Am I correct?

That sounds right, also.  I was using bestSolution within the
iteration loop to test for a better solution, but it's also needed to pick
which worker found the best result.

If I were you, I would play around with foreach in an interactive
session, without doing any parallel computing, to get a feel for
how everything works.  You can either use %do% instead of %dopar%,
or just don't register a parallel backend.  Running little interactive
experiments is really important to understanding anything in R, at least
for me.

Good luck,

- Steve

More information about the R-sig-hpc mailing list