在 Rcpp 项目中,我希望能够使用call an R function(cobs
包中的cobs函数来进行凹样条曲线拟合)或它所依赖的call the fortran code(cobs
函数使用quantreg
的rq.fit.sfnc函数来拟合受约束的样条模型,该模型又依赖于srqfnc中的quantreg
包中的Fortran编码pragma openmp parallel for loop函数(我的其余代码主要需要一些简单的线性代数,所以这没问题,但遗憾的是,每个内循环迭代也需要我进行凹样条拟合)。我想知道这是否被允许或可能,因为我认为这样的调用不是线程安全的?是否有一个简单的解决方法,比如用#pragma omp critical
包围这些电话?有没有人有这方面的例子?或者在这种情况下,唯一的方法是首先使用线程安全的 Armadillo 类来完成Rcpp
和cobs
函数的完整rq.fit.sfnc
端口?
最佳答案
引用 the manual :
Calling any of the R API from threaded code is ‘for experts only’ and strongly discouraged. Many functions in the R API modify internal R data structures and might corrupt these data structures if called simultaneously from multiple threads. Most R API functions can signal errors, which must only happen on the R main thread. Also, external libraries (e.g. LAPACK) may not be thread-safe.
我一直将其解释为“不得从线程代码中调用 R API 函数”。无论内部使用什么,从 omp 并行区域内部调用 R 函数就是这样。使用
#pragma omp critical
可能有用,但如果它坏了,你必须保留碎片......重新实现有问题的代码或在 C++/C/Fortran 中查找现有实现并直接调用它会更安全。
关于r - 是否允许/可能在 Rcpp 中的 pragma openmp parallel for 循环中调用 R 函数或 fortran 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55242152/