我已经使用DEoptim一段时间来测试水文算法的不同参数。代码主要用R编写,但有一个用Rcpp编写的函数。如果我在非并行模式下运行DEoptim,它运行正常,但如果我以并行模式运行(即paralleltype = 1),代码会返回一个错误,说它无法找到我的Rcpp函数。所以Rcpp函数看起来像这样:
loadcppfunctions <- function() {
eastfunc <<- 'NumericMatrix eastC(NumericMatrix e, NumericMatrix zerocolmatrix, NumericMatrix zerorowmatrix) {
int ecoln = e.ncol();
int ecolnlessone = ecoln - 1;
int erown = e.nrow();
int erownlessone = erown - 1;
NumericMatrix eout(e.nrow(),e.ncol()) ;
for (int j = 0;j < ecoln;j++) {
if (j > 0) {
eout(_,j) = e(_,j-1);
} else {
eout(_,j) = e(_,0);
}
}
eout(_,0) = zerocolmatrix(_,0);
return eout;
}'
eastC <<- cppFunction(eastfunc)
}
然后我只使用:
loadcppfunctions()
稍后在代码中我将此函数调用如下:
movefdrerunoff <- eastC(fdrerunoff, zerocolmatrix, zerorowmatrix)
正如我所说,一切正常 - 但如果我按如下方式运行DEoptim:
ans <- DEoptimone(Calibrate,lower,upper,DEoptim.control(trace=TRUE,parallelType=1,parVar=c(parVarnames),packages=c("raster","rgdal","maptools","matrixcalc","Rcpp","RcppArmadillo")))
它没说:
Error in checkForRemoteErrors(val) :
7 nodes produced errors; first error: could not find function "eastC"
那么当所有其他基于R的函数都正常时,如何让DEoptim看到这个函数。
谢谢,Antony Walker
答案 0 :(得分:-1)
我发现通过在主要的DEoptim函数(Calibrate)中添加Rcpp函数起作用。 Calibrate功能看起来像:
Calibrate <- function(x) {
eastfunc <<- 'NumericMatrix eastC(NumericMatrix e, NumericMatrix zerocolmatrix, NumericMatrix zerorowmatrix) {
int ecoln = e.ncol();
int ecolnlessone = ecoln - 1;
int erown = e.nrow();
int erownlessone = erown - 1;
NumericMatrix eout(e.nrow(),e.ncol()) ;
for (int j = 0;j < ecoln;j++) {
if (j > 0) {
eout(_,j) = e(_,j-1);
} else {
eout(_,j) = e(_,0);
}
}
eout(_,0) = zerocolmatrix(_,0);
return eout;
}'
eastC <<- cppFunction(eastfunc)
cmax <<- x[1]
Cr <<- x[2]
Cl <<- x[3]
Crb <<- x[4]
Clb <<- x[5]
returnflowriver <<- x[6]
returnflowland <<- x[7]
kd <<- x[8]
startyear()
-NashSutcliffe
}
然后运行DEoptim:
ans <- DEoptimone(Calibrate,lower,upper,DEoptim.control(trace=TRUE,parallelType=1,parVar=c(parVarnames),packages=c("raster","rgdal","maptools","matrixcalc","Rcpp","RcppArmadillo","moveCpp")))