Skip to content

Commit 2b9c314

Browse files
committed
perf: optimize ndgrid2 and ndgrid3 subroutines
1 parent 8c528be commit 2b9c314

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/forcad_utils.F90

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,17 @@ pure function kron_eye(A, dim) result(B)
602602
pure subroutine ndgrid2(X_dir1,X_dir2, Xt)
603603
real(rk), intent(in), contiguous :: X_dir1(:), X_dir2(:)
604604
real(rk), allocatable, intent(out) :: Xt(:,:)
605-
integer :: s1, s2, i, j
605+
integer :: s1, s2, j, off, s, e
606606

607607
s1 = size(X_dir1)
608608
s2 = size(X_dir2)
609609
allocate(Xt(s1*s2,2))
610-
do concurrent (j = 1:s2, i = 1: s1)
611-
Xt((j - 1) * s1 + i,1) = X_dir1(i)
612-
Xt((j - 1) * s1 + i,2) = X_dir2(j)
610+
do concurrent (j = 1:s2) local(off, s, e)
611+
off = (j-1)*s1
612+
s = off + 1
613+
e = off + s1
614+
Xt(s:e, 1) = X_dir1(:)
615+
Xt(s:e, 2) = X_dir2(j)
613616
end do
614617
end subroutine
615618
!===============================================================================
@@ -621,16 +624,19 @@ pure subroutine ndgrid2(X_dir1,X_dir2, Xt)
621624
pure subroutine ndgrid3(X_dir1,X_dir2,X_dir3, Xt)
622625
real(rk), intent(in), contiguous :: X_dir1(:), X_dir2(:), X_dir3(:)
623626
real(rk), allocatable, intent(out) :: Xt(:,:)
624-
integer :: s1, s2, s3, i, j, k
627+
integer :: s1, s2, s3, j, k, off, s, e
625628

626629
s1 = size(X_dir1)
627630
s2 = size(X_dir2)
628631
s3 = size(X_dir3)
629632
allocate(Xt(s1*s2*s3,3))
630-
do concurrent (k = 1:s3, j = 1:s2, i = 1: s1)
631-
Xt(((k - 1) * s2 + (j - 1)) * s1 + i,1) = X_dir1(i)
632-
Xt(((k - 1) * s2 + (j - 1)) * s1 + i,2) = X_dir2(j)
633-
Xt(((k - 1) * s2 + (j - 1)) * s1 + i,3) = X_dir3(k)
633+
do concurrent (k=1:s3, j=1:s2) local(off, s, e)
634+
off = ((k-1)*s2 + (j-1)) * s1
635+
s = off + 1
636+
e = off + s1
637+
Xt(s:e, 1) = X_dir1(:)
638+
Xt(s:e, 2) = X_dir2(j)
639+
Xt(s:e, 3) = X_dir3(k)
634640
end do
635641
end subroutine
636642
!===============================================================================

0 commit comments

Comments
 (0)