[Rcpp-devel] Using Rcpp::Function in parallel with TBB mutex lock & a reference.
Kumar MS
mskb01 at gmail.com
Fri Feb 7 19:04:45 CET 2025
Hello Rcpp developers,
The following R/Rcpp code attempts to take in a data frame, a model formula
(passed as string), and uses Rcpp::Function to call R's model matrix
function to create model matrices B times in parallel. Each time a model
matrix is created, it is formed after permuting a given column in the data
frame.
To avoid multi-threaded access to R, I had used the "locking" idea from the
Boost example from RInside to use a scoped lock on a mutex, and use a
single set of Rcpp::Function reference variables initialized ones.
Unfortunately, the code compiles fine, but when I run the code, I get a :
Error: C stack usage close to the limit.
I would greatly appreciate any advice !
Thank you, SK.
#### ---- C++ CODE ---------------########
// [[Rcpp::depends(RcppParallel)]]
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include <tbb/tbb.h>
using namespace arma;
using namespace Rcpp;
using namespace RcppParallel;
typedef tbb::spin_mutex FreeListMutexType;
class testParallelCallingR : public Worker {
private:
Rcpp::String col2perm;
Rcpp::String formulaStr;
Rcpp::DataFrame& df0;
arma::vec& results;
Rcpp::Environment& stats, base;
Rcpp::Function& formula, modelMatrix, subset ;
SEXP& formulaObj;
FreeListMutexType FreeListMutex;
public:
explicit testParallelCallingR( Rcpp::String col2perm1,
Rcpp::String formulaStr1,
Rcpp::DataFrame& df0, arma::vec& res,
Rcpp::Environment& stats,
Rcpp::Environment& base,
Rcpp::Function& formula, Rcpp::Function&
modelMatrix, Rcpp::Function& subset, SEXP& formulaObj
): df0(df0), results(res), stats(stats),
base(base),
formula(formula),
modelMatrix(modelMatrix), subset(subset), formulaObj(formulaObj) {
col2perm=col2perm1;
formulaStr=formulaStr1;
}
arma::mat getModelMat(){
//lock
FreeListMutexType::scoped_lock lock(FreeListMutex);
//permute the column col2perm
std::string timestr(col2perm);
Rcpp::DataFrame dfw = Rcpp::clone(df0);
Rcpp::NumericVector timevals = df0[timestr]; std::random_shuffle(
timevals.begin(), timevals.end() );
dfw[timestr]=timevals;
//construct model mat with the dataframe with the permuted column
SEXP modelMatw=modelMatrix( formulaObj, dfw );
arma::mat Z = Rcpp::as<arma::mat>( modelMatw );
return Z;
}
void operator()( std::size_t begin, std::size_t end ) {
for( std::size_t j=begin; j < end; j++ ){
arma::mat Z=getModelMat();
results(j)=Z(0,0); //just as an example result, store the first index
say
}
}
};
// [[Rcpp::export]]
arma::vec permDf( int B, Rcpp::String col2perm, Rcpp::String formulaStr,
Rcpp::DataFrame df0 ){
Rcpp::Environment stats("package:stats");
Rcpp::Environment base("package:base");
Rcpp::Function formula = stats["formula"];
Rcpp::Function modelMatrix=stats["model.matrix"];
Rcpp::Function subset("[.data.frame");
SEXP formulaObj = formula(formulaStr);
arma::vec results( B, arma::fill::zeros );
testParallelCallingR tpc( col2perm, formulaStr, df0, results,
stats, base,
formula, modelMatrix, subset,
formulaObj
);
parallelFor( 0, B, tpc);
return results;
}
# -- Call function from R side --
# Create B permutatations of the column Sepal.Width, and form model matrice
permDf( B=10,
col2perm = "Sepal.Width",
formulaStr = "~Sepal.Width + Sepal.Length",
df0=iris
)
# -- Output --
Error: C stack usage 17587445176704 is too close to the limit
Error: C stack usage 17587449383296 is too close to the limit
Error: C stack usage 17587419937152 is too close to the limit
Error: C stack usage 17587432556928 is too close to the limit
Error: C stack usage 17587436763520 is too close to the limit
Error: C stack usage 17587440970112 is too close to the limit
Error: C stack usage 17587428350336 is too close to the limit
Error: C stack usage 17587424143744 is too close to the limit
Error: C stack usage 17587453589888 is too close to the limit
Execution halted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20250207/d6f02238/attachment.htm>
More information about the Rcpp-devel
mailing list