Skip to content
Prev 462 / 2152 Next

Parallel version of the code below - Dual core or more

I was trying to say that it is possible to parallelize iterative solutions
in some cases.  For instance, if you're starting from a solution that is
based on randomness, and iteratively improving it, you could
parallelize it in this fashion:

    library(doSNOW)

    # Create, initialize, and register a snow socket cluster
    cl <- makeSOCKcluster(2)
    clusterSetupRNG(cl)
    registerDoSNOW(cl)

    # Define a simple search function
    lookForBestSolution <- function(itermax) {
      # Pick a starting solution based on randomness
      cursol <- pickRandomSolution()

      # Repeatedly try to improve the solution
      for (i in seq(length=itermax)) {
        x <- tryToImproveSolution(cursol)
        cursol <- bestSolution(x, cursol)
      }

      # Return the best solution found
      cursol
    }

    # Define some very silly example functions for purposes of illustration
    pickRandomSolution <- function() rnorm(1)
    tryToImproveSolution <- function(x) x - rnorm(1)
    bestSolution <- function(...) min(...)

    # Define the total number of times you're willing to improve your solution
    itermax <- 500

    # Create an iterator that will return "numWorkers" values that sum
to itermax
    it <- idiv(itermax, chunks=getDoParWorkers())

    # Pick the best solution that the cluster workers find
    bestsol <- foreach(i=it, .combine='bestSolution') %dopar% {
      lookForBestSolution(i)
    }
    print(bestsol)

    # Shutdown the socket cluster
    stopCluster(cl)


Note that the call to clusterSetupRNG is essential for programs
of this type.  Otherwise, you could have each worker compute
exactly the same result, and you don't gain anything from parallelism.

- Steve


On Wed, Dec 9, 2009 at 10:39 PM, Debabrata Midya
<Debabrata.Midya at services.nsw.gov.au> wrote: