LCOV - code coverage report
Current view: top level - src/fm - cp_cfm_basic_linalg.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:24d69ee) Lines: 84.3 % 466 393
Test Date: 2026-09-03 07:32:15 Functions: 90.9 % 22 20

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Basic linear algebra operations for complex full matrices.
      10              : !> \note
      11              : !>      - not all functionality implemented
      12              : !> \par History
      13              : !>      Nearly literal copy of Fawzi's routines
      14              : !> \author Joost VandeVondele
      15              : ! **************************************************************************************************
      16              : MODULE cp_cfm_basic_linalg
      17              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      18              :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      19              :                                               cp_cfm_get_info,&
      20              :                                               cp_cfm_release,&
      21              :                                               cp_cfm_to_cfm,&
      22              :                                               cp_cfm_type
      23              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_equivalent
      24              :    USE cp_fm_types,                     ONLY: cp_fm_type
      25              :    USE cp_log_handling,                 ONLY: cp_to_string
      26              :    USE kahan_sum,                       ONLY: accurate_dot_product
      27              :    USE kinds,                           ONLY: dp
      28              :    USE mathconstants,                   ONLY: z_one,&
      29              :                                               z_zero
      30              :    USE message_passing,                 ONLY: mp_comm_type
      31              : #include "../base/base_uses.f90"
      32              : 
      33              :    IMPLICIT NONE
      34              :    PRIVATE
      35              : 
      36              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      37              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_cfm_basic_linalg'
      38              : 
      39              :    PUBLIC :: cp_cfm_column_scale, &
      40              :              cp_cfm_gemm, &
      41              :              cp_cfm_get_diag, &
      42              :              cp_cfm_lu_decompose, &
      43              :              cp_cfm_lu_invert, &
      44              :              cp_cfm_norm, &
      45              :              cp_cfm_scale, &
      46              :              cp_cfm_scale_and_add, &
      47              :              cp_cfm_scale_and_add_fm, &
      48              :              cp_cfm_schur_product, &
      49              :              cp_cfm_solve, &
      50              :              cp_cfm_trace, &
      51              :              cp_cfm_transpose, &
      52              :              cp_cfm_triangular_invert, &
      53              :              cp_cfm_triangular_multiply, &
      54              :              cp_cfm_rot_rows, &
      55              :              cp_cfm_rot_cols, &
      56              :              cp_cfm_det, & ! determinant of a complex matrix with correct sign
      57              :              cp_cfm_uplo_to_full, &
      58              :              cp_cfm_vectorsnorm
      59              : 
      60              :    REAL(kind=dp), EXTERNAL :: zlange, pzlange
      61              : 
      62              :    INTERFACE cp_cfm_scale
      63              :       MODULE PROCEDURE cp_cfm_dscale, cp_cfm_zscale
      64              :    END INTERFACE cp_cfm_scale
      65              : 
      66              : ! **************************************************************************************************
      67              : 
      68              : CONTAINS
      69              : 
      70              : ! **************************************************************************************************
      71              : !> \brief Computes the determinant (with a correct sign even in parallel environment!) of a complex square matrix
      72              : !> \param matrix_a ...
      73              : !> \param det_a ...
      74              : !> \author A. Sinyavskiy (andrey.sinyavskiy@chem.uzh.ch)
      75              : ! **************************************************************************************************
      76         1500 :    SUBROUTINE cp_cfm_det(matrix_a, det_a)
      77              : 
      78              :       TYPE(cp_cfm_type), INTENT(IN)            :: matrix_a
      79              :       COMPLEX(KIND=dp), INTENT(OUT)            :: det_a
      80              :       COMPLEX(KIND=dp)                         :: determinant
      81              :       TYPE(cp_cfm_type)                        :: matrix_lu
      82         1500 :       COMPLEX(KIND=dp), DIMENSION(:, :), POINTER  :: a
      83              :       INTEGER                                  :: n, i, info, P
      84         1500 :       INTEGER, ALLOCATABLE, DIMENSION(:)       :: ipivot
      85         1500 :       COMPLEX(KIND=dp), DIMENSION(:), POINTER  :: diag
      86              : 
      87              : #if defined(__parallel)
      88              :       INTEGER                                  :: myprow, nprow, npcol, nrow_local, irow_local, &
      89              :                                                   mypcol, ncol_local, icol_local, j
      90              :       INTEGER, DIMENSION(9)                    :: desca
      91              : #endif
      92              : 
      93              :       CALL cp_cfm_create(matrix=matrix_lu, &
      94              :                          matrix_struct=matrix_a%matrix_struct, &
      95         1500 :                          name="A_lu"//TRIM(ADJUSTL(cp_to_string(1)))//"MATRIX")
      96         1500 :       CALL cp_cfm_to_cfm(matrix_a, matrix_lu)
      97              : 
      98         1500 :       a => matrix_lu%local_data
      99         1500 :       n = matrix_lu%matrix_struct%nrow_global
     100         4500 :       ALLOCATE (ipivot(n))
     101         1500 :       ipivot(:) = 0
     102         1500 :       P = 0
     103         4500 :       ALLOCATE (diag(n))
     104         8322 :       diag(:) = 0.0_dp
     105              : #if defined(__parallel)
     106              :       ! Use LU decomposition
     107        15000 :       desca(:) = matrix_lu%matrix_struct%descriptor(:)
     108         1500 :       CALL pzgetrf(n, n, a(1, 1), 1, 1, desca, ipivot, info)
     109         1500 :       myprow = matrix_lu%matrix_struct%context%mepos(1)
     110         1500 :       mypcol = matrix_lu%matrix_struct%context%mepos(2)
     111         1500 :       nprow = matrix_lu%matrix_struct%context%num_pe(1)
     112         1500 :       npcol = matrix_lu%matrix_struct%context%num_pe(2)
     113         1500 :       nrow_local = matrix_lu%matrix_struct%nrow_locals(myprow)
     114         1500 :       ncol_local = matrix_lu%matrix_struct%ncol_locals(mypcol)
     115              : 
     116         5031 :       DO irow_local = 1, nrow_local
     117         3531 :          i = matrix_lu%matrix_struct%row_indices(irow_local)
     118        40638 :          DO icol_local = 1, ncol_local
     119        35607 :             j = matrix_lu%matrix_struct%col_indices(icol_local)
     120        39138 :             IF (i == j) diag(i) = matrix_lu%local_data(irow_local, icol_local)
     121              :          END DO
     122              :       END DO
     123        15144 :       CALL matrix_lu%matrix_struct%para_env%sum(diag)
     124         8322 :       determinant = PRODUCT(diag)
     125         5031 :       DO irow_local = 1, nrow_local
     126         3531 :          i = matrix_lu%matrix_struct%row_indices(irow_local)
     127         5031 :          IF (ipivot(irow_local) /= i) P = P + 1
     128              :       END DO
     129         1500 :       CALL matrix_lu%matrix_struct%para_env%sum(P)
     130              :       ! very important fix
     131         1500 :       P = P/npcol
     132              : #else
     133              :       CALL zgetrf(n, n, a(1, 1), n, ipivot, info)
     134              :       DO i = 1, n
     135              :          diag(i) = matrix_lu%local_data(i, i)
     136              :       END DO
     137              :       determinant = PRODUCT(diag)
     138              :       DO i = 1, n
     139              :          IF (ipivot(i) /= i) P = P + 1
     140              :       END DO
     141              : #endif
     142         1500 :       DEALLOCATE (ipivot)
     143         1500 :       DEALLOCATE (diag)
     144         1500 :       CALL cp_cfm_release(matrix_lu)
     145         1500 :       det_a = determinant*(-2*MOD(P, 2) + 1.0_dp)
     146         1500 :    END SUBROUTINE cp_cfm_det
     147              : 
     148              : ! **************************************************************************************************
     149              : !> \brief Computes the element-wise (Schur) product of two matrices: C = A \circ B .
     150              : !> \param matrix_a the first input matrix
     151              : !> \param matrix_b the second input matrix
     152              : !> \param matrix_c matrix to store the result
     153              : ! **************************************************************************************************
     154          154 :    SUBROUTINE cp_cfm_schur_product(matrix_a, matrix_b, matrix_c)
     155              : 
     156              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a, matrix_b, matrix_c
     157              : 
     158              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_schur_product'
     159              : 
     160          154 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a, b, c
     161              :       INTEGER                                            :: handle, icol_local, irow_local, mypcol, &
     162              :                                                             myprow, ncol_local, nrow_local
     163              : 
     164          154 :       CALL timeset(routineN, handle)
     165              : 
     166          154 :       myprow = matrix_a%matrix_struct%context%mepos(1)
     167          154 :       mypcol = matrix_a%matrix_struct%context%mepos(2)
     168              : 
     169          154 :       a => matrix_a%local_data
     170          154 :       b => matrix_b%local_data
     171          154 :       c => matrix_c%local_data
     172              : 
     173          154 :       nrow_local = matrix_a%matrix_struct%nrow_locals(myprow)
     174          154 :       ncol_local = matrix_a%matrix_struct%ncol_locals(mypcol)
     175              : 
     176          462 :       DO icol_local = 1, ncol_local
     177          770 :          DO irow_local = 1, nrow_local
     178          616 :             c(irow_local, icol_local) = a(irow_local, icol_local)*b(irow_local, icol_local)
     179              :          END DO
     180              :       END DO
     181              : 
     182          154 :       CALL timestop(handle)
     183              : 
     184          154 :    END SUBROUTINE cp_cfm_schur_product
     185              : 
     186              : ! **************************************************************************************************
     187              : !> \brief Computes the element-wise (Schur) product of two matrices: C = A \circ conjg(B) .
     188              : !> \param matrix_a the first input matrix
     189              : !> \param matrix_b the second input matrix
     190              : !> \param matrix_c matrix to store the result
     191              : ! **************************************************************************************************
     192            0 :    SUBROUTINE cp_cfm_schur_product_cc(matrix_a, matrix_b, matrix_c)
     193              : 
     194              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a, matrix_b, matrix_c
     195              : 
     196              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_schur_product_cc'
     197              : 
     198            0 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a, b, c
     199              :       INTEGER                                            :: handle, icol_local, irow_local, mypcol, &
     200              :                                                             myprow, ncol_local, nrow_local
     201              : 
     202            0 :       CALL timeset(routineN, handle)
     203              : 
     204            0 :       myprow = matrix_a%matrix_struct%context%mepos(1)
     205            0 :       mypcol = matrix_a%matrix_struct%context%mepos(2)
     206              : 
     207            0 :       a => matrix_a%local_data
     208            0 :       b => matrix_b%local_data
     209            0 :       c => matrix_c%local_data
     210              : 
     211            0 :       nrow_local = matrix_a%matrix_struct%nrow_locals(myprow)
     212            0 :       ncol_local = matrix_a%matrix_struct%ncol_locals(mypcol)
     213              : 
     214            0 :       DO icol_local = 1, ncol_local
     215            0 :          DO irow_local = 1, nrow_local
     216            0 :             c(irow_local, icol_local) = a(irow_local, icol_local)*CONJG(b(irow_local, icol_local))
     217              :          END DO
     218              :       END DO
     219              : 
     220            0 :       CALL timestop(handle)
     221              : 
     222            0 :    END SUBROUTINE cp_cfm_schur_product_cc
     223              : 
     224              : ! **************************************************************************************************
     225              : !> \brief Scale and add two BLACS matrices (a = alpha*a + beta*b).
     226              : !> \param alpha ...
     227              : !> \param matrix_a ...
     228              : !> \param beta ...
     229              : !> \param matrix_b ...
     230              : !> \date    11.06.2001
     231              : !> \author  Matthias Krack
     232              : !> \version 1.0
     233              : !> \note
     234              : !>    Use explicit loops to avoid temporary arrays, as a compiler reasonably assumes that arrays
     235              : !>    matrix_a%local_data and matrix_b%local_data may overlap (they are referenced by pointers).
     236              : !>    In general case (alpha*a + beta*b) explicit loops appears to be up to two times more efficient
     237              : !>    than equivalent LAPACK calls (zscale, zaxpy). This is because using LAPACK calls implies
     238              : !>    two passes through each array, so data need to be retrieved twice if arrays are large
     239              : !>    enough to not fit into the processor's cache.
     240              : ! **************************************************************************************************
     241       662207 :    SUBROUTINE cp_cfm_scale_and_add(alpha, matrix_a, beta, matrix_b)
     242              :       COMPLEX(kind=dp), INTENT(in)                       :: alpha
     243              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a
     244              :       COMPLEX(kind=dp), INTENT(in), OPTIONAL             :: beta
     245              :       TYPE(cp_cfm_type), INTENT(IN), OPTIONAL            :: matrix_b
     246              : 
     247              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_scale_and_add'
     248              : 
     249              :       COMPLEX(kind=dp)                                   :: my_beta
     250       662207 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a, b
     251              :       INTEGER                                            :: handle, icol_local, irow_local, mypcol, &
     252              :                                                             myprow, ncol_local, nrow_local
     253              : 
     254       662207 :       CALL timeset(routineN, handle)
     255              : 
     256       662207 :       my_beta = z_zero
     257       662207 :       IF (PRESENT(beta)) my_beta = beta
     258       662207 :       NULLIFY (a, b)
     259              : 
     260              :       ! to do: use dscal,dcopy,daxp
     261       662207 :       myprow = matrix_a%matrix_struct%context%mepos(1)
     262       662207 :       mypcol = matrix_a%matrix_struct%context%mepos(2)
     263              : 
     264       662207 :       nrow_local = matrix_a%matrix_struct%nrow_locals(myprow)
     265       662207 :       ncol_local = matrix_a%matrix_struct%ncol_locals(mypcol)
     266              : 
     267       662207 :       a => matrix_a%local_data
     268              : 
     269       662207 :       IF (my_beta == z_zero) THEN
     270              : 
     271        67040 :          IF (alpha == z_zero) THEN
     272            0 :             a(:, :) = z_zero
     273        67040 :          ELSE IF (alpha == z_one) THEN
     274        67040 :             CALL timestop(handle)
     275        67040 :             RETURN
     276              :          ELSE
     277            0 :             a(:, :) = alpha*a(:, :)
     278              :          END IF
     279              : 
     280              :       ELSE
     281       595167 :          CPASSERT(PRESENT(matrix_b))
     282       595167 :          IF (matrix_a%matrix_struct%context /= matrix_b%matrix_struct%context) &
     283            0 :             CPABORT("matrixes must be in the same blacs context")
     284              : 
     285       595167 :          IF (cp_fm_struct_equivalent(matrix_a%matrix_struct, &
     286              :                                      matrix_b%matrix_struct)) THEN
     287              : 
     288       595167 :             b => matrix_b%local_data
     289              : 
     290       595167 :             IF (alpha == z_zero) THEN
     291           24 :                IF (my_beta == z_one) THEN
     292              :                   !a(:, :) = b(:, :)
     293         1144 :                   DO icol_local = 1, ncol_local
     294        92856 :                      DO irow_local = 1, nrow_local
     295        92832 :                         a(irow_local, icol_local) = b(irow_local, icol_local)
     296              :                      END DO
     297              :                   END DO
     298              :                ELSE
     299              :                   !a(:, :) = my_beta*b(:, :)
     300            0 :                   DO icol_local = 1, ncol_local
     301            0 :                      DO irow_local = 1, nrow_local
     302            0 :                         a(irow_local, icol_local) = my_beta*b(irow_local, icol_local)
     303              :                      END DO
     304              :                   END DO
     305              :                END IF
     306       595143 :             ELSE IF (alpha == z_one) THEN
     307       571500 :                IF (my_beta == z_one) THEN
     308              :                   !a(:, :) = a(:, :)+b(:, :)
     309      4223190 :                   DO icol_local = 1, ncol_local
     310     60538952 :                      DO irow_local = 1, nrow_local
     311     60121732 :                         a(irow_local, icol_local) = a(irow_local, icol_local) + b(irow_local, icol_local)
     312              :                      END DO
     313              :                   END DO
     314              :                ELSE
     315              :                   !a(:, :) = a(:, :)+my_beta*b(:, :)
     316      2275540 :                   DO icol_local = 1, ncol_local
     317     58596034 :                      DO irow_local = 1, nrow_local
     318     58441754 :                         a(irow_local, icol_local) = a(irow_local, icol_local) + my_beta*b(irow_local, icol_local)
     319              :                      END DO
     320              :                   END DO
     321              :                END IF
     322              :             ELSE
     323              :                !a(:, :) = alpha*a(:, :)+my_beta*b(:, :)
     324       349558 :                DO icol_local = 1, ncol_local
     325      7773762 :                   DO irow_local = 1, nrow_local
     326      7750119 :                      a(irow_local, icol_local) = alpha*a(irow_local, icol_local) + my_beta*b(irow_local, icol_local)
     327              :                   END DO
     328              :                END DO
     329              :             END IF
     330              :          ELSE
     331              :             CALL cp_abort(__LOCATION__, &
     332              :                           "cp_cfm_scale_and_add is not yet implemented for cases "// &
     333            0 :                           "where input two matrix structures are not equivalent")
     334              :          END IF
     335              :       END IF
     336       595167 :       CALL timestop(handle)
     337       662207 :    END SUBROUTINE cp_cfm_scale_and_add
     338              : 
     339              : ! **************************************************************************************************
     340              : !> \brief Scale and add two BLACS matrices (a = alpha*a + beta*b).
     341              : !>        where b is a real matrix (adapted from cp_cfm_scale_and_add).
     342              : !> \param alpha ...
     343              : !> \param matrix_a ...
     344              : !> \param beta ...
     345              : !> \param matrix_b ...
     346              : !> \date    01.08.2014
     347              : !> \author  JGH
     348              : !> \version 1.0
     349              : ! **************************************************************************************************
     350       415550 :    SUBROUTINE cp_cfm_scale_and_add_fm(alpha, matrix_a, beta, matrix_b)
     351              :       COMPLEX(kind=dp), INTENT(in)                       :: alpha
     352              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a
     353              :       COMPLEX(kind=dp), INTENT(in)                       :: beta
     354              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_b
     355              : 
     356              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_scale_and_add_fm'
     357              : 
     358       415550 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a
     359              :       INTEGER                                            :: handle, icol_local, irow_local, mypcol, &
     360              :                                                             myprow, ncol_local, nrow_local
     361       415550 :       REAL(kind=dp), DIMENSION(:, :), POINTER            :: b
     362              : 
     363       415550 :       CALL timeset(routineN, handle)
     364              : 
     365       415550 :       NULLIFY (a, b)
     366              : 
     367       415550 :       myprow = matrix_a%matrix_struct%context%mepos(1)
     368       415550 :       mypcol = matrix_a%matrix_struct%context%mepos(2)
     369              : 
     370       415550 :       nrow_local = matrix_a%matrix_struct%nrow_locals(myprow)
     371       415550 :       ncol_local = matrix_a%matrix_struct%ncol_locals(mypcol)
     372              : 
     373       415550 :       a => matrix_a%local_data
     374              : 
     375       415550 :       IF (beta == z_zero) THEN
     376              : 
     377            0 :          IF (alpha == z_zero) THEN
     378            0 :             a(:, :) = z_zero
     379            0 :          ELSE IF (alpha == z_one) THEN
     380            0 :             CALL timestop(handle)
     381            0 :             RETURN
     382              :          ELSE
     383            0 :             a(:, :) = alpha*a(:, :)
     384              :          END IF
     385              : 
     386              :       ELSE
     387       415550 :          IF (matrix_a%matrix_struct%context /= matrix_b%matrix_struct%context) &
     388            0 :             CPABORT("matrices must be in the same blacs context")
     389              : 
     390       415550 :          IF (cp_fm_struct_equivalent(matrix_a%matrix_struct, &
     391              :                                      matrix_b%matrix_struct)) THEN
     392              : 
     393       415550 :             b => matrix_b%local_data
     394              : 
     395       415550 :             IF (alpha == z_zero) THEN
     396       169170 :                IF (beta == z_one) THEN
     397              :                   !a(:, :) = b(:, :)
     398      4755648 :                   DO icol_local = 1, ncol_local
     399    197221156 :                      DO irow_local = 1, nrow_local
     400    197052022 :                         a(irow_local, icol_local) = b(irow_local, icol_local)
     401              :                      END DO
     402              :                   END DO
     403              :                ELSE
     404              :                   !a(:, :) = beta*b(:, :)
     405          684 :                   DO icol_local = 1, ncol_local
     406         6516 :                      DO irow_local = 1, nrow_local
     407         6480 :                         a(irow_local, icol_local) = beta*b(irow_local, icol_local)
     408              :                      END DO
     409              :                   END DO
     410              :                END IF
     411       246380 :             ELSE IF (alpha == z_one) THEN
     412       181634 :                IF (beta == z_one) THEN
     413              :                   !a(:, :) = a(:, :)+b(:, :)
     414       178296 :                   DO icol_local = 1, ncol_local
     415      2273536 :                      DO irow_local = 1, nrow_local
     416      2264592 :                         a(irow_local, icol_local) = a(irow_local, icol_local) + b(irow_local, icol_local)
     417              :                      END DO
     418              :                   END DO
     419              :                ELSE
     420              :                   !a(:, :) = a(:, :)+beta*b(:, :)
     421      4845612 :                   DO icol_local = 1, ncol_local
     422    198402392 :                      DO irow_local = 1, nrow_local
     423    198229702 :                         a(irow_local, icol_local) = a(irow_local, icol_local) + beta*b(irow_local, icol_local)
     424              :                      END DO
     425              :                   END DO
     426              :                END IF
     427              :             ELSE
     428              :                !a(:, :) = alpha*a(:, :)+beta*b(:, :)
     429       562290 :                DO icol_local = 1, ncol_local
     430      5184146 :                   DO irow_local = 1, nrow_local
     431      5119400 :                      a(irow_local, icol_local) = alpha*a(irow_local, icol_local) + beta*b(irow_local, icol_local)
     432              :                   END DO
     433              :                END DO
     434              :             END IF
     435              :          ELSE
     436              :             CALL cp_abort(__LOCATION__, &
     437              :                           "cp_cfm_scale_and_add_fm is not yet implemented for cases "// &
     438            0 :                           "where two input matrix structures are not equivalent")
     439              :          END IF
     440              :       END IF
     441       415550 :       CALL timestop(handle)
     442       415550 :    END SUBROUTINE cp_cfm_scale_and_add_fm
     443              : 
     444              : ! **************************************************************************************************
     445              : !> \brief Computes LU decomposition of a given matrix.
     446              : !> \param matrix_a     full matrix
     447              : !> \param determinant  determinant
     448              : !> \date    11.06.2001
     449              : !> \author  Matthias Krack
     450              : !> \version 1.0
     451              : !> \note
     452              : !>    The actual purpose right now is to efficiently compute the determinant of a given matrix.
     453              : !>    The original content of the matrix is destroyed.
     454              : ! **************************************************************************************************
     455            0 :    SUBROUTINE cp_cfm_lu_decompose(matrix_a, determinant)
     456              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a
     457              :       COMPLEX(kind=dp), INTENT(out)                      :: determinant
     458              : 
     459              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_lu_decompose'
     460              : 
     461            0 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a
     462              :       INTEGER                                            :: counter, handle, info, irow, nrow_global
     463            0 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: ipivot
     464              : 
     465              : #if defined(__parallel)
     466              :       INTEGER                                            :: icol, ncol_local, nrow_local
     467              :       INTEGER, DIMENSION(9)                              :: desca
     468            0 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
     469              : #else
     470              :       INTEGER                                            :: lda
     471              : #endif
     472              : 
     473            0 :       CALL timeset(routineN, handle)
     474              : 
     475            0 :       nrow_global = matrix_a%matrix_struct%nrow_global
     476            0 :       a => matrix_a%local_data
     477              : 
     478            0 :       ALLOCATE (ipivot(nrow_global))
     479              : #if defined(__parallel)
     480              :       CALL cp_cfm_get_info(matrix_a, nrow_local=nrow_local, ncol_local=ncol_local, &
     481            0 :                            row_indices=row_indices, col_indices=col_indices)
     482              : 
     483            0 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
     484            0 :       CALL pzgetrf(nrow_global, nrow_global, a(1, 1), 1, 1, desca, ipivot, info)
     485              : 
     486            0 :       counter = 0
     487            0 :       DO irow = 1, nrow_local
     488            0 :          IF (ipivot(irow) /= row_indices(irow)) counter = counter + 1
     489              :       END DO
     490              : 
     491            0 :       IF (MOD(counter, 2) == 0) THEN
     492            0 :          determinant = z_one
     493              :       ELSE
     494            0 :          determinant = -z_one
     495              :       END IF
     496              : 
     497              :       ! compute product of diagonal elements
     498              :       irow = 1
     499              :       icol = 1
     500            0 :       DO WHILE (irow <= nrow_local .AND. icol <= ncol_local)
     501            0 :          IF (row_indices(irow) < col_indices(icol)) THEN
     502            0 :             irow = irow + 1
     503            0 :          ELSE IF (row_indices(irow) > col_indices(icol)) THEN
     504            0 :             icol = icol + 1
     505              :          ELSE ! diagonal element
     506            0 :             determinant = determinant*a(irow, icol)
     507            0 :             irow = irow + 1
     508            0 :             icol = icol + 1
     509              :          END IF
     510              :       END DO
     511            0 :       CALL matrix_a%matrix_struct%para_env%prod(determinant)
     512              : #else
     513              :       lda = SIZE(a, 1)
     514              :       CALL zgetrf(nrow_global, nrow_global, a(1, 1), lda, ipivot, info)
     515              :       counter = 0
     516              :       determinant = z_one
     517              :       DO irow = 1, nrow_global
     518              :          IF (ipivot(irow) /= irow) counter = counter + 1
     519              :          determinant = determinant*a(irow, irow)
     520              :       END DO
     521              :       IF (MOD(counter, 2) == 1) determinant = -1.0_dp*determinant
     522              : #endif
     523              : 
     524              :       ! info is allowed to be zero
     525              :       ! this does just signal a zero diagonal element
     526            0 :       DEALLOCATE (ipivot)
     527              : 
     528            0 :       CALL timestop(handle)
     529            0 :    END SUBROUTINE cp_cfm_lu_decompose
     530              : 
     531              : ! **************************************************************************************************
     532              : !> \brief Performs one of the matrix-matrix operations:
     533              : !>        matrix_c = alpha * op1( matrix_a ) * op2( matrix_b ) + beta*matrix_c.
     534              : !> \param transa       form of op1( matrix_a ):
     535              : !>                     op1( matrix_a ) = matrix_a,   when transa == 'N' ,
     536              : !>                     op1( matrix_a ) = matrix_a^T, when transa == 'T' ,
     537              : !>                     op1( matrix_a ) = matrix_a^H, when transa == 'C' ,
     538              : !> \param transb       form of op2( matrix_b )
     539              : !> \param m            number of rows of the matrix op1( matrix_a )
     540              : !> \param n            number of columns of the matrix op2( matrix_b )
     541              : !> \param k            number of columns of the matrix op1( matrix_a ) as well as
     542              : !>                     number of rows of the matrix op2( matrix_b )
     543              : !> \param alpha        scale factor
     544              : !> \param matrix_a     matrix A
     545              : !> \param matrix_b     matrix B
     546              : !> \param beta         scale factor
     547              : !> \param matrix_c     matrix C
     548              : !> \param a_first_col  (optional) the first column of the matrix_a to multiply
     549              : !> \param a_first_row  (optional) the first row of the matrix_a to multiply
     550              : !> \param b_first_col  (optional) the first column of the matrix_b to multiply
     551              : !> \param b_first_row  (optional) the first row of the matrix_b to multiply
     552              : !> \param c_first_col  (optional) the first column of the matrix_c
     553              : !> \param c_first_row  (optional) the first row of the matrix_c
     554              : !> \date    07.06.2001
     555              : !> \author  Matthias Krack
     556              : !> \version 1.0
     557              : ! **************************************************************************************************
     558       928123 :    SUBROUTINE cp_cfm_gemm(transa, transb, m, n, k, alpha, matrix_a, matrix_b, beta, &
     559              :                           matrix_c, a_first_col, a_first_row, b_first_col, b_first_row, c_first_col, &
     560              :                           c_first_row)
     561              :       CHARACTER(len=1), INTENT(in)                       :: transa, transb
     562              :       INTEGER, INTENT(in)                                :: m, n, k
     563              :       COMPLEX(kind=dp), INTENT(in)                       :: alpha
     564              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a, matrix_b
     565              :       COMPLEX(kind=dp), INTENT(in)                       :: beta
     566              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_c
     567              :       INTEGER, INTENT(in), OPTIONAL                      :: a_first_col, a_first_row, b_first_col, &
     568              :                                                             b_first_row, c_first_col, c_first_row
     569              : 
     570              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_gemm'
     571              : 
     572       928123 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a, b, c
     573              :       INTEGER                                            :: handle, i_a, i_b, i_c, j_a, j_b, j_c
     574              : #if defined(__parallel)
     575              :       INTEGER, DIMENSION(9)                              :: desca, descb, descc
     576              : #else
     577              :       INTEGER                                            :: lda, ldb, ldc
     578              : #endif
     579              : 
     580       928123 :       CALL timeset(routineN, handle)
     581       928123 :       a => matrix_a%local_data
     582       928123 :       b => matrix_b%local_data
     583       928123 :       c => matrix_c%local_data
     584              : 
     585       928123 :       i_a = 1
     586       928123 :       IF (PRESENT(a_first_row)) i_a = a_first_row
     587              : 
     588       928123 :       j_a = 1
     589       928123 :       IF (PRESENT(a_first_col)) j_a = a_first_col
     590              : 
     591       928123 :       i_b = 1
     592       928123 :       IF (PRESENT(b_first_row)) i_b = b_first_row
     593              : 
     594       928123 :       j_b = 1
     595       928123 :       IF (PRESENT(b_first_col)) j_b = b_first_col
     596              : 
     597       928123 :       i_c = 1
     598       928123 :       IF (PRESENT(c_first_row)) i_c = c_first_row
     599              : 
     600       928123 :       j_c = 1
     601       928123 :       IF (PRESENT(c_first_col)) j_c = c_first_col
     602              : 
     603              : #if defined(__parallel)
     604      9281230 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
     605      9281230 :       descb(:) = matrix_b%matrix_struct%descriptor(:)
     606      9281230 :       descc(:) = matrix_c%matrix_struct%descriptor(:)
     607              : 
     608              :       CALL pzgemm(transa, transb, m, n, k, alpha, a(1, 1), i_a, j_a, desca, &
     609       928123 :                   b(1, 1), i_b, j_b, descb, beta, c(1, 1), i_c, j_c, descc)
     610              : #else
     611              :       lda = SIZE(a, 1)
     612              :       ldb = SIZE(b, 1)
     613              :       ldc = SIZE(c, 1)
     614              : 
     615              :       ! consider zgemm3m
     616              :       CALL zgemm(transa, transb, m, n, k, alpha, a(i_a, j_a), &
     617              :                  lda, b(i_b, j_b), ldb, beta, c(i_c, j_c), ldc)
     618              : #endif
     619       928123 :       CALL timestop(handle)
     620       928123 :    END SUBROUTINE cp_cfm_gemm
     621              : 
     622              : ! **************************************************************************************************
     623              : !> \brief Scales columns of the full matrix by corresponding factors.
     624              : !> \param matrix_a matrix to scale
     625              : !> \param scaling  scale factors for every column. The actual number of scaled columns is
     626              : !>                 limited by the number of scale factors given or by the actual number of columns
     627              : !>                 whichever is smaller.
     628              : !> \author Joost VandeVondele
     629              : ! **************************************************************************************************
     630        60207 :    SUBROUTINE cp_cfm_column_scale(matrix_a, scaling)
     631              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a
     632              :       COMPLEX(kind=dp), DIMENSION(:), INTENT(in)         :: scaling
     633              : 
     634              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_column_scale'
     635              : 
     636        60207 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a
     637              :       INTEGER                                            :: handle, icol_local, ncol_local, &
     638              :                                                             nrow_local
     639              : #if defined(__parallel)
     640        60207 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices
     641              : #endif
     642              : 
     643        60207 :       CALL timeset(routineN, handle)
     644              : 
     645        60207 :       a => matrix_a%local_data
     646              : 
     647              : #if defined(__parallel)
     648        60207 :       CALL cp_cfm_get_info(matrix_a, nrow_local=nrow_local, ncol_local=ncol_local, col_indices=col_indices)
     649        60207 :       ncol_local = MIN(ncol_local, SIZE(scaling))
     650              : 
     651      1234142 :       DO icol_local = 1, ncol_local
     652      1234142 :          CALL zscal(nrow_local, scaling(col_indices(icol_local)), a(1, icol_local), 1)
     653              :       END DO
     654              : #else
     655              :       nrow_local = SIZE(a, 1)
     656              :       ncol_local = MIN(SIZE(a, 2), SIZE(scaling))
     657              : 
     658              :       DO icol_local = 1, ncol_local
     659              :          CALL zscal(nrow_local, scaling(icol_local), a(1, icol_local), 1)
     660              :       END DO
     661              : #endif
     662              : 
     663        60207 :       CALL timestop(handle)
     664        60207 :    END SUBROUTINE cp_cfm_column_scale
     665              : 
     666              : ! **************************************************************************************************
     667              : !> \brief Scales a complex matrix by a real number.
     668              : !>      matrix_a = alpha * matrix_b
     669              : !> \param alpha    scale factor
     670              : !> \param matrix_a complex matrix to scale
     671              : ! **************************************************************************************************
     672        21108 :    SUBROUTINE cp_cfm_dscale(alpha, matrix_a)
     673              :       REAL(kind=dp), INTENT(in)                          :: alpha
     674              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a
     675              : 
     676              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cp_cfm_dscale'
     677              : 
     678        21108 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a
     679              :       INTEGER                                            :: handle
     680              : 
     681        21108 :       CALL timeset(routineN, handle)
     682              : 
     683        21108 :       NULLIFY (a)
     684              : 
     685        21108 :       a => matrix_a%local_data
     686              : 
     687        63324 :       CALL zdscal(SIZE(a), alpha, a(1, 1), 1)
     688              : 
     689        21108 :       CALL timestop(handle)
     690        21108 :    END SUBROUTINE cp_cfm_dscale
     691              : 
     692              : ! **************************************************************************************************
     693              : !> \brief Scales a complex matrix by a complex number.
     694              : !>      matrix_a = alpha * matrix_b
     695              : !> \param alpha    scale factor
     696              : !> \param matrix_a complex matrix to scale
     697              : !> \note
     698              : !>      use cp_fm_set_all to zero (avoids problems with nan)
     699              : ! **************************************************************************************************
     700        40493 :    SUBROUTINE cp_cfm_zscale(alpha, matrix_a)
     701              :       COMPLEX(kind=dp), INTENT(IN)                       :: alpha
     702              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a
     703              : 
     704              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cp_cfm_zscale'
     705              : 
     706        40493 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a
     707              :       INTEGER                                            :: handle, size_a
     708              : 
     709        40493 :       CALL timeset(routineN, handle)
     710              : 
     711        40493 :       NULLIFY (a)
     712              : 
     713        40493 :       a => matrix_a%local_data
     714        40493 :       size_a = SIZE(a, 1)*SIZE(a, 2)
     715              : 
     716        40493 :       CALL zscal(size_a, alpha, a(1, 1), 1)
     717              : 
     718        40493 :       CALL timestop(handle)
     719        40493 :    END SUBROUTINE cp_cfm_zscale
     720              : 
     721              : ! **************************************************************************************************
     722              : !> \brief Solve the system of linear equations A*b=A_general using LU decomposition.
     723              : !>        Pay attention that both matrices are overwritten on exit and that
     724              : !>        the result is stored into the matrix 'general_a'.
     725              : !> \param matrix_a     matrix A (overwritten on exit)
     726              : !> \param general_a    (input) matrix A_general, (output) matrix B
     727              : !> \param determinant  (optional) determinant
     728              : !> \author Florian Schiffmann
     729              : ! **************************************************************************************************
     730         7146 :    SUBROUTINE cp_cfm_solve(matrix_a, general_a, determinant)
     731              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a, general_a
     732              :       COMPLEX(kind=dp), OPTIONAL                         :: determinant
     733              : 
     734              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_solve'
     735              : 
     736         7146 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: a, a_general
     737              :       INTEGER                                            :: counter, handle, info, irow, nrow_global
     738         7146 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: ipivot
     739              : 
     740              : #if defined(__parallel)
     741              :       INTEGER                                            :: icol, ncol_local, nrow_local
     742              :       INTEGER, DIMENSION(9)                              :: desca, descb
     743         7146 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
     744              : #else
     745              :       INTEGER                                            :: lda, ldb
     746              : #endif
     747              : 
     748         7146 :       CALL timeset(routineN, handle)
     749              : 
     750         7146 :       a => matrix_a%local_data
     751         7146 :       a_general => general_a%local_data
     752         7146 :       nrow_global = matrix_a%matrix_struct%nrow_global
     753        21438 :       ALLOCATE (ipivot(nrow_global))
     754              : 
     755              : #if defined(__parallel)
     756        71460 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
     757        71460 :       descb(:) = general_a%matrix_struct%descriptor(:)
     758         7146 :       CALL pzgetrf(nrow_global, nrow_global, a(1, 1), 1, 1, desca, ipivot, info)
     759         7146 :       IF (PRESENT(determinant)) THEN
     760              :          CALL cp_cfm_get_info(matrix_a, nrow_local=nrow_local, ncol_local=ncol_local, &
     761         6418 :                               row_indices=row_indices, col_indices=col_indices)
     762              : 
     763         6418 :          counter = 0
     764        19302 :          DO irow = 1, nrow_local
     765        19302 :             IF (ipivot(irow) /= row_indices(irow)) counter = counter + 1
     766              :          END DO
     767              : 
     768         6418 :          IF (MOD(counter, 2) == 0) THEN
     769         6408 :             determinant = z_one
     770              :          ELSE
     771           10 :             determinant = -z_one
     772              :          END IF
     773              : 
     774              :          ! compute product of diagonal elements
     775              :          irow = 1
     776              :          icol = 1
     777        28941 :          DO WHILE (irow <= nrow_local .AND. icol <= ncol_local)
     778        28941 :             IF (row_indices(irow) < col_indices(icol)) THEN
     779            0 :                irow = irow + 1
     780        22523 :             ELSE IF (row_indices(irow) > col_indices(icol)) THEN
     781         9639 :                icol = icol + 1
     782              :             ELSE ! diagonal element
     783        12884 :                determinant = determinant*a(irow, icol)
     784        12884 :                irow = irow + 1
     785        12884 :                icol = icol + 1
     786              :             END IF
     787              :          END DO
     788         6418 :          CALL matrix_a%matrix_struct%para_env%prod(determinant)
     789              :       END IF
     790              : 
     791              :       CALL pzgetrs("N", nrow_global, nrow_global, a(1, 1), 1, 1, desca, &
     792         7146 :                    ipivot, a_general(1, 1), 1, 1, descb, info)
     793              : #else
     794              :       lda = SIZE(a, 1)
     795              :       ldb = SIZE(a_general, 1)
     796              :       CALL zgetrf(nrow_global, nrow_global, a(1, 1), lda, ipivot, info)
     797              :       IF (PRESENT(determinant)) THEN
     798              :          counter = 0
     799              :          determinant = z_one
     800              :          DO irow = 1, nrow_global
     801              :             IF (ipivot(irow) /= irow) counter = counter + 1
     802              :             determinant = determinant*a(irow, irow)
     803              :          END DO
     804              :          IF (MOD(counter, 2) == 1) determinant = -1.0_dp*determinant
     805              :       END IF
     806              :       CALL zgetrs("N", nrow_global, nrow_global, a(1, 1), lda, ipivot, a_general(1, 1), ldb, info)
     807              : #endif
     808              : 
     809              :       ! info is allowed to be zero
     810              :       ! this does just signal a zero diagonal element
     811         7146 :       DEALLOCATE (ipivot)
     812         7146 :       CALL timestop(handle)
     813              : 
     814         7146 :    END SUBROUTINE cp_cfm_solve
     815              : 
     816              : ! **************************************************************************************************
     817              : !> \brief Inverts a matrix using LU decomposition. The input matrix will be overwritten.
     818              : !> \param matrix     input a general square non-singular matrix, outputs its inverse
     819              : !> \param info_out   optional, if present outputs the info from (p)zgetri
     820              : !> \author Lianheng Tong
     821              : ! **************************************************************************************************
     822       120050 :    SUBROUTINE cp_cfm_lu_invert(matrix, info_out)
     823              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix
     824              :       INTEGER, INTENT(out), OPTIONAL                     :: info_out
     825              : 
     826              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_lu_invert'
     827              : 
     828       120050 :       COMPLEX(kind=dp), ALLOCATABLE, DIMENSION(:)        :: work
     829              :       COMPLEX(kind=dp), DIMENSION(1)                     :: work1
     830       120050 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: mat
     831              :       INTEGER                                            :: handle, info, lwork, nrows_global
     832       120050 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: ipivot
     833              : 
     834              : #if defined(__parallel)
     835              :       INTEGER                                            :: liwork
     836       120050 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: iwork
     837              :       INTEGER, DIMENSION(1)                              :: iwork1
     838              :       INTEGER, DIMENSION(9)                              :: desca
     839              : #else
     840              :       INTEGER                                            :: lda
     841              : #endif
     842              : 
     843       120050 :       CALL timeset(routineN, handle)
     844              : 
     845       120050 :       mat => matrix%local_data
     846       120050 :       nrows_global = matrix%matrix_struct%nrow_global
     847       120050 :       CPASSERT(nrows_global == matrix%matrix_struct%ncol_global)
     848       360150 :       ALLOCATE (ipivot(nrows_global))
     849              : 
     850              :       ! do LU decomposition
     851              : #if defined(__parallel)
     852      1200500 :       desca = matrix%matrix_struct%descriptor
     853              :       CALL pzgetrf(nrows_global, nrows_global, &
     854       120050 :                    mat(1, 1), 1, 1, desca, ipivot, info)
     855              : #else
     856              :       lda = SIZE(mat, 1)
     857              :       CALL zgetrf(nrows_global, nrows_global, &
     858              :                   mat(1, 1), lda, ipivot, info)
     859              : #endif
     860       120050 :       IF (info /= 0) THEN
     861            0 :          CALL cp_abort(__LOCATION__, "LU decomposition has failed")
     862              :       END IF
     863              : 
     864              :       ! do inversion
     865              : #if defined(__parallel)
     866              :       CALL pzgetri(nrows_global, mat(1, 1), 1, 1, desca, &
     867       120050 :                    ipivot, work1, -1, iwork1, -1, info)
     868       120050 :       lwork = INT(work1(1))
     869       120050 :       liwork = INT(iwork1(1))
     870       360150 :       ALLOCATE (work(lwork))
     871       360150 :       ALLOCATE (iwork(liwork))
     872              :       CALL pzgetri(nrows_global, mat(1, 1), 1, 1, desca, &
     873       120050 :                    ipivot, work, lwork, iwork, liwork, info)
     874       120050 :       DEALLOCATE (iwork)
     875              : #else
     876              :       CALL zgetri(nrows_global, mat(1, 1), lda, ipivot, work1, -1, info)
     877              :       lwork = INT(work1(1))
     878              :       ALLOCATE (work(lwork))
     879              :       CALL zgetri(nrows_global, mat(1, 1), lda, ipivot, work, lwork, info)
     880              : #endif
     881       120050 :       DEALLOCATE (work)
     882       120050 :       DEALLOCATE (ipivot)
     883              : 
     884       120050 :       IF (PRESENT(info_out)) THEN
     885            0 :          info_out = info
     886              :       ELSE
     887       120050 :          IF (info /= 0) &
     888            0 :             CALL cp_abort(__LOCATION__, "LU inversion has failed")
     889              :       END IF
     890              : 
     891       120050 :       CALL timestop(handle)
     892              : 
     893       120050 :    END SUBROUTINE cp_cfm_lu_invert
     894              : 
     895              : ! **************************************************************************************************
     896              : !> \brief Returns the trace of matrix_a^T matrix_b, i.e
     897              : !>      sum_{i,j}(matrix_a(i,j)*matrix_b(i,j)) .
     898              : !> \param matrix_a a complex matrix
     899              : !> \param matrix_b another complex matrix
     900              : !> \param trace    value of the trace operator
     901              : !> \par History
     902              : !>    * 09.2017 created [Sergey Chulkov]
     903              : !> \author Sergey Chulkov
     904              : !> \note
     905              : !>      Based on the subroutine cp_fm_trace(). Note the transposition of matrix_a!
     906              : ! **************************************************************************************************
     907       165927 :    SUBROUTINE cp_cfm_trace(matrix_a, matrix_b, trace)
     908              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix_a, matrix_b
     909              :       COMPLEX(kind=dp), INTENT(out)                      :: trace
     910              : 
     911              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cp_cfm_trace'
     912              : 
     913              :       INTEGER                                            :: handle, mypcol, myprow, ncol_local, &
     914              :                                                             npcol, nprow, nrow_local
     915              :       TYPE(cp_blacs_env_type), POINTER                   :: context
     916              :       TYPE(mp_comm_type)                                 :: group
     917              : 
     918       165927 :       CALL timeset(routineN, handle)
     919              : 
     920       165927 :       context => matrix_a%matrix_struct%context
     921       165927 :       myprow = context%mepos(1)
     922       165927 :       mypcol = context%mepos(2)
     923       165927 :       nprow = context%num_pe(1)
     924       165927 :       npcol = context%num_pe(2)
     925              : 
     926       165927 :       group = matrix_a%matrix_struct%para_env
     927              : 
     928       165927 :       nrow_local = MIN(matrix_a%matrix_struct%nrow_locals(myprow), matrix_b%matrix_struct%nrow_locals(myprow))
     929       165927 :       ncol_local = MIN(matrix_a%matrix_struct%ncol_locals(mypcol), matrix_b%matrix_struct%ncol_locals(mypcol))
     930              : 
     931              :       ! compute an accurate dot-product
     932              :       trace = accurate_dot_product(matrix_a%local_data(1:nrow_local, 1:ncol_local), &
     933       165927 :                                    matrix_b%local_data(1:nrow_local, 1:ncol_local))
     934              : 
     935       165927 :       CALL group%sum(trace)
     936              : 
     937       165927 :       CALL timestop(handle)
     938              : 
     939       165927 :    END SUBROUTINE cp_cfm_trace
     940              : 
     941              : ! **************************************************************************************************
     942              : !> \brief Multiplies in place by a triangular matrix:
     943              : !>       matrix_b = alpha op(triangular_matrix) matrix_b
     944              : !>      or (if side='R')
     945              : !>       matrix_b = alpha matrix_b op(triangular_matrix)
     946              : !>      op(triangular_matrix) is:
     947              : !>       triangular_matrix (if transa="N" and invert_tr=.false.)
     948              : !>       triangular_matrix^T (if transa="T" and invert_tr=.false.)
     949              : !>       triangular_matrix^H (if transa="C" and invert_tr=.false.)
     950              : !>       triangular_matrix^(-1) (if transa="N" and invert_tr=.true.)
     951              : !>       triangular_matrix^(-T) (if transa="T" and invert_tr=.true.)
     952              : !>       triangular_matrix^(-H) (if transa="C" and invert_tr=.true.)
     953              : !> \param triangular_matrix the triangular matrix that multiplies the other
     954              : !> \param matrix_b the matrix that gets multiplied and stores the result
     955              : !> \param side on which side of matrix_b stays op(triangular_matrix)
     956              : !>        (defaults to 'L')
     957              : !> \param transa_tr ...
     958              : !> \param invert_tr if the triangular matrix should be inverted
     959              : !>        (defaults to false)
     960              : !> \param uplo_tr if triangular_matrix is stored in the upper ('U') or
     961              : !>        lower ('L') triangle (defaults to 'U')
     962              : !> \param unit_diag_tr if the diagonal elements of triangular_matrix should
     963              : !>        be assumed to be 1 (defaults to false)
     964              : !> \param n_rows the number of rows of the result (defaults to
     965              : !>        size(matrix_b,1))
     966              : !> \param n_cols the number of columns of the result (defaults to
     967              : !>        size(matrix_b,2))
     968              : !> \param alpha ...
     969              : !> \par History
     970              : !>      08.2002 created [fawzi]
     971              : !> \author Fawzi Mohamed
     972              : !> \note
     973              : !>      needs an mpi env
     974              : ! **************************************************************************************************
     975       483654 :    SUBROUTINE cp_cfm_triangular_multiply(triangular_matrix, matrix_b, side, &
     976              :                                          transa_tr, invert_tr, uplo_tr, unit_diag_tr, n_rows, n_cols, &
     977              :                                          alpha)
     978              :       TYPE(cp_cfm_type), INTENT(IN)                      :: triangular_matrix, matrix_b
     979              :       CHARACTER, INTENT(in), OPTIONAL                    :: side, transa_tr
     980              :       LOGICAL, INTENT(in), OPTIONAL                      :: invert_tr
     981              :       CHARACTER, INTENT(in), OPTIONAL                    :: uplo_tr
     982              :       LOGICAL, INTENT(in), OPTIONAL                      :: unit_diag_tr
     983              :       INTEGER, INTENT(in), OPTIONAL                      :: n_rows, n_cols
     984              :       COMPLEX(kind=dp), INTENT(in), OPTIONAL             :: alpha
     985              : 
     986              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_triangular_multiply'
     987              : 
     988              :       CHARACTER                                          :: side_char, transa, unit_diag, uplo
     989              :       COMPLEX(kind=dp)                                   :: al
     990              :       INTEGER                                            :: handle, m, n
     991              :       LOGICAL                                            :: invert
     992              : 
     993       241827 :       CALL timeset(routineN, handle)
     994       241827 :       side_char = 'L'
     995       241827 :       unit_diag = 'N'
     996       241827 :       uplo = 'U'
     997       241827 :       transa = 'N'
     998       241827 :       invert = .FALSE.
     999       241827 :       al = z_one
    1000       241827 :       CALL cp_cfm_get_info(matrix_b, nrow_global=m, ncol_global=n)
    1001       241827 :       IF (PRESENT(side)) side_char = side
    1002       241827 :       IF (PRESENT(invert_tr)) invert = invert_tr
    1003       241827 :       IF (PRESENT(uplo_tr)) uplo = uplo_tr
    1004       241827 :       IF (PRESENT(unit_diag_tr)) THEN
    1005            0 :          IF (unit_diag_tr) THEN
    1006            0 :             unit_diag = 'U'
    1007              :          ELSE
    1008              :             unit_diag = 'N'
    1009              :          END IF
    1010              :       END IF
    1011       241827 :       IF (PRESENT(transa_tr)) transa = transa_tr
    1012       241827 :       IF (PRESENT(alpha)) al = alpha
    1013       241827 :       IF (PRESENT(n_rows)) m = n_rows
    1014       241827 :       IF (PRESENT(n_cols)) n = n_cols
    1015              : 
    1016       241827 :       IF (invert) THEN
    1017              : 
    1018              : #if defined(__parallel)
    1019              :          CALL pztrsm(side_char, uplo, transa, unit_diag, m, n, al, &
    1020              :                      triangular_matrix%local_data(1, 1), 1, 1, &
    1021              :                      triangular_matrix%matrix_struct%descriptor, &
    1022              :                      matrix_b%local_data(1, 1), 1, 1, &
    1023         2064 :                      matrix_b%matrix_struct%descriptor(1))
    1024              : #else
    1025              :          CALL ztrsm(side_char, uplo, transa, unit_diag, m, n, al, &
    1026              :                     triangular_matrix%local_data(1, 1), &
    1027              :                     SIZE(triangular_matrix%local_data, 1), &
    1028              :                     matrix_b%local_data(1, 1), SIZE(matrix_b%local_data, 1))
    1029              : #endif
    1030              : 
    1031              :       ELSE
    1032              : 
    1033              : #if defined(__parallel)
    1034              :          CALL pztrmm(side_char, uplo, transa, unit_diag, m, n, al, &
    1035              :                      triangular_matrix%local_data(1, 1), 1, 1, &
    1036              :                      triangular_matrix%matrix_struct%descriptor, &
    1037              :                      matrix_b%local_data(1, 1), 1, 1, &
    1038       239763 :                      matrix_b%matrix_struct%descriptor(1))
    1039              : #else
    1040              :          CALL ztrmm(side_char, uplo, transa, unit_diag, m, n, al, &
    1041              :                     triangular_matrix%local_data(1, 1), &
    1042              :                     SIZE(triangular_matrix%local_data, 1), &
    1043              :                     matrix_b%local_data(1, 1), SIZE(matrix_b%local_data, 1))
    1044              : #endif
    1045              : 
    1046              :       END IF
    1047              : 
    1048       241827 :       CALL timestop(handle)
    1049              : 
    1050       241827 :    END SUBROUTINE cp_cfm_triangular_multiply
    1051              : 
    1052              : ! **************************************************************************************************
    1053              : !> \brief Inverts a triangular matrix.
    1054              : !> \param matrix_a ...
    1055              : !> \param uplo ...
    1056              : !> \param info_out ...
    1057              : !> \author MI
    1058              : ! **************************************************************************************************
    1059        79921 :    SUBROUTINE cp_cfm_triangular_invert(matrix_a, uplo, info_out)
    1060              :       TYPE(cp_cfm_type), INTENT(IN)            :: matrix_a
    1061              :       CHARACTER, INTENT(in), OPTIONAL          :: uplo
    1062              :       INTEGER, INTENT(out), OPTIONAL           :: info_out
    1063              : 
    1064              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_triangular_invert'
    1065              : 
    1066              :       CHARACTER                                :: unit_diag, my_uplo
    1067              :       INTEGER                                  :: handle, info, ncol_global
    1068              :       COMPLEX(kind=dp), DIMENSION(:, :), &
    1069        79921 :          POINTER                               :: a
    1070              : #if defined(__parallel)
    1071              :       INTEGER, DIMENSION(9)                    :: desca
    1072              : #endif
    1073              : 
    1074        79921 :       CALL timeset(routineN, handle)
    1075              : 
    1076        79921 :       unit_diag = 'N'
    1077        79921 :       my_uplo = 'U'
    1078        79921 :       IF (PRESENT(uplo)) my_uplo = uplo
    1079              : 
    1080        79921 :       ncol_global = matrix_a%matrix_struct%ncol_global
    1081              : 
    1082        79921 :       a => matrix_a%local_data
    1083              : 
    1084              : #if defined(__parallel)
    1085       799210 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
    1086        79921 :       CALL pztrtri(my_uplo, unit_diag, ncol_global, a(1, 1), 1, 1, desca, info)
    1087              : #else
    1088              :       CALL ztrtri(my_uplo, unit_diag, ncol_global, a(1, 1), ncol_global, info)
    1089              : #endif
    1090              : 
    1091        79921 :       IF (PRESENT(info_out)) THEN
    1092            0 :          info_out = info
    1093              :       ELSE
    1094        79921 :          IF (info /= 0) &
    1095              :             CALL cp_abort(__LOCATION__, &
    1096            0 :                           "triangular invert failed: matrix is not positive definite  or ill-conditioned")
    1097              :       END IF
    1098              : 
    1099        79921 :       CALL timestop(handle)
    1100        79921 :    END SUBROUTINE cp_cfm_triangular_invert
    1101              : 
    1102              : ! **************************************************************************************************
    1103              : !> \brief Transposes a BLACS distributed complex matrix.
    1104              : !> \param matrix    input matrix
    1105              : !> \param trans     'T' for transpose, 'C' for Hermitian conjugate
    1106              : !> \param matrixt   output matrix
    1107              : !> \author Lianheng Tong
    1108              : ! **************************************************************************************************
    1109        31162 :    SUBROUTINE cp_cfm_transpose(matrix, trans, matrixt)
    1110              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix
    1111              :       CHARACTER, INTENT(in)                              :: trans
    1112              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrixt
    1113              : 
    1114              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_transpose'
    1115              : 
    1116        31162 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: aa, cc
    1117              :       INTEGER                                            :: handle, ncol_global, nrow_global
    1118              : #if defined(__parallel)
    1119              :       INTEGER, DIMENSION(9)                              :: desca, descc
    1120              : #elif !defined(__MKL)
    1121              :       INTEGER                                            :: ii, jj
    1122              : #endif
    1123              : 
    1124        31162 :       CALL timeset(routineN, handle)
    1125              : 
    1126        31162 :       nrow_global = matrix%matrix_struct%nrow_global
    1127        31162 :       ncol_global = matrix%matrix_struct%ncol_global
    1128              : 
    1129        31162 :       CPASSERT(matrixt%matrix_struct%nrow_global == ncol_global)
    1130        31162 :       CPASSERT(matrixt%matrix_struct%ncol_global == nrow_global)
    1131              : 
    1132        31162 :       aa => matrix%local_data
    1133        31162 :       cc => matrixt%local_data
    1134              : 
    1135              : #if defined(__parallel)
    1136       311620 :       desca = matrix%matrix_struct%descriptor
    1137       311620 :       descc = matrixt%matrix_struct%descriptor
    1138        13338 :       SELECT CASE (trans)
    1139              :       CASE ('T')
    1140              :          CALL pztranu(nrow_global, ncol_global, &
    1141              :                       z_one, aa(1, 1), 1, 1, desca, &
    1142        13338 :                       z_zero, cc(1, 1), 1, 1, descc)
    1143              :       CASE ('C')
    1144              :          CALL pztranc(nrow_global, ncol_global, &
    1145              :                       z_one, aa(1, 1), 1, 1, desca, &
    1146        17824 :                       z_zero, cc(1, 1), 1, 1, descc)
    1147              :       CASE DEFAULT
    1148        31162 :          CPABORT("trans only accepts 'T' or 'C'")
    1149              :       END SELECT
    1150              : #elif defined(__MKL)
    1151              :       CALL mkl_zomatcopy('C', trans, nrow_global, ncol_global, 1.0_dp, aa(1, 1), nrow_global, cc(1, 1), ncol_global)
    1152              : #else
    1153              :       SELECT CASE (trans)
    1154              :       CASE ('T')
    1155              :          DO jj = 1, ncol_global
    1156              :             DO ii = 1, nrow_global
    1157              :                cc(ii, jj) = aa(jj, ii)
    1158              :             END DO
    1159              :          END DO
    1160              :       CASE ('C')
    1161              :          DO jj = 1, ncol_global
    1162              :             DO ii = 1, nrow_global
    1163              :                cc(ii, jj) = CONJG(aa(jj, ii))
    1164              :             END DO
    1165              :          END DO
    1166              :       CASE DEFAULT
    1167              :          CPABORT("trans only accepts 'T' or 'C'")
    1168              :       END SELECT
    1169              : #endif
    1170              : 
    1171        31162 :       CALL timestop(handle)
    1172        31162 :    END SUBROUTINE cp_cfm_transpose
    1173              : 
    1174              : ! **************************************************************************************************
    1175              : !> \brief Norm of matrix using (p)zlange.
    1176              : !> \param matrix     input a general matrix
    1177              : !> \param mode       'M' max abs element value,
    1178              : !>                   '1' or 'O' one norm, i.e. maximum column sum,
    1179              : !>                   'I' infinity norm, i.e. maximum row sum,
    1180              : !>                   'F' or 'E' Frobenius norm, i.e. sqrt of sum of all squares of elements
    1181              : !> \return the norm according to mode
    1182              : !> \author Lianheng Tong
    1183              : ! **************************************************************************************************
    1184       237258 :    FUNCTION cp_cfm_norm(matrix, mode) RESULT(res)
    1185              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix
    1186              :       CHARACTER, INTENT(IN)                              :: mode
    1187              :       REAL(kind=dp)                                      :: res
    1188              : 
    1189              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_norm'
    1190              : 
    1191       237258 :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: aa
    1192              :       INTEGER                                            :: handle, lwork, ncols, ncols_local, &
    1193              :                                                             nrows, nrows_local
    1194       237258 :       REAL(kind=dp), ALLOCATABLE, DIMENSION(:)           :: work
    1195              : 
    1196              : #if defined(__parallel)
    1197              :       INTEGER, DIMENSION(9)                              :: desca
    1198              : #else
    1199              :       INTEGER                                            :: lda
    1200              : #endif
    1201              : 
    1202       237258 :       CALL timeset(routineN, handle)
    1203              : 
    1204              :       CALL cp_cfm_get_info(matrix=matrix, &
    1205              :                            nrow_global=nrows, &
    1206              :                            ncol_global=ncols, &
    1207              :                            nrow_local=nrows_local, &
    1208       237258 :                            ncol_local=ncols_local)
    1209       237258 :       aa => matrix%local_data
    1210              : 
    1211              :       SELECT CASE (mode)
    1212              :       CASE ('M', 'm')
    1213            0 :          lwork = 1
    1214              :       CASE ('1', 'O', 'o')
    1215              : #if defined(__parallel)
    1216            0 :          lwork = ncols_local
    1217              : #else
    1218              :          lwork = 1
    1219              : #endif
    1220              :       CASE ('I', 'i')
    1221              : #if defined(__parallel)
    1222            0 :          lwork = nrows_local
    1223              : #else
    1224              :          lwork = nrows
    1225              : #endif
    1226              :       CASE ('F', 'f', 'E', 'e')
    1227            0 :          lwork = 1
    1228              :       CASE DEFAULT
    1229       237258 :          CPABORT("mode input is not valid")
    1230              :       END SELECT
    1231              : 
    1232       711774 :       ALLOCATE (work(lwork))
    1233              : 
    1234              : #if defined(__parallel)
    1235      2372580 :       desca = matrix%matrix_struct%descriptor
    1236       237258 :       res = pzlange(mode, nrows, ncols, aa(1, 1), 1, 1, desca, work)
    1237              : #else
    1238              :       lda = SIZE(aa, 1)
    1239              :       res = zlange(mode, nrows, ncols, aa(1, 1), lda, work)
    1240              : #endif
    1241              : 
    1242       237258 :       DEALLOCATE (work)
    1243       237258 :       CALL timestop(handle)
    1244       237258 :    END FUNCTION cp_cfm_norm
    1245              : 
    1246              : ! **************************************************************************************************
    1247              : !> \brief Applies a planar rotation defined by cs and sn to the i'th and j'th rows.
    1248              : !> \param matrix ...
    1249              : !> \param irow ...
    1250              : !> \param jrow ...
    1251              : !> \param cs cosine of the rotation angle
    1252              : !> \param sn sinus of the rotation angle
    1253              : !> \author Ole Schuett
    1254              : ! **************************************************************************************************
    1255       375120 :    SUBROUTINE cp_cfm_rot_rows(matrix, irow, jrow, cs, sn)
    1256              :       TYPE(cp_cfm_type), INTENT(IN)            :: matrix
    1257              :       INTEGER, INTENT(IN)                      :: irow, jrow
    1258              :       REAL(dp), INTENT(IN)                     :: cs, sn
    1259              : 
    1260              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_rot_rows'
    1261              :       INTEGER                                  :: handle, ncol
    1262              :       COMPLEX(KIND=dp)                         :: sn_cmplx
    1263              : 
    1264              : #if defined(__parallel)
    1265              :       INTEGER                                  :: info, lwork
    1266              :       INTEGER, DIMENSION(9)                    :: desc
    1267       375120 :       REAL(dp), DIMENSION(:), ALLOCATABLE      :: work
    1268              : #endif
    1269       375120 :       CALL timeset(routineN, handle)
    1270       375120 :       CALL cp_cfm_get_info(matrix, ncol_global=ncol)
    1271       375120 :       sn_cmplx = CMPLX(sn, 0.0_dp, dp)
    1272              : #if defined(__parallel)
    1273       375120 :       IF (1 /= matrix%matrix_struct%context%n_pid) THEN
    1274       375120 :          lwork = 2*ncol + 1
    1275      1125360 :          ALLOCATE (work(lwork))
    1276      3751200 :          desc(:) = matrix%matrix_struct%descriptor(:)
    1277       375120 :          info = 0
    1278              :          CALL pzrot(ncol, &
    1279              :                     matrix%local_data(1, 1), irow, 1, desc, ncol, &
    1280              :                     matrix%local_data(1, 1), jrow, 1, desc, ncol, &
    1281       375120 :                     cs, sn_cmplx, work, lwork, info)
    1282       375120 :          CPASSERT(info == 0)
    1283       375120 :          DEALLOCATE (work)
    1284              :       ELSE
    1285              : #endif
    1286            0 :          CALL zrot(ncol, matrix%local_data(irow, 1), ncol, matrix%local_data(jrow, 1), ncol, cs, sn_cmplx)
    1287              : #if defined(__parallel)
    1288              :       END IF
    1289              : #endif
    1290       375120 :       CALL timestop(handle)
    1291       375120 :    END SUBROUTINE cp_cfm_rot_rows
    1292              : 
    1293              : ! **************************************************************************************************
    1294              : !> \brief Applies a planar rotation defined by cs and sn to the i'th and j'th columnns.
    1295              : !> \param matrix ...
    1296              : !> \param icol ...
    1297              : !> \param jcol ...
    1298              : !> \param cs cosine of the rotation angle
    1299              : !> \param sn sinus of the rotation angle
    1300              : !> \author Ole Schuett
    1301              : ! **************************************************************************************************
    1302       422760 :    SUBROUTINE cp_cfm_rot_cols(matrix, icol, jcol, cs, sn)
    1303              :       TYPE(cp_cfm_type), INTENT(IN)            :: matrix
    1304              :       INTEGER, INTENT(IN)                      :: icol, jcol
    1305              :       REAL(dp), INTENT(IN)                     :: cs, sn
    1306              : 
    1307              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_cfm_rot_cols'
    1308              :       INTEGER                                  :: handle, nrow
    1309              :       COMPLEX(KIND=dp)                         :: sn_cmplx
    1310              : 
    1311              : #if defined(__parallel)
    1312              :       INTEGER                                  :: info, lwork
    1313              :       INTEGER, DIMENSION(9)                    :: desc
    1314       422760 :       REAL(dp), DIMENSION(:), ALLOCATABLE      :: work
    1315              : #endif
    1316       422760 :       CALL timeset(routineN, handle)
    1317       422760 :       CALL cp_cfm_get_info(matrix, nrow_global=nrow)
    1318       422760 :       sn_cmplx = CMPLX(sn, 0.0_dp, dp)
    1319              : #if defined(__parallel)
    1320       422760 :       IF (1 /= matrix%matrix_struct%context%n_pid) THEN
    1321       422760 :          lwork = 2*nrow + 1
    1322      1268280 :          ALLOCATE (work(lwork))
    1323      4227600 :          desc(:) = matrix%matrix_struct%descriptor(:)
    1324       422760 :          info = 0
    1325              :          CALL pzrot(nrow, &
    1326              :                     matrix%local_data(1, 1), 1, icol, desc, 1, &
    1327              :                     matrix%local_data(1, 1), 1, jcol, desc, 1, &
    1328       422760 :                     cs, sn_cmplx, work, lwork, info)
    1329       422760 :          CPASSERT(info == 0)
    1330       422760 :          DEALLOCATE (work)
    1331              :       ELSE
    1332              : #endif
    1333            0 :          CALL zrot(nrow, matrix%local_data(1, icol), 1, matrix%local_data(1, jcol), 1, cs, sn_cmplx)
    1334              : #if defined(__parallel)
    1335              :       END IF
    1336              : #endif
    1337       422760 :       CALL timestop(handle)
    1338       422760 :    END SUBROUTINE cp_cfm_rot_cols
    1339              : 
    1340              : ! **************************************************************************************************
    1341              : !> \brief ...
    1342              : !> \param matrix ...
    1343              : !> \param workspace ...
    1344              : !> \param uplo triangular format; defaults to 'U'
    1345              : !> \par History
    1346              : !>      12.2024 Added optional workspace as input [Rocco Meli]
    1347              : !> \author Jan Wilhelm
    1348              : ! **************************************************************************************************
    1349        12276 :    SUBROUTINE cp_cfm_uplo_to_full(matrix, workspace, uplo)
    1350              : 
    1351              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix
    1352              :       TYPE(cp_cfm_type), INTENT(IN), OPTIONAL            :: workspace
    1353              :       CHARACTER, INTENT(IN), OPTIONAL                    :: uplo
    1354              : 
    1355              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_cfm_uplo_to_full'
    1356              : 
    1357              :       CHARACTER                                          :: myuplo
    1358              :       INTEGER                                            :: handle, i_global, iiB, j_global, jjB, &
    1359              :                                                             ncol_local, nrow_local
    1360         6138 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    1361              :       TYPE(cp_cfm_type)                                  :: work
    1362              : 
    1363         6138 :       CALL timeset(routineN, handle)
    1364              : 
    1365         6138 :       IF (.NOT. PRESENT(workspace)) THEN
    1366         5456 :          CALL cp_cfm_create(work, matrix%matrix_struct)
    1367              :       ELSE
    1368          682 :          work = workspace
    1369              :       END IF
    1370              : 
    1371         6138 :       myuplo = 'U'
    1372         6138 :       IF (PRESENT(uplo)) myuplo = uplo
    1373              : 
    1374              :       ! get info of fm_mat_Q
    1375              :       CALL cp_cfm_get_info(matrix=matrix, &
    1376              :                            nrow_local=nrow_local, &
    1377              :                            ncol_local=ncol_local, &
    1378              :                            row_indices=row_indices, &
    1379         6138 :                            col_indices=col_indices)
    1380              : 
    1381       249926 :       DO jjB = 1, ncol_local
    1382       243788 :          j_global = col_indices(jjB)
    1383      8314789 :          DO iiB = 1, nrow_local
    1384      8064863 :             i_global = row_indices(iiB)
    1385      8308651 :             IF (MERGE(j_global < i_global, j_global > i_global, (myuplo == "U") .OR. (myuplo == "u"))) THEN
    1386      3969480 :                matrix%local_data(iiB, jjB) = z_zero
    1387      4095383 :             ELSE IF (j_global == i_global) THEN
    1388       125903 :                matrix%local_data(iiB, jjB) = matrix%local_data(iiB, jjB)/(2.0_dp, 0.0_dp)
    1389              :             END IF
    1390              :          END DO
    1391              :       END DO
    1392              : 
    1393         6138 :       CALL cp_cfm_transpose(matrix, 'C', work)
    1394              : 
    1395         6138 :       CALL cp_cfm_scale_and_add(z_one, matrix, z_one, work)
    1396              : 
    1397         6138 :       IF (.NOT. PRESENT(workspace)) THEN
    1398         5456 :          CALL cp_cfm_release(work)
    1399              :       END IF
    1400              : 
    1401         6138 :       CALL timestop(handle)
    1402              : 
    1403         6138 :    END SUBROUTINE cp_cfm_uplo_to_full
    1404              : 
    1405              : ! **************************************************************************************************
    1406              : !> \brief find the norm of each column norm_{j}= sqrt( \sum_{i} A_{ij}*conjg(A_{ij}) )
    1407              : !>        Complex-valued mirror of cp_fm_vectorsnorm.
    1408              : !> \param matrix ...
    1409              : !> \param norm_array ...
    1410              : ! **************************************************************************************************
    1411        48605 :    SUBROUTINE cp_cfm_vectorsnorm(matrix, norm_array)
    1412              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix
    1413              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: norm_array
    1414              : 
    1415              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_cfm_vectorsnorm'
    1416              : 
    1417              :       INTEGER                                            :: handle, i, j, ncol_local, nrow_local
    1418        48605 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices
    1419              : 
    1420        48605 :       CALL timeset(routineN, handle)
    1421              : 
    1422              :       CALL cp_cfm_get_info(matrix, col_indices=col_indices, nrow_local=nrow_local, &
    1423        48605 :                            ncol_local=ncol_local)
    1424              : 
    1425              :       ! the efficiency could be improved by making use of the row-col distribution of scalapack
    1426       792126 :       norm_array = 0.0_dp
    1427       792126 :       DO j = 1, ncol_local
    1428     19948794 :          DO i = 1, nrow_local
    1429              :             norm_array(col_indices(j)) = norm_array(col_indices(j)) + &
    1430              :                                          REAL(matrix%local_data(i, j), KIND=dp)**2 + &
    1431     19900189 :                                          AIMAG(matrix%local_data(i, j))**2
    1432              :          END DO
    1433              :       END DO
    1434      1535647 :       CALL matrix%matrix_struct%para_env%sum(norm_array)
    1435       792126 :       norm_array = SQRT(norm_array)
    1436              : 
    1437        48605 :       CALL timestop(handle)
    1438        48605 :    END SUBROUTINE cp_cfm_vectorsnorm
    1439              : 
    1440              : ! **************************************************************************************************
    1441              : !> \brief returns the diagonal of a complex full matrix: diag(i)= A_{ii}.
    1442              : !>        Each diagonal entry is owned by one process. The sum over the
    1443              : !>        process grid collects the entries.
    1444              : !> \param matrix ...
    1445              : !> \param diag ...
    1446              : ! **************************************************************************************************
    1447        17055 :    SUBROUTINE cp_cfm_get_diag(matrix, diag)
    1448              :       TYPE(cp_cfm_type), INTENT(IN)                      :: matrix
    1449              :       COMPLEX(KIND=dp), DIMENSION(:), INTENT(OUT)        :: diag
    1450              : 
    1451              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'cp_cfm_get_diag'
    1452              : 
    1453              :       INTEGER                                            :: handle, i, j, ncol_local, nrow_local
    1454        17055 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    1455              : 
    1456        17055 :       CALL timeset(routineN, handle)
    1457              : 
    1458              :       CALL cp_cfm_get_info(matrix, col_indices=col_indices, row_indices=row_indices, &
    1459        17055 :                            nrow_local=nrow_local, ncol_local=ncol_local)
    1460              : 
    1461       267906 :       diag = z_zero
    1462       267906 :       DO j = 1, ncol_local
    1463      2497366 :          DO i = 1, nrow_local
    1464      2480311 :             IF (row_indices(i) == col_indices(j)) THEN
    1465       126480 :                diag(col_indices(j)) = matrix%local_data(i, j)
    1466              :             END IF
    1467              :          END DO
    1468              :       END DO
    1469       518757 :       CALL matrix%matrix_struct%para_env%sum(diag)
    1470              : 
    1471        17055 :       CALL timestop(handle)
    1472        17055 :    END SUBROUTINE cp_cfm_get_diag
    1473              : 
    1474              : END MODULE cp_cfm_basic_linalg
        

Generated by: LCOV version 2.0-1