[Rcpp-devel] OpenMp and IntegerVector

Andreas Prescher apres at kabelmail.de
Wed Mar 12 21:49:43 CET 2014


OK, got it working.

The crux was to throw all
Rcpp related stuff away from the parallel
context, even the Rcpp::List returned by
m_psiMinRVec[i]->init(...).

Replacing only
parInit(const Rcpp::List& seedList) with
parInit(std::vector<std::vector<int> >& made it
a "little bit more robust", but still produced
"Warning: stack imbalance in '.External'" messages
in R randomly.

Now the method looks like this (seedsVec converted outside):
Rcpp::List PsiMinRContainer::parInit(std::vector<std::vector<int> >& 
seedsVec)
{
	Rcpp::List resultList(m_psiMinRVec.size() - 1);
	std::vector<boost::tuple<double, double, bool> > 
resultVec(m_psiMinRVec.size() - 1);

	#pragma omp parallel for
	for(int i=1; i < m_psiMinRVec.size(); i++) {
		resultVec[i - 1] = m_psiMinRVec[i]->init(seedsVec[0]);
	}
	for(int i = 0; i < resultVec.size(); i++) {
		resultList[i] = Rcpp::List::create(/*...put tuple stuff inside here*/);
     }
	
	return 	resultList;
}

(Don't be confused why vector<vector> and so on,
I shortened my function in the postings)

Puh, made me headaches....

Andreas


Am 11.03.2014 20:56, schrieb Dirk Eddelbuettel:
>
> 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
>




More information about the Rcpp-devel mailing list