DISCLAIMER: I have never programmed with Fortan. So my code most-likely has bugs.
But using logic and assuming that what you have stated is correct i.e., that the only dependency is the 'i' variable. You can try the following:
do q=1,pppp
DO j=1, pppp
do c1=1,3
vect(c1)=xx(q,c1)-xx(j,c1)
end do
dist(q,j)=sqrt(vect(1)**2+vect(2)**2+vect(3)**2)
ENDDO
ENDDO
So you parallelize the outer loop with OpenMP. This way you can run in parallel the most computational demanding part of the loop without the 'i' dependency. Notice that I am saving the result of the distances into a vector.
Then sequentially you can do the remaining loop computation as follows:
do q=1,pppp
i=0
DO j=1, pppp
if(dist(q,j) < 0.0001)then
i=i+1
if(i>10)i=10
caravec(q,i)=j
endif
ENDDO
ENDDO
The downside of the current approach is that besides requiring refactoring of the code it also uses more memory than the original one. On the other hand it removes the need for synchronization among threads.
As @Ian Bush mentioned in the comments you can further improve your sequential code by
testing n distance squared rather than distance will avoid the call
to sqrt
i
(andvect
anddist
) private, there is no problem with it.sqrt
, Finally the language is officially spelt Fortran, and has been so for over 3 decades.