对 R 中的 foreach/dopar 进行故障排除,在较大的模型函数中出现错误,但在 foreach 循环中没有错误

问题描述 投票:0回答:0

我正在使用 foreachdoParallel 并行化元群体(疾病)模型。简而言之,我尝试为每个元群体并行运行相同的过程(有大约 2000 个元群体)。然后,我在更大的 for 循环 中运行 60 天/步骤。这是该结构的简化版本(不幸的是,无法重现——该模型在这里写起来有点复杂,但我希望这能给出它的要点):

loaded.package.names <- c(sessionInfo()$basePkgs, names(sessionInfo()$otherPkgs))
loaded.functions <- c("%notin%", "is.blank")
loaded.objects <- c("beta")

model <- function(...){
for(i in 1:timesteps){
y <- foreach(p = 1:nrow(patches_status), 
     .combine = rbind,
     .packages = loaded.package.names, 
     .export = c(loaded.functions, loaded.objects),
     .verbose = TRUE) %dopar% {

exposed.n = data.table(exposed=min(sing_S[p],
        rpois(1, (sing_S[p]*(**beta*sum(sing_I[,p])** + 
              kernels$kernel.tot[p]*sum(sing_I))/(sing_S[p] + 
              sum(sing_E[ ,p]) + sum(sing_I[ ,p]) + sing_R[p]))

return(exposed.n)
} # end foreach loop
} # end timestep loop
                 ```
My issue is that when I run the main function, I get an error in the foreach loop:

**Error in { : task 1 failed - "non-numeric argument to binary operator**

This is related to the multiplication of two values which I believe are both numeric:
 - **beta** : a numeric value of 0.6
 - **sum(sing_I[ ,p])** : a numeric value consisting of the sum of a column extracted from a matrix, determined by the iteration of the loop (i.e., 'p'). This value is frequently zero.

Is one of these values not numeric somehow? Is there another issue?

Strangely, running the foreach loop alone works perfectly.

Thank you for any help!
r foreach parallel-processing numeric doparallel
© www.soinside.com 2019 - 2024. All rights reserved.