-2

I can't get the output result correct once applied openMP, is it anywhere get this right?

!$OMP PARALLEL DO SHARED(outmtresult,inpa,inpb,dynindexlist) PRIVATE(i,j) REDUCTION(+:outcountb)

do i=1,size1  
  do j=1, size1
    outcountb = outcountb + 1
    outmtresult(j) = tan(inpa(j) + inpb(j)) + alpha1 + dynindexlist(i) 
  enddo
enddo

!$OMP END PARALLEL DO
3

1 Answer 1

0

Just swap your loops and everything will be fine:

!$OMP PARALLEL DO SHARED(outmtresult,inpa,inpb,dynindexlist) PRIVATE(i,j) REDUCTION(+:outcountb)

do j=1,size1        ! <-- Swap i and
  do i=1, size1     !       j here
    outcountb = outcountb + 1
    outmtresult(j) = tan(inpa(j) + inpb(j)) + alpha1 + dynindexlist(i) 
  enddo
enddo

!$OMP END PARALLEL DO

In your example, multiple threads write into the same memory address outmtresult(j) since you parallelize the do i loop.

By swapping the loops, you parallelize over do j and you will not write at the same destination with multiple concurrent threads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.