The use of k = k+1 will not work as required in this code, as the initial value of k is not adjusted for each thread (based on the value “i”). “k” can not be shared and must be private.
You could try either of the following, which should be identical with most optimising compilers
! explicit definition of k
!$OMP PARALLEL DO SHARED(arr,val,n) PRIVATE(i,j,k) SCHEDULE(STATIC)
do i=1,n
do j=1,n
k = (i-1)*n+j
arr(k)%x=val(i)
arr(k)%y=val(j)
end do
end do
!$OMP END PARALLEL DO
! incremental version of k
!$OMP PARALLEL DO SHARED(arr,val,n) PRIVATE(i,j,k) SCHEDULE(STATIC)
do i=1,n
k = (i-1)*n + 1
do j=1,n
arr(k)%x=val(i)
arr(k)%y=val(j)
k = k+1
end do
end do
!$OMP END PARALLEL DO
This approach applies PARALLEL DO to the DO i loop.
It is unlikely this will improve performance as there is “low arithmetic intensity” https://fortran-lang.discourse.group/t/compiling-only-part-of-code-with-ffast-math/3196/28