From pawel.matykiewicz at gmail.com  Tue Aug 30 18:57:48 2011
From: pawel.matykiewicz at gmail.com (pawelm)
Date: Tue, 30 Aug 2011 09:57:48 -0700 (PDT)
Subject: [Rd] How to safely using OpenMP pragma inside a .C() function?
In-Reply-To: <1314705201298-3778482.post@n4.nabble.com>
References: <1314643732236-3777036.post@n4.nabble.com>
	<B2571962-7567-4F59-A1CB-415088594F47@r-project.org>
	<1314705201298-3778482.post@n4.nabble.com>
Message-ID: <1314723468677-3779214.post@n4.nabble.com>

Simon,

I found that files R-2.13.1/src/library/stats/src/distance.c and
R-2.13.1/src/main/array.c have openmp code (example below). I have couple
questions regarding best practices when using R internals and openmp. 

Can we use R-2.13.1/src/library/stats/src/distance.c and
R-2.13.1/src/main/array.c as an example how to interact with R code and R
internals?
What are my other options if I want to work with SEXP structures in my
parallel code?

Thank you
Regards

=============

#ifdef HAVE_OPENMP
        /* This gives a spurious -Wunused-but-set-variable error */
        if (R_num_math_threads > 0) 
            nthreads = R_num_math_threads;
        else 
            nthreads = 1; /* for now */
#pragma omp parallel for num_threads(nthreads) default(none) \
    private(j, i, ix, rx) \
    firstprivate(x, ans, n, p, type, cnt, sum, \
                 NaRm, keepNA, R_NaReal, R_NaInt, OP)
#endif
        for (j = 0; j < p; j++) {
            switch (type) {
            case REALSXP:
                rx = REAL(x) + n*j; 
                if (keepNA)
                    for (sum = 0., i = 0; i < n; i++) sum += *rx++;
                else {
                    for (cnt = 0, sum = 0., i = 0; i < n; i++, rx++)
                        if (!ISNAN(*rx)) {cnt++; sum += *rx;}
                        else if (keepNA) {sum = NA_REAL; break;}
                }    
                break;
            case INTSXP:
                ix = INTEGER(x) + n*j; 
                for (cnt = 0, sum = 0., i = 0; i < n; i++, ix++)
                    if (*ix != NA_INTEGER) {cnt++; sum += *ix;}
                    else if (keepNA) {sum = NA_REAL; break;}
                break;
            case LGLSXP:
                ix = LOGICAL(x) + n*j; 
                for (cnt = 0, sum = 0., i = 0; i < n; i++, ix++)
                    if (*ix != NA_LOGICAL) {cnt++; sum += *ix;}
                    else if (keepNA) {sum = NA_REAL; break;}
                break;
            default:
                /* we checked the type above, but be sure */
                UNIMPLEMENTED_TYPEt("do_colsum", type);
            }
            if (OP == 1) {
                if (cnt > 0) sum /= cnt; else sum = NA_REAL;
            }
            REAL(ans)[j] = sum;
        }


--
View this message in context: http://r.789695.n4.nabble.com/How-to-safely-use-OpenMP-pragma-inside-a-C-function-tp3777036p3779214.html
Sent from the R devel mailing list archive at Nabble.com.



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