Skip to content

Parallel R expression evaluations

5 messages · JaiReddy, Simon Urbanek, Peter Dalgaard

#
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.

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.
2 days later
#
On Oct 16, 2013, at 9:57 AM, JaiReddy wrote:

            
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
#
Thanks Simon.

May I know how R works if two expressions come at the same time for
evaluation. When I debug my case I found that issue was found with indexed
values of protected items. 

As R is single threaded engine, I just want to know how does R behave when
2nd expression comes for parsing and evaluation even before 1st expression
evaluation does complete?


Thanks
Jai



--
View this message in context: http://r.789695.n4.nabble.com/Parallel-R-expression-evaluations-tp4678352p4678587.html
Sent from the R devel mailing list archive at Nabble.com.
1 day later
#
Jai,
On Oct 19, 2013, at 1:37 AM, JaiReddy wrote:

            
As I said, no API calls may be performed in parallel, that includes parsing and evaluation. Typically, R is run in a sequential loop: read, parse, eval, print so anything that is not processed is blocked until the loop is back to reading. However, given that R can be called from arbitrary front-ends, it's really up to the front-end to decide what to do - as long as it serializes the calls appropriately.

Cheers,
Simon
#
On Oct 21, 2013, at 02:24 , Simon Urbanek wrote:

            
Things like GUI callbacks can get evaluated while other evaluation is in progress. In that case, a stack discipline is maintained, i.e. expression1 does not continue until expression2 is evaluated, but expression3 may arrive and block the other two. This isn't really much different from what happens if expression1 encounters an object that requires lazy evaluation. If the intermittent calls have no side effects, or at least keep them within a well-defined universe, the interrupted call should be unaffected.

However, this will not happen while expression1 is evaluating C code, only when the R evaluator is called. 

-pd