[Rcpp-devel] OpenMp and IntegerVector

Hao Ye hye at ucsd.edu
Tue Mar 11 21:06:50 CET 2014


> Just accessing / setting data structures should work (but test first...),
> RNGs is clearly asking for trouble.

FWIW, I've been able to get this working using Rcpp and C++11 threads. (but using native C++ data structures, of course)

Best,
--
Hao Ye
hye at ucsd.edu

On Mar 11, 2014, at 12:56 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

> 
> Andreas,
> 
> On 11 March 2014 at 20:30, Andreas Prescher wrote:
> | Hello,
> | 
> | I got strange behaviour when using
> | OpenMp and IntegerVector (without
> | OpenMp all works fine!!)
> | 
> |  From my package (Rcpp 0.10.4 used, let some unimportant
> | arguments out)
> | 
> | Rcpp::List PsiMinRContainer::parInit(const Rcpp::List& seedList)
> | {
> | 	Rcpp::List resultList(m_psiMinRVec.size() - 1);
> | 	Rcpp::IntegerVector seedVec(seedList[0]);
> | 	
> | 	#pragma omp parallel for
> | 	for(int i=1; i < m_psiMinRVec.size(); i++) {
> | 		resultList[i - 1] = m_psiMinRVec[i]->init(seedVec);
> | 	}
> 
> I would recommend being more careful / paranoid here and make sure that
> within the '#pragma omp parallel loop' you do NOT use a single R variable,
> and you shoult NOT use the R random number generator.
> 
> OpenMP is by design multithreaded. R is very famously not set up for that.
> By calling back into R, you set yourself up for trouble,
> 
> Just accessing / setting data structures should work (but test first...),
> RNGs is clearly asking for trouble.
> 
> In short, for OpenMP use plain C++ constructs. But not R. Use R and and Rcpp
> to get your data to your OpemMP code portions, run those (carefully) in
> vanilla C++ (or even vanilla C++11) and then return to R.
> 
> A few CRAN packages use OpenMP along with Rcpp. 
> 
> Dirk
> 
> 
> | 	return resultList;
> | }
> | in a module definition file:
> | 
> | function( "parInit", &parInit,List::create( _["seedList"]) ),
> | calling my internal pointer with psiMinRContainer->parInit(seedList)
> | 
> | The problem seems to be the seedVec.
> | (I also tried using seedvec as pointer allocated with new)
> | list created in in R:
> | seeds <- list(c(1,12),c(1,2))
> | parInit(seedList = seeds)
> | 
> | Some errors I can give:
> | 
> | SET_VECTOR_ELT() can only be applied to a 'list', not a 'character'
> | 
> | Warning: stack imbalance in '.External', 9 then 11
> | Warning: stack imbalance in '{', 6 then 8
> | 
> | error: getCharCE have to be called for CHARSXP
> | (Fehler: 'getCharCE' muss für CHARSXP aufgerufen werden)
> | 
> | What I found so far are hints about
> | protect/unprotect in writing R-extensions (eg 
> | http://www.hep.by/gnu/r-patched/r-exts/R-exts_103.html).
> | 
> | Any ideas?
> | 
> | Andreas
> | 
> | 
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> 
> -- 
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel



More information about the Rcpp-devel mailing list