c++ - Does the keyword "shared" prevent race conditions? -
i'm unclear "shared" in openmp. see spec states shared "declares 1 or more list items shared tasks..." seems unclear me.
for example, if have following code:
#pragma omp parallel shared(num1) for(i=0; i<m; i++) { for(j=0; j < n; j++) { if(myfunc(i,j) < 0) { num1 += 256*u(i,j); } } }
will stop race conditions num1 , give accurate result @ end of loop? if not, do?
will stop race conditions?
no, won't:
it programmer's responsibility ensure multiple threads access shared variables (such via critical sections)
shared section make same variable visible multiple threads.
you can use critical sections or atomic access syncronization, in this case you'd better use reduction clause:
#pragma omp parallel reduction(+:num1)
Comments
Post a Comment