I have written some Rcpp functions in SPDSC.cpp file and I tried to pass these functions (Norm, Sgn, SPDTestStat, and SPDPermTest) through "foreach" function. But I was getting the error message
Error in { : task 1 failed - "NULL value passed as symbol address" In addition: Warning message: In e$fun(obj, substitute(ex), parent.frame(), e$data) : already exporting variable(s): SPDPermTest
I tried a bunch of things searching many resources, but I couldn't get it to work. Any help would be appreciated. Thanks in advance!!
library(Rcpp)
sourceCpp("my/address/SPDSC.cpp")
library(parallel)
library(foreach)
library(doParallel)
library(doSNOW)
library(doMPI)
powLoc <- function(theta,p)
{
n <- 10
iter <- 50
r <- 0
for(it in 1:iter)
{
data1 <- matrix(0,n,p)
for(i in 1:n)
{
data1[i,]<-rnorm(p)
}
data2 <- matrix(0,n,p)
for(i in 1:n)
{
data2[i,]<-rnorm(p,theta,1)
}
p <- SPDPermTest(data1,data2,20)
r <- r+ifelse(p<0.05,1,0)
}
return(r/iter)
}
no_cores <- detectCores() - 1
cl <- makeCluster(no_cores, type = "SOCK")
thetav <- seq(-3,3,by=1)
pv <- 2*10^(1:2)
registerDoSNOW(cl)
x <- foreach(theta = thetav, .combine = 'cbind',.packages = "Rcpp"
,.export = c("Norm","Sgn","SPD","SPDPermTest","SPDTestStat")
) %:%
foreach(p = pv, .combine = 'c' ,.packages = "Rcpp"
,.export = c("Norm","Sgn","SPD","SPDPermTest","SPDTestStat")
) %dopar% {
powLoc(theta,p)
}
stopCluster(cl)