简体   繁体   English

如何从 R 包中的动态库评估 C 函数?

[英]How can I evaluate a C function from a dynamic library in an R package?

I'm trying to implement parallel computing in an R package that calls C from R with the .C function.我正在尝试在 R 包中实现并行计算,该包使用.C函数从 R 调用 C。 It seems that the nodes of the cluster can't access the dynamic library.好像集群的节点无法访问动态库。 I have made a parallel socket cluster, like this:我制作了一个并行套接字集群,如下所示:

cl <- makeCluster(2)

I would like to evaluate a C function called valgrad from my R package on each of the nodes in my cluster using clusterEvalQ , from the R package parallel.我想评价称作C函数valgrad从我ř包上的每个使用我的群集节点的clusterEvalQ ,从平行将R包。 However, my code is producing an error.但是,我的代码产生了错误。 I compile my package, but when I run我编译了我的包,但是当我运行时

out <- clusterEvalQ(cl, cresults <- .C(C_valgrad, …))

where represents the arguments in the C function valgrad .其中表示 C 函数valgrad的参数。 I get this error:我收到此错误:

Error in checkForRemoteErrors(lapply(cl, recvResult)) : 
  2 nodes produced errors; first error: object 'C_valgrad' not found

I suspect there is a problem with clusterEvalQ 's ability to access the dynamic library.我怀疑clusterEvalQ访问动态库的能力有问题。 I attempted to fix this problem by loading the glmm package into the cluster using我试图通过使用将 glmm 包加载到集群中来解决这个问题

clusterEvalQ(cl, library(glmm))

but that did not fix the problem.但这并没有解决问题。

I can evaluate valgrad on each of the clusters using the foreach function from the foreach R package, like this:我可以使用 foreach R 包中的foreach函数在每个集群上评估valgrad ,如下所示:

out <- foreach(1:no_cores) %dopar% {.C(C_valgrad, …)}

no_cores is the number of nodes in my cluster. no_cores是我的集群中的节点数。 However, this function doesn't allow any of the results of the evaluation of valgrad to be accessed in any subsequent calculation on the cluster.但是,此函数不允许在集群的任何后续计算中访问valgrad任何评估结果。

How can I either我怎样才能

(1) make the results of the evaluation of valgrad accessible for later calculations on the cluster or (1) 使valgrad的评估结果可用于稍后对集群的计算或

(2) use clusterEvalQ to evaluate valgrad ? (2) 使用clusterEvalQ来评估valgrad

You have to load the external library.您必须加载外部库。 But this is not done with library calls, it's done with dyn.load .但这不是通过library调用完成的,而是通过dyn.load完成的。
The following two functions are usefull if you work with more than one operating system, they use the built-in variable .Platform$dynlib.ext .如果您使用多个操作系统,以下两个函数.Platform$dynlib.ext用,它们使用内置变量.Platform$dynlib.ext
Note also the unload function.还要注意卸载功能。 You will need it if you develop a C functions library.如果您开发 C 函数库,您将需要它。 If you change a C function before testing it the dynamic library has to be unloaded, then (the new version) reloaded.如果在测试之前更改 C 函数,则必须卸载动态库,然后重新加载(新版本)。

See Writing R Extensions , file R-exts.pdf in the doc folder, section 5 or on CRAN .请参阅编写 R 扩展,文档文件夹中的文件 R-exts.pdf,第 5 节或CRAN

dynLoad <- function(dynlib){
    dynlib <- paste(dynlib, .Platform$dynlib.ext, sep = "")
    dyn.load(dynlib)
}

dynUnload <- function(dynlib){
    dynlib <- paste(dynlib, .Platform$dynlib.ext, sep = "")
    dyn.unload(dynlib)
}

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

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