相关疑难解决方法(0)

1511
推荐指数
11
解决办法
72万
查看次数

是否允许/可能在 Rcpp 中的 pragma openmp 并行 for 循环中调用 R 函数或 fortran 代码?

在 Rcpp 项目中,我希望能够调用 R 函数(包中的cobs函数cobs进行凹样条拟合)或调用它所依赖的fortran 代码cobs(该函数使用quantregrq.fit.sfnc函数来拟合约束样条模型,该模型又依赖于包中的 fortran 编码的srqfnc函数)在编译指示 openmp 并行 for 循环中(我的代码的其余部分主要需要一些简单的线性代数,所以这不会有问题,但是遗憾的是,每个内部循环迭代还需要我进行凹样条拟合)。我想知道这是否被允许或可能,因为我认为此类调用不是线程安全的?是否有一个简单的解决方案,比如用 包围这些调用?有人有这方面的例子吗?或者在这种情况下唯一的方法是首先使用线程安全的犰狳类来完成&函数的完整移植?quantreg#pragma omp criticalRcppcobsrq.fit.sfnc

r openmp spline rcpp quantile-regression

4
推荐指数
1
解决办法
267
查看次数

在 Rcpp 中编译多个源文件

我有以下目录结构

my_func
    - my_func_r.cpp
    - my_func.c
    - my_func.h
    - my_func_test.c
    - matrix/
      - matrix.h
      - matrix.c
Run Code Online (Sandbox Code Playgroud)

matrix目录包含一些矩阵结构matrix.h和一些初始化、自由、打印等功能matrix.c。该my_func.h文件类似于

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "matrix/matrix.h"

... some structures and templates ...
Run Code Online (Sandbox Code Playgroud)

然后my_func.c文件是

#include "my_func.h"

... helper functions ...

int my_func(...) {
    ... my_func stuff ...     
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

my_func_test.c是一样的东西

#include "my_func.h"

int main() {
    ... some test ...
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有了gcc/g++我可以运行得很好

gcc my_func_test.c my_func.c matrix/matrix.c …
Run Code Online (Sandbox Code Playgroud)

c++ r rcpp

2
推荐指数
1
解决办法
977
查看次数

Rcpp的mvtnorm :: pmvnorm实现比原始R函数慢

我正在尝试获得pmvnorm的Rcpp版本以至少与R中的mvtnorm :: pmvnorm一样快地工作。

我已经找到https://github.com/zhanxw/libMvtnorm并使用相关的源文件创建了一个Rcpp框架包。我添加了以下使用Armadillo的函数(因为我正在我编写的其他代码中使用它)。

//[[Rcpp::export]]
arma::vec triangl(const arma::mat& X){
  arma::mat LL = arma::trimatl(X, -1);  // omit the main diagonal
  return LL.elem(arma::find(LL != 0));
}

//[[Rcpp::export]]
double pmvnorm_cpp(arma::vec& bound, arma::vec& lowtrivec){
  double error;
  int n = bound.n_elem;
  double* boundptr = bound.memptr();
  double* lowtrivecptr = lowtrivec.memptr();
  double result = pmvnorm_P(n, boundptr, lowtrivecptr, &error);
  return result;
}
Run Code Online (Sandbox Code Playgroud)

从R构建程序包之后,这是一个可复制的示例:

set.seed(1)
covar <- rWishart(1, 10, diag(5))[,,1]
sds <- diag(covar) ^-.5
corrmat <- diag(sds) %*% covar %*% diag(sds)
triang <- triangl(corrmat)

bounds <- c(0.5, …
Run Code Online (Sandbox Code Playgroud)

r normal-distribution armadillo rcpp cdf

2
推荐指数
1
解决办法
284
查看次数