Why is parallel R code not faster on machine with more cores?

Brent Ferrier Source

I have lenovo w540 64 bit windows 7, 16 gb ram, Intel(R) Core(TM) i7-4600M CPU @ 2.90 GHz running 64 bit R 3.2.2

I also have a pc with 64 bit windows 7, 16 gb ram, Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40 GHz (processors)

I have a function that I run in parallel. On my 4-core laptop I ran the following code

library(splitstackshape)
library(foreach)
library(doParallel)

cores=detectCores()
cl <- makeCluster(cores[1]-1) 
registerDoParallel(cl)

#Initialize variables
partsOrder <- list()
numSim <- 10

#start clock
start <- proc.time()

SimResults <- foreach(j=1:100) %dopar%{
  RobotSimulation()
}

#stop using all cores
stopCluster(cl)

#stop the clock
end <- proc.time()-start
end[3]

What I dont understand is that on my 4 core laptop it takes 30 mins to do 100 iterations. On my 24 core laptop it takes the same amount of time. I can see all 24 cores being used in the task manager though and there are 24 R processes running. Does it matter to R if the cores are logical or physical?

EDIT: added RobotSimulation() code and some sample data

partsList <- data.frame(JobNum = sample(c('a','b','c','d'),4000,replace=TRUE)
,DS.CT = sample.int(10,4000,replace=TRUE)
,C1.CT = sample.int(10,4000,replace=TRUE)
,C2.CT = sample.int(10,4000,replace=TRUE)
,C3.CT = sample.int(10,4000,replace=TRUE)
,C4.CT = sample.int(10,4000,replace=TRUE)
,C5D5.CT = sample.int(10,4000,replace=TRUE)
,C6D6.CT = sample.int(10,4000,replace=TRUE)
,C5D7.CT = sample.int(10,4000,replace=TRUE)
,C6D8.CT = sample.int(10,4000,replace=TRUE)
,C7CD.CT = sample.int(10,4000,replace=TRUE)
)

RobotSimulation <- function(){

#randomize the dataset of parts and record the order
parts <- partsList[sample(nrow(partsList)),]
partsOrder <- list(parts$JobNum)

#choose a random sample of parts to populate the conveyor belts
#this random assignment will be constant across all iterations of the simulation
set.seed(101)
LineParts <- parts[sample(nrow(parts),234,replace=FALSE),]

#pass parts through system one at a time and record cycle times at each dip
LineParts_dfList <- lapply(seq(nrow(parts)), function(i){      
    #Index line
    LinePartsTemp <- parts[1,]
    LinePartsTemp[2:nrow(LineParts),] <- LineParts[1:nrow(LineParts)-1,]

    #put new part into system
    LinePartsTemp[1,] <- parts[i,]

    #update the list of parts on the line
    LineParts <<- LinePartsTemp      
})

#combine the results of passing all parts through the system
LinePartsAll <- do.call(rbind, LineParts_dfList)

#find the cycle times at each robot for each time a new part went through
otherstations_veclist <- 
  list(
    DS = LinePartsAll[seq(1,nrow(LinePartsAll), by=234),'DS.CT'],
    D1 = LinePartsAll[seq(10,nrow(LinePartsAll), by=234),'C1.CT'],
    D2 = LinePartsAll[seq(26,nrow(LinePartsAll), by=234),'C2.CT'],
    D3 = LinePartsAll[seq(42,nrow(LinePartsAll), by=234),'C3.CT'],
    D4 = LinePartsAll[seq(57,nrow(LinePartsAll), by=234),'C4.CT'],
    D5 = LinePartsAll[seq(85,nrow(LinePartsAll), by=234),'C5D5.CT'],
    D6 = LinePartsAll[seq(120,nrow(LinePartsAll), by=234),'C6D6.CT'],
    D7 = LinePartsAll[seq(167,nrow(LinePartsAll), by=234),'C5D7.CT'],
    D8 = LinePartsAll[seq(210,nrow(LinePartsAll), by=234),'C6D8.CT'],
    D9 = LinePartsAll[seq(216,nrow(LinePartsAll), by=234),'C7CD.CT']
  )

#record results
result <- list(partsOrder = partsOrder, CT = otherstations_veclist)
return(result)
}
rforeachparallel-processing

Answers

comments powered by Disqus