简体   繁体   English

r - 使用 DoParallel 进行并行计算会导致错误

[英]r - Parallel Computing using DoParallel result in errors

I try to use DoParallel to speed up the process time of a sequential Monte Carlo simulation.我尝试使用 DoParallel 来加快顺序蒙特卡罗模拟的处理时间。 However for some reason I get several errors and I do not know what these errors causes.但是由于某种原因,我得到了几个错误,我不知道这些错误是什么原因造成的。

This is the code I have written:这是我写的代码:

cl <- makeCluster(5)

registerDoParallel(cl)

getDoParWorkers()



nsim2 = 100

NMS <- rep(NA,nsim2)



foreach(i=1:nsim2) %dopar% {

nsim1=100



TEST <- disc_YT(acc=10)

accY1 <- 1



Y1_valuation <- replicate(nsim1, disc_YT(steps = 52, acc=accY1, r0=TEST$r10, initial = 1000, Sf0=TEST$Sf10, dummy = 10, maxdummy = TEST$second))



Y1_payoff = rep(NA,nsim1)

for(j in 1:nsim1){

Y1_payoff[j]=Y1_valuation[1,j]}



Y1_payoff <- unlist(Y1_payoff, recursive = TRUE, use.names = TRUE)

Y1_payoff <- mean(Y1_payoff)



Y1_interest = rep(NA,nsim1)

for(j in 1:nsim1){

Y1_interest[j]=Y1_valuation[2,j]}



Y1_interest <- unlist(Y1_interest, recursive = TRUE, use.names = TRUE)

Y1_interest <- exp(-accY1*mean(Y1_interest))



final_payoff <- TEST$'discount 10 years' * max(TEST$'pay off', Y1_interest*Y1_payoff)

NMS[i] <- final_payoff

}

stopCluster(cl)

I got the following error messages:我收到以下错误消息:

Error: unexpected '}' in "}"

And

Error in { : task 1 failed - "could not find function "%>%""

It is not clear to me where these errors come from.我不清楚这些错误来自哪里。 What could cause these errors?什么可能导致这些错误?

Regarding the second error, you have to import the package dplyr when running the for-loop otherwise in your "worker"/"slave" the package is not loaded, eg关于第二个错误,您必须在运行 for 循环时导入 package dplyr 否则在“worker”/“slave”中 package 未加载,例如

foreach(i=1:nsim2,.packages=c("dplyr") %dopar% { ...}

Also, it could be that other packages are necessary to load as @Rui Barradas pointed out.此外,正如@Rui Barradas 指出的那样,可能需要加载其他包。 Make sure that all needed libraries are transferred to the workers/slaves.确保将所有需要的库转移到工人/奴隶。

The first error possibly occurs because of the second one.第一个错误可能是由于第二个错误而发生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM