LCOV - code coverage report
Current view: top level - src/fm - cp_fm_basic_linalg.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:dbd702d) Lines: 76.6 % 758 581
Test Date: 2026-11-22 01:17:58 Functions: 70.5 % 44 31

            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 full matrices.
      10              : !> \par History
      11              : !>      08.2002 split out of qs_blacs [fawzi]
      12              : !> \author Fawzi Mohamed
      13              : ! **************************************************************************************************
      14              : MODULE cp_fm_basic_linalg
      15              :    USE cp_blacs_env, ONLY: cp_blacs_env_type
      16              :    USE cp_fm_struct, ONLY: cp_fm_struct_equivalent
      17              :    USE cp_fm_types, ONLY: &
      18              :       cp_fm_create, cp_fm_get_diag, cp_fm_get_info, cp_fm_get_submatrix, cp_fm_p_type, &
      19              :       cp_fm_release, cp_fm_set_all, cp_fm_set_element, cp_fm_set_submatrix, cp_fm_to_fm, &
      20              :       cp_fm_type
      21              :    USE cp_log_handling, ONLY: cp_logger_get_default_unit_nr, &
      22              :                               cp_to_string
      23              :    USE kahan_sum, ONLY: accurate_dot_product, &
      24              :                         accurate_sum
      25              :    USE kinds, ONLY: dp, &
      26              :                     int_8
      27              :    USE machine, ONLY: m_memory
      28              :    USE mathlib, ONLY: get_pseudo_inverse_svd, &
      29              :                       invert_matrix
      30              :    USE message_passing, ONLY: mp_comm_type
      31              : #if defined (__HAS_IEEE_EXCEPTIONS)
      32              :    USE ieee_exceptions, ONLY: ieee_get_halting_mode, &
      33              :                               ieee_set_halting_mode, &
      34              :                               IEEE_ALL
      35              : #endif
      36              : #include "../base/base_uses.f90"
      37              : 
      38              :    IMPLICIT NONE
      39              :    PRIVATE
      40              : 
      41              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      42              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_fm_basic_linalg'
      43              : 
      44              :    PUBLIC :: cp_fm_scale, & ! scale a matrix
      45              :              cp_fm_scale_and_add, & ! scale and add two matrices
      46              :              cp_fm_geadd, & ! general addition
      47              :              cp_fm_add_columns, & ! daxpy type column add
      48              :              cp_fm_column_scale, & ! scale columns of a matrix
      49              :              cp_fm_row_scale, & ! scale rows of a matrix
      50              :              cp_fm_trace, & ! trace of the transpose(A)*B
      51              :              cp_fm_contracted_trace, & ! sum_{i,...,k} Tr [A(i,...,k)^T * B(i,...,k)]
      52              :              cp_fm_norm, & ! different norms of A
      53              :              cp_fm_schur_product, & ! schur product
      54              :              cp_fm_transpose, & ! transpose a matrix
      55              :              cp_fm_uplo_to_full, & ! symmetrise a triangular matrix
      56              :              cp_fm_syrk, & ! rank k update
      57              :              cp_fm_triangular_multiply, & ! triangular matrix multiply / solve
      58              :              cp_fm_symm, & ! multiply a symmetric with a non-symmetric matrix
      59              :              cp_fm_gemm, & ! multiply two matrices
      60              :              cp_complex_fm_gemm, & ! multiply two complex matrices, represented by non_complex fm matrices
      61              :              cp_fm_invert, & ! computes the inverse and determinant
      62              :              cp_fm_frobenius_norm, & ! frobenius norm
      63              :              cp_fm_triangular_invert, & ! compute the reciprocal of a triangular matrix
      64              :              cp_fm_qr_factorization, & ! compute the QR factorization of a rectangular matrix
      65              :              cp_fm_solve, & ! solves the equation  A*B=C A and C are input
      66              :              cp_fm_pdgeqpf, & ! compute a QR factorization with column pivoting of a M-by-N distributed matrix
      67              :              cp_fm_pdorgqr, & ! generates an M-by-N as first N columns of a product of K elementary reflectors
      68              :              cp_fm_potrf, & ! Cholesky decomposition
      69              :              cp_fm_potri, & ! Invert triangular matrix
      70              :              cp_fm_rot_rows, & ! rotates two rows
      71              :              cp_fm_rot_cols, & ! rotates two columns
      72              :              cp_fm_Gram_Schmidt_orthonorm, & ! Gram-Schmidt orthonormalization of columns of a full matrix, &
      73              :              cp_fm_det, & ! determinant of a real matrix with correct sign
      74              :              cp_fm_matvec, & ! matrix-vector multiplication (vector replicated)
      75              :              cp_fm_add_on_diag ! add a scalar to the diagonal
      76              : 
      77              :    REAL(KIND=dp), EXTERNAL :: dlange, pdlange, pdlatra
      78              : 
      79              :    INTERFACE cp_fm_trace
      80              :       MODULE PROCEDURE cp_fm_trace_a0b0t0
      81              :       MODULE PROCEDURE cp_fm_trace_a1b0t1_a
      82              :       MODULE PROCEDURE cp_fm_trace_a1b0t1_p
      83              :       MODULE PROCEDURE cp_fm_trace_a1b1t1_aa
      84              :       MODULE PROCEDURE cp_fm_trace_a1b1t1_ap
      85              :       MODULE PROCEDURE cp_fm_trace_a1b1t1_pa
      86              :       MODULE PROCEDURE cp_fm_trace_a1b1t1_pp
      87              :    END INTERFACE cp_fm_trace
      88              : 
      89              :    INTERFACE cp_fm_contracted_trace
      90              :       MODULE PROCEDURE cp_fm_contracted_trace_a2b2t2_aa
      91              :       MODULE PROCEDURE cp_fm_contracted_trace_a2b2t2_ap
      92              :       MODULE PROCEDURE cp_fm_contracted_trace_a2b2t2_pa
      93              :       MODULE PROCEDURE cp_fm_contracted_trace_a2b2t2_pp
      94              :    END INTERFACE cp_fm_contracted_trace
      95              : CONTAINS
      96              : 
      97              : ! **************************************************************************************************
      98              : !> \brief Computes the determinant (with a correct sign even in parallel environment!) of a real square matrix
      99              : !> \author A. Sinyavskiy (andrey.sinyavskiy@chem.uzh.ch)
     100              : ! **************************************************************************************************
     101            0 :    SUBROUTINE cp_fm_det(matrix_a, det_a)
     102              : 
     103              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a
     104              :       REAL(KIND=dp), INTENT(OUT)               :: det_a
     105              :       REAL(KIND=dp)                            :: determinant
     106              :       TYPE(cp_fm_type)                         :: matrix_lu
     107              :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
     108              :       INTEGER                                  :: n, i, info, P
     109            0 :       INTEGER, ALLOCATABLE, DIMENSION(:)       :: ipivot
     110              :       REAL(KIND=dp), DIMENSION(:), POINTER     :: diag
     111              : 
     112              : #if defined(__parallel)
     113              :       INTEGER                                  :: myprow, nprow, npcol, nrow_local, nrow_block, irow_local
     114              :       INTEGER, DIMENSION(9)                    :: desca
     115              : #endif
     116              : 
     117              :       CALL cp_fm_create(matrix=matrix_lu, &
     118              :                         matrix_struct=matrix_a%matrix_struct, &
     119            0 :                         name="A_lu"//TRIM(ADJUSTL(cp_to_string(1)))//"MATRIX")
     120            0 :       CALL cp_fm_to_fm(matrix_a, matrix_lu)
     121              : 
     122            0 :       a => matrix_lu%local_data
     123            0 :       n = matrix_lu%matrix_struct%nrow_global
     124            0 :       ALLOCATE (ipivot(n))
     125            0 :       ipivot(:) = 0
     126            0 :       P = 0
     127            0 :       ALLOCATE (diag(n))
     128            0 :       diag(:) = 0.0_dp
     129              : #if defined(__parallel)
     130              :       ! Use LU decomposition
     131            0 :       desca(:) = matrix_lu%matrix_struct%descriptor(:)
     132            0 :       CALL pdgetrf(n, n, a, 1, 1, desca, ipivot, info)
     133            0 :       CALL cp_fm_get_diag(matrix_lu, diag)
     134            0 :       determinant = PRODUCT(diag)
     135            0 :       myprow = matrix_lu%matrix_struct%context%mepos(1)
     136            0 :       nprow = matrix_lu%matrix_struct%context%num_pe(1)
     137            0 :       npcol = matrix_lu%matrix_struct%context%num_pe(2)
     138            0 :       nrow_local = matrix_lu%matrix_struct%nrow_locals(myprow)
     139            0 :       nrow_block = matrix_lu%matrix_struct%nrow_block
     140            0 :       DO irow_local = 1, nrow_local
     141            0 :          i = matrix_lu%matrix_struct%row_indices(irow_local)
     142            0 :          IF (ipivot(irow_local) /= i) P = P + 1
     143              :       END DO
     144            0 :       CALL matrix_lu%matrix_struct%para_env%sum(P)
     145              :       ! very important fix
     146            0 :       P = P/npcol
     147              : #else
     148              :       CALL dgetrf(n, n, a, n, ipivot, info)
     149              :       CALL cp_fm_get_diag(matrix_lu, diag)
     150              :       determinant = PRODUCT(diag)
     151              :       DO i = 1, n
     152              :          IF (ipivot(i) /= i) P = P + 1
     153              :       END DO
     154              : #endif
     155            0 :       DEALLOCATE (ipivot)
     156            0 :       DEALLOCATE (diag)
     157            0 :       CALL cp_fm_release(matrix_lu)
     158            0 :       det_a = determinant*(-2*MOD(P, 2) + 1.0_dp)
     159            0 :    END SUBROUTINE cp_fm_det
     160              : 
     161              : ! **************************************************************************************************
     162              : !> \brief calc A <- alpha*A + beta*B
     163              : !>      optimized for alpha == 1.0 (just add beta*B) and beta == 0.0 (just
     164              : !>      scale A)
     165              : !> \param alpha ...
     166              : !> \param matrix_a ...
     167              : !> \param beta ...
     168              : !> \param matrix_b ...
     169              : ! **************************************************************************************************
     170      1400600 :    SUBROUTINE cp_fm_scale_and_add(alpha, matrix_a, beta, matrix_b)
     171              : 
     172              :       REAL(KIND=dp), INTENT(IN)                          :: alpha
     173              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_a
     174              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: beta
     175              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: matrix_b
     176              : 
     177              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_scale_and_add'
     178              : 
     179              :       INTEGER                                            :: handle, size_a, size_b
     180              :       REAL(KIND=dp)                                      :: my_beta
     181              :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: a, b
     182              : 
     183      1400600 :       CALL timeset(routineN, handle)
     184              : 
     185      1400600 :       my_beta = 0.0_dp
     186      1400600 :       IF (PRESENT(matrix_b)) my_beta = 1.0_dp
     187      1400600 :       IF (PRESENT(beta)) my_beta = beta
     188              :       NULLIFY (a, b)
     189              : 
     190      1400600 :       IF (PRESENT(beta)) THEN
     191      1400598 :          CPASSERT(PRESENT(matrix_b))
     192      1400598 :          IF (ASSOCIATED(matrix_a%local_data, matrix_b%local_data)) THEN
     193            0 :             CPWARN("Bad use of routine. Call cp_fm_scale instead")
     194            0 :             CALL cp_fm_scale(alpha + beta, matrix_a)
     195            0 :             CALL timestop(handle)
     196            0 :             RETURN
     197              :          END IF
     198              :       END IF
     199              : 
     200      1400600 :       a => matrix_a%local_data
     201              : 
     202      1400600 :       size_a = SIZE(a, 1)*SIZE(a, 2)
     203              : 
     204      1400600 :       IF (alpha /= 1.0_dp) THEN
     205        86826 :          CALL dscal(size_a, alpha, a, 1)
     206              :       END IF
     207      1400600 :       IF (my_beta /= 0.0_dp) THEN
     208      1389998 :          IF (matrix_a%matrix_struct%context /= matrix_b%matrix_struct%context) &
     209            0 :             CPABORT("Matrices must be in the same blacs context")
     210              : 
     211      1389998 :          IF (cp_fm_struct_equivalent(matrix_a%matrix_struct, &
     212              :                                      matrix_b%matrix_struct)) THEN
     213              : 
     214      1389998 :             b => matrix_b%local_data
     215      1389998 :             size_b = SIZE(b, 1)*SIZE(b, 2)
     216      1389998 :             IF (size_a /= size_b) &
     217            0 :                CPABORT("Matrices must have same local sizes")
     218              : 
     219      1389998 :             CALL daxpy(size_a, my_beta, b, 1, a, 1)
     220              : 
     221              :          ELSE
     222              :             CALL cp_abort(__LOCATION__, &
     223              :                           "cp_fm_scale_and_add is not yet implemented for cases "// &
     224            0 :                           "where two input matrix structures are not equivalent")
     225              :          END IF
     226              : 
     227              :       END IF
     228              : 
     229      1400600 :       CALL timestop(handle)
     230              : 
     231              :    END SUBROUTINE cp_fm_scale_and_add
     232              : 
     233              : ! **************************************************************************************************
     234              : !> \brief interface to BLACS geadd:
     235              : !>                matrix_b = beta*matrix_b + alpha*opt(matrix_a)
     236              : !>        where opt(matrix_a) can be either:
     237              : !>              'N':  matrix_a
     238              : !>              'T':  matrix_a^T
     239              : !>              'C':  matrix_a^H (Hermitian conjugate)
     240              : !>        note that this is a level three routine, use cp_fm_scale_and_add if that
     241              : !>        is sufficient for your needs
     242              : !> \param alpha  : complex scalar
     243              : !> \param trans  : 'N' normal, 'T' transposed
     244              : !> \param matrix_a : input matrix_a
     245              : !> \param beta   : complex scalar
     246              : !> \param matrix_b : input matrix_b, upon out put the updated matrix_b
     247              : !> \author  Lianheng Tong
     248              : ! **************************************************************************************************
     249          650 :    SUBROUTINE cp_fm_geadd(alpha, trans, matrix_a, beta, matrix_b)
     250              :       REAL(KIND=dp), INTENT(IN) :: alpha, beta
     251              :       CHARACTER, INTENT(IN) :: trans
     252              :       TYPE(cp_fm_type), INTENT(IN) :: matrix_a, matrix_b
     253              : 
     254              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_geadd'
     255              : 
     256              :       INTEGER :: nrow_global, ncol_global, handle
     257              :       REAL(KIND=dp), DIMENSION(:, :), POINTER :: aa, bb
     258              : #if defined(__parallel)
     259              :       INTEGER, DIMENSION(9) :: desca, descb
     260              : #elif !defined(__MKL)
     261              :       INTEGER :: ii, jj
     262              : #endif
     263              : 
     264          650 :       CALL timeset(routineN, handle)
     265              : 
     266          650 :       nrow_global = matrix_a%matrix_struct%nrow_global
     267          650 :       ncol_global = matrix_a%matrix_struct%ncol_global
     268          650 :       CPASSERT(nrow_global == matrix_b%matrix_struct%nrow_global)
     269          650 :       CPASSERT(ncol_global == matrix_b%matrix_struct%ncol_global)
     270              : 
     271          650 :       aa => matrix_a%local_data
     272          650 :       bb => matrix_b%local_data
     273              : 
     274              : #if defined(__parallel)
     275         6500 :       desca = matrix_a%matrix_struct%descriptor
     276         6500 :       descb = matrix_b%matrix_struct%descriptor
     277              :       CALL pdgeadd(trans, &
     278              :                    nrow_global, &
     279              :                    ncol_global, &
     280              :                    alpha, &
     281              :                    aa, &
     282              :                    1, 1, &
     283              :                    desca, &
     284              :                    beta, &
     285              :                    bb, &
     286              :                    1, 1, &
     287          650 :                    descb)
     288              : #elif defined(__MKL)
     289              :       CALL mkl_domatadd('C', trans, 'N', nrow_global, ncol_global, &
     290              :                         alpha, aa, nrow_global, beta, bb, nrow_global, bb, nrow_global)
     291              : #else
     292              :       ! dgeadd is not a standard BLAS function, although it is implemented
     293              :       ! in some libraries like OpenBLAS, so not going to use it here
     294              :       SELECT CASE (trans)
     295              :       CASE ('T')
     296              :          DO jj = 1, ncol_global
     297              :             DO ii = 1, nrow_global
     298              :                bb(ii, jj) = beta*bb(ii, jj) + alpha*aa(jj, ii)
     299              :             END DO
     300              :          END DO
     301              :       CASE DEFAULT
     302              :          DO jj = 1, ncol_global
     303              :             DO ii = 1, nrow_global
     304              :                bb(ii, jj) = beta*bb(ii, jj) + alpha*aa(ii, jj)
     305              :             END DO
     306              :          END DO
     307              :       END SELECT
     308              : #endif
     309              : 
     310          650 :       CALL timestop(handle)
     311              : 
     312          650 :    END SUBROUTINE cp_fm_geadd
     313              : 
     314              : ! **************************************************************************************************
     315              : !> \brief Add (and scale) a subset of columns of a fm to a fm
     316              : !>        b = alpha*a + b
     317              : !> \param msource ...
     318              : !> \param mtarget ...
     319              : !> \param ncol ...
     320              : !> \param alpha ...
     321              : !> \param source_start ...
     322              : !> \param target_start ...
     323              : ! **************************************************************************************************
     324            0 :    SUBROUTINE cp_fm_add_columns(msource, mtarget, ncol, alpha, source_start, target_start)
     325              : 
     326              :       TYPE(cp_fm_type), INTENT(IN)             :: msource, mtarget
     327              :       INTEGER, INTENT(IN)                      :: ncol
     328              :       REAL(KIND=dp), INTENT(IN), OPTIONAL      :: alpha
     329              :       INTEGER, INTENT(IN), OPTIONAL            :: source_start, target_start
     330              : 
     331              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_fm_add_columns'
     332              : 
     333              :       INTEGER                                  :: handle, n, ss, ts, i
     334              :       REAL(KIND=dp)                            :: fscale
     335              :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a, b
     336              : #if defined(__parallel)
     337              :       INTEGER, DIMENSION(9)                    :: desca, descb
     338              : #endif
     339              : 
     340            0 :       CALL timeset(routineN, handle)
     341              : 
     342            0 :       ss = 1
     343            0 :       ts = 1
     344              : 
     345            0 :       IF (PRESENT(source_start)) ss = source_start
     346            0 :       IF (PRESENT(target_start)) ts = target_start
     347              : 
     348            0 :       fscale = 1.0_dp
     349            0 :       IF (PRESENT(alpha)) fscale = alpha
     350              : 
     351            0 :       n = msource%matrix_struct%nrow_global
     352              : 
     353            0 :       a => msource%local_data
     354            0 :       b => mtarget%local_data
     355              : 
     356              : #if defined(__parallel)
     357              :       MARK_USED(i)
     358            0 :       desca(:) = msource%matrix_struct%descriptor(:)
     359            0 :       descb(:) = mtarget%matrix_struct%descriptor(:)
     360            0 :       CALL pdgeadd("N", n, ncol, fscale, a, 1, ss, desca, 1.0_dp, b, 1, ts, descb)
     361              : #else
     362              :       DO i = 0, ncol - 1
     363              :          b(1:n, ts + i) = b(1:n, ts + i) + fscale*a(1:n, ss + i)
     364              :       END DO
     365              : #endif
     366              : 
     367            0 :       CALL timestop(handle)
     368              : 
     369            0 :    END SUBROUTINE cp_fm_add_columns
     370              : 
     371              : ! **************************************************************************************************
     372              : !> \brief Computes the LU-decomposition of the matrix, and the determinant of the matrix
     373              : !>      IMPORTANT : the sign of the determinant is not defined correctly yet ....
     374              : !> \param matrix_a ...
     375              : !> \param almost_determinant ...
     376              : !> \param correct_sign ...
     377              : !> \par History
     378              : !>      added correct_sign 02.07 (fschiff)
     379              : !> \author Joost VandeVondele
     380              : !> \note
     381              : !>      - matrix_a is overwritten
     382              : !>      - the sign of the determinant might be wrong
     383              : !>      - SERIOUS WARNING (KNOWN BUG) : the sign of the determinant depends on ipivot
     384              : !>      - one should be able to find out if ipivot is an even or an odd permutation...
     385              : !>        if you need the correct sign, just add correct_sign==.TRUE. (fschiff)
     386              : !>      - Use cp_fm_get_diag instead of n times cp_fm_get_element (A. Bussy)
     387              : ! **************************************************************************************************
     388            0 :    SUBROUTINE cp_fm_lu_decompose(matrix_a, almost_determinant, correct_sign)
     389              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a
     390              :       REAL(KIND=dp), INTENT(OUT)               :: almost_determinant
     391              :       LOGICAL, INTENT(IN), OPTIONAL            :: correct_sign
     392              : 
     393              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_lu_decompose'
     394              : 
     395              :       INTEGER                                  :: handle, i, info, n
     396            0 :       INTEGER, ALLOCATABLE, DIMENSION(:)       :: ipivot
     397              :       REAL(KIND=dp)                            :: determinant
     398              :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
     399              : #if defined(__parallel)
     400              :       INTEGER, DIMENSION(9)                    :: desca
     401            0 :       REAL(KIND=dp), DIMENSION(:), POINTER     :: diag
     402              : #else
     403              :       INTEGER                                  :: lda
     404              : #endif
     405              : 
     406            0 :       CALL timeset(routineN, handle)
     407              : 
     408            0 :       a => matrix_a%local_data
     409            0 :       n = matrix_a%matrix_struct%nrow_global
     410            0 :       ALLOCATE (ipivot(n + matrix_a%matrix_struct%nrow_block))
     411              : 
     412              : #if defined(__parallel)
     413              :       MARK_USED(correct_sign)
     414            0 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
     415            0 :       CALL pdgetrf(n, n, a, 1, 1, desca, ipivot, info)
     416              : 
     417            0 :       ALLOCATE (diag(n))
     418            0 :       diag(:) = 0.0_dp
     419            0 :       CALL cp_fm_get_diag(matrix_a, diag)
     420            0 :       determinant = 1.0_dp
     421            0 :       DO i = 1, n
     422            0 :          determinant = determinant*diag(i)
     423              :       END DO
     424            0 :       DEALLOCATE (diag)
     425              : #else
     426              :       lda = SIZE(a, 1)
     427              :       CALL dgetrf(n, n, a, lda, ipivot, info)
     428              :       determinant = 1.0_dp
     429              :       IF (correct_sign) THEN
     430              :          DO i = 1, n
     431              :             IF (ipivot(i) /= i) THEN
     432              :                determinant = -determinant*a(i, i)
     433              :             ELSE
     434              :                determinant = determinant*a(i, i)
     435              :             END IF
     436              :          END DO
     437              :       ELSE
     438              :          DO i = 1, n
     439              :             determinant = determinant*a(i, i)
     440              :          END DO
     441              :       END IF
     442              : #endif
     443              :       ! info is allowed to be zero
     444              :       ! this does just signal a zero diagonal element
     445            0 :       DEALLOCATE (ipivot)
     446            0 :       almost_determinant = determinant ! notice that the sign is random
     447            0 :       CALL timestop(handle)
     448            0 :    END SUBROUTINE cp_fm_lu_decompose
     449              : 
     450              : ! **************************************************************************************************
     451              : !> \brief computes matrix_c = beta * matrix_c + alpha * ( matrix_a  ** transa ) * ( matrix_b ** transb )
     452              : !> \param transa : 'N' -> normal   'T' -> transpose
     453              : !>      alpha,beta :: can be 0.0_dp and 1.0_dp
     454              : !> \param transb same operation choices as transa
     455              : !> \param m ...
     456              : !> \param n ...
     457              : !> \param k ...
     458              : !> \param alpha ...
     459              : !> \param matrix_a : m x k matrix ( ! for transa = 'N')
     460              : !> \param matrix_b : k x n matrix ( ! for transb = 'N')
     461              : !> \param beta ...
     462              : !> \param matrix_c : m x n matrix
     463              : !> \param a_first_col ...
     464              : !> \param a_first_row ...
     465              : !> \param b_first_col : the k x n matrix starts at col b_first_col of matrix_b (avoid usage)
     466              : !> \param b_first_row ...
     467              : !> \param c_first_col ...
     468              : !> \param c_first_row ...
     469              : !> \author Matthias Krack
     470              : !> \note
     471              : !>      matrix_c should have no overlap with matrix_a, matrix_b
     472              : ! **************************************************************************************************
     473        20804 :    SUBROUTINE cp_fm_gemm(transa, transb, m, n, k, alpha, matrix_a, matrix_b, beta, &
     474              :                          matrix_c, a_first_col, a_first_row, b_first_col, b_first_row, &
     475              :                          c_first_col, c_first_row)
     476              : 
     477              :       CHARACTER(LEN=1), INTENT(IN)             :: transa, transb
     478              :       INTEGER, INTENT(IN)                      :: m, n, k
     479              :       REAL(KIND=dp), INTENT(IN)                :: alpha
     480              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a, matrix_b
     481              :       REAL(KIND=dp), INTENT(IN)                :: beta
     482              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_c
     483              :       INTEGER, INTENT(IN), OPTIONAL            :: a_first_col, a_first_row, &
     484              :                                                   b_first_col, b_first_row, &
     485              :                                                   c_first_col, c_first_row
     486              : 
     487              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_gemm'
     488              : 
     489              :       INTEGER                                  :: handle, i_a, i_b, i_c, j_a, &
     490              :                                                   j_b, j_c
     491              :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a, b, c
     492              : #if defined(__parallel)
     493              :       INTEGER, DIMENSION(9)                    :: desca, descb, descc
     494              : #else
     495              :       INTEGER                                  :: lda, ldb, ldc
     496              : #endif
     497              : 
     498        20804 :       CALL timeset(routineN, handle)
     499              : 
     500              :       !sample peak memory
     501        20804 :       CALL m_memory()
     502              : 
     503        20804 :       a => matrix_a%local_data
     504        20804 :       b => matrix_b%local_data
     505        20804 :       c => matrix_c%local_data
     506              : 
     507        20804 :       i_a = 1
     508        20804 :       IF (PRESENT(a_first_row)) i_a = a_first_row
     509              : 
     510        20804 :       j_a = 1
     511        20804 :       IF (PRESENT(a_first_col)) j_a = a_first_col
     512              : 
     513        20804 :       i_b = 1
     514        20804 :       IF (PRESENT(b_first_row)) i_b = b_first_row
     515              : 
     516        20804 :       j_b = 1
     517        20804 :       IF (PRESENT(b_first_col)) j_b = b_first_col
     518              : 
     519        20804 :       i_c = 1
     520        20804 :       IF (PRESENT(c_first_row)) i_c = c_first_row
     521              : 
     522        20804 :       j_c = 1
     523        20804 :       IF (PRESENT(c_first_col)) j_c = c_first_col
     524              : 
     525              : #if defined(__parallel)
     526              : 
     527       208040 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
     528       208040 :       descb(:) = matrix_b%matrix_struct%descriptor(:)
     529       208040 :       descc(:) = matrix_c%matrix_struct%descriptor(:)
     530              : 
     531              :       CALL pdgemm(transa, transb, m, n, k, alpha, a, i_a, j_a, desca, b, i_b, j_b, &
     532        20804 :                   descb, beta, c, i_c, j_c, descc)
     533              : 
     534              : #else
     535              : 
     536              :       lda = SIZE(a, 1)
     537              :       ldb = SIZE(b, 1)
     538              :       ldc = SIZE(c, 1)
     539              : 
     540              :       CALL dgemm(transa, transb, m, n, k, alpha, a(i_a, j_a), lda, b(i_b, j_b), ldb, beta, c(i_c, j_c), ldc)
     541              : 
     542              : #endif
     543        20804 :       CALL timestop(handle)
     544              : 
     545        20804 :    END SUBROUTINE cp_fm_gemm
     546              : 
     547              : ! **************************************************************************************************
     548              : !> \brief computes matrix_c = beta * matrix_c + alpha *  matrix_a  *  matrix_b
     549              : !>      computes matrix_c = beta * matrix_c + alpha *  matrix_b  *  matrix_a
     550              : !>      where matrix_a is symmetric
     551              : !> \param side : 'L' -> matrix_a is on the left 'R' -> matrix_a is on the right
     552              : !>      alpha,beta :: can be 0.0_dp and 1.0_dp
     553              : !> \param uplo triangular format
     554              : !> \param m ...
     555              : !> \param n ...
     556              : !> \param alpha ...
     557              : !> \param matrix_a : m x m matrix
     558              : !> \param matrix_b : m x n matrix
     559              : !> \param beta ...
     560              : !> \param matrix_c : m x n matrix
     561              : !> \author Matthias Krack
     562              : !> \note
     563              : !>      matrix_c should have no overlap with matrix_a, matrix_b
     564              : !>      all matrices in QS are triangular according to uplo
     565              : !>      matrix_a is always an m x m matrix
     566              : !>      typically slower than cp_fm_gemm (especially in parallel easily 50 percent)
     567              : ! **************************************************************************************************
     568       220266 :    SUBROUTINE cp_fm_symm(side, uplo, m, n, alpha, matrix_a, matrix_b, beta, matrix_c)
     569              : 
     570              :       CHARACTER(LEN=1), INTENT(IN)             :: side, uplo
     571              :       INTEGER, INTENT(IN)                      :: m, n
     572              :       REAL(KIND=dp), INTENT(IN)                :: alpha
     573              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a, matrix_b
     574              :       REAL(KIND=dp), INTENT(IN)                :: beta
     575              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_c
     576              : 
     577              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_symm'
     578              : 
     579              :       INTEGER                                  :: handle
     580       220266 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a, b, c
     581              : #if defined(__parallel)
     582              :       INTEGER, DIMENSION(9)                    :: desca, descb, descc
     583              : #else
     584              :       INTEGER                                  :: lda, ldb, ldc
     585              : #endif
     586              : 
     587       220266 :       CALL timeset(routineN, handle)
     588              : 
     589       220266 :       a => matrix_a%local_data
     590       220266 :       b => matrix_b%local_data
     591       220266 :       c => matrix_c%local_data
     592              : 
     593              : #if defined(__parallel)
     594              : 
     595      2202660 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
     596      2202660 :       descb(:) = matrix_b%matrix_struct%descriptor(:)
     597      2202660 :       descc(:) = matrix_c%matrix_struct%descriptor(:)
     598              : 
     599       220266 :       CALL pdsymm(side, uplo, m, n, alpha, a(1, 1), 1, 1, desca, b(1, 1), 1, 1, descb, beta, c(1, 1), 1, 1, descc)
     600              : 
     601              : #else
     602              : 
     603              :       lda = matrix_a%matrix_struct%local_leading_dimension
     604              :       ldb = matrix_b%matrix_struct%local_leading_dimension
     605              :       ldc = matrix_c%matrix_struct%local_leading_dimension
     606              : 
     607              :       CALL dsymm(side, uplo, m, n, alpha, a(1, 1), lda, b(1, 1), ldb, beta, c(1, 1), ldc)
     608              : 
     609              : #endif
     610       220266 :       CALL timestop(handle)
     611              : 
     612       220266 :    END SUBROUTINE cp_fm_symm
     613              : 
     614              : ! **************************************************************************************************
     615              : !> \brief computes the Frobenius norm of matrix_a
     616              : !> \brief computes the Frobenius norm of matrix_a
     617              : !> \param matrix_a : m x n matrix
     618              : !> \return ...
     619              : !> \author VW
     620              : ! **************************************************************************************************
     621         7926 :    FUNCTION cp_fm_frobenius_norm(matrix_a) RESULT(norm)
     622              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a
     623              :       REAL(KIND=dp)                            :: norm
     624              : 
     625              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_frobenius_norm'
     626              : 
     627              :       INTEGER                                  :: handle, size_a
     628         7926 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
     629              :       REAL(KIND=dp), EXTERNAL                  :: DDOT
     630              : #if defined(__parallel)
     631              :       TYPE(mp_comm_type)                       :: group
     632              : #endif
     633              : 
     634         7926 :       CALL timeset(routineN, handle)
     635              : 
     636              :       norm = 0.0_dp
     637         7926 :       a => matrix_a%local_data
     638         7926 :       size_a = SIZE(a, 1)*SIZE(a, 2)
     639         7926 :       norm = DDOT(size_a, a(1, 1), 1, a(1, 1), 1)
     640              : #if defined(__parallel)
     641         7926 :       group = matrix_a%matrix_struct%para_env
     642         7926 :       CALL group%sum(norm)
     643              : #endif
     644         7926 :       norm = SQRT(norm)
     645              : 
     646         7926 :       CALL timestop(handle)
     647              : 
     648         7926 :    END FUNCTION cp_fm_frobenius_norm
     649              : 
     650              : ! **************************************************************************************************
     651              : !> \brief performs a rank-k update of a symmetric matrix_c
     652              : !>         matrix_c = beta * matrix_c + alpha * matrix_a * transpose ( matrix_a )
     653              : !> \param uplo : 'U'   ('L')
     654              : !> \param trans : 'N'  ('T')
     655              : !> \param k : number of cols to use in matrix_a
     656              : !>      ia,ja ::  1,1 (could be used for selecting subblock of a)
     657              : !> \param alpha ...
     658              : !> \param matrix_a ...
     659              : !> \param ia ...
     660              : !> \param ja ...
     661              : !> \param beta ...
     662              : !> \param matrix_c ...
     663              : !> \author Matthias Krack
     664              : ! **************************************************************************************************
     665        12426 :    SUBROUTINE cp_fm_syrk(uplo, trans, k, alpha, matrix_a, ia, ja, beta, matrix_c)
     666              :       CHARACTER(LEN=1), INTENT(IN)             :: uplo, trans
     667              :       INTEGER, INTENT(IN)                      :: k
     668              :       REAL(KIND=dp), INTENT(IN)                :: alpha
     669              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a
     670              :       INTEGER, INTENT(IN)                      :: ia, ja
     671              :       REAL(KIND=dp), INTENT(IN)                :: beta
     672              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_c
     673              : 
     674              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_syrk'
     675              : 
     676              :       INTEGER                                  :: handle, n
     677        12426 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a, c
     678              : #if defined(__parallel)
     679              :       INTEGER, DIMENSION(9)                    :: desca, descc
     680              : #else
     681              :       INTEGER                                  :: lda, ldc
     682              : #endif
     683              : #if defined (__HAS_IEEE_EXCEPTIONS)
     684              :       LOGICAL, DIMENSION(5)                    :: halt
     685              : #endif
     686              : 
     687        12426 :       CALL timeset(routineN, handle)
     688              : 
     689        12426 :       n = matrix_c%matrix_struct%nrow_global
     690              : 
     691        12426 :       a => matrix_a%local_data
     692        12426 :       c => matrix_c%local_data
     693              : 
     694              : #if defined (__HAS_IEEE_EXCEPTIONS)
     695              :       CALL ieee_get_halting_mode(IEEE_ALL, halt)
     696              :       CALL ieee_set_halting_mode(IEEE_ALL, .FALSE.)
     697              : #endif
     698              : #if defined(__parallel)
     699       124260 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
     700       124260 :       descc(:) = matrix_c%matrix_struct%descriptor(:)
     701              : 
     702        12426 :       CALL pdsyrk(uplo, trans, n, k, alpha, a(1, 1), ia, ja, desca, beta, c(1, 1), 1, 1, descc)
     703              : #else
     704              :       lda = SIZE(a, 1)
     705              :       ldc = SIZE(c, 1)
     706              : 
     707              :       CALL dsyrk(uplo, trans, n, k, alpha, a(ia, ja), lda, beta, c(1, 1), ldc)
     708              : #endif
     709              : #if defined (__HAS_IEEE_EXCEPTIONS)
     710              :       CALL ieee_set_halting_mode(IEEE_ALL, halt)
     711              : #endif
     712        12426 :       CALL timestop(handle)
     713              : 
     714        12426 :    END SUBROUTINE cp_fm_syrk
     715              : 
     716              : ! **************************************************************************************************
     717              : !> \brief computes the schur product of two matrices
     718              : !>       c_ij = a_ij * b_ij
     719              : !> \param matrix_a ...
     720              : !> \param matrix_b ...
     721              : !> \param matrix_c ...
     722              : !> \author Joost VandeVondele
     723              : ! **************************************************************************************************
     724        15304 :    SUBROUTINE cp_fm_schur_product(matrix_a, matrix_b, matrix_c)
     725              : 
     726              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_a, matrix_b, matrix_c
     727              : 
     728              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_schur_product'
     729              : 
     730              :       INTEGER                                            :: handle, icol_local, irow_local, mypcol, &
     731              :                                                             myprow, ncol_local, &
     732              :                                                             nrow_local
     733        15304 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: a, b, c
     734              :       TYPE(cp_blacs_env_type), POINTER                   :: context
     735              : 
     736        15304 :       CALL timeset(routineN, handle)
     737              : 
     738        15304 :       context => matrix_a%matrix_struct%context
     739        15304 :       myprow = context%mepos(1)
     740        15304 :       mypcol = context%mepos(2)
     741              : 
     742        15304 :       a => matrix_a%local_data
     743        15304 :       b => matrix_b%local_data
     744        15304 :       c => matrix_c%local_data
     745              : 
     746        15304 :       nrow_local = matrix_a%matrix_struct%nrow_locals(myprow)
     747        15304 :       ncol_local = matrix_a%matrix_struct%ncol_locals(mypcol)
     748              : 
     749       130200 :       DO icol_local = 1, ncol_local
     750      6849133 :          DO irow_local = 1, nrow_local
     751      6833829 :             c(irow_local, icol_local) = a(irow_local, icol_local)*b(irow_local, icol_local)
     752              :          END DO
     753              :       END DO
     754              : 
     755        15304 :       CALL timestop(handle)
     756              : 
     757        15304 :    END SUBROUTINE cp_fm_schur_product
     758              : 
     759              : ! **************************************************************************************************
     760              : !> \brief returns the trace of matrix_a^T matrix_b, i.e
     761              : !>      sum_{i,j}(matrix_a(i,j)*matrix_b(i,j))
     762              : !> \param matrix_a a matrix
     763              : !> \param matrix_b another matrix
     764              : !> \param trace ...
     765              : !> \par History
     766              : !>      11.06.2001 Creation (Matthias Krack)
     767              : !>      12.2002 added doc [fawzi]
     768              : !> \author Matthias Krack
     769              : !> \note
     770              : !>      note the transposition of matrix_a!
     771              : ! **************************************************************************************************
     772       286305 :    SUBROUTINE cp_fm_trace_a0b0t0(matrix_a, matrix_b, trace)
     773              : 
     774              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_a, matrix_b
     775              :       REAL(KIND=dp), INTENT(OUT)                         :: trace
     776              : 
     777              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_trace_a0b0t0'
     778              : 
     779              :       INTEGER                                            :: handle, mypcol, myprow, ncol_local, &
     780              :                                                             nrow_local
     781       286305 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: a, b
     782              :       TYPE(cp_blacs_env_type), POINTER                   :: context
     783              :       TYPE(mp_comm_type)                                 :: group
     784              : 
     785       286305 :       CALL timeset(routineN, handle)
     786              : 
     787       286305 :       context => matrix_a%matrix_struct%context
     788       286305 :       myprow = context%mepos(1)
     789       286305 :       mypcol = context%mepos(2)
     790              : 
     791       286305 :       group = matrix_a%matrix_struct%para_env
     792              : 
     793       286305 :       a => matrix_a%local_data
     794       286305 :       b => matrix_b%local_data
     795              : 
     796       286305 :       nrow_local = MIN(matrix_a%matrix_struct%nrow_locals(myprow), matrix_b%matrix_struct%nrow_locals(myprow))
     797       286305 :       ncol_local = MIN(matrix_a%matrix_struct%ncol_locals(mypcol), matrix_b%matrix_struct%ncol_locals(mypcol))
     798              : 
     799              :       ! cries for an accurate_dot_product
     800              :       trace = accurate_dot_product(a(1:nrow_local, 1:ncol_local), &
     801       286305 :                                    b(1:nrow_local, 1:ncol_local))
     802              : 
     803       286305 :       CALL group%sum(trace)
     804              : 
     805       286305 :       CALL timestop(handle)
     806              : 
     807       286305 :    END SUBROUTINE cp_fm_trace_a0b0t0
     808              : 
     809              :    #:mute
     810              :       #:set types = [("cp_fm_type", "a", ""), ("cp_fm_p_type", "p","%matrix")]
     811              :    #:endmute
     812              : 
     813              : ! **************************************************************************************************
     814              : !> \brief Compute trace(k) = Tr (matrix_a(k)^T matrix_b) for each pair of matrices A_k and B.
     815              : !> \param matrix_a list of A matrices
     816              : !> \param matrix_b B matrix
     817              : !> \param trace    computed traces
     818              : !> \par History
     819              : !>    * 08.2018 forked from cp_fm_trace() [Sergey Chulkov]
     820              : !> \note \parblock
     821              : !>      Computing the trace requires collective communication between involved MPI processes
     822              : !>      that implies a synchronisation point between them. The aim of this subroutine is to reduce
     823              : !>      the amount of time wasted in such synchronisation by performing one large collective
     824              : !>      operation which involves all the matrices in question.
     825              : !>
     826              : !>      The subroutine's suffix reflects dimensionality of dummy arrays; 'a1b0t1' means that
     827              : !>      the dummy variables 'matrix_a' and 'trace' are 1-dimensional arrays, while the variable
     828              : !>      'matrix_b' is a single matrix.
     829              : !>      \endparblock
     830              : ! **************************************************************************************************
     831              :    #:for longname, shortname, appendix in types
     832        94025 :       SUBROUTINE cp_fm_trace_a1b0t1_${shortname}$ (matrix_a, matrix_b, trace)
     833              :          TYPE(${longname}$), DIMENSION(:), INTENT(IN)       :: matrix_a
     834              :          TYPE(cp_fm_type), INTENT(IN)                       :: matrix_b
     835              :          REAL(kind=dp), DIMENSION(:), INTENT(OUT)           :: trace
     836              : 
     837              :          CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_trace_a1b0t1_${shortname}$'
     838              : 
     839              :          INTEGER                                            :: handle, imatrix, n_matrices, &
     840              :                                                                ncols_local, nrows_local
     841        94025 :          REAL(kind=dp), DIMENSION(:, :), POINTER            :: ldata_a, ldata_b
     842              :          TYPE(mp_comm_type)                                 :: group
     843              : 
     844        94025 :          CALL timeset(routineN, handle)
     845              : 
     846        94025 :          n_matrices = SIZE(trace)
     847        94025 :          CPASSERT(SIZE(matrix_a) == n_matrices)
     848              : 
     849        94025 :          CALL cp_fm_get_info(matrix_b, nrow_local=nrows_local, ncol_local=ncols_local)
     850              : 
     851        94025 :          ldata_b => matrix_b%local_data(1:nrows_local, 1:ncols_local)
     852              : 
     853              : !$OMP PARALLEL DO DEFAULT(NONE), &
     854              : !$OMP             PRIVATE(imatrix, ldata_a), &
     855              : !$OMP             SHARED(ldata_b, matrix_a, matrix_b), &
     856        94025 : !$OMP             SHARED(ncols_local, nrows_local, n_matrices, trace)
     857              : 
     858              :          DO imatrix = 1, n_matrices
     859              : 
     860              :             ! assume that the matrices A(i) and B have identical shapes and distribution schemes
     861              :             ldata_a => matrix_a(imatrix) ${appendix}$%local_data(1:nrows_local, 1:ncols_local)
     862              :             trace(imatrix) = accurate_dot_product(ldata_a, ldata_b)
     863              :          END DO
     864              : !$OMP END PARALLEL DO
     865              : 
     866        94025 :          group = matrix_b%matrix_struct%para_env
     867       756531 :          CALL group%sum(trace)
     868              : 
     869        94025 :          CALL timestop(handle)
     870        94025 :       END SUBROUTINE cp_fm_trace_a1b0t1_${shortname}$
     871              :    #:endfor
     872              : 
     873              : ! **************************************************************************************************
     874              : !> \brief Compute trace(k) = Tr (matrix_a(k)^T matrix_b(k)) for each pair of matrices A_k and B_k.
     875              : !> \param matrix_a list of A matrices
     876              : !> \param matrix_b list of B matrices
     877              : !> \param trace    computed traces
     878              : !> \param accurate ...
     879              : !> \par History
     880              : !>    * 11.2016 forked from cp_fm_trace() [Sergey Chulkov]
     881              : !> \note \parblock
     882              : !>      Computing the trace requires collective communication between involved MPI processes
     883              : !>      that implies a synchronisation point between them. The aim of this subroutine is to reduce
     884              : !>      the amount of time wasted in such synchronisation by performing one large collective
     885              : !>      operation which involves all the matrices in question.
     886              : !>
     887              : !>      The subroutine's suffix reflects dimensionality of dummy arrays; 'a1b1t1' means that
     888              : !>      all dummy variables (matrix_a, matrix_b, and trace) are 1-dimensional arrays.
     889              : !>      \endparblock
     890              : ! **************************************************************************************************
     891              :    #:for longname1, shortname1, appendix1 in types
     892              :       #:for longname2, shortname2, appendix2 in types
     893       186468 :          SUBROUTINE cp_fm_trace_a1b1t1_${shortname1}$${shortname2}$ (matrix_a, matrix_b, trace, accurate)
     894              :             TYPE(${longname1}$), DIMENSION(:), INTENT(IN)      :: matrix_a
     895              :             TYPE(${longname2}$), DIMENSION(:), INTENT(IN)      :: matrix_b
     896              :             REAL(kind=dp), DIMENSION(:), INTENT(OUT)           :: trace
     897              :             LOGICAL, INTENT(IN), OPTIONAL                      :: accurate
     898              : 
     899              :             CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_trace_a1b1t1_${shortname1}$${shortname2}$'
     900              : 
     901              :             INTEGER                                            :: handle, imatrix, n_matrices, &
     902              :                                                                   ncols_local, nrows_local
     903              :             LOGICAL                                            :: use_accurate_sum
     904       186468 :             REAL(kind=dp), DIMENSION(:, :), POINTER            :: ldata_a, ldata_b
     905              :             TYPE(mp_comm_type)                                 :: group
     906              : 
     907       186468 :             CALL timeset(routineN, handle)
     908              : 
     909       186468 :             n_matrices = SIZE(trace)
     910       186468 :             CPASSERT(SIZE(matrix_a) == n_matrices)
     911       186468 :             CPASSERT(SIZE(matrix_b) == n_matrices)
     912              : 
     913       186468 :             use_accurate_sum = .TRUE.
     914       186468 :             IF (PRESENT(accurate)) use_accurate_sum = accurate
     915              : 
     916              : !$OMP PARALLEL DO DEFAULT(NONE), &
     917              : !$OMP             PRIVATE(imatrix, ldata_a, ldata_b, ncols_local), &
     918              : !$OMP             PRIVATE(nrows_local), &
     919       186468 : !$OMP             SHARED(matrix_a, matrix_b, n_matrices, trace, use_accurate_sum)
     920              :             DO imatrix = 1, n_matrices
     921              :                CALL cp_fm_get_info(matrix_a(imatrix) ${appendix1}$, nrow_local=nrows_local, ncol_local=ncols_local)
     922              : 
     923              :                ! assume that the matrices A(i) and B(i) have identical shapes and distribution schemes
     924              :                ldata_a => matrix_a(imatrix) ${appendix1}$%local_data(1:nrows_local, 1:ncols_local)
     925              :                ldata_b => matrix_b(imatrix) ${appendix2}$%local_data(1:nrows_local, 1:ncols_local)
     926              :                IF (use_accurate_sum) THEN
     927              :                   trace(imatrix) = accurate_dot_product(ldata_a, ldata_b)
     928              :                ELSE
     929              :                   trace(imatrix) = SUM(ldata_a*ldata_b)
     930              :                END IF
     931              :             END DO
     932              : !$OMP END PARALLEL DO
     933              : 
     934       186468 :             group = matrix_a(1) ${appendix1}$%matrix_struct%para_env
     935       647588 :             CALL group%sum(trace)
     936              : 
     937       186468 :             CALL timestop(handle)
     938       186468 :          END SUBROUTINE cp_fm_trace_a1b1t1_${shortname1}$${shortname2}$
     939              :       #:endfor
     940              :    #:endfor
     941              : 
     942              : ! **************************************************************************************************
     943              : !> \brief Compute trace(i,j) = \sum_k Tr (matrix_a(k,i)^T matrix_b(k,j)).
     944              : !> \param matrix_a list of A matrices
     945              : !> \param matrix_b list of B matrices
     946              : !> \param trace    computed traces
     947              : !> \param accurate ...
     948              : ! **************************************************************************************************
     949              :    #:for longname1, shortname1, appendix1 in types
     950              :       #:for longname2, shortname2, appendix2 in types
     951        17524 :          SUBROUTINE cp_fm_contracted_trace_a2b2t2_${shortname1}$${shortname2}$ (matrix_a, matrix_b, trace, accurate)
     952              :             TYPE(${longname1}$), DIMENSION(:, :), INTENT(IN)   :: matrix_a
     953              :             TYPE(${longname2}$), DIMENSION(:, :), INTENT(IN)   :: matrix_b
     954              :             REAL(kind=dp), DIMENSION(:, :), INTENT(OUT)        :: trace
     955              :             LOGICAL, INTENT(IN), OPTIONAL                      :: accurate
     956              : 
     957              :             CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_contracted_trace_a2b2t2_${shortname1}$${shortname2}$'
     958              : 
     959              :             INTEGER                                            :: handle, ia, ib, iz, na, nb, ncols_local, &
     960              :                                                                   nrows_local, nz
     961              :             INTEGER(kind=int_8)                                :: ib8, itrace, na8, ntraces
     962              :             LOGICAL                                            :: use_accurate_sum
     963              :             REAL(kind=dp)                                      :: t
     964        17524 :             REAL(kind=dp), DIMENSION(:, :), POINTER            :: ldata_a, ldata_b
     965              :             TYPE(mp_comm_type)                                 :: group
     966              : 
     967        17524 :             CALL timeset(routineN, handle)
     968              : 
     969        17524 :             nz = SIZE(matrix_a, 1)
     970        17524 :             CPASSERT(SIZE(matrix_b, 1) == nz)
     971              : 
     972        17524 :             na = SIZE(matrix_a, 2)
     973        17524 :             nb = SIZE(matrix_b, 2)
     974        17524 :             CPASSERT(SIZE(trace, 1) == na)
     975        17524 :             CPASSERT(SIZE(trace, 2) == nb)
     976              : 
     977        17524 :             use_accurate_sum = .TRUE.
     978        17524 :             IF (PRESENT(accurate)) use_accurate_sum = accurate
     979              : 
     980              :             ! here we use one running index (itrace) instead of two (ia, ib) in order to
     981              :             ! improve load balance between shared-memory threads
     982        17524 :             ntraces = na*nb
     983        17524 :             na8 = INT(na, kind=int_8)
     984              : 
     985              : !$OMP PARALLEL DO DEFAULT(NONE), &
     986              : !$OMP             PRIVATE(ia, ib, ib8, itrace, iz, ldata_a, ldata_b, ncols_local), &
     987              : !$OMP             PRIVATE(nrows_local, t), &
     988        17524 : !$OMP             SHARED(matrix_a, matrix_b, na, na8, nb, ntraces, nz, trace, use_accurate_sum)
     989              :             DO itrace = 1, ntraces
     990              :                ib8 = (itrace - 1)/na8
     991              :                ia = INT(itrace - ib8*na8)
     992              :                ib = INT(ib8) + 1
     993              : 
     994              :                t = 0.0_dp
     995              :                DO iz = 1, nz
     996              :                   CALL cp_fm_get_info(matrix_a(iz, ia) ${appendix1}$, nrow_local=nrows_local, ncol_local=ncols_local)
     997              : 
     998              :                   ! assume that the matrices A(iz, ia) and B(iz, ib) have identical shapes and distribution schemes
     999              :                   ldata_a => matrix_a(iz, ia) ${appendix1}$%local_data(1:nrows_local, 1:ncols_local)
    1000              :                   ldata_b => matrix_b(iz, ib) ${appendix2}$%local_data(1:nrows_local, 1:ncols_local)
    1001              :                   IF (use_accurate_sum) THEN
    1002              :                      t = t + accurate_dot_product(ldata_a, ldata_b)
    1003              :                   ELSE
    1004              :                      t = t + SUM(ldata_a*ldata_b)
    1005              :                   END IF
    1006              :                END DO
    1007              :                trace(ia, ib) = t
    1008              :             END DO
    1009              : !$OMP END PARALLEL DO
    1010              : 
    1011        17524 :             group = matrix_a(1, 1) ${appendix1}$%matrix_struct%para_env
    1012       829416 :             CALL group%sum(trace)
    1013              : 
    1014        17524 :             CALL timestop(handle)
    1015        17524 :          END SUBROUTINE cp_fm_contracted_trace_a2b2t2_${shortname1}$${shortname2}$
    1016              :       #:endfor
    1017              :    #:endfor
    1018              : 
    1019              : ! **************************************************************************************************
    1020              : !> \brief multiplies in place by a triangular matrix:
    1021              : !>       matrix_b = alpha op(triangular_matrix) matrix_b
    1022              : !>      or (if side='R')
    1023              : !>       matrix_b = alpha matrix_b op(triangular_matrix)
    1024              : !>      op(triangular_matrix) is:
    1025              : !>       triangular_matrix (if transpose_tr=.false. and invert_tr=.false.)
    1026              : !>       triangular_matrix^T (if transpose_tr=.true. and invert_tr=.false.)
    1027              : !>       triangular_matrix^(-1) (if transpose_tr=.false. and invert_tr=.true.)
    1028              : !>       triangular_matrix^(-T) (if transpose_tr=.true. and invert_tr=.true.)
    1029              : !> \param triangular_matrix the triangular matrix that multiplies the other
    1030              : !> \param matrix_b the matrix that gets multiplied and stores the result
    1031              : !> \param side on which side of matrix_b stays op(triangular_matrix)
    1032              : !>        (defaults to 'L')
    1033              : !> \param transpose_tr if the triangular matrix should be transposed
    1034              : !>        (defaults to false)
    1035              : !> \param invert_tr if the triangular matrix should be inverted
    1036              : !>        (defaults to false)
    1037              : !> \param uplo_tr if triangular_matrix is stored in the upper ('U') or
    1038              : !>        lower ('L') triangle (defaults to 'U')
    1039              : !> \param unit_diag_tr if the diagonal elements of triangular_matrix should
    1040              : !>        be assumed to be 1 (defaults to false)
    1041              : !> \param n_rows the number of rows of the result (defaults to
    1042              : !>        size(matrix_b,1))
    1043              : !> \param n_cols the number of columns of the result (defaults to
    1044              : !>        size(matrix_b,2))
    1045              : !> \param alpha ...
    1046              : !> \par History
    1047              : !>      08.2002 created [fawzi]
    1048              : !> \author Fawzi Mohamed
    1049              : !> \note
    1050              : !>      needs an mpi env
    1051              : ! **************************************************************************************************
    1052       142620 :    SUBROUTINE cp_fm_triangular_multiply(triangular_matrix, matrix_b, side, &
    1053              :                                         transpose_tr, invert_tr, uplo_tr, unit_diag_tr, n_rows, n_cols, &
    1054              :                                         alpha)
    1055              :       TYPE(cp_fm_type), INTENT(IN)                       :: triangular_matrix, matrix_b
    1056              :       CHARACTER, INTENT(IN), OPTIONAL                    :: side
    1057              :       LOGICAL, INTENT(IN), OPTIONAL                      :: transpose_tr, invert_tr
    1058              :       CHARACTER, INTENT(IN), OPTIONAL                    :: uplo_tr
    1059              :       LOGICAL, INTENT(IN), OPTIONAL                      :: unit_diag_tr
    1060              :       INTEGER, INTENT(IN), OPTIONAL                      :: n_rows, n_cols
    1061              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: alpha
    1062              : 
    1063              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_triangular_multiply'
    1064              : 
    1065              :       CHARACTER                                          :: side_char, transa, unit_diag, uplo
    1066              :       INTEGER                                            :: handle, mdim, m, n
    1067              :       LOGICAL                                            :: invert
    1068              :       REAL(KIND=dp)                                      :: al
    1069              : 
    1070        71310 :       CALL timeset(routineN, handle)
    1071        71310 :       side_char = 'L'
    1072        71310 :       unit_diag = 'N'
    1073        71310 :       uplo = 'U'
    1074        71310 :       transa = 'N'
    1075        71310 :       invert = .FALSE.
    1076        71310 :       al = 1.0_dp
    1077        71310 :       CALL cp_fm_get_info(matrix_b, nrow_global=m, ncol_global=n)
    1078        71310 :       IF (PRESENT(side)) side_char = side
    1079              :       mdim = MERGE(1, 2, 'L' == side_char)
    1080        71310 :       IF (PRESENT(invert_tr)) invert = invert_tr
    1081        71310 :       IF (PRESENT(uplo_tr)) uplo = uplo_tr
    1082        71310 :       IF (PRESENT(unit_diag_tr)) THEN
    1083            0 :          IF (unit_diag_tr) THEN
    1084            0 :             unit_diag = 'U'
    1085              :          ELSE
    1086              :             unit_diag = 'N'
    1087              :          END IF
    1088              :       END IF
    1089        71310 :       IF (PRESENT(transpose_tr)) THEN
    1090         6700 :          IF (transpose_tr) THEN
    1091         3058 :             transa = 'T'
    1092              :          ELSE
    1093              :             transa = 'N'
    1094              :          END IF
    1095              :       END IF
    1096        71310 :       IF (PRESENT(alpha)) al = alpha
    1097        71310 :       IF (PRESENT(n_rows)) m = n_rows
    1098        71310 :       IF (PRESENT(n_cols)) n = n_cols
    1099              : 
    1100        71310 :       IF (invert) THEN
    1101              : 
    1102              : #if defined(__parallel)
    1103              :          CALL pdtrsm(side_char, uplo, transa, unit_diag, m, n, al, &
    1104              :                      triangular_matrix%local_data(1, 1), 1, 1, &
    1105              :                      triangular_matrix%matrix_struct%descriptor, &
    1106              :                      matrix_b%local_data(1, 1), 1, 1, &
    1107        53760 :                      matrix_b%matrix_struct%descriptor(1))
    1108              : #else
    1109              :          CALL dtrsm(side_char, uplo, transa, unit_diag, m, n, al, &
    1110              :                     triangular_matrix%local_data(1, 1), &
    1111              :                     SIZE(triangular_matrix%local_data, mdim), &
    1112              :                     matrix_b%local_data(1, 1), SIZE(matrix_b%local_data, 1))
    1113              : #endif
    1114              : 
    1115              :       ELSE
    1116              : 
    1117              : #if defined(__parallel)
    1118              :          CALL pdtrmm(side_char, uplo, transa, unit_diag, m, n, al, &
    1119              :                      triangular_matrix%local_data(1, 1), 1, 1, &
    1120              :                      triangular_matrix%matrix_struct%descriptor, &
    1121              :                      matrix_b%local_data(1, 1), 1, 1, &
    1122        17550 :                      matrix_b%matrix_struct%descriptor(1))
    1123              : #else
    1124              :          CALL dtrmm(side_char, uplo, transa, unit_diag, m, n, al, &
    1125              :                     triangular_matrix%local_data(1, 1), &
    1126              :                     SIZE(triangular_matrix%local_data, mdim), &
    1127              :                     matrix_b%local_data(1, 1), SIZE(matrix_b%local_data, 1))
    1128              : #endif
    1129              : 
    1130              :       END IF
    1131              : 
    1132        71310 :       CALL timestop(handle)
    1133        71310 :    END SUBROUTINE cp_fm_triangular_multiply
    1134              : 
    1135              : ! **************************************************************************************************
    1136              : !> \brief scales a matrix
    1137              : !>      matrix_a = alpha * matrix_b
    1138              : !> \param alpha ...
    1139              : !> \param matrix_a ...
    1140              : !> \note
    1141              : !>      use cp_fm_set_all to zero (avoids problems with nan)
    1142              : ! **************************************************************************************************
    1143        68437 :    SUBROUTINE cp_fm_scale(alpha, matrix_a)
    1144              :       REAL(KIND=dp), INTENT(IN)                          :: alpha
    1145              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_a
    1146              : 
    1147              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cp_fm_scale'
    1148              : 
    1149              :       INTEGER                                            :: handle, size_a
    1150              :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: a
    1151              : 
    1152        68437 :       CALL timeset(routineN, handle)
    1153              : 
    1154              :       NULLIFY (a)
    1155              : 
    1156        68437 :       a => matrix_a%local_data
    1157        68437 :       size_a = SIZE(a, 1)*SIZE(a, 2)
    1158              : 
    1159        68437 :       CALL DSCAL(size_a, alpha, a, 1)
    1160              : 
    1161        68437 :       CALL timestop(handle)
    1162              : 
    1163        68437 :    END SUBROUTINE cp_fm_scale
    1164              : 
    1165              : ! **************************************************************************************************
    1166              : !> \brief transposes a matrix
    1167              : !>      matrixt = matrix ^ T
    1168              : !> \param matrix ...
    1169              : !> \param matrixt ...
    1170              : !> \note
    1171              : !>      all matrix elements are transposed (see cp_fm_uplo_to_full to symmetrise a matrix)
    1172              : ! **************************************************************************************************
    1173        37076 :    SUBROUTINE cp_fm_transpose(matrix, matrixt)
    1174              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix, matrixt
    1175              : 
    1176              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_transpose'
    1177              : 
    1178              :       INTEGER                                  :: handle, ncol_global, &
    1179              :                                                   nrow_global, ncol_globalt, nrow_globalt
    1180        18538 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a, c
    1181              : #if defined(__parallel)
    1182              :       INTEGER, DIMENSION(9)                    :: desca, descc
    1183              : #elif !defined(__MKL)
    1184              :       INTEGER                                  :: i, j
    1185              : #endif
    1186              : 
    1187        18538 :       nrow_global = matrix%matrix_struct%nrow_global
    1188        18538 :       ncol_global = matrix%matrix_struct%ncol_global
    1189        18538 :       nrow_globalt = matrixt%matrix_struct%nrow_global
    1190        18538 :       ncol_globalt = matrixt%matrix_struct%ncol_global
    1191            0 :       CPASSERT(nrow_global == ncol_globalt)
    1192        18538 :       CPASSERT(nrow_globalt == ncol_global)
    1193              : 
    1194        18538 :       CALL timeset(routineN, handle)
    1195              : 
    1196        18538 :       a => matrix%local_data
    1197        18538 :       c => matrixt%local_data
    1198              : 
    1199              : #if defined(__parallel)
    1200       185380 :       desca(:) = matrix%matrix_struct%descriptor(:)
    1201       185380 :       descc(:) = matrixt%matrix_struct%descriptor(:)
    1202        18538 :       CALL pdtran(ncol_global, nrow_global, 1.0_dp, a(1, 1), 1, 1, desca, 0.0_dp, c(1, 1), 1, 1, descc)
    1203              : #elif defined(__MKL)
    1204              :       CALL mkl_domatcopy('C', 'T', nrow_global, ncol_global, 1.0_dp, a(1, 1), nrow_global, c(1, 1), ncol_global)
    1205              : #else
    1206              :       DO j = 1, ncol_global
    1207              :          DO i = 1, nrow_global
    1208              :             c(j, i) = a(i, j)
    1209              :          END DO
    1210              :       END DO
    1211              : #endif
    1212        18538 :       CALL timestop(handle)
    1213              : 
    1214        18538 :    END SUBROUTINE cp_fm_transpose
    1215              : 
    1216              : ! **************************************************************************************************
    1217              : !> \brief given a triangular matrix according to uplo, computes the corresponding full matrix
    1218              : !> \param matrix the triangular matrix as input, the full matrix as output
    1219              : !> \param work a matrix of the same size as matrix
    1220              : !> \param uplo triangular format; defaults to 'U'
    1221              : !> \author Matthias Krack
    1222              : !> \note
    1223              : !>       the opposite triangular part is irrelevant
    1224              : ! **************************************************************************************************
    1225       390008 :    SUBROUTINE cp_fm_uplo_to_full(matrix, work, uplo)
    1226              : 
    1227              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix, work
    1228              :       CHARACTER, INTENT(IN), OPTIONAL          :: uplo
    1229              : 
    1230              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_uplo_to_full'
    1231              : 
    1232              :       CHARACTER                                :: myuplo
    1233              :       INTEGER                                  :: handle, icol_global, irow_global, &
    1234              :                                                   mypcol, myprow, ncol_global, &
    1235              :                                                   nrow_global
    1236       195004 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
    1237              :       TYPE(cp_blacs_env_type), POINTER         :: context
    1238              : 
    1239              : #if defined(__parallel)
    1240              :       INTEGER                                  :: icol_local, irow_local, &
    1241              :                                                   ncol_block, ncol_local, &
    1242              :                                                   nrow_block, nrow_local
    1243              :       INTEGER, DIMENSION(9)                    :: desca, descc
    1244       195004 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: c
    1245              : #endif
    1246              : 
    1247       195004 :       myuplo = 'U'
    1248            0 :       IF (PRESENT(uplo)) myuplo = uplo
    1249              : 
    1250       195004 :       nrow_global = matrix%matrix_struct%nrow_global
    1251       195004 :       ncol_global = matrix%matrix_struct%ncol_global
    1252       195004 :       CPASSERT(nrow_global == ncol_global)
    1253       195004 :       nrow_global = work%matrix_struct%nrow_global
    1254       195004 :       ncol_global = work%matrix_struct%ncol_global
    1255       195004 :       CPASSERT(nrow_global == ncol_global)
    1256              : 
    1257       195004 :       CALL timeset(routineN, handle)
    1258              : 
    1259       195004 :       context => matrix%matrix_struct%context
    1260       195004 :       myprow = context%mepos(1)
    1261       195004 :       mypcol = context%mepos(2)
    1262              : 
    1263              : #if defined(__parallel)
    1264              : 
    1265       195004 :       nrow_block = matrix%matrix_struct%nrow_block
    1266       195004 :       ncol_block = matrix%matrix_struct%ncol_block
    1267              : 
    1268       195004 :       nrow_local = matrix%matrix_struct%nrow_locals(myprow)
    1269       195004 :       ncol_local = matrix%matrix_struct%ncol_locals(mypcol)
    1270              : 
    1271       195004 :       a => work%local_data
    1272      1950040 :       desca(:) = work%matrix_struct%descriptor(:)
    1273       195004 :       c => matrix%local_data
    1274      1950040 :       descc(:) = matrix%matrix_struct%descriptor(:)
    1275              : 
    1276      5995599 :       DO icol_local = 1, ncol_local
    1277      5800595 :          icol_global = matrix%matrix_struct%col_indices(icol_local)
    1278    239397781 :          DO irow_local = 1, nrow_local
    1279    233402182 :             irow_global = matrix%matrix_struct%row_indices(irow_local)
    1280    239202777 :             IF (MERGE(irow_global > icol_global, irow_global < icol_global, (myuplo == "U") .OR. (myuplo == "u"))) THEN
    1281    115127141 :                c(irow_local, icol_local) = 0.0_dp
    1282    118275041 :             ELSE IF (irow_global == icol_global) THEN
    1283      3147248 :                c(irow_local, icol_local) = 0.5_dp*c(irow_local, icol_local)
    1284              :             END IF
    1285              :          END DO
    1286              :       END DO
    1287              : 
    1288      5995599 :       DO icol_local = 1, ncol_local
    1289    239397781 :       DO irow_local = 1, nrow_local
    1290    239202777 :          a(irow_local, icol_local) = c(irow_local, icol_local)
    1291              :       END DO
    1292              :       END DO
    1293              : 
    1294       195004 :       CALL pdtran(nrow_global, ncol_global, 1.0_dp, a(1, 1), 1, 1, desca, 1.0_dp, c(1, 1), 1, 1, descc)
    1295              : 
    1296              : #else
    1297              : 
    1298              :       a => matrix%local_data
    1299              : 
    1300              :       IF ((myuplo == "U") .OR. (myuplo == "u")) THEN
    1301              :          DO irow_global = 1, nrow_global
    1302              :          DO icol_global = irow_global + 1, ncol_global
    1303              :             a(icol_global, irow_global) = a(irow_global, icol_global)
    1304              :          END DO
    1305              :          END DO
    1306              :       ELSE
    1307              :          DO icol_global = 1, ncol_global
    1308              :          DO irow_global = icol_global + 1, nrow_global
    1309              :             a(irow_global, icol_global) = a(icol_global, irow_global)
    1310              :          END DO
    1311              :          END DO
    1312              :       END IF
    1313              : 
    1314              : #endif
    1315       195004 :       CALL timestop(handle)
    1316              : 
    1317       195004 :    END SUBROUTINE cp_fm_uplo_to_full
    1318              : 
    1319              : ! **************************************************************************************************
    1320              : !> \brief Scales columns of the full matrix by corresponding factors.
    1321              : !> \param matrixa target matrix; columns outside the selected window are unchanged
    1322              : !> \param scaling factors for consecutive columns starting at first_col;
    1323              : !>        the window is truncated at the last global column of the matrix
    1324              : !> \param first_col first global column to scale (defaults to 1)
    1325              : !> \param source optional source with the same shape and distribution as matrixa;
    1326              : !>        if omitted, matrixa is scaled in place
    1327              : !> \author Joost VandeVondele
    1328              : !> \note
    1329              : !>      this is very useful as a first step in the computation of C = sum_i alpha_i A_i transpose (A_i)
    1330              : !>      that is a rank-k update (cp_fm_syrk , cp_sm_plus_fm_fm_t)
    1331              : !>      this procedure can be up to 20 times faster than calling cp_fm_syrk n times
    1332              : !>      where every vector has a different prefactor
    1333              : ! **************************************************************************************************
    1334       140874 :    SUBROUTINE cp_fm_column_scale(matrixa, scaling, first_col, source)
    1335              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrixa
    1336              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: scaling
    1337              :       INTEGER, INTENT(IN), OPTIONAL                      :: first_col
    1338              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: source
    1339              : 
    1340              :       INTEGER                                            :: first, i_factor, icol_local, ncol_local, &
    1341              :                                                             nrow_local
    1342       140874 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices
    1343              :       LOGICAL                                            :: compatible_layout
    1344       140874 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: a, b
    1345              : 
    1346       140874 :       a => matrixa%local_data
    1347       140874 :       b => a
    1348       140874 :       IF (PRESENT(source)) THEN
    1349        14790 :          compatible_layout = cp_fm_struct_equivalent(matrixa%matrix_struct, source%matrix_struct)
    1350        14790 :          CPASSERT(compatible_layout)
    1351        14790 :          b => source%local_data
    1352              :       END IF
    1353       140874 :       first = 1
    1354       140874 :       IF (PRESENT(first_col)) first = first_col
    1355        14784 :       CPASSERT(first >= 1)
    1356       140874 :       CALL cp_fm_get_info(matrixa, nrow_local=nrow_local, ncol_local=ncol_local, col_indices=col_indices)
    1357              : 
    1358      2027081 :       DO icol_local = 1, ncol_local
    1359      1886207 :          i_factor = col_indices(icol_local) - first + 1
    1360      1886207 :          IF (i_factor < 1 .OR. i_factor > SIZE(scaling)) CYCLE
    1361     91659950 :          a(1:nrow_local, icol_local) = scaling(i_factor)*b(1:nrow_local, icol_local)
    1362              :       END DO
    1363       140874 :    END SUBROUTINE cp_fm_column_scale
    1364              : 
    1365              : ! **************************************************************************************************
    1366              : !> \brief scales row i of matrix a with scaling(i)
    1367              : !> \param matrixa ...
    1368              : !> \param scaling : an array used for scaling the columns,
    1369              : !> \author JGH
    1370              : !> \note
    1371              : ! **************************************************************************************************
    1372        11344 :    SUBROUTINE cp_fm_row_scale(matrixa, scaling)
    1373              :       TYPE(cp_fm_type), INTENT(IN)             :: matrixa
    1374              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)  :: scaling
    1375              : 
    1376              :       INTEGER                                  :: n, m, nrow_global, nrow_local, ncol_local
    1377        11344 :       INTEGER, DIMENSION(:), POINTER           :: row_indices
    1378        11344 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
    1379              : #if defined(__parallel)
    1380              :       INTEGER                                  :: irow_global, icol, irow
    1381              : #else
    1382              :       INTEGER                                  :: j
    1383              : #endif
    1384              : 
    1385              :       CALL cp_fm_get_info(matrixa, row_indices=row_indices, nrow_global=nrow_global, &
    1386        11344 :                           nrow_local=nrow_local, ncol_local=ncol_local)
    1387        11344 :       CPASSERT(SIZE(scaling) == nrow_global)
    1388              : 
    1389        11344 :       a => matrixa%local_data
    1390        11344 :       n = SIZE(a, 1)
    1391        11344 :       m = SIZE(a, 2)
    1392              : 
    1393              : #if defined(__parallel)
    1394       106503 :       DO icol = 1, ncol_local
    1395      6823678 :          DO irow = 1, nrow_local
    1396      6717175 :             irow_global = row_indices(irow)
    1397      6812334 :             a(irow, icol) = scaling(irow_global)*a(irow, icol)
    1398              :          END DO
    1399              :       END DO
    1400              : #else
    1401              :       DO j = 1, m
    1402              :          a(1:n, j) = scaling(1:n)*a(1:n, j)
    1403              :       END DO
    1404              : #endif
    1405        11344 :    END SUBROUTINE cp_fm_row_scale
    1406              : 
    1407              : ! **************************************************************************************************
    1408              : !> \brief Inverts a cp_fm_type matrix, optionally returning the determinant of the input matrix
    1409              : !> \param matrix_a the matrix to invert
    1410              : !> \param matrix_inverse the inverse of matrix_a
    1411              : !> \param det_a the determinant of matrix_a
    1412              : !> \param eps_svd optional parameter to active SVD based inversion, singular values below eps_svd
    1413              : !>                are screened
    1414              : !> \param eigval optionally return matrix eigenvalues/singular values
    1415              : !> \par History
    1416              : !>      note of Jan Wilhelm (12.2015)
    1417              : !>      - computation of determinant corrected
    1418              : !>      - determinant only computed if det_a is present
    1419              : !>      12.2016 added option to use SVD instead of LU [Nico Holmberg]
    1420              : !>      - Use cp_fm_get diag instead of n times cp_fm_get_element (A. Bussy)
    1421              : !> \author Florian Schiffmann(02.2007)
    1422              : ! **************************************************************************************************
    1423         1850 :    SUBROUTINE cp_fm_invert(matrix_a, matrix_inverse, det_a, eps_svd, eigval)
    1424              : 
    1425              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a, matrix_inverse
    1426              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL     :: det_a
    1427              :       REAL(KIND=dp), INTENT(IN), OPTIONAL      :: eps_svd
    1428              :       REAL(KIND=dp), DIMENSION(:), POINTER, &
    1429              :          INTENT(INOUT), OPTIONAL               :: eigval
    1430              : 
    1431              :       INTEGER                                  :: n
    1432         1850 :       INTEGER, ALLOCATABLE, DIMENSION(:)       :: ipivot
    1433              :       REAL(KIND=dp)                            :: determinant, my_eps_svd
    1434              :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
    1435              :       TYPE(cp_fm_type)                         :: matrix_lu
    1436              : 
    1437              : #if defined(__parallel)
    1438              :       TYPE(cp_fm_type)                         :: u, vt, sigma, inv_sigma_ut
    1439              :       TYPE(mp_comm_type) :: group
    1440              :       INTEGER                                  :: i, info, liwork, lwork, exponent_of_minus_one
    1441              :       INTEGER, DIMENSION(9)                    :: desca
    1442              :       LOGICAL                                  :: quenched
    1443              :       REAL(KIND=dp)                            :: alpha, beta
    1444         1850 :       REAL(KIND=dp), DIMENSION(:), POINTER     :: diag
    1445         1850 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: work
    1446              : #else
    1447              :       LOGICAL                                  :: sign
    1448              :       REAL(KIND=dp)                            :: eps1
    1449              : #endif
    1450              : 
    1451         1850 :       my_eps_svd = 0.0_dp
    1452          480 :       IF (PRESENT(eps_svd)) my_eps_svd = eps_svd
    1453              : 
    1454              :       CALL cp_fm_create(matrix=matrix_lu, &
    1455              :                         matrix_struct=matrix_a%matrix_struct, &
    1456         1850 :                         name="A_lu"//TRIM(ADJUSTL(cp_to_string(1)))//"MATRIX")
    1457         1850 :       CALL cp_fm_to_fm(matrix_a, matrix_lu)
    1458              : 
    1459         1850 :       a => matrix_lu%local_data
    1460         1850 :       n = matrix_lu%matrix_struct%nrow_global
    1461         5550 :       ALLOCATE (ipivot(n + matrix_a%matrix_struct%nrow_block))
    1462         1850 :       ipivot(:) = 0
    1463              : #if defined(__parallel)
    1464         1850 :       IF (my_eps_svd == 0.0_dp) THEN
    1465              :          ! Use LU decomposition
    1466              :          lwork = 3*n
    1467              :          liwork = 3*n
    1468        18200 :          desca(:) = matrix_lu%matrix_struct%descriptor(:)
    1469         1820 :          CALL pdgetrf(n, n, a, 1, 1, desca, ipivot, info)
    1470              : 
    1471         1820 :          IF (PRESENT(det_a) .OR. PRESENT(eigval)) THEN
    1472              : 
    1473         1134 :             ALLOCATE (diag(n))
    1474          928 :             diag(:) = 0.0_dp
    1475          378 :             CALL cp_fm_get_diag(matrix_lu, diag)
    1476              : 
    1477          378 :             exponent_of_minus_one = 0
    1478          378 :             determinant = 1.0_dp
    1479          928 :             DO i = 1, n
    1480          550 :                determinant = determinant*diag(i)
    1481          928 :                IF (ipivot(i) /= i) THEN
    1482          224 :                   exponent_of_minus_one = exponent_of_minus_one + 1
    1483              :                END IF
    1484              :             END DO
    1485          378 :             IF (PRESENT(eigval)) THEN
    1486            0 :                CPASSERT(.NOT. ASSOCIATED(eigval))
    1487            0 :                ALLOCATE (eigval(n))
    1488            0 :                eigval(:) = diag
    1489              :             END IF
    1490          378 :             DEALLOCATE (diag)
    1491              : 
    1492          378 :             group = matrix_lu%matrix_struct%para_env
    1493          378 :             CALL group%sum(exponent_of_minus_one)
    1494              : 
    1495          378 :             determinant = determinant*(-1.0_dp)**exponent_of_minus_one
    1496              : 
    1497              :          END IF
    1498              : 
    1499         1820 :          alpha = 0.0_dp
    1500         1820 :          beta = 1.0_dp
    1501         1820 :          CALL cp_fm_set_all(matrix_inverse, alpha, beta)
    1502         1820 :          CALL pdgetrs('N', n, n, matrix_lu%local_data, 1, 1, desca, ipivot, matrix_inverse%local_data, 1, 1, desca, info)
    1503              :       ELSE
    1504              :          ! Use singular value decomposition
    1505              :          CALL cp_fm_create(matrix=u, &
    1506              :                            matrix_struct=matrix_a%matrix_struct, &
    1507           30 :                            name="LEFT_SINGULAR_MATRIX")
    1508           30 :          CALL cp_fm_set_all(u, alpha=0.0_dp)
    1509              :          CALL cp_fm_create(matrix=vt, &
    1510              :                            matrix_struct=matrix_a%matrix_struct, &
    1511           30 :                            name="RIGHT_SINGULAR_MATRIX")
    1512           30 :          CALL cp_fm_set_all(vt, alpha=0.0_dp)
    1513           90 :          ALLOCATE (diag(n))
    1514           96 :          diag(:) = 0.0_dp
    1515          300 :          desca(:) = matrix_lu%matrix_struct%descriptor(:)
    1516           30 :          ALLOCATE (work(1))
    1517              :          ! Workspace query
    1518           30 :          lwork = -1
    1519              :          CALL pdgesvd('V', 'V', n, n, matrix_lu%local_data, 1, 1, desca, diag, u%local_data, &
    1520           30 :                       1, 1, desca, vt%local_data, 1, 1, desca, work, lwork, info)
    1521           30 :          lwork = INT(work(1))
    1522           30 :          DEALLOCATE (work)
    1523           90 :          ALLOCATE (work(lwork))
    1524              :          ! SVD
    1525              :          CALL pdgesvd('V', 'V', n, n, matrix_lu%local_data, 1, 1, desca, diag, u%local_data, &
    1526           30 :                       1, 1, desca, vt%local_data, 1, 1, desca, work, lwork, info)
    1527              :          ! info == n+1 implies homogeneity error when the number of procs is large
    1528              :          ! this likely isnt a problem, but maybe we should handle it separately
    1529           30 :          IF (info /= 0 .AND. info /= n + 1) &
    1530            0 :             CPABORT("Singular value decomposition of matrix failed.")
    1531              :          ! (Pseudo)inverse and (pseudo)determinant
    1532              :          CALL cp_fm_create(matrix=sigma, &
    1533              :                            matrix_struct=matrix_a%matrix_struct, &
    1534           30 :                            name="SINGULAR_VALUE_MATRIX")
    1535           30 :          CALL cp_fm_set_all(sigma, alpha=0.0_dp)
    1536           30 :          determinant = 1.0_dp
    1537           30 :          quenched = .FALSE.
    1538           30 :          IF (PRESENT(eigval)) THEN
    1539           28 :             CPASSERT(.NOT. ASSOCIATED(eigval))
    1540           84 :             ALLOCATE (eigval(n))
    1541          156 :             eigval(:) = diag
    1542              :          END IF
    1543           96 :          DO i = 1, n
    1544           66 :             IF (diag(i) < my_eps_svd) THEN
    1545           18 :                diag(i) = 0.0_dp
    1546           18 :                quenched = .TRUE.
    1547              :             ELSE
    1548           48 :                determinant = determinant*diag(i)
    1549           48 :                diag(i) = 1.0_dp/diag(i)
    1550              :             END IF
    1551           96 :             CALL cp_fm_set_element(sigma, i, i, diag(i))
    1552              :          END DO
    1553           30 :          DEALLOCATE (diag)
    1554           30 :          IF (quenched) &
    1555              :             CALL cp_warn(__LOCATION__, &
    1556              :                          "Linear dependencies were detected in the SVD inversion of matrix "//TRIM(ADJUSTL(matrix_a%name))// &
    1557           12 :                          ". At least one singular value has been quenched.")
    1558              :          ! Sigma^-1 * U^T
    1559              :          CALL cp_fm_create(matrix=inv_sigma_ut, &
    1560              :                            matrix_struct=matrix_a%matrix_struct, &
    1561           30 :                            name="SINGULAR_VALUE_MATRIX")
    1562           30 :          CALL cp_fm_set_all(inv_sigma_ut, alpha=0.0_dp)
    1563              :          CALL pdgemm('N', 'T', n, n, n, 1.0_dp, sigma%local_data, 1, 1, desca, &
    1564           30 :                      u%local_data, 1, 1, desca, 0.0_dp, inv_sigma_ut%local_data, 1, 1, desca)
    1565              :          ! A^-1 = V * (Sigma^-1 * U^T)
    1566           30 :          CALL cp_fm_set_all(matrix_inverse, alpha=0.0_dp)
    1567              :          CALL pdgemm('T', 'N', n, n, n, 1.0_dp, vt%local_data, 1, 1, desca, &
    1568           30 :                      inv_sigma_ut%local_data, 1, 1, desca, 0.0_dp, matrix_inverse%local_data, 1, 1, desca)
    1569              :          ! Clean up
    1570           30 :          DEALLOCATE (work)
    1571           30 :          CALL cp_fm_release(u)
    1572           30 :          CALL cp_fm_release(vt)
    1573           30 :          CALL cp_fm_release(sigma)
    1574          150 :          CALL cp_fm_release(inv_sigma_ut)
    1575              :       END IF
    1576              : #else
    1577              :       IF (my_eps_svd == 0.0_dp) THEN
    1578              :          sign = .TRUE.
    1579              :          CALL invert_matrix(matrix_a%local_data, matrix_inverse%local_data, &
    1580              :                             eval_error=eps1)
    1581              :          CALL cp_fm_lu_decompose(matrix_lu, determinant, correct_sign=sign)
    1582              :          IF (PRESENT(eigval)) &
    1583              :             CALL cp_abort(__LOCATION__, &
    1584              :                           "NYI. Eigenvalues not available for return without SCALAPACK.")
    1585              :       ELSE
    1586              :          CALL get_pseudo_inverse_svd(matrix_a%local_data, matrix_inverse%local_data, eps_svd, &
    1587              :                                      determinant, eigval)
    1588              :       END IF
    1589              : #endif
    1590         1850 :       CALL cp_fm_release(matrix_lu)
    1591         1850 :       DEALLOCATE (ipivot)
    1592         1850 :       IF (PRESENT(det_a)) det_a = determinant
    1593         1850 :    END SUBROUTINE cp_fm_invert
    1594              : 
    1595              : ! **************************************************************************************************
    1596              : !> \brief inverts a triangular matrix
    1597              : !> \param matrix_a ...
    1598              : !> \param uplo_tr triangular format; defaults to 'U'
    1599              : !> \author MI
    1600              : ! **************************************************************************************************
    1601         5924 :    SUBROUTINE cp_fm_triangular_invert(matrix_a, uplo_tr)
    1602              : 
    1603              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a
    1604              :       CHARACTER, INTENT(IN), OPTIONAL          :: uplo_tr
    1605              : 
    1606              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_fm_triangular_invert'
    1607              : 
    1608              :       CHARACTER                                :: unit_diag, uplo
    1609              :       INTEGER                                  :: handle, info, ncol_global
    1610         5924 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
    1611              : #if defined(__parallel)
    1612              :       INTEGER, DIMENSION(9)                    :: desca
    1613              : #endif
    1614              : 
    1615         5924 :       CALL timeset(routineN, handle)
    1616              : 
    1617         5924 :       unit_diag = 'N'
    1618         5924 :       uplo = 'U'
    1619         5924 :       IF (PRESENT(uplo_tr)) uplo = uplo_tr
    1620              : 
    1621         5924 :       ncol_global = matrix_a%matrix_struct%ncol_global
    1622              : 
    1623         5924 :       a => matrix_a%local_data
    1624              : 
    1625              : #if defined(__parallel)
    1626        59240 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
    1627              : 
    1628         5924 :       CALL pdtrtri(uplo, unit_diag, ncol_global, a(1, 1), 1, 1, desca, info)
    1629              : 
    1630              : #else
    1631              :       CALL dtrtri(uplo, unit_diag, ncol_global, a(1, 1), ncol_global, info)
    1632              : #endif
    1633              : 
    1634         5924 :       CALL timestop(handle)
    1635         5924 :    END SUBROUTINE cp_fm_triangular_invert
    1636              : 
    1637              : ! **************************************************************************************************
    1638              : !> \brief  performs a QR factorization of the input rectangular matrix A or of a submatrix of A
    1639              : !>         the computed triangular matrix R is in output of the submatrix sub(A) of size NxN
    1640              : !>         M and M give the dimension of the submatrix that has to be factorized (MxN) with M>N
    1641              : !> \param matrix_a ...
    1642              : !> \param matrix_r ...
    1643              : !> \param nrow_fact ...
    1644              : !> \param ncol_fact ...
    1645              : !> \param first_row ...
    1646              : !> \param first_col ...
    1647              : !> \author MI
    1648              : ! **************************************************************************************************
    1649        23324 :    SUBROUTINE cp_fm_qr_factorization(matrix_a, matrix_r, nrow_fact, ncol_fact, first_row, first_col, uplo)
    1650              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a, matrix_r
    1651              :       INTEGER, INTENT(IN), OPTIONAL            :: nrow_fact, ncol_fact, &
    1652              :                                                   first_row, first_col
    1653              :       CHARACTER, INTENT(IN), OPTIONAL          :: uplo
    1654              : 
    1655              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_fm_qr_factorization'
    1656              : 
    1657              :       CHARACTER                                :: myuplo
    1658              :       INTEGER                                  :: handle, i, icol, info, irow, &
    1659              :                                                   j, lda, lwork, ncol, &
    1660              :                                                   ndim, nrow
    1661        23324 :       REAL(dp), ALLOCATABLE, DIMENSION(:)      :: tau, work
    1662        23324 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)   :: r_mat
    1663              :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
    1664              : #if defined(__parallel)
    1665              :       INTEGER, DIMENSION(9)                    :: desca
    1666              : #endif
    1667              : 
    1668        23324 :       CALL timeset(routineN, handle)
    1669              : 
    1670        23324 :       myuplo = 'U'
    1671        23324 :       IF (PRESENT(uplo)) myuplo = uplo
    1672              : 
    1673        23324 :       ncol = matrix_a%matrix_struct%ncol_global
    1674        23324 :       nrow = matrix_a%matrix_struct%nrow_global
    1675        23324 :       lda = nrow
    1676              : 
    1677        23324 :       a => matrix_a%local_data
    1678              : 
    1679        23324 :       IF (PRESENT(nrow_fact)) nrow = nrow_fact
    1680        23324 :       IF (PRESENT(ncol_fact)) ncol = ncol_fact
    1681        23324 :       irow = 1
    1682        23324 :       IF (PRESENT(first_row)) irow = first_row
    1683        23324 :       icol = 1
    1684        23324 :       IF (PRESENT(first_col)) icol = first_col
    1685              : 
    1686        23324 :       CPASSERT(nrow >= ncol)
    1687        23324 :       ndim = SIZE(a, 2)
    1688        69972 :       ALLOCATE (tau(ndim))
    1689              : 
    1690              : #if defined(__parallel)
    1691              : 
    1692       233240 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
    1693              : 
    1694        23324 :       lwork = -1
    1695        69972 :       ALLOCATE (work(2*ndim))
    1696        23324 :       CALL pdgeqrf(nrow, ncol, a, irow, icol, desca, tau, work, lwork, info)
    1697        23324 :       lwork = INT(work(1))
    1698        23324 :       DEALLOCATE (work)
    1699        69972 :       ALLOCATE (work(lwork))
    1700        23324 :       CALL pdgeqrf(nrow, ncol, a, irow, icol, desca, tau, work, lwork, info)
    1701              : 
    1702              : #else
    1703              :       lwork = -1
    1704              :       ALLOCATE (work(2*ndim))
    1705              :       CALL dgeqrf(nrow, ncol, a, lda, tau, work, lwork, info)
    1706              :       lwork = INT(work(1))
    1707              :       DEALLOCATE (work)
    1708              :       ALLOCATE (work(lwork))
    1709              :       CALL dgeqrf(nrow, ncol, a, lda, tau, work, lwork, info)
    1710              : 
    1711              : #endif
    1712              : 
    1713        93296 :       ALLOCATE (r_mat(ncol, ncol))
    1714        23324 :       CALL cp_fm_get_submatrix(matrix_a, r_mat, 1, 1, ncol, ncol)
    1715        23324 :       IF ((myuplo == "U") .OR. (myuplo == "u")) THEN
    1716        56462 :          DO i = 1, ncol
    1717        75474 :          DO j = i + 1, ncol
    1718        52150 :             r_mat(j, i) = 0.0_dp
    1719              :          END DO
    1720              :          END DO
    1721              :       ELSE
    1722            0 :          DO j = 1, ncol
    1723            0 :          DO i = j + 1, ncol
    1724            0 :             r_mat(i, j) = 0.0_dp
    1725              :          END DO
    1726              :          END DO
    1727              :       END IF
    1728        23324 :       CALL cp_fm_set_submatrix(matrix_r, r_mat, 1, 1, ncol, ncol)
    1729              : 
    1730        23324 :       DEALLOCATE (tau, work, r_mat)
    1731              : 
    1732        23324 :       CALL timestop(handle)
    1733              : 
    1734        23324 :    END SUBROUTINE cp_fm_qr_factorization
    1735              : 
    1736              : ! **************************************************************************************************
    1737              : !> \brief computes the the solution to A*b=A_general using lu decomposition
    1738              : !> \param matrix_a input matrix; will be overwritten
    1739              : !> \param general_a contains the result
    1740              : !> \author Florian Schiffmann
    1741              : ! **************************************************************************************************
    1742         8008 :    SUBROUTINE cp_fm_solve(matrix_a, general_a)
    1743              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix_a, general_a
    1744              : 
    1745              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_solve'
    1746              : 
    1747              :       INTEGER                                  :: handle, info, n, nrhs
    1748         8008 :       INTEGER, ALLOCATABLE, DIMENSION(:)       :: ipivot
    1749              :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a, a_general
    1750              : #if defined(__parallel)
    1751              :       INTEGER, DIMENSION(9)                    :: desca, descb
    1752              : #else
    1753              :       INTEGER                                  :: lda, ldb
    1754              : #endif
    1755              : 
    1756         8008 :       CALL timeset(routineN, handle)
    1757              : 
    1758         8008 :       a => matrix_a%local_data
    1759         8008 :       a_general => general_a%local_data
    1760         8008 :       n = matrix_a%matrix_struct%nrow_global
    1761         8008 :       nrhs = general_a%matrix_struct%ncol_global
    1762        24024 :       ALLOCATE (ipivot(n + matrix_a%matrix_struct%nrow_block))
    1763              : 
    1764              : #if defined(__parallel)
    1765        80080 :       desca(:) = matrix_a%matrix_struct%descriptor(:)
    1766        80080 :       descb(:) = general_a%matrix_struct%descriptor(:)
    1767         8008 :       CALL pdgetrf(n, n, a, 1, 1, desca, ipivot, info)
    1768              :       CALL pdgetrs("N", n, nrhs, a, 1, 1, desca, ipivot, a_general, &
    1769         8008 :                    1, 1, descb, info)
    1770              : 
    1771              : #else
    1772              :       lda = SIZE(a, 1)
    1773              :       ldb = SIZE(a_general, 1)
    1774              :       CALL dgetrf(n, n, a, lda, ipivot, info)
    1775              :       CALL dgetrs("N", n, nrhs, a, lda, ipivot, a_general, ldb, info)
    1776              : 
    1777              : #endif
    1778              :       ! info is allowed to be zero
    1779              :       ! this does just signal a zero diagonal element
    1780         8008 :       DEALLOCATE (ipivot)
    1781         8008 :       CALL timestop(handle)
    1782         8008 :    END SUBROUTINE
    1783              : 
    1784              : ! **************************************************************************************************
    1785              : !> \brief Convenience function. Computes the matrix multiplications needed
    1786              : !>        for the multiplication of complex matrices.
    1787              : !>        C = beta * C + alpha * ( A  ** transa ) * ( B ** transb )
    1788              : !> \param transa : 'N' -> normal   'T' -> transpose   'C' -> conjugate transpose
    1789              : !>      alpha,beta :: can be 0.0_dp and 1.0_dp
    1790              : !> \param transb ...
    1791              : !> \param m ...
    1792              : !> \param n ...
    1793              : !> \param k ...
    1794              : !> \param alpha ...
    1795              : !> \param A_re m x k matrix ( ! for transa = 'N'), real part
    1796              : !> \param A_im m x k matrix ( ! for transa = 'N'), imaginary part
    1797              : !> \param B_re k x n matrix ( ! for transa = 'N'), real part
    1798              : !> \param B_im k x n matrix ( ! for transa = 'N'), imaginary part
    1799              : !> \param beta ...
    1800              : !> \param C_re m x n matrix, real part
    1801              : !> \param C_im m x n matrix, imaginary part
    1802              : !> \param a_first_col ...
    1803              : !> \param a_first_row ...
    1804              : !> \param b_first_col : the k x n matrix starts at col b_first_col of matrix_b (avoid usage)
    1805              : !> \param b_first_row ...
    1806              : !> \param c_first_col ...
    1807              : !> \param c_first_row ...
    1808              : !> \author Samuel Andermatt
    1809              : !> \note
    1810              : !>      C should have no overlap with A, B
    1811              : ! **************************************************************************************************
    1812         4946 :    SUBROUTINE cp_complex_fm_gemm(transa, transb, m, n, k, alpha, A_re, A_im, B_re, B_im, beta, &
    1813              :                                  C_re, C_im, a_first_col, a_first_row, b_first_col, b_first_row, c_first_col, &
    1814              :                                  c_first_row)
    1815              :       CHARACTER(LEN=1), INTENT(IN)                       :: transa, transb
    1816              :       INTEGER, INTENT(IN)                                :: m, n, k
    1817              :       REAL(KIND=dp), INTENT(IN)                          :: alpha
    1818              :       TYPE(cp_fm_type), INTENT(IN)                       :: A_re, A_im, B_re, B_im
    1819              :       REAL(KIND=dp), INTENT(IN)                          :: beta
    1820              :       TYPE(cp_fm_type), INTENT(IN)                       :: C_re, C_im
    1821              :       INTEGER, INTENT(IN), OPTIONAL                      :: a_first_col, a_first_row, b_first_col, &
    1822              :                                                             b_first_row, c_first_col, c_first_row
    1823              : 
    1824              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_complex_fm_gemm'
    1825              : 
    1826              :       CHARACTER(LEN=1)                                   :: transa_real, transb_real
    1827              :       INTEGER                                            :: handle
    1828              :       REAL(KIND=dp)                                      :: sign_a, sign_b
    1829              : 
    1830         4946 :       CALL timeset(routineN, handle)
    1831              : 
    1832         4946 :       transa_real = transa
    1833         4946 :       transb_real = transb
    1834         4946 :       sign_a = 1.0_dp
    1835         4946 :       sign_b = 1.0_dp
    1836         4946 :       IF (transa == "C") THEN
    1837         3499 :          transa_real = "T"
    1838         3499 :          sign_a = -1.0_dp
    1839              :       END IF
    1840         4946 :       IF (transb == "C") THEN
    1841          279 :          transb_real = "T"
    1842          279 :          sign_b = -1.0_dp
    1843              :       END IF
    1844              : 
    1845              :       CALL cp_fm_gemm(transa_real, transb_real, m, n, k, alpha, A_re, B_re, beta, C_re, &
    1846              :                       a_first_col=a_first_col, &
    1847              :                       a_first_row=a_first_row, &
    1848              :                       b_first_col=b_first_col, &
    1849              :                       b_first_row=b_first_row, &
    1850              :                       c_first_col=c_first_col, &
    1851         4946 :                       c_first_row=c_first_row)
    1852              :       CALL cp_fm_gemm(transa_real, transb_real, m, n, k, -sign_a*sign_b*alpha, &
    1853              :                       A_im, B_im, 1.0_dp, C_re, &
    1854              :                       a_first_col=a_first_col, &
    1855              :                       a_first_row=a_first_row, &
    1856              :                       b_first_col=b_first_col, &
    1857              :                       b_first_row=b_first_row, &
    1858              :                       c_first_col=c_first_col, &
    1859         4946 :                       c_first_row=c_first_row)
    1860              :       CALL cp_fm_gemm(transa_real, transb_real, m, n, k, sign_b*alpha, A_re, B_im, beta, C_im, &
    1861              :                       a_first_col=a_first_col, &
    1862              :                       a_first_row=a_first_row, &
    1863              :                       b_first_col=b_first_col, &
    1864              :                       b_first_row=b_first_row, &
    1865              :                       c_first_col=c_first_col, &
    1866         4946 :                       c_first_row=c_first_row)
    1867              :       CALL cp_fm_gemm(transa_real, transb_real, m, n, k, sign_a*alpha, A_im, B_re, 1.0_dp, C_im, &
    1868              :                       a_first_col=a_first_col, &
    1869              :                       a_first_row=a_first_row, &
    1870              :                       b_first_col=b_first_col, &
    1871              :                       b_first_row=b_first_row, &
    1872              :                       c_first_col=c_first_col, &
    1873         4946 :                       c_first_row=c_first_row)
    1874              : 
    1875         4946 :       CALL timestop(handle)
    1876              : 
    1877         4946 :    END SUBROUTINE cp_complex_fm_gemm
    1878              : 
    1879              : ! **************************************************************************************************
    1880              : !> \brief inverts a matrix using LU decomposition
    1881              : !>        the input matrix will be overwritten
    1882              : !> \param matrix   : input a general square non-singular matrix, outputs its inverse
    1883              : !> \param info_out : optional, if present outputs the info from (p)zgetri
    1884              : !> \author Lianheng Tong
    1885              : ! **************************************************************************************************
    1886            0 :    SUBROUTINE cp_fm_lu_invert(matrix, info_out)
    1887              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix
    1888              :       INTEGER, INTENT(OUT), OPTIONAL           :: info_out
    1889              : 
    1890              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_lu_invert'
    1891              : 
    1892              :       INTEGER :: nrows_global, handle, info, lwork
    1893            0 :       INTEGER, DIMENSION(:), ALLOCATABLE       :: ipivot
    1894              :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: mat
    1895            0 :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: work
    1896              : #if defined(__parallel)
    1897              :       INTEGER                                  :: liwork
    1898              :       INTEGER, DIMENSION(9)                    :: desca
    1899            0 :       INTEGER, DIMENSION(:), ALLOCATABLE       :: iwork
    1900              : #else
    1901              :       INTEGER                                  :: lda
    1902              : #endif
    1903              : 
    1904            0 :       CALL timeset(routineN, handle)
    1905              : 
    1906            0 :       mat => matrix%local_data
    1907            0 :       nrows_global = matrix%matrix_struct%nrow_global
    1908            0 :       CPASSERT(nrows_global == matrix%matrix_struct%ncol_global)
    1909            0 :       ALLOCATE (ipivot(nrows_global))
    1910              :       ! do LU decomposition
    1911              : #if defined(__parallel)
    1912            0 :       desca = matrix%matrix_struct%descriptor
    1913              :       CALL pdgetrf(nrows_global, nrows_global, &
    1914            0 :                    mat, 1, 1, desca, ipivot, info)
    1915              : #else
    1916              :       lda = SIZE(mat, 1)
    1917              :       CALL dgetrf(nrows_global, nrows_global, &
    1918              :                   mat, lda, ipivot, info)
    1919              : #endif
    1920            0 :       IF (info /= 0) THEN
    1921            0 :          CALL cp_abort(__LOCATION__, "LU decomposition has failed")
    1922              :       END IF
    1923              :       ! do inversion
    1924              : #if defined(__parallel)
    1925            0 :       ALLOCATE (iwork(1))
    1926              :       CALL pdgetri(nrows_global, mat, 1, 1, desca, &
    1927            0 :                    ipivot, work, -1, iwork, -1, info)
    1928              :       lwork = INT(work(1))
    1929            0 :       DEALLOCATE (work)
    1930              :       ALLOCATE (work(lwork))
    1931              :       liwork = INT(iwork(1))
    1932              :       DEALLOCATE (iwork)
    1933              :       ALLOCATE (iwork(liwork))
    1934              :       CALL pdgetri(nrows_global, mat, 1, 1, desca, &
    1935              :                    ipivot, work, lwork, iwork, liwork, info)
    1936              :       DEALLOCATE (iwork)
    1937              : #else
    1938              :       CALL dgetri(nrows_global, mat, lda, &
    1939              :                   ipivot, work, -1, info)
    1940              :       lwork = INT(work(1))
    1941              :       DEALLOCATE (work)
    1942              :       ALLOCATE (work(lwork))
    1943              :       CALL dgetri(nrows_global, mat, lda, &
    1944              :                   ipivot, work, lwork, info)
    1945              : #endif
    1946              :       DEALLOCATE (work)
    1947              :       DEALLOCATE (ipivot)
    1948              : 
    1949              :       IF (PRESENT(info_out)) THEN
    1950              :          info_out = info
    1951              :       ELSE
    1952              :          IF (info /= 0) &
    1953              :             CALL cp_abort(__LOCATION__, "LU inversion has failed")
    1954              :       END IF
    1955              : 
    1956              :       CALL timestop(handle)
    1957              : 
    1958            0 :    END SUBROUTINE cp_fm_lu_invert
    1959              : 
    1960              : ! **************************************************************************************************
    1961              : !> \brief norm of matrix using (p)dlange
    1962              : !> \param matrix   : input a general matrix
    1963              : !> \param mode     : 'M' max abs element value,
    1964              : !>                   '1' or 'O' one norm, i.e. maximum column sum
    1965              : !>                   'I' infinity norm, i.e. maximum row sum
    1966              : !>                   'F' or 'E' Frobenius norm, i.e. sqrt of sum of all squares of elements
    1967              : !> \return : the norm according to mode
    1968              : !> \author Lianheng Tong
    1969              : ! **************************************************************************************************
    1970         1332 :    FUNCTION cp_fm_norm(matrix, mode) RESULT(res)
    1971              :       TYPE(cp_fm_type), INTENT(IN) :: matrix
    1972              :       CHARACTER, INTENT(IN) :: mode
    1973              :       REAL(KIND=dp) :: res
    1974              : 
    1975              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_norm'
    1976              : 
    1977              :       INTEGER :: nrows, ncols, handle, lwork, nrows_local, ncols_local
    1978              :       REAL(KIND=dp), DIMENSION(:, :), POINTER :: aa
    1979         1332 :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: work
    1980              : #if defined(__parallel)
    1981              :       INTEGER, DIMENSION(9) :: desca
    1982              : #else
    1983              :       INTEGER :: lda
    1984              : #endif
    1985              : 
    1986         1332 :       CALL timeset(routineN, handle)
    1987              : 
    1988              :       CALL cp_fm_get_info(matrix=matrix, &
    1989              :                           nrow_global=nrows, &
    1990              :                           ncol_global=ncols, &
    1991              :                           nrow_local=nrows_local, &
    1992         1332 :                           ncol_local=ncols_local)
    1993         1332 :       aa => matrix%local_data
    1994              : 
    1995              : #if defined(__parallel)
    1996        13320 :       desca = matrix%matrix_struct%descriptor
    1997              :       SELECT CASE (mode)
    1998              :       CASE ('M', 'm')
    1999         1332 :          lwork = 1
    2000              :       CASE ('1', 'O', 'o')
    2001         1332 :          lwork = ncols_local
    2002              :       CASE ('I', 'i')
    2003            0 :          lwork = nrows_local
    2004              :       CASE ('F', 'f', 'E', 'e')
    2005            0 :          lwork = 1
    2006              :       CASE DEFAULT
    2007         1332 :          CPABORT("mode input is not valid")
    2008              :       END SELECT
    2009         3996 :       ALLOCATE (work(lwork))
    2010         1332 :       res = pdlange(mode, nrows, ncols, aa, 1, 1, desca, work)
    2011         1332 :       DEALLOCATE (work)
    2012              : #else
    2013              :       SELECT CASE (mode)
    2014              :       CASE ('M', 'm')
    2015              :          lwork = 1
    2016              :       CASE ('1', 'O', 'o')
    2017              :          lwork = 1
    2018              :       CASE ('I', 'i')
    2019              :          lwork = nrows
    2020              :       CASE ('F', 'f', 'E', 'e')
    2021              :          lwork = 1
    2022              :       CASE DEFAULT
    2023              :          CPABORT("mode input is not valid")
    2024              :       END SELECT
    2025              :       ALLOCATE (work(lwork))
    2026              :       lda = SIZE(aa, 1)
    2027              :       res = dlange(mode, nrows, ncols, aa, lda, work)
    2028              :       DEALLOCATE (work)
    2029              : #endif
    2030              : 
    2031         1332 :       CALL timestop(handle)
    2032              : 
    2033         1332 :    END FUNCTION cp_fm_norm
    2034              : 
    2035              : ! **************************************************************************************************
    2036              : !> \brief trace of a matrix using pdlatra
    2037              : !> \param matrix   : input a square matrix
    2038              : !> \return : the trace
    2039              : !> \author Lianheng Tong
    2040              : ! **************************************************************************************************
    2041            0 :    FUNCTION cp_fm_latra(matrix) RESULT(res)
    2042              :       TYPE(cp_fm_type), INTENT(IN) :: matrix
    2043              :       REAL(KIND=dp) :: res
    2044              : 
    2045              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_latra'
    2046              : 
    2047              :       INTEGER :: nrows, ncols, handle
    2048              :       REAL(KIND=dp), DIMENSION(:, :), POINTER :: aa
    2049              : #if defined(__parallel)
    2050              :       INTEGER, DIMENSION(9) :: desca
    2051              : #else
    2052              :       INTEGER :: ii
    2053              : #endif
    2054              : 
    2055            0 :       CALL timeset(routineN, handle)
    2056              : 
    2057            0 :       nrows = matrix%matrix_struct%nrow_global
    2058            0 :       ncols = matrix%matrix_struct%ncol_global
    2059            0 :       CPASSERT(nrows == ncols)
    2060            0 :       aa => matrix%local_data
    2061              : 
    2062              : #if defined(__parallel)
    2063            0 :       desca = matrix%matrix_struct%descriptor
    2064            0 :       res = pdlatra(nrows, aa, 1, 1, desca)
    2065              : #else
    2066              :       res = 0.0_dp
    2067              :       DO ii = 1, nrows
    2068              :          res = res + aa(ii, ii)
    2069              :       END DO
    2070              : #endif
    2071              : 
    2072            0 :       CALL timestop(handle)
    2073              : 
    2074            0 :    END FUNCTION cp_fm_latra
    2075              : 
    2076              : ! **************************************************************************************************
    2077              : !> \brief compute a QR factorization with column pivoting of a M-by-N distributed matrix
    2078              : !>        sub( A ) = A(IA:IA+M-1,JA:JA+N-1)
    2079              : !> \param matrix   : input M-by-N distributed matrix sub( A ) which is to be factored
    2080              : !> \param tau      : scalar factors TAU of the elementary reflectors. TAU is tied to the distributed matrix A
    2081              : !> \param nrow ...
    2082              : !> \param ncol ...
    2083              : !> \param first_row ...
    2084              : !> \param first_col ...
    2085              : !> \author MI
    2086              : ! **************************************************************************************************
    2087           38 :    SUBROUTINE cp_fm_pdgeqpf(matrix, tau, nrow, ncol, first_row, first_col)
    2088              : 
    2089              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix
    2090              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: tau
    2091              :       INTEGER, INTENT(IN)                                :: nrow, ncol, first_row, first_col
    2092              : 
    2093              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_pdgeqpf'
    2094              : 
    2095              :       INTEGER                                            :: handle
    2096              :       INTEGER                                            :: info, lwork
    2097           38 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: ipiv
    2098              :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: a
    2099              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: work
    2100              : #if defined(__parallel)
    2101              :       INTEGER, DIMENSION(9) :: descc
    2102              : #else
    2103              :       INTEGER :: lda
    2104              : #endif
    2105              : 
    2106           38 :       CALL timeset(routineN, handle)
    2107              : 
    2108           38 :       a => matrix%local_data
    2109           38 :       lwork = -1
    2110          114 :       ALLOCATE (work(2*nrow))
    2111          114 :       ALLOCATE (ipiv(ncol))
    2112           38 :       info = 0
    2113              : 
    2114              : #if defined(__parallel)
    2115          380 :       descc(:) = matrix%matrix_struct%descriptor(:)
    2116              :       ! Call SCALAPACK routine to get optimal work dimension
    2117           38 :       CALL pdgeqpf(nrow, ncol, a, first_row, first_col, descc, ipiv, tau, work, lwork, info)
    2118           38 :       lwork = INT(work(1))
    2119           38 :       DEALLOCATE (work)
    2120          114 :       ALLOCATE (work(lwork))
    2121          254 :       tau = 0.0_dp
    2122           38 :       ipiv = 0
    2123              : 
    2124              :       ! Call SCALAPACK routine to get QR decomposition of CTs
    2125           38 :       CALL pdgeqpf(nrow, ncol, a, first_row, first_col, descc, ipiv, tau, work, lwork, info)
    2126              : #else
    2127              :       CPASSERT(first_row == 1 .AND. first_col == 1)
    2128              :       lda = SIZE(a, 1)
    2129              :       CALL dgeqp3(nrow, ncol, a, lda, ipiv, tau, work, lwork, info)
    2130              :       lwork = INT(work(1))
    2131              :       DEALLOCATE (work)
    2132              :       ALLOCATE (work(lwork))
    2133              :       tau = 0.0_dp
    2134              :       ipiv = 0
    2135              :       CALL dgeqp3(nrow, ncol, a, lda, ipiv, tau, work, lwork, info)
    2136              : #endif
    2137           38 :       CPASSERT(info == 0)
    2138              : 
    2139           38 :       DEALLOCATE (work)
    2140           38 :       DEALLOCATE (ipiv)
    2141              : 
    2142           38 :       CALL timestop(handle)
    2143              : 
    2144           38 :    END SUBROUTINE cp_fm_pdgeqpf
    2145              : 
    2146              : ! **************************************************************************************************
    2147              : !> \brief generates an M-by-N real distributed matrix Q denoting A(IA:IA+M-1,JA:JA+N-1)
    2148              : !>         with orthonormal columns, which is defined as the first N columns of a product of K
    2149              : !>         elementary reflectors of order M
    2150              : !> \param matrix : On entry, the j-th column must contain the vector which defines the elementary reflector
    2151              : !>                  as returned from PDGEQRF
    2152              : !>                 On exit it contains  the M-by-N distributed matrix Q
    2153              : !> \param tau :   contains the scalar factors TAU of elementary reflectors  as returned by PDGEQRF
    2154              : !> \param nrow ...
    2155              : !> \param first_row ...
    2156              : !> \param first_col ...
    2157              : !> \author MI
    2158              : ! **************************************************************************************************
    2159           38 :    SUBROUTINE cp_fm_pdorgqr(matrix, tau, nrow, first_row, first_col)
    2160              : 
    2161              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix
    2162              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: tau
    2163              :       INTEGER, INTENT(IN)                                :: nrow, first_row, first_col
    2164              : 
    2165              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_pdorgqr'
    2166              : 
    2167              :       INTEGER                                            :: handle
    2168              :       INTEGER                                            :: info, lwork
    2169              :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: a
    2170              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: work
    2171              : #if defined(__parallel)
    2172              :       INTEGER, DIMENSION(9) :: descc
    2173              : #else
    2174              :       INTEGER :: lda
    2175              : #endif
    2176              : 
    2177           38 :       CALL timeset(routineN, handle)
    2178              : 
    2179           38 :       a => matrix%local_data
    2180           38 :       lwork = -1
    2181          114 :       ALLOCATE (work(2*nrow))
    2182           38 :       info = 0
    2183              : 
    2184              : #if defined(__parallel)
    2185          380 :       descc(:) = matrix%matrix_struct%descriptor(:)
    2186              : 
    2187           38 :       CALL pdorgqr(nrow, nrow, nrow, a, first_row, first_col, descc, tau, work, lwork, info)
    2188           38 :       CPASSERT(info == 0)
    2189           38 :       lwork = INT(work(1))
    2190           38 :       DEALLOCATE (work)
    2191          114 :       ALLOCATE (work(lwork))
    2192              : 
    2193              :       ! Call SCALAPACK routine to get Q
    2194           38 :       CALL pdorgqr(nrow, nrow, nrow, a, first_row, first_col, descc, tau, work, lwork, info)
    2195              : #else
    2196              :       CPASSERT(first_row == 1 .AND. first_col == 1)
    2197              :       lda = SIZE(a, 1)
    2198              :       CALL dorgqr(nrow, nrow, nrow, a, lda, tau, work, lwork, info)
    2199              :       lwork = INT(work(1))
    2200              :       DEALLOCATE (work)
    2201              :       ALLOCATE (work(lwork))
    2202              :       CALL dorgqr(nrow, nrow, nrow, a, lda, tau, work, lwork, info)
    2203              : #endif
    2204           38 :       CPASSERT(INFO == 0)
    2205              : 
    2206           38 :       DEALLOCATE (work)
    2207           38 :       CALL timestop(handle)
    2208              : 
    2209           38 :    END SUBROUTINE cp_fm_pdorgqr
    2210              : 
    2211              : ! **************************************************************************************************
    2212              : !> \brief Applies a planar rotation defined by cs and sn to the i'th and j'th rows.
    2213              : !> \param matrix ...
    2214              : !> \param irow ...
    2215              : !> \param jrow ...
    2216              : !> \param cs cosine of the rotation angle
    2217              : !> \param sn sinus of the rotation angle
    2218              : !> \author Ole Schuett
    2219              : ! **************************************************************************************************
    2220        31928 :    SUBROUTINE cp_fm_rot_rows(matrix, irow, jrow, cs, sn)
    2221              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix
    2222              :       INTEGER, INTENT(IN)                      :: irow, jrow
    2223              :       REAL(dp), INTENT(IN)                     :: cs, sn
    2224              : 
    2225              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_rot_rows'
    2226              :       INTEGER                                  :: handle, ncol
    2227              : 
    2228              : #if defined(__parallel)
    2229              :       INTEGER                                  :: info, lwork
    2230              :       INTEGER, DIMENSION(9)                    :: desc
    2231        31928 :       REAL(dp), DIMENSION(:), ALLOCATABLE      :: work
    2232              : #endif
    2233        31928 :       CALL timeset(routineN, handle)
    2234        31928 :       CALL cp_fm_get_info(matrix, ncol_global=ncol)
    2235              : #if defined(__parallel)
    2236        31928 :       IF (1 /= matrix%matrix_struct%context%n_pid) THEN
    2237        31928 :          lwork = 2*ncol + 1
    2238        95784 :          ALLOCATE (work(lwork))
    2239       319280 :          desc(:) = matrix%matrix_struct%descriptor(:)
    2240              :          CALL pdrot(ncol, &
    2241              :                     matrix%local_data(1, 1), irow, 1, desc, ncol, &
    2242              :                     matrix%local_data(1, 1), jrow, 1, desc, ncol, &
    2243        31928 :                     cs, sn, work, lwork, info)
    2244        31928 :          CPASSERT(info == 0)
    2245        31928 :          DEALLOCATE (work)
    2246              :       ELSE
    2247              : #endif
    2248            0 :          CALL drot(ncol, matrix%local_data(irow, 1), ncol, matrix%local_data(jrow, 1), ncol, cs, sn)
    2249              : #if defined(__parallel)
    2250              :       END IF
    2251              : #endif
    2252        31928 :       CALL timestop(handle)
    2253        31928 :    END SUBROUTINE cp_fm_rot_rows
    2254              : 
    2255              : ! **************************************************************************************************
    2256              : !> \brief Applies a planar rotation defined by cs and sn to the i'th and j'th columnns.
    2257              : !> \param matrix ...
    2258              : !> \param icol ...
    2259              : !> \param jcol ...
    2260              : !> \param cs cosine of the rotation angle
    2261              : !> \param sn sinus of the rotation angle
    2262              : !> \author Ole Schuett
    2263              : ! **************************************************************************************************
    2264        36928 :    SUBROUTINE cp_fm_rot_cols(matrix, icol, jcol, cs, sn)
    2265              :       TYPE(cp_fm_type), INTENT(IN)             :: matrix
    2266              :       INTEGER, INTENT(IN)                      :: icol, jcol
    2267              :       REAL(dp), INTENT(IN)                     :: cs, sn
    2268              : 
    2269              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_rot_cols'
    2270              :       INTEGER                                  :: handle, nrow
    2271              : 
    2272              : #if defined(__parallel)
    2273              :       INTEGER                                  :: info, lwork
    2274              :       INTEGER, DIMENSION(9)                    :: desc
    2275        36928 :       REAL(dp), DIMENSION(:), ALLOCATABLE      :: work
    2276              : #endif
    2277        36928 :       CALL timeset(routineN, handle)
    2278        36928 :       CALL cp_fm_get_info(matrix, nrow_global=nrow)
    2279              : #if defined(__parallel)
    2280        36928 :       IF (1 /= matrix%matrix_struct%context%n_pid) THEN
    2281        36928 :          lwork = 2*nrow + 1
    2282       110784 :          ALLOCATE (work(lwork))
    2283       369280 :          desc(:) = matrix%matrix_struct%descriptor(:)
    2284              :          CALL pdrot(nrow, &
    2285              :                     matrix%local_data(1, 1), 1, icol, desc, 1, &
    2286              :                     matrix%local_data(1, 1), 1, jcol, desc, 1, &
    2287        36928 :                     cs, sn, work, lwork, info)
    2288        36928 :          CPASSERT(info == 0)
    2289        36928 :          DEALLOCATE (work)
    2290              :       ELSE
    2291              : #endif
    2292            0 :          CALL drot(nrow, matrix%local_data(1, icol), 1, matrix%local_data(1, jcol), 1, cs, sn)
    2293              : #if defined(__parallel)
    2294              :       END IF
    2295              : #endif
    2296        36928 :       CALL timestop(handle)
    2297        36928 :    END SUBROUTINE cp_fm_rot_cols
    2298              : 
    2299              : ! **************************************************************************************************
    2300              : !> \brief Orthonormalizes selected rows and columns of a full matrix, matrix_a
    2301              : !> \param matrix_a ...
    2302              : !> \param B ...
    2303              : !> \param nrows number of rows of matrix_a, optional, defaults to size(matrix_a,1)
    2304              : !> \param ncols number of columns of matrix_a, optional, defaults to size(matrix_a, 2)
    2305              : !> \param start_row starting index of rows, optional, defaults to 1
    2306              : !> \param start_col starting index of columns, optional, defaults to 1
    2307              : !> \param do_norm ...
    2308              : !> \param do_print ...
    2309              : ! **************************************************************************************************
    2310            0 :    SUBROUTINE cp_fm_Gram_Schmidt_orthonorm(matrix_a, B, nrows, ncols, start_row, start_col, &
    2311              :                                            do_norm, do_print)
    2312              : 
    2313              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix_a
    2314              :       REAL(kind=dp), DIMENSION(:, :), INTENT(OUT)        :: B
    2315              :       INTEGER, INTENT(IN), OPTIONAL                      :: nrows, ncols, start_row, start_col
    2316              :       LOGICAL, INTENT(IN), OPTIONAL                      :: do_norm, do_print
    2317              : 
    2318              :       CHARACTER(len=*), PARAMETER :: routineN = 'cp_fm_Gram_Schmidt_orthonorm'
    2319              : 
    2320              :       INTEGER :: end_col_global, end_col_local, end_row_global, end_row_local, handle, i, j, &
    2321              :                  j_col, ncol_global, ncol_local, nrow_global, nrow_local, start_col_global, &
    2322              :                  start_col_local, start_row_global, start_row_local, this_col, unit_nr
    2323            0 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    2324              :       LOGICAL                                            :: my_do_norm, my_do_print
    2325              :       REAL(KIND=dp)                                      :: norm
    2326            0 :       REAL(kind=dp), DIMENSION(:, :), POINTER            :: a
    2327              : 
    2328            0 :       CALL timeset(routineN, handle)
    2329              : 
    2330            0 :       my_do_norm = .TRUE.
    2331            0 :       IF (PRESENT(do_norm)) my_do_norm = do_norm
    2332              : 
    2333            0 :       my_do_print = .FALSE.
    2334            0 :       IF (PRESENT(do_print) .AND. (my_do_norm)) my_do_print = do_print
    2335              : 
    2336            0 :       unit_nr = -1
    2337            0 :       IF (my_do_print) THEN
    2338            0 :          unit_nr = cp_logger_get_default_unit_nr()
    2339            0 :          IF (unit_nr < 1) my_do_print = .FALSE.
    2340              :       END IF
    2341              : 
    2342            0 :       IF (SIZE(B) /= 0) THEN
    2343            0 :          IF (PRESENT(nrows)) THEN
    2344            0 :             nrow_global = nrows
    2345              :          ELSE
    2346            0 :             nrow_global = SIZE(B, 1)
    2347              :          END IF
    2348              : 
    2349            0 :          IF (PRESENT(ncols)) THEN
    2350            0 :             ncol_global = ncols
    2351              :          ELSE
    2352            0 :             ncol_global = SIZE(B, 2)
    2353              :          END IF
    2354              : 
    2355            0 :          IF (PRESENT(start_row)) THEN
    2356            0 :             start_row_global = start_row
    2357              :          ELSE
    2358              :             start_row_global = 1
    2359              :          END IF
    2360              : 
    2361            0 :          IF (PRESENT(start_col)) THEN
    2362            0 :             start_col_global = start_col
    2363              :          ELSE
    2364              :             start_col_global = 1
    2365              :          END IF
    2366              : 
    2367            0 :          end_row_global = start_row_global + nrow_global - 1
    2368            0 :          end_col_global = start_col_global + ncol_global - 1
    2369              : 
    2370              :          CALL cp_fm_get_info(matrix=matrix_a, &
    2371              :                              nrow_global=nrow_global, ncol_global=ncol_global, &
    2372              :                              nrow_local=nrow_local, ncol_local=ncol_local, &
    2373            0 :                              row_indices=row_indices, col_indices=col_indices)
    2374            0 :          IF (end_row_global > nrow_global) THEN
    2375              :             end_row_global = nrow_global
    2376              :          END IF
    2377            0 :          IF (end_col_global > ncol_global) THEN
    2378              :             end_col_global = ncol_global
    2379              :          END IF
    2380              : 
    2381              :          ! find out row/column indices of locally stored matrix elements that
    2382              :          ! needs to be copied.
    2383              :          ! Arrays row_indices and col_indices are assumed to be sorted in
    2384              :          ! ascending order
    2385            0 :          DO start_row_local = 1, nrow_local
    2386            0 :             IF (row_indices(start_row_local) >= start_row_global) EXIT
    2387              :          END DO
    2388              : 
    2389            0 :          DO end_row_local = start_row_local, nrow_local
    2390            0 :             IF (row_indices(end_row_local) > end_row_global) EXIT
    2391              :          END DO
    2392            0 :          end_row_local = end_row_local - 1
    2393              : 
    2394            0 :          DO start_col_local = 1, ncol_local
    2395            0 :             IF (col_indices(start_col_local) >= start_col_global) EXIT
    2396              :          END DO
    2397              : 
    2398            0 :          DO end_col_local = start_col_local, ncol_local
    2399            0 :             IF (col_indices(end_col_local) > end_col_global) EXIT
    2400              :          END DO
    2401            0 :          end_col_local = end_col_local - 1
    2402              : 
    2403            0 :          a => matrix_a%local_data
    2404              : 
    2405            0 :          this_col = col_indices(start_col_local) - start_col_global + 1
    2406              : 
    2407            0 :          B(:, this_col) = a(:, start_col_local)
    2408              : 
    2409            0 :          IF (my_do_norm) THEN
    2410            0 :             norm = SQRT(accurate_dot_product(B(:, this_col), B(:, this_col)))
    2411            0 :             B(:, this_col) = B(:, this_col)/norm
    2412            0 :             IF (my_do_print) WRITE (unit_nr, '(I3,F8.3)') this_col, norm
    2413              :          END IF
    2414              : 
    2415            0 :          DO i = start_col_local + 1, end_col_local
    2416            0 :             this_col = col_indices(i) - start_col_global + 1
    2417            0 :             B(:, this_col) = a(:, i)
    2418            0 :             DO j = start_col_local, i - 1
    2419            0 :                j_col = col_indices(j) - start_col_global + 1
    2420              :                B(:, this_col) = B(:, this_col) - &
    2421              :                                 accurate_dot_product(B(:, j_col), B(:, this_col))* &
    2422            0 :                                 B(:, j_col)/accurate_dot_product(B(:, j_col), B(:, j_col))
    2423              :             END DO
    2424              : 
    2425            0 :             IF (my_do_norm) THEN
    2426            0 :                norm = SQRT(accurate_dot_product(B(:, this_col), B(:, this_col)))
    2427            0 :                B(:, this_col) = B(:, this_col)/norm
    2428            0 :                IF (my_do_print) WRITE (unit_nr, '(I3,F8.3)') this_col, norm
    2429              :             END IF
    2430              : 
    2431              :          END DO
    2432            0 :          CALL matrix_a%matrix_struct%para_env%sum(B)
    2433              :       END IF
    2434              : 
    2435            0 :       CALL timestop(handle)
    2436              : 
    2437            0 :    END SUBROUTINE cp_fm_Gram_Schmidt_orthonorm
    2438              : 
    2439              : ! **************************************************************************************************
    2440              : !> \brief Cholesky decomposition
    2441              : !> \param fm_matrix ...
    2442              : !> \param n ...
    2443              : !> \param uplo triangular format; defaults to 'U'
    2444              : ! **************************************************************************************************
    2445        11485 :    SUBROUTINE cp_fm_potrf(fm_matrix, n, uplo)
    2446              :       TYPE(cp_fm_type)                         :: fm_matrix
    2447              :       INTEGER, INTENT(IN)                      :: n
    2448              :       CHARACTER, INTENT(IN), OPTIONAL          :: uplo
    2449              : 
    2450              :       CHARACTER                                :: myuplo
    2451              :       INTEGER                                  :: info
    2452        11485 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
    2453              : #if defined(__parallel)
    2454              :       INTEGER, DIMENSION(9)                    :: desca
    2455              : #endif
    2456              : 
    2457        11485 :       myuplo = 'U'
    2458            0 :       IF (PRESENT(uplo)) myuplo = uplo
    2459              : 
    2460        11485 :       a => fm_matrix%local_data
    2461              : #if defined(__parallel)
    2462       114850 :       desca(:) = fm_matrix%matrix_struct%descriptor(:)
    2463        11485 :       CALL pdpotrf(myuplo, n, a(1, 1), 1, 1, desca, info)
    2464              : #else
    2465              :       CALL dpotrf(myuplo, n, a(1, 1), SIZE(a, 1), info)
    2466              : #endif
    2467        11485 :       IF (info /= 0) &
    2468            0 :          CPABORT("Cholesky decomposition failed. Matrix ill-conditioned?")
    2469              : 
    2470        11485 :    END SUBROUTINE cp_fm_potrf
    2471              : 
    2472              : ! **************************************************************************************************
    2473              : !> \brief Invert trianguar matrix
    2474              : !> \param fm_matrix the matrix to invert (triangular matrix according to uplo)
    2475              : !> \param n size of the matrix to invert
    2476              : !> \param uplo triangular format; defaults to 'U'
    2477              : ! **************************************************************************************************
    2478        10569 :    SUBROUTINE cp_fm_potri(fm_matrix, n, uplo)
    2479              :       TYPE(cp_fm_type)                         :: fm_matrix
    2480              :       INTEGER, INTENT(IN)                      :: n
    2481              :       CHARACTER, INTENT(IN), OPTIONAL          :: uplo
    2482              : 
    2483              :       CHARACTER                                :: myuplo
    2484        10569 :       REAL(KIND=dp), DIMENSION(:, :), POINTER  :: a
    2485              :       INTEGER                                  :: info
    2486              : #if defined(__parallel)
    2487              :       INTEGER, DIMENSION(9)                    :: desca
    2488              : #endif
    2489              : 
    2490        10569 :       myuplo = 'U'
    2491            0 :       IF (PRESENT(uplo)) myuplo = uplo
    2492              : 
    2493        10569 :       a => fm_matrix%local_data
    2494              : #if defined(__parallel)
    2495       105690 :       desca(:) = fm_matrix%matrix_struct%descriptor(:)
    2496        10569 :       CALL pdpotri(myuplo, n, a(1, 1), 1, 1, desca, info)
    2497              : #else
    2498              :       CALL dpotri(myuplo, n, a(1, 1), SIZE(a, 1), info)
    2499              : #endif
    2500        10569 :       CPASSERT(info == 0)
    2501        10569 :    END SUBROUTINE cp_fm_potri
    2502              : 
    2503              : ! **************************************************************************************************
    2504              : !> \brief Calculates
    2505              : !>        yv = alpha*amat*xv + beta*yv
    2506              : !>        where amat: fm matrix
    2507              : !>              xv  : vector replicated
    2508              : !>              yv  : vector replicated
    2509              : !>        Defaults: alpha = 1, beta = 0
    2510              : ! **************************************************************************************************
    2511        26126 :    SUBROUTINE cp_fm_matvec(amat, xv, yv, alpha, beta)
    2512              :       TYPE(cp_fm_type), INTENT(IN)                   :: amat
    2513              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)        :: xv
    2514              :       REAL(KIND=dp), DIMENSION(:), INTENT(INOUT)     :: yv
    2515              :       REAL(KIND=dp), OPTIONAL, INTENT(IN)            :: alpha, beta
    2516              : 
    2517              :       INTEGER                                        :: na, nc, nx, ny
    2518              :       REAL(KIND=dp)                                  :: aval, bval
    2519              : #if defined(__parallel)
    2520              :       INTEGER                                        :: nrl, ncl, ic, ir
    2521        26126 :       INTEGER, DIMENSION(:), POINTER                 :: rind, cind
    2522        26126 :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE       :: xvl, yvl, yvm
    2523              : #endif
    2524              : 
    2525        26126 :       aval = 1.0_dp
    2526        24328 :       IF (PRESENT(alpha)) aval = alpha
    2527        26126 :       bval = 0.0_dp
    2528        26126 :       IF (PRESENT(beta)) bval = beta
    2529              : 
    2530        26126 :       CALL cp_fm_get_info(amat, nrow_global=na, ncol_global=nc)
    2531        26126 :       nx = SIZE(xv)
    2532        26126 :       ny = SIZE(yv)
    2533        26126 :       IF ((nx /= ny) .OR. (nc /= nx)) THEN
    2534            0 :          CPABORT("cp_fm_matvec: incompatible dimensions")
    2535              :       END IF
    2536              : #if defined(__parallel)
    2537              :       CALL cp_fm_get_info(amat, nrow_local=nrl, ncol_local=ncl, &
    2538        26126 :                           row_indices=rind, col_indices=cind)
    2539       182882 :       ALLOCATE (xvl(ncl), yvl(nrl), yvm(ny))
    2540       543482 :       DO ic = 1, ncl
    2541       543482 :          xvl(ic) = xv(cind(ic))
    2542              :       END DO
    2543        26126 :       yvl = 0.0_dp
    2544        26126 :       IF (nrl > 0 .AND. ncl > 0) THEN
    2545     29709080 :          yvl(1:nrl) = MATMUL(amat%local_data(1:nrl, 1:ncl), xvl)
    2546              :       END IF
    2547        26126 :       yvm = 0.0_dp
    2548       284804 :       DO ir = 1, nrl
    2549       284804 :          yvm(rind(ir)) = yvl(ir)
    2550              :       END DO
    2551        26126 :       CALL amat%matrix_struct%para_env%sum(yvm)
    2552        26126 :       IF (bval == 0.0_dp) THEN
    2553       449204 :          yv = aval*yvm
    2554              :       ELSE
    2555        94278 :          yv = bval*yv + aval*yvm
    2556              :       END IF
    2557              : #else
    2558              :       IF (bval == 0.0_dp) THEN
    2559              :          yv = aval*MATMUL(amat%local_data, xv)
    2560              :       ELSE
    2561              :          yv = bval*yv + aval*MATMUL(amat%local_data, xv)
    2562              :       END IF
    2563              : #endif
    2564              : 
    2565       104504 :    END SUBROUTINE cp_fm_matvec
    2566              : 
    2567              : ! **************************************************************************************************
    2568              : !> \brief Adds a scalar to the diagonal of a distributed real full matrix.
    2569              : !> \param matrix input/output matrix
    2570              : !> \param alpha scalar added to each diagonal element
    2571              : !> \param n_active optional number of physical rows/columns
    2572              : ! **************************************************************************************************
    2573        52258 :    SUBROUTINE cp_fm_add_on_diag(matrix, alpha, n_active)
    2574              :       TYPE(cp_fm_type), INTENT(IN)                       :: matrix
    2575              :       REAL(KIND=dp), INTENT(IN)                          :: alpha
    2576              :       INTEGER, INTENT(IN), OPTIONAL                      :: n_active
    2577              : 
    2578              :       INTEGER                                            :: i_global, n_diag, ncol_global, nrow_global
    2579              : #if defined(__parallel)
    2580              :       INTEGER                                            :: icol_local, ipcol, iprow, irow, irow_local, &
    2581              :                                                             mypcol, myprow, npcol, nprow, nrow_local
    2582              :       INTEGER, DIMENSION(9)                              :: desca
    2583        26129 :       INTEGER, DIMENSION(:), POINTER                     :: row_indices
    2584              :       TYPE(cp_blacs_env_type), POINTER                   :: context
    2585              : #endif
    2586              : 
    2587        26129 :       CALL cp_fm_get_info(matrix, nrow_global=nrow_global, ncol_global=ncol_global)
    2588              : 
    2589        26129 :       n_diag = MIN(nrow_global, ncol_global)
    2590        26129 :       IF (PRESENT(n_active)) n_diag = n_active
    2591        26129 :       CPASSERT(n_diag >= 0 .AND. n_diag <= MIN(nrow_global, ncol_global))
    2592              : 
    2593              : #if defined(__parallel)
    2594        26129 :       CALL cp_fm_get_info(matrix, nrow_local=nrow_local, row_indices=row_indices)
    2595        26129 :       context => matrix%matrix_struct%context
    2596        26129 :       myprow = context%mepos(1)
    2597        26129 :       mypcol = context%mepos(2)
    2598        26129 :       nprow = context%num_pe(1)
    2599        26129 :       npcol = context%num_pe(2)
    2600       261290 :       desca(:) = matrix%matrix_struct%descriptor(:)
    2601              : 
    2602      1144708 :       DO irow = 1, nrow_local
    2603      1118579 :          i_global = row_indices(irow)
    2604      1118579 :          IF (i_global > n_diag) EXIT
    2605              :          CALL infog2l(i_global, i_global, desca, nprow, npcol, myprow, mypcol, &
    2606      1118579 :                       irow_local, icol_local, iprow, ipcol)
    2607      1144708 :          IF ((iprow == myprow) .AND. (ipcol == mypcol)) THEN
    2608      1117971 :             matrix%local_data(irow_local, icol_local) = matrix%local_data(irow_local, icol_local) + alpha
    2609              :          END IF
    2610              :       END DO
    2611              : #else
    2612              :       DO i_global = 1, n_diag
    2613              :          matrix%local_data(i_global, i_global) = matrix%local_data(i_global, i_global) + alpha
    2614              :       END DO
    2615              : #endif
    2616              : 
    2617        26129 :    END SUBROUTINE cp_fm_add_on_diag
    2618              : 
    2619              : END MODULE cp_fm_basic_linalg
        

Generated by: LCOV version 2.0-1