我正在尝试合并一个大data.frame
的和一个小的,并并行化计算。下面的代码完美运行,最大限度地利用了我机器的所有内核:
len <- 2000000
set.seed(666)
dat = paste(sample(letters, len, rep = T), sample(0:9, len, rep = T), sample(letters, len, rep = T), sep = '') # create a vector of strings that are 3-long
head(dat)
set.seed(777)
num <- sample(0:9, len, replace = T)
bigDF <- data.frame(dat = dat, num = num)
smallDF <- data.frame(num = 0:9, caps = toupper(letters[1:10]))
startP <- 1
chunk <- 10000
nodes <- detectCores()
cl <- makeCluster(nodes)
registerDoParallel(cl)
mergedList <- foreach(i = 0:(len/chunk - 1)) %dopar% {
tmpDF = bigDF[(startP + i * chunk):(startP - 1 + (i + 1) * chunk), ]
merge(tmpDF, smallDF, by = 'num', all.x = T)
}
stopCluster(cl)
一旦我将向量更改dat
为包含 5 长的字符串,并行性就会崩溃,尽管没有错误或警告,但只有 1 个核心参与计算:
len <- 2000000
set.seed(666)
dat = paste(sample(letters, len, rep = T), sample(0:9, len, rep = T), sample(letters, len, rep = T), sample(letters, len, rep = T), sample(letters, len, rep = T), sample(letters, len, rep = T), sep = '') # create a vector of strings that are 6-long
head(dat)
set.seed(777)
num <- sample(0:9, len, replace = T)
bigDF <- data.frame(dat = dat, num = num)
smallDF <- data.frame(num = 0:9, caps = toupper(letters[1:10]))
startP <- 1
chunk <- 10000
nodes <- detectCores()
cl <- makeCluster(nodes)
registerDoParallel(cl)
mergedList <- foreach(i = 0:(len/chunk - 1)) %dopar% {
tmpDF = bigDF[(startP + i * chunk):(startP - 1 + (i + 1) * chunk), ]
merge(tmpDF, smallDF, by = 'num', all.x = T)
}
stopCluster(cl)
为什么会出现这种不一致,以及如何解决它?在特定示例中,如果有人索引dat
整数,则代码有效。但索引并不是所有情况下的答案。为什么弦的长度与所使用的核心数量有关?