[Rd] How to safely using OpenMP pragma inside a .C() function?

Simon Urbanek simon.urbanek at r-project.org
Tue Aug 30 02:36:24 CEST 2011

On Aug 29, 2011, at 7:48 PM, Alireza Mahani wrote:

> I am trying to parallelize part of a C function that is called from R (via
> .C) using OpenMP's "parallel for" pragma. I get mixed results: some runs
> finish with no problem, but some lead to R crashing after issuing a long
> error message involving memory violations.

You'll have to provide the code. In general it works (even R uses it itself), but there are strict requirements (no R API calls) that you must adhere to.

> I found this post, which describes how a .Call() function can be made to
> avoid crashing R by raising the stack limit:
> http://www.r-bloggers.com/using-openmp-ized-c-code-with-r/

I skimmed through the post and all of the examples are broken - they will only work (incidentally) as R internals, not officially (and they are unnecessary inefficient).

> However, trying this in my .C function doesn't help things. Any
> suggestions/tips on whether I can safely use OpenMP inside a .C function,
> and if yes how?

There are issues with OpenMP on some platforms in general (in fact pretty much every platform had some issue at some point in time), but apart from those you only have to make sure that you declare shared/private variables properly and don't use *any* R API calls in the parallel part (this includes things like LENGTH, REAL, ...).


> Thank you,
> Alireza Mahani
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-safely-using-OpenMP-pragma-inside-a-C-function-tp3777036p3777036.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

More information about the R-devel mailing list