[Rd] R thread safe

Olaf.Schenk@unibas.ch Olaf.Schenk at unibas.ch
Mon Nov 7 19:57:28 CET 2005

Dear R-dev,

I would like to accelerate my R computation by using parallel OpenMP compilers
(e.g from Pathscale) on a 2-processor AMD server and I would like to know
whether R is a tread safe library. The main kernel of the OpenMP
parallelization is a C SEXP function that performs the computational routine in
parallel with:

SEXP example(SEXP list, SEXP expr, SEXP rho)
       R_len_t i, n = length(list);
       SEXP ans, alocal;

       omp_lock_t lck;
       PROTECT(ans = allocVector(VECSXP, n));
       ans = allocVector(VECSXP, n);
#pragma omp parallel for default(none) private(i, alocal) shared(list,
lck,rho, ans, n, expr)
       for(i = 0; i < n; i++) {

             PROTECT(alocal = allocVector(VECSXP, 1));
             alocal = allocVector(VECSXP, 1);
             defineVar(install("x"), VECTOR_ELT(list, i), rho);

          /* do computational kernel in parallel */
          alocal = eval(expr, rho);

             SET_VECTOR_ELT(ans, i, alocal);

       setAttrib(ans, R_NamesSymbol, getAttrib(list, R_NamesSymbol));


The code works fine using one thread and breaks currently down with 2 threads. 
I am using a recent R distribution and  the complete R code is compile with
"-openmp" and the Pathscale compiler suite.

Thanks in advance,

More information about the R-devel mailing list