Wednesday, December 30, 2015

parallel R using foreach and doMC

Using foreach and doMC for parallel R computing. The foreach run has to be assigned to an variable. 

See:
https://github.com/hongqin/demo/blob/master/R-parallel/foreach1.R
https://github.com/hongqin/mactower-network-failure-simulation/blob/master/1.grid.network.2015Oct/netwk_aging_sim.v0.1b.R


registerDoMC(cores=inputCores)


while ((j <= popSize) && ( count < popSize*5)) {
  myParrallStep = inputCores * 5
  bufferAges = foreach(i=1:myParrallStep) %dopar% {
    currentNetworkAge = single_network_failure_v2(lambda1, lambda2, degreeThreshold, p, pairs, essenLookupTb)
  }
  goodAges = bufferAges [bufferAges>0] #goodAges() return NULL? 
  count = count + myParrallStep
  if( length(goodAges)> 0 ){ 
    currentEnd = j + length(goodAges)-1
    popAges[j:currentEnd] = goodAges
    j = currentEnd + 1
  } else {
    print(paste("newk_aging_sim:: goodAges", goodAges))
  }
  if(debug>2) { 
    print(paste("netwk_aging_sim::bufferAges=",bufferAges))
  }
  if(debug) { 
    print(paste("netwk_aging_sim::bufferAges=",bufferAges))
    print(paste("netwk_aging_sim::count=",count, "  j=", j))
  } 
}# end of j while-loop, population loop

No comments:

Post a Comment