-
-
Save tkoskela/59d4787d1fb8b4eaaf195263a3ffd4a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program array_reduction | |
use omp_lib | |
implicit none | |
integer, parameter :: n = 20 | |
integer, parameter :: m = 1000000 | |
integer:: i,j | |
integer, dimension(n) :: array_to_reduce | |
integer, dimension(m) :: array_to_read | |
array_to_reduce = 0 | |
do i = 1,m | |
array_to_read(i) = 5 | |
end do | |
! This loop has a data race because multiple iterations are writing into | |
! array_to_reduce with the same value of j. It can be resolved by adding | |
! a reduction(+:array_to_reduce) clause to omp parallel do. | |
!$omp parallel do default(shared) private(i,j) schedule(static) | |
do i = 1,m | |
j = mod(i,n) + 1 | |
array_to_reduce(j) = array_to_reduce(j) + array_to_read(i) | |
end do | |
!$omp end parallel do | |
write(*,*) 'I am running with this many threads:', omp_get_max_threads() | |
do i = 1,n | |
write(*,*) array_to_reduce(i) | |
end do | |
end program array_reduction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment