我知道我遇到的问题是线程安全问题。正如我现在所拥有的代码,将使用“ seThreadOptions(1)”执行。我的问题是克服这种情况的最佳做法。
我知道这一点:Threadsafe function pointer with Rcpp and RcppParallel via std::shared_ptr将以某种方式发挥作用。而且我也一直在思考/试着让内部函数成为并行工作者的结构的一部分。实际上,我在调用两个内部函数,我希望一个是可变的,另一个是恒定的,这使我认为我将需要两个解决方案。
错误是rstudio中的R会话崩溃。 这里需要注意的两件事: 1.如果我“ setThreadOptions(1)”可以正常运行。 2.如果我将“ myfunc”移动到主cpp文件中并简单地进行调用“ myfunc”,则此操作也很好。
这是一个详细的示例:
第一个cpp文件:
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::interfaces(cpp)]]
// [[Rcpp::plugins(cpp11)]]
#include "RcppArmadillo.h"
using namespace arma;
using namespace std;
// [[Rcpp::export]]
double myfunc(arma::vec vec_in){
int Len = arma::size(vec_in)[0];
return (vec_in[0] +vec_in[1])/Len;
}
第二个cpp文件:
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(RcppParallel)]]
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(ParallelExample)]]
#include "RcppArmadillo.h"
#include "RcppParallel.h"
#include "ParallelExample.h"
#include <random>
#include <memory>
#include <math.h>
using namespace Rcpp;
using namespace arma;
using namespace RcppParallel;
using namespace std;
struct PARALLEL_WORKER : public Worker{
const arma::vec &input;
arma::vec &output;
PARALLEL_WORKER(const arma::vec &input, arma::vec &output) : input(input), output(output) {}
void operator()(std::size_t begin, std::size_t end){
std::mt19937 engine(1);
// Create a loop that runs through a selected section of the total Boot_reps
for( int k = begin; k < end; k ++){
engine.seed(k);
arma::vec index = input;
std::shuffle( index.begin(), index.end(), engine);
output[k] = ParallelExample::myfunc(index);
}
}
};
// [[Rcpp::export]]
arma::vec Parallelfunc(int Len_in){
arma::vec input = arma::regspace(0, 500);
arma::vec output(Len_in);
PARALLEL_WORKER parallel_woker(input, output);
parallelFor( 0, Len_in, parallel_woker);
return output;
}
Makevars,因为我使用的是Macintosh:
CXX_STD = CXX11
PKG_CXXFLAGS += -I../inst/include
和命名空间:
exportPattern("^[[:alpha:]]+")
importFrom(Rcpp, evalCpp)
importFrom(RcppParallel,RcppParallelLibs)
useDynLib(ParallelExample, .registration = TRUE)
export(Parallelfunc)
答案 0 :(得分:1)
调用ParallelExample::myfunc
时,您正在调用inst/include/ParallelExample_RcppExport.h
中定义的使用R API的函数。这是在平行上下文中绝对不能做的事情。我看到两种可能性:
myfunc
转换为仅标头并将其包含在int/include/ParallelExample.h
中。myfunc
的适当声明放入src/first.h
中,将该文件同时包含在src/first.cpp
和src/second.cpp
中,然后调用myfunc
,而不是ParallelExample::myfunc
。毕竟,如果您只想在同一程序包中调用函数,则不必向R注册函数。使用R注册是用于从外部调用的函数。答案 1 :(得分:0)
在某种程度上,这有点违反了Rcpp的内置接口cpp功能的目的。
首先,将cpp保存为“ ExampleInternal.h”:
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins(cpp11)]]
#include "RcppArmadillo.h"
using namespace arma;
using namespace std;
namespace ExampleInternal
{
double myfunc3(arma::vec vec_in){
int Len = arma::size(vec_in)[0];
return (vec_in[0] +vec_in[1])/Len;
}
}
第二:
#include "ParallelExample.h"
#include "ExampleInternal.h"
#include <random>
#include <memory>
#include <math.h>
using namespace Rcpp;
using namespace arma;
using namespace RcppParallel;
using namespace ExampleInternal;
using namespace std;
struct PARALLEL_WORKER : public Worker{
const arma::vec &input;
arma::vec &output;
PARALLEL_WORKER(const arma::vec &input, arma::vec &output) : input(input), output(output) {}
void operator()(std::size_t begin, std::size_t end){
std::mt19937 engine(1);
// Create a loop that runs through a selected section of the total Boot_reps
for( int k = begin; k < end; k ++){
engine.seed(k);
arma::vec index = input;
std::shuffle( index.begin(), index.end(), engine);
output[k] = ExampleInternal::myfunc3(index);
}
}
};
// [[Rcpp::export]]
arma::vec Parallelfunc(int Len_in){
arma::vec input = arma::regspace(0, 500);
arma::vec output(Len_in);
PARALLEL_WORKER parallel_woker(input, output);
parallelFor( 0, Len_in, parallel_woker);
return output;
}