Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the compiler ignore OpenMP pragmas?
    primarykey
    data
    text
    <p>In the following C code I am using OpenMP in a nested loop. Since race condition occurs, I want to perform atomic operations at the end:</p> <pre><code>double mysumallatomic() { double S2 = 0.; #pragma omp parallel for shared(S2) for(int a=0; a&lt;128; a++){ for(int b=0; b&lt;128;b++){ double myterm = (double)a*b; #pragma omp atomic S2 += myterm; } } return S2; } </code></pre> <p>The thing is that <code>#pragma omp atomic</code> has no effect on the program behaviour, even if I remove it, nothing happens. Even if I change it to <code>#pragma oh_my_god</code>, I get no error!</p> <p>I wonder what is going wrong here, whether I can tell the compiler to be more strict when checking omp pragmas or why I do not get an error when I make the last change</p> <p>PS: For compilation I use:</p> <pre><code>gcc-4.2 -fopenmp main.c functions.c -o main_elec_gcc.exe </code></pre> <p>PS2: New code that gives me the same problem and based on gillespie idea:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;omp.h&gt; #include &lt;math.h&gt; #define NRACK 64 #define NSTARS 1024 double mysumallatomic_serial(float rocks[NRACK][3], float moon[NSTARS][3], float qr[NRACK],float ql[NSTARS]) { int j,i; float temp_div=0.,temp_sqrt=0.; float difx,dify,difz; float mod2x, mod2y, mod2z; double S2 = 0.; for(j=0; j&lt;NRACK; j++){ for(i=0; i&lt;NSTARS;i++){ difx=rocks[j][0]-moon[i][0]; dify=rocks[j][1]-moon[i][1]; difz=rocks[j][2]-moon[i][2]; mod2x=difx*difx; mod2y=dify*dify; mod2z=difz*difz; temp_sqrt=sqrt(mod2x+mod2y+mod2z); temp_div=1/temp_sqrt; S2 += ql[i]*temp_div*qr[j]; } } return S2; } double mysumallatomic(float rocks[NRACK][3], float moon[NSTARS][3], float qr[NRACK],float ql[NSTARS]) { float temp_div=0.,temp_sqrt=0.; float difx,dify,difz; float mod2x, mod2y, mod2z; double S2 = 0.; #pragma omp parallel for shared(S2) for(int j=0; j&lt;NRACK; j++){ for(int i=0; i&lt;NSTARS;i++){ difx=rocks[j][0]-moon[i][0]; dify=rocks[j][1]-moon[i][1]; difz=rocks[j][2]-moon[i][2]; mod2x=difx*difx; mod2y=dify*dify; mod2z=difz*difz; temp_sqrt=sqrt(mod2x+mod2y+mod2z); temp_div=1/temp_sqrt; float myterm=ql[i]*temp_div*qr[j]; #pragma omp atomic S2 += myterm; } } return S2; } int main(int argc, char *argv[]) { float rocks[NRACK][3], moon[NSTARS][3]; float qr[NRACK], ql[NSTARS]; int i,j; for(j=0;j&lt;NRACK;j++){ rocks[j][0]=j; rocks[j][1]=j+1; rocks[j][2]=j+2; qr[j] = j*1e-4+1e-3; //qr[j] = 1; } for(i=0;i&lt;NSTARS;i++){ moon[i][0]=12000+i; moon[i][1]=12000+i+1; moon[i][2]=12000+i+2; ql[i] = i*1e-3 +1e-2 ; //ql[i] = 1 ; } printf(" serial: %f\n", mysumallatomic_serial(rocks,moon,qr,ql)); printf(" openmp: %f\n", mysumallatomic(rocks,moon,qr,ql)); return(0); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload