Repository navigation
DependencyTools misses a simple opportunity for parallelisation? #3224
Description
Activity
- changed the title
[-]DependecyTools misses a simple opportunity for parallelisation?[/-][+]DependencyTools misses a simple opportunity for parallelisation?[/+]on Nov 17, 2025 Another slightly curious case that's cropped up during testing:
subroutine double_loop(arr) integer, dimension(:,:), intent(inout) :: arr integer :: i, j, k do i = 1, size(arr, 2), 1 do j = 1, size(arr, 1), 1 arr(j,i) = 0 enddo do k = 1, size(arr, 1), 1 arr(k,i) = arr(k,i) + 1 enddo enddo end subroutine double_loop
Here, the
iloop is not parallelised byParallelLoopTrans.from psyclone.psyir.transformations import OMPLoopTrans, TransformationError
from psyclone.psyir.nodes import Loop, Routinedef trans(psyir):
for loop in psyir.walk(Loop):
OMPLoopTrans(omp_directive="paralleldo").apply(loop)@mn416 what PSyclone version did you test with? Using exactly this I get:
module subroutine_example implicit none public contains subroutine main() integer :: i real, dimension(10) :: re_m real, dimension(10) :: im_m !$omp parallel do default(shared) private(i) schedule(auto) do i = 1, 10, 1 call sub(re_m(i), im_m(i)) enddo !$omp end parallel do end subroutine main pure subroutine sub(a, b) real, intent(inout) :: a real, intent(inout) :: b end subroutine sub end module subroutine_exampleI tested with current master
The second one does still fail though - this is the error I get:
psyclone.psyir.transformations.transformation_error.TransformationError: Transformation Error: Loop cannot be parallelised because: Error: The write access to 'arr(j,i)' and the read access to 'arr(k,i)' are dependent and cannot be parallelised. Variable: 'arr'. Consider using the "ignore_dependencies_for" transformation option if this is a false dependency Consider using the "array_privatisation" transformation option if this is a write-write dependencyI suspect this is not so easy to solve (since j/k are modified inside the outer loop) and probably we just expect
ignore_dependencies_fororforceto be the solution if the outer loop needs to be parallelised over at the moment. I think this outer/inner loop behaviour we could benefit from improving in a few places, but its not so straightforward to know what the solution is? I guess we could in theory resolve that index fully (i.e. we have accesses toarr(:,i)for each value of i, but I'm not sure how this stuff works in detail at the moment).Thanks @LonelyCat124. My version of PSyclone is a month old and I confirm that the first example works on master. I notice @sergisiso has made changes to
DependencyToolssince then, which must have fixed it. Great!Regarding the second example, I am also not overly familiar with how
DependencyToolsworks but I found it surprising that it can parallelise theiloop if either of the two inner loops is present, but not both. Maybe it is just a tricky case, in which case please close the issue. (The analysis in #3213 can handle it but, I imagine, it works in quite a different way.)
While working on PR #3213 I encountered the following loop that I thought
DependencyToolswould be able to parallelise, but was surprised to find it couldn't:Here's a small PSyclone script to try to parallelise it:
@hiker may be interested in this.