parSapply错误。函数不并行运行

时间:2019-07-09 11:57:46

标签: r amazon-web-services ubuntu parallel-processing

我有一个正常运行的功能,但是在AWS EC2上运行时,它仅使用一个核心,我想转换为并行

原始问题已解决(无并行):apply - test multiple conditions before moving rows

我已经在AWS EC2服务器上安装了英特尔MKL,并且仍然仅使用一个内核即可运行。我试图按如下方式编辑代码。

PipSize <- 0.00886

myfun <- function(x, df = EURUSD, Limit = PipSize, StopLoss = PipSize) {

  highComp <- which(df$High - df$Open[x] > Limit)
  highCompMin <- if(length(highComp) == 0) 0 else min(highComp)
  lowComp <- which(df$Open[x] - df$Low > StopLoss)
  lowCompMin <- if(length(lowComp) == 0) 0 else min(lowComp)

  if(highCompMin == 0 & lowCompMin == 0) {
    result <<- c(Limit = NA, Open = df$Open[x])
  } else if (highCompMin <= lowCompMin) {
    result <<- c(Limit = 1, Open = df$Open[x])
  } else {
    result <<- c(Limit= 0, Open = df$Open[x])
  }

  return(result)

}

t(sapply(1:10, function(x) myfun(x, df = EURUSD, Limit = PipSize, StopLoss = PipSize)))

返回错误的其他并行代码

library("parallel")
library("doParallel")
n.cores <- detectCores()
cl <- makeCluster(n.cores, type="FORK")
clusterExport(cl, "myfun")
parSapply(cl, 1:10, myfun)

stopCluster(clust)

错误:

Error in checkForRemoteErrors(val) : 
  8 nodes produced errors; first error: can only subtract from "POSIXt" objects

EURUSD负责人:

                  Date    Open    High     Low   Close
1  2016-01-03 17:00:00 1.08701 1.08713 1.08701 1.08713
2  2016-01-03 17:01:00 1.08712 1.08712 1.08712 1.08712
3  2016-01-03 17:02:00 1.08708 1.08722 1.08708 1.08722
4  2016-01-03 17:03:00 1.08717 1.08723 1.08717 1.08723
5  2016-01-03 17:04:00 1.08718 1.08718 1.08711 1.08711
6  2016-01-03 17:05:00 1.08703 1.08716 1.08701 1.08712
7  2016-01-03 17:06:00 1.08721 1.08721 1.08710 1.08710
8  2016-01-03 17:07:00 1.08712 1.08715 1.08712 1.08712
9  2016-01-03 17:08:00 1.08711 1.08720 1.08711 1.08713
10 2016-01-03 17:09:00 1.08716 1.08723 1.08708 1.08708

预期结果: 如果(高开盘价>限制)则返回1,如果(开盘价-低点> StopLoss)返回0。如果两者都不是,则将相同的开盘价与下一时期的高点和低点进行比较。返回1或0时,将“打开”增加+ 1,然后重复该过程。

1 个答案:

答案 0 :(得分:0)

我不确定问题的根源是什么,但是我弄弄了代码,它现在可以工作了,并认为它可以使其他人受益。

library("parallel")
library("doParallel")

n.cores <- detectCores()
cl <- makeCluster(n.cores, type="FORK")
clusterExport(cl, "myfun")
t(parSapply(cl, i:j, function(x) myfun(x, df = EURUSD2, Limit = k, StopLoss = k)))
stopCluster(cl)

我将参数添加到parSapply的myfun部分,并更改了stopCluster参数。