我有一个大的 RDS 文件,我想使用 R 并行处理它。加载时该文件需要 7.3 GB 的内存。
如果我尝试使用多个内核,R 会因为内存不足而崩溃。有没有办法告诉 mclapply 使用共享内存而不是复制对象?
这是我拥有的代码:
results <- readRDS('ResultsICC.RDS')
rand <- 0
Icc <- c(.5, 1, 1.5)
n <- c(.1, .5, 1)
phi <- c(0, .5, 1)
parameterSettings <- expand.grid(rand=rand, Icc=Icc, n=n, phi=phi)
rr <- list()
Ns <- results[[1]][[1]][[2]][,c('Country', 'n')]
EstimatedBestPFiveArmRaw <- matrix(NA, 26, 1000)
EstimatedBestP <- matrix(NA, 26, 1000)
outterloop <- function(dataIN){
for(k in 1:1000){ #1000
best <- dataIN[[k]][[2]]
EstimatedBestPFiveArmRaw[,k] <- rep(weighted.mean(best$estimatedBestPFiveArmRaw, best$n), 26)
pHat <- dataIN[[k]][[3]]
best <- Ns
best$estimatedBest <- best$estimateBestP <- NA
for(j in 1:26){ #26
best$estimatedBest <- sapply(split(pHat[,paste0('cohort', j+1, 'pHat')], pHat$Country),
which.max)
for(i in 1:nrow(best)) #nrow(best)
best$estimatedBestP[i] <- pHat$p[pHat$Country==best$Country[i] &
pHat$treatNum==best$estimatedBest[i]]
EstimatedBestP[j, k] <- weighted.mean(best$estimatedBestP, best$n)
}
rr <- (EstimatedBestP/EstimatedBestPFiveArmRaw-1)*100
}
return(rr)
}
library(parallel)
rr <- mclapply(X = results, FUN = outterloop, mc.cores = 27, mc.preschedule = T)
我在具有 32 个内核和 64GB 内存的 linux 机器中运行它。
谢谢!