Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data race in solver parallelised reduction #1

Open
rookiehpc opened this issue Jul 16, 2022 · 0 comments
Open

Data race in solver parallelised reduction #1

rookiehpc opened this issue Jul 16, 2022 · 0 comments

Comments

@rookiehpc
Copy link

In the file solver.f90 at lines 81 and 125 appears this block:

!$OMP PARALLEL DO 
do i=1,n; norm=norm+r(i)*r(i); end do
!$OMP END PARALLEL DO

There is a data race here as threads handling different iterations of the reduction loop are trying to update the same variable concurrently. In order to restore correctness, the OpenMP reduction clause can be used, it will ensure that each thread sum its r(i)*r(i) increments into a thread-local temporary variable. At the end of the loop, OpenMP will make those threads sum the final value of their thread-local temporary variable back into the original norm variable, taking care of potential data races in the process. The corrected block would look as follows:

!$OMP PARALLEL DO REDUCTION(+:norm)
do i=1,n; norm=norm+r(i)*r(i); end do
!$OMP END PARALLEL DO

There might be other blocks in a similar situation, for which the same fix applies, such as the one at line 113 in the same file solver.f90:

!$OMP PARALLEL DO 
do i=1,n; x(i)=x(i)+alpha*p(i)+omega*ss(i); end do
!$OMP END PARALLEL DO

You can refer to https://rookiehpc.github.io/openmp/docs/reduction/index.html for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant