From 0f9b2dbfe48f821ec9f67bf0008488d93a0ca2de Mon Sep 17 00:00:00 2001 From: ydamour Date: Tue, 17 Jan 2023 01:54:58 +0100 Subject: [PATCH 1/2] fast restore symmetry --- src/utils/linear_algebra.irp.f | 126 +++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 4 deletions(-) diff --git a/src/utils/linear_algebra.irp.f b/src/utils/linear_algebra.irp.f index 1e33c7dc..9517766e 100644 --- a/src/utils/linear_algebra.irp.f +++ b/src/utils/linear_algebra.irp.f @@ -1690,6 +1690,19 @@ subroutine restore_symmetry(m,n,A,LDA,thresh) thresh2 = dsqrt(thresh) call nullify_small_elements(m,n,A,LDA,thresh) + ! Debug + !double precision, allocatable :: B(:,:) + !double precision :: max_diff, ti,tf + !allocate(B(m,n)) + !B = A + !call wall_time(ti) + !call restore_symmetry_fast(m,n,B,LDA,thresh) + !call wall_time(tf) + !print*,'' + !print*,'Restore_symmetry' + !print*,'Fast version:',tf-ti,'s' + !call wall_time(ti) + ! if (.not.restore_symm) then ! return ! endif @@ -1749,23 +1762,128 @@ subroutine restore_symmetry(m,n,A,LDA,thresh) enddo enddo + ! Debug + !call wall_time(tf) + !print*,'Old version:',tf-ti,'s' + + !max_diff = 0d0 + !do j = 1, n + ! do i = 1, n + ! if (dabs(A(i,j)-B(i,j)) > max_diff) then + ! max_diff = dabs(A(i,j)-B(i,j)) + ! endif + ! enddo + !enddo + !print*,'Max diff:', max_diff + !deallocate(B) + end +subroutine restore_symmetry_fast(m,n,A,LDA,thresh) + implicit none + BEGIN_DOC + ! Tries to find the matrix elements that are the same, and sets them + ! to the average value. + ! If restore_symm is False, only nullify small elements + END_DOC + integer, intent(in) :: m,n,LDA + double precision, intent(inout) :: A(LDA,n) + double precision, intent(in) :: thresh + double precision, allocatable :: copy(:), copy_sign(:) + integer, allocatable :: key(:) + integer :: sze, pi, pf, idx, i,j,k + double precision :: average, val, thresh2 + thresh2 = dsqrt(thresh) + call nullify_small_elements(m,n,A,LDA,thresh) + + sze = m * n + allocate(copy(sze),copy_sign(sze),key(sze)) + + ! Copy to 1D + !$OMP PARALLEL & + !$OMP SHARED(A,m,n,sze,copy_sign,copy,key) & + !$OMP PRIVATE(i,j,k) & + !$OMP DEFAULT(NONE) + !$OMP DO + do j = 1, n + do i = 1, m + copy(i+(j-1)*m) = A(i,j) + enddo + enddo + !$OMP END DO + ! Copy sign + !$OMP DO + do i = 1,sze + copy_sign(i) = sign(1d0,copy(i)) + copy(i) = dabs(copy(i)) + enddo + !$OMP END DO NOWAIT + ! Keys + !$OMP DO + do i = 1, sze + key(i) = i + enddo + !$OMP END DO + !$OMP END PARALLEL + ! Sort + call dsort(copy,key,sze) + ! Symmetrize + i = 1 + do while(i < sze) + pi = i + pf = i + val = copy(i) + do while (dabs(val - copy(pf+1)) < thresh2) + pf = pf + 1 + ! if pf == sze, copy(pf+1) will not be valid + if (pf == sze) then + exit + endif + enddo + ! if pi and pf are different do the average from pi to pf + if (pf - pi > 0) then + average = 0d0 + do j = pi, pf + average = average + copy(j) + enddo + average = average / (pf-pi+1) + do j = pi, pf + copy(j) = average + enddo + ! Update i + i = pf + endif + ! Update i + i = i + 1 + enddo + !$OMP PARALLEL & + !$OMP SHARED(m,sze,copy_sign,copy,key,A) & + !$OMP PRIVATE(i,j,k,idx) & + !$OMP DEFAULT(NONE) + ! copy -> A + !$OMP DO + do k = 1, sze + idx = key(k) + i = mod(idx-1,m) + 1 + j = (idx-1) / m + 1 + ! New value with the right sign + A(i,j) = sign(copy(k),copy_sign(idx)) + enddo + !$OMP END DO + !$OMP END PARALLEL + deallocate(copy,copy_sign,key) - - - - +end !subroutine svd_s(A, LDA, U, LDU, D, Vt, LDVt, m, n) ! implicit none From 17add36bdac7e0093ba993ced16e62d30cb4d7b7 Mon Sep 17 00:00:00 2001 From: ydamour Date: Tue, 17 Jan 2023 09:49:14 +0100 Subject: [PATCH 2/2] remove error, replace symmetry condition --- src/utils/linear_algebra.irp.f | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/utils/linear_algebra.irp.f b/src/utils/linear_algebra.irp.f index 9517766e..20599325 100644 --- a/src/utils/linear_algebra.irp.f +++ b/src/utils/linear_algebra.irp.f @@ -1835,13 +1835,21 @@ subroutine restore_symmetry_fast(m,n,A,LDA,thresh) ! Sort call dsort(copy,key,sze) + !TODO + ! Parallelization with OMP + + ! Jump all the elements below thresh + i = 1 + do while (copy(i) <= thresh) + i = i + 1 + enddo + ! Symmetrize - i = 1 do while(i < sze) pi = i pf = i - val = copy(i) - do while (dabs(val - copy(pf+1)) < thresh2) + val = 1d0/copy(i) + do while (dabs(val * copy(pf+1) - 1d0) < thresh2) pf = pf + 1 ! if pf == sze, copy(pf+1) will not be valid if (pf == sze) then