[R-sig-hpc] Trying to change OPENBLAS_NUM_THREADS from within R

Simon Fuller simonfuller9 at gmail.com
Mon Jun 11 17:17:42 CEST 2012


I hope this is the relevant mailing list for my enquiry.

I have googled the above point but have not been able to find a
solution which works. I would be very grateful if anyone had any
suggestions based on their own knowledge and experience.

I have installed openblas and have it linked as my shared BLAS to R.

So I can do "export OPENBLAS_NUM_THREADS = x" and then when I start R,
I can run with x threads, no problem - the extra threads are visible
in htop and there is speed improvement for crossprod etc.

However, for some operations I find that the increased threads have a
negative effect on my code.

Therefore I would like to be able to change the number of threads from
within the R session - the idea being that I can parcel certain
functions along with an appropriate thread setting.

However, e.g. Sys.setenv() does not work, probably not least because
Sys.getenv() does not include the relevant variable(s) e.g.

I have also tried running a C function with getenv / setenv. Here,
getenv can access the value of OPENBLAS_NUM_THREADS set before R was
called, but setenv's changes have no effect on the operation of the
BLAS, and they are lost when R is closed. I thought this might have to
do with the way UNIX's child processes receive only copies of the
parent's variables, and that the BLAS must be called at a level where
the parent's variables inform the operation. However I do not know
enough about Unix and the way that R works on it to know where to go
from here.

I can see a few possibilities, all or none of which might work.

1) A different install of R explicitly using openblas.

2) The number of threads might be manipulable through another value or
function (e.g. another environment variable - although I could not
find a likely candidate.) There could be something elemental here I am

3) Use some kind of hack to send system() calls from a C function -
but this seems messy if at all possible.

4) Start R with different options.

5) There is an openblas_set_numthreads function in the source for
openblas, but it was not clear to me how or indeed whether this can be
used to alter the threads, and I could find no instances of people
using it or similar functions. If anyone has managed to link some the
openblas code into a project in a way to implement this function I
would be very glad to hear about it.

My apologies if this question has been covered before.

Any help is much appreciated.

Best wishes,


More information about the R-sig-hpc mailing list