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

From: Simon Urbanek <>
Date: Tue, 30 Aug 2011 01:36:24 +0100

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:

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:
> Sent from the R devel mailing list archive at
> ______________________________________________
> mailing list
> mailing list Received on Tue 30 Aug 2011 - 00:38:19 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 12 Sep 2011 - 21:10:29 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive