From simon.urbanek at r-project.org  Fri Oct 18 16:43:00 2013
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Fri, 18 Oct 2013 10:43:00 -0400
Subject: [Rd] Parallel R expression evaluations
In-Reply-To: <1381931846577-4678352.post@n4.nabble.com>
References: <1381931846577-4678352.post@n4.nabble.com>
Message-ID: <77E2443B-BF01-42F8-B3AE-6AAF5CA9B391@r-project.org>

On Oct 16, 2013, at 9:57 AM, JaiReddy wrote:

> Hi all,
> 
> I am using R-3.0.1 under Linux platform to embed R into my C++ code.
> 
> I am facing an error while executing more than 1 R-expressions parallelly.
> 

R is not thread-safe so you cannot execute any API calls in parallel. Also you can use the embedded init only once. If you break any of these rules, the behavior is undefined (and everything comes down crashing).

Cheers,
Simon



> I am executing round(X) and abs(X) parallelly on a set of 50 input rows 
> which resulted in segmentation fault after getting the following errors.
> 
> Error: unprotect_ptr: pointer not found
> Error: argument to 'findVar' is not an environment
> 
> I am using the following code snippet for initializing R, parsing and
> evaluation of R expression
> 
> // For initialization
> 
>        int res= Rf_initEmbeddedR(R_argc, (char**)R_argv);
> 
> // For parsing and evaluation
> 
>        SEXP cmd1= Rf_mkChar(rscript);
> 	SEXP cmdSexp, cmdexpr, sresult = R_NilValue;
> 	ParseStatus status;
> 	R_len_t i=0;
> 
> 	PROTECT(cmdSexp = Rf_allocVector(STRSXP, 1));
> 	SET_STRING_ELT(cmdSexp, 0, cmd1);
> 	// parsing vector for R expressions
> 	cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &status, R_NilValue));
> 	if (status != PARSE_OK) {
> 		UNPROTECT(2);
> 		// error handling
> 		return;
> 	}
> 	
> 	for(i = 0; i < Rf_length(cmdexpr); i++)
> 	{
> 		int error;
> 		sresult = R_tryEval(VECTOR_ELT(cmdexpr, i), R_GlobalEnv, &error);	// R
> expression evaluation 
> 		if(error)		// checking for error
> 		{
> 			// error handling
> 			return;
> 		}
> 	}
> 	UNPROTECT(2);
> 
> 
> I wonder if R supports parallel evaluations within a single session. I have
> seen parallel evaluations of R using Rserve package. As I am trying to
> overcome the overhead (using Rserve) in creating new connection for each
> evaluation, here I am trying using embeded R.
> 
> I tried Rf_endEmbeddedR each time after one evaluation and initializing R
> for the next set of evaluation. Even that did't work.
> 
> Please suggest me possible solution if any.
> 
> Thanks in advance.
> 
> Jai
> 
> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Parallel-R-expression-evaluations-tp4678352.html
> Sent from the R devel mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] Parallel R expression evaluations
Simon Urbanek