0

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)
EzLo
  • 13,780
  • 10
  • 33
  • 38
John_Wick
  • 101
  • 1
  • 2
    In short, write a package and access the C++ functions from the package. In long, the SPDSC file must be compiled on each node of the cluster. If you look at the function definition, you'll notice it goes to a pointer. – coatless Dec 20 '18 at 15:07
  • Can you please recommend me some resources? Thanks. – John_Wick Dec 20 '18 at 15:10
  • Here a walk through for creating a simple package: https://stackoverflow.com/questions/50354302/rcpp-sourcecpp-undefined-symbol/50358745#50358745 – Ralf Stubner Dec 20 '18 at 15:40

0 Answers0