(git:3d5d2e5)
Loading...
Searching...
No Matches
cp_fm_diag.F
Go to the documentation of this file.
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 used for collecting some of the diagonalization schemes available for
10!> cp_fm_type. cp_fm_power also moved here as it is very related
11!> \note
12!> first version : most routines imported
13!> \par History
14!> - unused Jacobi routines removed, cosmetics (05.04.06,MK)
15!> \author Joost VandeVondele (2003-08)
16! **************************************************************************************************
21 cp_fm_gemm, &
23 cp_fm_syrk, &
30 USE cp_fm_elpa, ONLY: cp_fm_diag_elpa, &
37#if defined(__DLAF)
40#endif
41 USE cp_fm_types, ONLY: cp_fm_get_info, &
44 cp_fm_type, &
65 USE kinds, ONLY: default_string_length, &
66 dp
67 USE machine, ONLY: default_output_unit, &
70#if defined (__parallel)
72#endif
73#if defined (__HAS_IEEE_EXCEPTIONS)
74 USE ieee_exceptions, ONLY: ieee_get_halting_mode, &
75 ieee_set_halting_mode, &
76 ieee_all
77#endif
78#include "../base/base_uses.f90"
79
80 IMPLICIT NONE
81 PRIVATE
82
83 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'cp_fm_diag'
84
85 REAL(kind=dp), PARAMETER, PUBLIC :: eps_check_diag_default = 5.0e-14_dp
86
87 ! Placeholder eigenvalue for linearly dependent modes removed by canonical orthogonalization.
88 ! Pushes unphysical states above the real spectrum so downstream routines can ignore them.
89 REAL(kind=dp), PARAMETER, PUBLIC :: set_removed_eigval_to = 10000.0_dp
90
91 ! The following saved variables are diagonalization global
92 ! Stores the default library for diagonalization
93 INTEGER, SAVE, PUBLIC :: diag_type = 0
94 ! Whether the diagonalization library was requested explicitly (e.g. via PREFERRED_DIAG_LIBRARY);
95 ! complex matrices use ELPA only in that case.
96 LOGICAL, SAVE, PUBLIC :: diag_lib_explicit = .false.
97 ! Minimum number of eigenvectors for the use of the ELPA eigensolver.
98 ! The ScaLAPACK eigensolver is used as fallback for all smaller cases.
99 INTEGER, SAVE, PUBLIC :: elpa_neigvec_min = 0
100 ! Minimum matrix size for the use of the cuSOLVERMp eigensolver.
101 ! Smaller matrices use the ScaLAPACK fallback to avoid GPU launch overheads.
102 INTEGER, PARAMETER, PUBLIC :: cusolver_n_min = 64
103 ! Minimum number of eigenvectors for the use of the DLAF eigensolver.
104 ! The ScaLAPACK eigensolver is used as fallback for all smaller cases.
105 INTEGER, SAVE, PUBLIC :: dlaf_neigvec_min = 0
106 LOGICAL, SAVE, PUBLIC :: direct_generalized_diagonalization = .false.
107 ! Threshold value for the orthonormality check of the eigenvectors obtained
108 ! after a diagonalization. A negative value disables the check.
109 REAL(kind=dp), SAVE :: eps_check_diag = -1.0_dp
110
111 ! Constants for the diag_type above
112 INTEGER, PARAMETER, PUBLIC :: fm_diag_type_scalapack = 101, &
113 fm_diag_type_elpa = 102, &
114 fm_diag_type_cusolver = 103, &
116#if defined(__CUSOLVERMP)
117 INTEGER, PARAMETER, PUBLIC :: fm_diag_type_default = fm_diag_type_cusolver
118#elif defined(__ELPA)
119 INTEGER, PARAMETER, PUBLIC :: fm_diag_type_default = fm_diag_type_elpa
120#else
121 INTEGER, PARAMETER, PUBLIC :: fm_diag_type_default = fm_diag_type_scalapack
122#endif
123
124 ! Public subroutines
125 PUBLIC :: choose_eigv_solver, &
127 cp_fm_power, &
128 cp_fm_syevd, &
129 cp_fm_syevx, &
130 cp_fm_svd, &
131 cp_fm_geeig, &
135 diag_init, &
137
138CONTAINS
139
140! **************************************************************************************************
141!> \brief Setup the diagonalization library to be used
142!> \param diag_lib diag_library flag from GLOBAL section in input
143!> \param fallback_applied .TRUE. if support for the requested library was not compiled-in and fallback
144!> to ScaLAPACK was applied, .FALSE. otherwise.
145!> \param elpa_kernel integer that determines which ELPA kernel to use for diagonalization
146!> \param elpa_c_kernel ...
147!> \param elpa_neigvec_min_input ...
148!> \param elpa_qr logical that determines if ELPA should try to use QR to accelerate the
149!> diagonalization procedure of suitably sized matrices
150!> \param elpa_print logical that determines if information about the ELPA diagonalization should
151!> be printed
152!> \param elpa_one_stage logical that enables the one-stage solver
153!> \param dlaf_neigvec_min_input ...
154!> \param eps_check_diag_input ...
155!> \param direct_generalized_diagonalization_input ...
156!> \param diag_lib_explicit_input ...
157!> \par History
158!> - Add support for DLA-Future (05.09.2023, RMeli)
159!> \author MI 11.2013
160! **************************************************************************************************
161 SUBROUTINE diag_init(diag_lib, fallback_applied, elpa_kernel, elpa_c_kernel, elpa_neigvec_min_input, &
162 elpa_qr, elpa_print, elpa_one_stage, dlaf_neigvec_min_input, eps_check_diag_input, &
163 direct_generalized_diagonalization_input, diag_lib_explicit_input)
164 CHARACTER(LEN=*), INTENT(IN) :: diag_lib
165 LOGICAL, INTENT(OUT) :: fallback_applied
166 INTEGER, INTENT(IN) :: elpa_kernel
167 INTEGER, INTENT(IN), OPTIONAL :: elpa_c_kernel
168 INTEGER, INTENT(IN) :: elpa_neigvec_min_input
169 LOGICAL, INTENT(IN) :: elpa_qr, elpa_print, elpa_one_stage
170 INTEGER, INTENT(IN) :: dlaf_neigvec_min_input
171 REAL(kind=dp), INTENT(IN) :: eps_check_diag_input
172 LOGICAL, INTENT(IN), OPTIONAL :: direct_generalized_diagonalization_input, &
173 diag_lib_explicit_input
174
175 LOGICAL, SAVE :: initialized = .false.
176
177 fallback_applied = .false.
178
179 IF (diag_lib == "ScaLAPACK") THEN
181 ELSE IF (diag_lib == "ELPA") THEN
182#if defined (__ELPA)
183 ! ELPA is requested and available
185#else
186 ! ELPA library requested but not linked, switch back to SL
188 fallback_applied = .true.
189#endif
190 ELSE IF (diag_lib == "cuSOLVER") THEN
192 ELSE IF (diag_lib == "DLAF") THEN
193#if defined (__DLAF)
195#else
196 cpabort("ERROR in diag_init: CP2K was not compiled with DLA-Future support")
197#endif
198 ELSE
199 cpabort("ERROR in diag_init: Initialization of unknown diagonalization library requested")
200 END IF
201
202 ! Complex matrices use ELPA only when the diagonalization library was
203 ! requested explicitly (e.g. via PREFERRED_DIAG_LIBRARY ELPA).
204 diag_lib_explicit = .true.
205 IF (PRESENT(diag_lib_explicit_input)) diag_lib_explicit = diag_lib_explicit_input
206
207 ! Initialization of requested diagonalization library
208 IF (.NOT. initialized .AND. diag_type == fm_diag_type_elpa) THEN
209 CALL initialize_elpa_library(one_stage=elpa_one_stage, qr=elpa_qr, should_print=elpa_print)
210 CALL set_elpa_kernel(elpa_kernel)
211 IF (PRESENT(elpa_c_kernel)) CALL set_elpa_c_kernel(elpa_c_kernel)
212 initialized = .true.
213 END IF
214#if defined(__DLAF)
215 IF (.NOT. initialized .AND. diag_type == fm_diag_type_dlaf) THEN
216 CALL cp_dlaf_initialize()
217 initialized = .true.
218 END IF
219 dlaf_neigvec_min = dlaf_neigvec_min_input
220#else
221 mark_used(dlaf_neigvec_min_input)
222#endif
223
224 elpa_neigvec_min = elpa_neigvec_min_input
225 eps_check_diag = eps_check_diag_input
226 IF (PRESENT(direct_generalized_diagonalization_input)) THEN
227 direct_generalized_diagonalization = direct_generalized_diagonalization_input
228 ELSE
230 END IF
231
232 END SUBROUTINE diag_init
233
234! **************************************************************************************************
235!> \brief Finalize the diagonalization library
236! **************************************************************************************************
237 SUBROUTINE diag_finalize()
238#if defined (__ELPA)
241#endif
242#if defined (__DLAF)
244 CALL cp_dlaf_finalize()
245#endif
246 END SUBROUTINE diag_finalize
247
248! **************************************************************************************************
249!> \brief Choose the Eigensolver depending on which library is available
250!> ELPA seems to be unstable for small systems
251!> \param matrix ...
252!> \param eigenvectors ...
253!> \param eigenvalues ...
254!> \param info ...
255!> \par info If present returns error code and prevents program stops.
256!> Works currently only for cp_fm_syevd with ScaLAPACK.
257!> Other solvers will end the program regardless of PRESENT(info).
258!> \par History
259!> - Do not use ELPA for small matrices and use instead ScaLAPACK as fallback (10.05.2021, MK)
260! **************************************************************************************************
261 SUBROUTINE choose_eigv_solver(matrix, eigenvectors, eigenvalues, info)
262
263 TYPE(cp_fm_type), INTENT(IN) :: matrix, eigenvectors
264 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
265 INTEGER, INTENT(OUT), OPTIONAL :: info
266
267 CHARACTER(LEN=*), PARAMETER :: routinen = 'choose_eigv_solver'
268
269 ! Sample peak memory
270 CALL m_memory()
271
272 IF (PRESENT(info)) info = 0 ! Default for solvers that do not return an info.
273
275 CALL cp_fm_syevd(matrix, eigenvectors, eigenvalues, info)
276
277 ELSE IF (diag_type == fm_diag_type_elpa) THEN
278 IF (matrix%matrix_struct%nrow_global < elpa_neigvec_min) THEN
279 ! We don't trust ELPA with very small matrices.
280 CALL cp_fm_syevd(matrix, eigenvectors, eigenvalues, info)
281 ELSE
282 CALL cp_fm_diag_elpa(matrix, eigenvectors, eigenvalues)
283 END IF
284
285 ELSE IF (diag_type == fm_diag_type_cusolver) THEN
286 IF (matrix%matrix_struct%nrow_global < cusolver_n_min) THEN
287 ! We don't trust cuSolver with very small matrices.
288 CALL cp_fm_syevd(matrix, eigenvectors, eigenvalues, info)
289 ELSE
290 CALL cp_fm_diag_cusolver(matrix, eigenvectors, eigenvalues)
291 END IF
292
293#if defined(__DLAF)
294 ELSE IF (diag_type == fm_diag_type_dlaf) THEN
295 IF (matrix%matrix_struct%nrow_global < dlaf_neigvec_min) THEN
296 ! Use ScaLAPACK for small matrices
297 CALL cp_fm_syevd(matrix, eigenvectors, eigenvalues, info)
298 ELSE
299 CALL cp_fm_diag_dlaf(matrix, eigenvectors, eigenvalues)
300 END IF
301#endif
302
303 ELSE
304 cpabort("ERROR in "//routinen//": Invalid diagonalization type requested")
305 END IF
306
307 CALL check_diag(matrix, eigenvectors, nvec=SIZE(eigenvalues))
308
309 END SUBROUTINE choose_eigv_solver
310
311! **************************************************************************************************
312!> \brief Return whether diagonalization checks should be performed.
313!> \return ...
314! **************************************************************************************************
315 FUNCTION diag_check_requested() RESULT(check_requested)
316 LOGICAL :: check_requested
317
318#if defined(__CHECK_DIAG)
319 check_requested = .true.
320#else
321 check_requested = eps_check_diag >= 0.0_dp
322#endif
323
324 END FUNCTION diag_check_requested
325
326! **************************************************************************************************
327!> \brief Return the warning threshold for diagonalization checks.
328!> \return ...
329! **************************************************************************************************
330 FUNCTION diag_check_warning_threshold() RESULT(eps_warning)
331 REAL(kind=dp) :: eps_warning
332
333 eps_warning = eps_check_diag_default
334 IF (eps_check_diag >= 0.0_dp) THEN
335 eps_warning = eps_check_diag
336 END IF
337
339
340! **************************************************************************************************
341!> \brief Check result of diagonalization, i.e. the orthonormality of the eigenvectors
342!> \param matrix Work matrix
343!> \param eigenvectors Eigenvectors to be checked
344!> \param nvec ...
345! **************************************************************************************************
346 SUBROUTINE check_diag(matrix, eigenvectors, nvec)
347
348 TYPE(cp_fm_type), INTENT(IN) :: matrix, eigenvectors
349 INTEGER, INTENT(IN) :: nvec
350
351 CHARACTER(LEN=*), PARAMETER :: routinen = 'check_diag'
352
353 CHARACTER(LEN=default_string_length) :: diag_type_name
354 REAL(kind=dp) :: eps, eps_abort, eps_warning, gold, &
355 test
356 INTEGER :: handle, i, j, ncol, nrow, output_unit
357 LOGICAL :: check_eigenvectors
358#if defined(__parallel)
359 TYPE(cp_blacs_env_type), POINTER :: context
360 INTEGER :: il, jl, ipcol, iprow, &
361 mypcol, myprow, npcol, nprow
362 INTEGER, DIMENSION(9) :: desca
363#endif
364
365 CALL timeset(routinen, handle)
366
367 output_unit = default_output_unit
368 check_eigenvectors = diag_check_requested()
369 eps_warning = diag_check_warning_threshold()
370 eps_abort = 10.0_dp*eps_warning
371
372 gold = 0.0_dp
373 test = 0.0_dp
374 eps = 0.0_dp
375
376 IF (check_eigenvectors) THEN
377#if defined(__parallel)
378 nrow = eigenvectors%matrix_struct%nrow_global
379 ncol = min(eigenvectors%matrix_struct%ncol_global, nvec)
380 CALL cp_fm_gemm("T", "N", ncol, ncol, nrow, 1.0_dp, eigenvectors, eigenvectors, 0.0_dp, matrix)
381 context => matrix%matrix_struct%context
382 myprow = context%mepos(1)
383 mypcol = context%mepos(2)
384 nprow = context%num_pe(1)
385 npcol = context%num_pe(2)
386 desca(:) = matrix%matrix_struct%descriptor(:)
387 outer: DO j = 1, ncol
388 DO i = 1, ncol
389 CALL infog2l(i, j, desca, nprow, npcol, myprow, mypcol, il, jl, iprow, ipcol)
390 IF ((iprow == myprow) .AND. (ipcol == mypcol)) THEN
391 gold = merge(0.0_dp, 1.0_dp, i /= j)
392 test = matrix%local_data(il, jl)
393 eps = abs(test - gold)
394 IF (eps > eps_warning) EXIT outer
395 END IF
396 END DO
397 END DO outer
398#else
399 nrow = SIZE(eigenvectors%local_data, 1)
400 ncol = min(SIZE(eigenvectors%local_data, 2), nvec)
401 CALL dgemm("T", "N", ncol, ncol, nrow, 1.0_dp, &
402 eigenvectors%local_data(1, 1), nrow, &
403 eigenvectors%local_data(1, 1), nrow, &
404 0.0_dp, matrix%local_data(1, 1), nrow)
405 outer: DO j = 1, ncol
406 DO i = 1, ncol
407 gold = merge(0.0_dp, 1.0_dp, i /= j)
408 test = matrix%local_data(i, j)
409 eps = abs(test - gold)
410 IF (eps > eps_warning) EXIT outer
411 END DO
412 END DO outer
413#endif
414 IF (eps > eps_warning) THEN
416 diag_type_name = "SYEVD"
417 ELSE IF (diag_type == fm_diag_type_elpa) THEN
418 diag_type_name = "ELPA"
419 ELSE IF (diag_type == fm_diag_type_cusolver) THEN
420 diag_type_name = "CUSOLVER"
421 ELSE IF (diag_type == fm_diag_type_dlaf) THEN
422 diag_type_name = "DLAF"
423 ELSE
424 cpabort("Unknown diag_type")
425 END IF
426 WRITE (unit=output_unit, fmt="(/,T2,A,/,T2,A,I0,A,I0,A,F0.15,/,T2,A,F0.0,A,ES10.3)") &
427 "The eigenvectors returned by "//trim(diag_type_name)//" are not orthonormal", &
428 "Matrix element (", i, ", ", j, ") = ", test, &
429 "The deviation from the expected value ", gold, " is", eps
430 IF (eps > eps_abort) THEN
431 cpabort("ERROR in "//routinen//": Check of matrix diagonalization failed")
432 ELSE
433 cpwarn("Check of matrix diagonalization failed in routine "//routinen)
434 END IF
435 END IF
436 END IF
437
438 CALL timestop(handle)
439
440 END SUBROUTINE check_diag
441
442! **************************************************************************************************
443!> \brief Check C^T*S*C = I for a generalized eigenvalue problem.
444!> \param overlap original overlap matrix S; used as work matrix and overwritten
445!> \param eigenvectors eigenvectors C to be checked
446!> \param scratch work matrix
447!> \param nvec ...
448! **************************************************************************************************
449 SUBROUTINE check_generalized_diag(overlap, eigenvectors, scratch, nvec)
450
451 TYPE(cp_fm_type), INTENT(IN) :: eigenvectors
452 TYPE(cp_fm_type), INTENT(INOUT) :: overlap, scratch
453 INTEGER, INTENT(IN) :: nvec
454
455 CHARACTER(LEN=*), PARAMETER :: routinen = 'check_generalized_diag'
456
457 CHARACTER(LEN=default_string_length) :: diag_type_name
458 REAL(kind=dp) :: eps, eps_abort, eps_warning, gold, &
459 test
460 INTEGER :: handle, i, j, ncol, nrow, output_unit
461#if defined(__parallel)
462 TYPE(cp_blacs_env_type), POINTER :: context
463 INTEGER :: il, jl, ipcol, iprow, &
464 mypcol, myprow, npcol, nprow
465 INTEGER, DIMENSION(9) :: desca
466#endif
467
468 CALL timeset(routinen, handle)
469
470 IF (.NOT. diag_check_requested()) THEN
471 CALL timestop(handle)
472 RETURN
473 END IF
474
475 output_unit = default_output_unit
476 eps_warning = diag_check_warning_threshold()
477 eps_abort = 10.0_dp*eps_warning
478
479 nrow = eigenvectors%matrix_struct%nrow_global
480 ncol = min(eigenvectors%matrix_struct%ncol_global, nvec)
481
482 CALL parallel_gemm("N", "N", nrow, ncol, nrow, 1.0_dp, overlap, eigenvectors, 0.0_dp, scratch)
483 CALL parallel_gemm("T", "N", ncol, ncol, nrow, 1.0_dp, eigenvectors, scratch, 0.0_dp, overlap)
484
485 gold = 0.0_dp
486 test = 0.0_dp
487 eps = 0.0_dp
488
489#if defined(__parallel)
490 context => overlap%matrix_struct%context
491 myprow = context%mepos(1)
492 mypcol = context%mepos(2)
493 nprow = context%num_pe(1)
494 npcol = context%num_pe(2)
495 desca(:) = overlap%matrix_struct%descriptor(:)
496 outer: DO j = 1, ncol
497 DO i = 1, ncol
498 CALL infog2l(i, j, desca, nprow, npcol, myprow, mypcol, il, jl, iprow, ipcol)
499 IF ((iprow == myprow) .AND. (ipcol == mypcol)) THEN
500 gold = merge(0.0_dp, 1.0_dp, i /= j)
501 test = overlap%local_data(il, jl)
502 eps = abs(test - gold)
503 IF (eps > eps_warning) EXIT outer
504 END IF
505 END DO
506 END DO outer
507#else
508 outer: DO j = 1, ncol
509 DO i = 1, ncol
510 gold = merge(0.0_dp, 1.0_dp, i /= j)
511 test = overlap%local_data(i, j)
512 eps = abs(test - gold)
513 IF (eps > eps_warning) EXIT outer
514 END DO
515 END DO outer
516#endif
517
518 IF (eps > eps_warning) THEN
520 diag_type_name = "SYGVX"
521 ELSE IF (diag_type == fm_diag_type_elpa) THEN
522 diag_type_name = "ELPA"
523 ELSE IF (diag_type == fm_diag_type_cusolver) THEN
524 diag_type_name = "CUSOLVER"
525 ELSE IF (diag_type == fm_diag_type_dlaf) THEN
526 diag_type_name = "DLAF"
527 ELSE
528 cpabort("Unknown diag_type")
529 END IF
530 WRITE (unit=output_unit, fmt="(/,T2,A,/,T2,A,I0,A,I0,A,F0.15,/,T2,A,F0.0,A,ES10.3)") &
531 "The generalized eigenvectors returned by "//trim(diag_type_name)//" are not S-orthonormal", &
532 "Matrix element (", i, ", ", j, ") = ", test, &
533 "The deviation from the expected value ", gold, " is", eps
534 IF (eps > eps_abort) THEN
535 cpabort("ERROR in "//routinen//": Check of generalized matrix diagonalization failed")
536 ELSE
537 cpwarn("Check of generalized matrix diagonalization failed in routine "//routinen)
538 END IF
539 END IF
540
541 CALL timestop(handle)
542
543 END SUBROUTINE check_generalized_diag
544
545! **************************************************************************************************
546!> \brief Issues an error messages and exits (optionally only warns).
547!> \param mesg message to be issued
548!> \param info error code (optional)
549!> \param warn only warn (optional)
550! **************************************************************************************************
551 SUBROUTINE cp_fm_error(mesg, info, warn)
552 CHARACTER(LEN=*), INTENT(IN) :: mesg
553 INTEGER, INTENT(IN), OPTIONAL :: info
554 LOGICAL, INTENT(IN), OPTIONAL :: warn
555
556 CHARACTER(LEN=2*default_string_length) :: message
557 LOGICAL :: warning
558
559 IF (PRESENT(info)) THEN
560 WRITE (message, "(A,A,I0,A)") mesg, " (INFO = ", info, ")"
561 ELSE
562 WRITE (message, "(A)") mesg
563 END IF
564
565 IF (PRESENT(warn)) THEN
566 warning = warn
567 ELSE ! abort
568 warning = .false.
569 END IF
570
571 IF (warning) THEN
572 cpwarn(trim(message))
573 ELSE
574 cpabort(trim(message))
575 END IF
576 END SUBROUTINE cp_fm_error
577
578! **************************************************************************************************
579!> \brief Computes all eigenvalues and vectors of a real symmetric matrix
580!> significantly faster than syevx, scales also much better.
581!> Needs workspace to allocate all the eigenvectors
582!> \param matrix ...
583!> \param eigenvectors ...
584!> \param eigenvalues ...
585!> \param info ...
586!> \par matrix is supposed to be in upper triangular form, and overwritten by this routine
587!> \par info If present returns error code and prevents program stops.
588!> Works currently only for scalapack.
589!> Other solvers will end the program regardless of PRESENT(info).
590! **************************************************************************************************
591 SUBROUTINE cp_fm_syevd(matrix, eigenvectors, eigenvalues, info)
592
593 TYPE(cp_fm_type), INTENT(IN) :: matrix, eigenvectors
594 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
595 INTEGER, INTENT(OUT), OPTIONAL :: info
596
597 CHARACTER(LEN=*), PARAMETER :: routinen = 'cp_fm_syevd'
598
599 INTEGER :: handle, myinfo, n, nmo
600 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eig
601#if defined(__parallel)
602 TYPE(cp_fm_type) :: eigenvectors_new, matrix_new
603#else
604 INTEGER :: liwork, lwork
605 INTEGER, DIMENSION(:), POINTER :: iwork
606 REAL(kind=dp), DIMENSION(:, :), POINTER :: m
607 REAL(kind=dp), DIMENSION(:), POINTER :: work
608 INTEGER, TARGET :: v(1)
609 REAL(kind=dp), TARGET :: w(1)
610#endif
611
612 CALL timeset(routinen, handle)
613
614 myinfo = 0
615
616 n = matrix%matrix_struct%nrow_global
617 ALLOCATE (eig(n))
618
619#if defined(__parallel)
620 ! Determine if the input matrix needs to be redistributed before diagonalization.
621 ! Heuristics are used to determine the optimal number of CPUs for diagonalization.
622 ! The redistributed matrix is stored in matrix_new, which is just a pointer
623 ! to the original matrix if no redistribution is required
624 CALL cp_fm_redistribute_start(matrix, eigenvectors, matrix_new, eigenvectors_new)
625
626 ! Call scalapack on CPUs that hold the new matrix
627 IF (ASSOCIATED(matrix_new%matrix_struct)) THEN
628 IF (PRESENT(info)) THEN
629 CALL cp_fm_syevd_base(matrix_new, eigenvectors_new, eig, myinfo)
630 ELSE
631 CALL cp_fm_syevd_base(matrix_new, eigenvectors_new, eig)
632 END IF
633 END IF
634 ! Redistribute results and clean up
635 CALL cp_fm_redistribute_end(matrix, eigenvectors, eig, matrix_new, eigenvectors_new)
636#else
637 ! Retrieve the optimal work array sizes first
638 lwork = -1
639 liwork = -1
640 m => matrix%local_data
641 iwork => v
642 work => w
643
644 CALL dsyevd('V', 'U', n, m(1, 1), SIZE(m, 1), eig(1), work(1), lwork, iwork(1), liwork, myinfo)
645
646 IF (myinfo /= 0) THEN
647 CALL cp_fm_error("ERROR in DSYEVD: Work space query failed", myinfo, PRESENT(info))
648 END IF
649
650 ! Reallocate work arrays and perform diagonalisation
651 lwork = nint(work(1))
652 ALLOCATE (work(lwork))
653
654 liwork = iwork(1)
655 ALLOCATE (iwork(liwork))
656
657 CALL dsyevd('V', 'U', n, m(1, 1), SIZE(m, 1), eig(1), work(1), lwork, iwork(1), liwork, myinfo)
658
659 IF (myinfo /= 0) THEN
660 CALL cp_fm_error("ERROR in DSYEVD: Matrix diagonalization failed", myinfo, PRESENT(info))
661 END IF
662
663 CALL cp_fm_to_fm(matrix, eigenvectors)
664
665 DEALLOCATE (iwork)
666 DEALLOCATE (work)
667#endif
668
669 IF (PRESENT(info)) info = myinfo
670
671 nmo = SIZE(eigenvalues, 1)
672 IF (nmo > n) THEN
673 eigenvalues(1:n) = eig(1:n)
674 ELSE
675 eigenvalues(1:nmo) = eig(1:nmo)
676 END IF
677
678 DEALLOCATE (eig)
679
680 CALL check_diag(matrix, eigenvectors, n)
681
682 CALL timestop(handle)
683
684 END SUBROUTINE cp_fm_syevd
685
686! **************************************************************************************************
687!> \brief ...
688!> \param matrix ...
689!> \param eigenvectors ...
690!> \param eigenvalues ...
691!> \param info ...
692! **************************************************************************************************
693 SUBROUTINE cp_fm_syevd_base(matrix, eigenvectors, eigenvalues, info)
694
695 TYPE(cp_fm_type), INTENT(IN) :: matrix, eigenvectors
696 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
697 INTEGER, INTENT(OUT), OPTIONAL :: info
698
699 CHARACTER(LEN=*), PARAMETER :: routinen = 'cp_fm_syevd_base'
700
701 INTEGER :: handle, myinfo
702#if defined(__parallel)
703 TYPE(cp_blacs_env_type), POINTER :: context
704 INTEGER :: liwork, lwork, n
705 INTEGER, DIMENSION(9) :: descm, descv
706 INTEGER, DIMENSION(:), POINTER :: iwork
707 REAL(kind=dp), DIMENSION(:), POINTER :: work
708 REAL(kind=dp), DIMENSION(:, :), POINTER :: m, v
709 REAL(kind=dp), TARGET :: w(1)
710#if defined (__HAS_IEEE_EXCEPTIONS)
711 LOGICAL, DIMENSION(5) :: halt
712#endif
713#endif
714
715 CALL timeset(routinen, handle)
716
717 myinfo = 0
718
719#if defined(__parallel)
720
721 n = matrix%matrix_struct%nrow_global
722 m => matrix%local_data
723 context => matrix%matrix_struct%context
724 descm(:) = matrix%matrix_struct%descriptor(:)
725
726 v => eigenvectors%local_data
727 descv(:) = eigenvectors%matrix_struct%descriptor(:)
728
729 liwork = 7*n + 8*context%num_pe(2) + 2
730 ALLOCATE (iwork(liwork))
731
732 ! Work space query
733 lwork = -1
734 work => w
735
736 CALL pdsyevd('V', 'U', n, m(1, 1), 1, 1, descm, eigenvalues(1), v(1, 1), 1, 1, descv, &
737 work(1), lwork, iwork(1), liwork, myinfo)
738
739 IF (matrix%matrix_struct%para_env%is_source() .AND. (myinfo /= 0)) THEN
740 CALL cp_fm_error("ERROR in PDSYEVD: Work space query failed", myinfo, PRESENT(info))
741 END IF
742
743 lwork = nint(work(1)) ! can be insufficient due to bug in reference ScaLAPACK
744#if !defined(__SCALAPACK_NO_WA)
745 ! Query workspace for QDORMTR as called by reference ScaLAPACK (PDSYEVD).
746 CALL pdormtr('L', 'U', 'N', n, n, m(1, 1), 1, 1, descm, m(1, 1), &
747 v(1, 1), 1, 1, descv, work(1), -1, myinfo)
748
749 IF (matrix%matrix_struct%para_env%is_source() .AND. (myinfo /= 0)) THEN
750 CALL cp_fm_error("ERROR in PDORMTR: Work space query failed", myinfo, PRESENT(info))
751 END IF
752
753 IF (lwork < (work(1) + 2*n)) THEN
754 lwork = nint(work(1)) + 2*n ! still wrong by 2*N
755 END IF
756#endif
757 ALLOCATE (work(lwork))
758
759 ! Initial/documented amount of liwork is exceeded (slightly worrisome too).
760 IF (liwork < iwork(1)) THEN
761 liwork = iwork(1)
762 DEALLOCATE (iwork)
763 ALLOCATE (iwork(liwork))
764 END IF
765
766 ! ScaLAPACK takes advantage of IEEE754 exceptions for speedup.
767 ! Therefore, we disable floating point traps temporarily.
768#if defined (__HAS_IEEE_EXCEPTIONS)
769 CALL ieee_get_halting_mode(ieee_all, halt)
770 CALL ieee_set_halting_mode(ieee_all, .false.)
771#endif
772
773 CALL pdsyevd('V', 'U', n, m(1, 1), 1, 1, descm, eigenvalues(1), v(1, 1), 1, 1, descv, &
774 work(1), lwork, iwork(1), liwork, myinfo)
775
776#if defined (__HAS_IEEE_EXCEPTIONS)
777 CALL ieee_set_halting_mode(ieee_all, halt)
778#endif
779 IF (matrix%matrix_struct%para_env%is_source() .AND. (myinfo /= 0)) THEN
780 CALL cp_fm_error("ERROR in PDSYEVD: Matrix diagonalization failed", myinfo, PRESENT(info))
781 END IF
782
783 IF (PRESENT(info)) info = myinfo
784
785 DEALLOCATE (work)
786 DEALLOCATE (iwork)
787#else
788 mark_used(matrix)
789 mark_used(eigenvectors)
790 mark_used(eigenvalues)
791 myinfo = -1
792 IF (PRESENT(info)) info = myinfo
793 CALL cp_fm_error("ERROR in "//trim(routinen)// &
794 ": Matrix diagonalization using PDSYEVD requested without ScaLAPACK support")
795#endif
796
797 CALL timestop(handle)
798
799 END SUBROUTINE cp_fm_syevd_base
800
801! **************************************************************************************************
802!> \brief compute eigenvalues and optionally eigenvectors of a real symmetric matrix using scalapack.
803!> If eigenvectors are required this routine will replicate a full matrix on each CPU...
804!> if more than a handful of vectors are needed, use cp_fm_syevd instead
805!> \param matrix ...
806!> \param eigenvectors ...
807!> \param eigenvalues ...
808!> \param neig ...
809!> \param work_syevx ...
810!> \par matrix is supposed to be in upper triangular form, and overwritten by this routine
811!> neig is the number of vectors needed (default all)
812!> work_syevx evec calculation only, is the fraction of the working buffer allowed (1.0 use full buffer)
813!> reducing this saves time, but might cause the routine to fail
814! **************************************************************************************************
815 SUBROUTINE cp_fm_syevx(matrix, eigenvectors, eigenvalues, neig, work_syevx)
816
817 ! Diagonalise the symmetric n by n matrix using the LAPACK library.
818
819 TYPE(cp_fm_type), INTENT(IN) :: matrix
820 TYPE(cp_fm_type), OPTIONAL, INTENT(IN) :: eigenvectors
821 REAL(kind=dp), OPTIONAL, INTENT(IN) :: work_syevx
822 INTEGER, INTENT(IN), OPTIONAL :: neig
823 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
824
825 CHARACTER(LEN=*), PARAMETER :: routinen = "cp_fm_syevx"
826
827#if defined(__parallel)
828 REAL(kind=dp), PARAMETER :: orfac = -1.0_dp
829#endif
830 REAL(kind=dp), PARAMETER :: vl = 0.0_dp, &
831 vu = 0.0_dp
832
833 TYPE(cp_blacs_env_type), POINTER :: context
834 TYPE(cp_logger_type), POINTER :: logger
835 CHARACTER(LEN=1) :: job_type
836 REAL(kind=dp) :: abstol, work_syevx_local
837 INTEGER :: handle, info, liwork, lwork, &
838 m, n, nb, npcol, nprow, &
839 output_unit, neig_local
840 LOGICAL :: ionode, needs_evecs
841 INTEGER, DIMENSION(:), ALLOCATABLE :: ifail, iwork
842 REAL(kind=dp), DIMENSION(:), ALLOCATABLE :: w, work
843 REAL(kind=dp), DIMENSION(:, :), POINTER :: a, z
844
845 REAL(kind=dp), EXTERNAL :: dlamch
846
847#if defined(__parallel)
848 INTEGER :: nn, np0, npe, nq0, nz
849 INTEGER, DIMENSION(9) :: desca, descz
850 INTEGER, DIMENSION(:), ALLOCATABLE :: iclustr
851 REAL(kind=dp), DIMENSION(:), ALLOCATABLE :: gap
852 INTEGER, EXTERNAL :: iceil, numroc
853#else
854 INTEGER :: nla, nlz
855 INTEGER, EXTERNAL :: ilaenv
856#endif
857#if defined (__HAS_IEEE_EXCEPTIONS)
858 LOGICAL, DIMENSION(5) :: halt
859#endif
860
861 ! by default all
862 n = matrix%matrix_struct%nrow_global
863 neig_local = n
864 IF (PRESENT(neig)) neig_local = neig
865 IF (neig_local == 0) RETURN
866
867 CALL timeset(routinen, handle)
868
869 needs_evecs = PRESENT(eigenvectors)
870
871 logger => cp_get_default_logger()
872 ionode = logger%para_env%is_source()
873 n = matrix%matrix_struct%nrow_global
874
875 ! by default allocate all needed space
876 work_syevx_local = 1.0_dp
877 IF (PRESENT(work_syevx)) work_syevx_local = work_syevx
878
879 ! set scalapack job type
880 IF (needs_evecs) THEN
881 job_type = "V"
882 ELSE
883 job_type = "N"
884 END IF
885
886 ! target the most accurate calculation of the eigenvalues
887 abstol = 2.0_dp*dlamch("S")
888
889 context => matrix%matrix_struct%context
890 nprow = context%num_pe(1)
891 npcol = context%num_pe(2)
892
893 ALLOCATE (w(n))
894 eigenvalues(:) = 0.0_dp
895#if defined(__parallel)
896
897 IF (matrix%matrix_struct%nrow_block /= matrix%matrix_struct%ncol_block) THEN
898 cpabort("ERROR in "//routinen//": Invalid blocksize (no square blocks) found")
899 END IF
900
901 a => matrix%local_data
902 desca(:) = matrix%matrix_struct%descriptor(:)
903
904 IF (needs_evecs) THEN
905 z => eigenvectors%local_data
906 descz(:) = eigenvectors%matrix_struct%descriptor(:)
907 ELSE
908 ! z will not be referenced
909 z => matrix%local_data
910 descz = desca
911 END IF
912
913 ! Get the optimal work storage size
914
915 npe = nprow*npcol
916 nb = matrix%matrix_struct%nrow_block
917 nn = max(n, nb, 2)
918 np0 = numroc(nn, nb, 0, 0, nprow)
919 nq0 = max(numroc(nn, nb, 0, 0, npcol), nb)
920
921 IF (needs_evecs) THEN
922 lwork = 5*n + max(5*nn, np0*nq0) + iceil(neig_local, npe)*nn + 2*nb*nb + &
923 int(work_syevx_local*real((neig_local - 1)*n, dp)) !!!! allocates a full matrix on every CPU !!!!!
924 ELSE
925 lwork = 5*n + max(5*nn, nb*(np0 + 1))
926 END IF
927 liwork = 6*max(n, npe + 1, 4)
928
929 ALLOCATE (gap(npe))
930 gap = 0.0_dp
931 ALLOCATE (iclustr(2*npe))
932 iclustr = 0
933 ALLOCATE (ifail(n))
934 ifail = 0
935 ALLOCATE (iwork(liwork))
936 ALLOCATE (work(lwork))
937
938 ! ScaLAPACK takes advantage of IEEE754 exceptions for speedup.
939 ! Therefore, we disable floating point traps temporarily.
940#if defined (__HAS_IEEE_EXCEPTIONS)
941 CALL ieee_get_halting_mode(ieee_all, halt)
942 CALL ieee_set_halting_mode(ieee_all, .false.)
943#endif
944 CALL pdsyevx(job_type, "I", "U", n, a(1, 1), 1, 1, desca, vl, vu, 1, neig_local, abstol, m, nz, w(1), orfac, &
945 z(1, 1), 1, 1, descz, work(1), lwork, iwork(1), liwork, ifail(1), iclustr(1), gap, info)
946#if defined (__HAS_IEEE_EXCEPTIONS)
947 CALL ieee_set_halting_mode(ieee_all, halt)
948#endif
949
950 ! Error handling
951 IF (info /= 0) THEN
952 IF (ionode) THEN
953 output_unit = cp_logger_get_unit_nr(logger, local=.false.)
954 WRITE (unit=output_unit, fmt="(/,(T3,A,T12,1X,I10))") &
955 "info = ", info, &
956 "lwork = ", lwork, &
957 "liwork = ", liwork, &
958 "nz = ", nz
959 IF (info > 0) THEN
960 WRITE (unit=output_unit, fmt="(/,T3,A,(T12,6(1X,I10)))") &
961 "ifail = ", ifail
962 WRITE (unit=output_unit, fmt="(/,T3,A,(T12,6(1X,I10)))") &
963 "iclustr = ", iclustr
964 WRITE (unit=output_unit, fmt="(/,T3,A,(T12,6(1X,E10.3)))") &
965 "gap = ", gap
966 END IF
967 END IF
968 cpabort("ERROR in PDSYEVX (ScaLAPACK)")
969 END IF
970
971 ! Release work storage
972 DEALLOCATE (gap)
973 DEALLOCATE (iclustr)
974
975#else
976
977 a => matrix%local_data
978 IF (needs_evecs) THEN
979 z => eigenvectors%local_data
980 ELSE
981 ! z will not be referenced
982 z => matrix%local_data
983 END IF
984
985 ! Get the optimal work storage size
986
987 nb = max(ilaenv(1, "DSYTRD", "U", n, -1, -1, -1), &
988 ilaenv(1, "DORMTR", "U", n, -1, -1, -1))
989
990 lwork = max((nb + 3)*n, 8*n) + n ! sun bug fix
991 liwork = 5*n
992
993 ALLOCATE (ifail(n))
994 ifail = 0
995 ALLOCATE (iwork(liwork))
996 ALLOCATE (work(lwork))
997 info = 0
998 nla = SIZE(a, 1)
999 nlz = SIZE(z, 1)
1000
1001 ! LAPACK takes advantage of IEEE754 exceptions for speedup.
1002 ! Therefore, we disable floating point traps temporarily.
1003#if defined (__HAS_IEEE_EXCEPTIONS)
1004 CALL ieee_get_halting_mode(ieee_all, halt)
1005 CALL ieee_set_halting_mode(ieee_all, .false.)
1006#endif
1007 CALL dsyevx(job_type, "I", "U", n, a(1, 1), nla, vl, vu, 1, neig_local, &
1008 abstol, m, w, z(1, 1), nlz, work(1), lwork, iwork(1), ifail(1), info)
1009#if defined (__HAS_IEEE_EXCEPTIONS)
1010 CALL ieee_set_halting_mode(ieee_all, halt)
1011#endif
1012
1013 ! Error handling
1014 IF (info /= 0) THEN
1015 output_unit = cp_logger_get_unit_nr(logger, local=.false.)
1016 WRITE (unit=output_unit, fmt="(/,(T3,A,T12,1X,I10))") &
1017 "info = ", info
1018 IF (info > 0) THEN
1019 WRITE (unit=output_unit, fmt="(/,T3,A,(T12,6(1X,I10)))") &
1020 "ifail = ", ifail
1021 END IF
1022 cpabort("Error in DSYEVX (ScaLAPACK)")
1023 END IF
1024
1025#endif
1026 ! Release work storage
1027 DEALLOCATE (ifail)
1028 DEALLOCATE (iwork)
1029 DEALLOCATE (work)
1030 eigenvalues(1:neig_local) = w(1:neig_local)
1031 DEALLOCATE (w)
1032
1033 IF (needs_evecs) CALL check_diag(matrix, eigenvectors, neig_local)
1034
1035 CALL timestop(handle)
1036
1037 END SUBROUTINE cp_fm_syevx
1038
1039! **************************************************************************************************
1040!> \brief decomposes a quadratic matrix into its singular value decomposition
1041!> \param matrix_a ...
1042!> \param matrix_eigvl ...
1043!> \param matrix_eigvr_t ...
1044!> \param eigval ...
1045!> \param info ...
1046!> \author Maximilian Graml
1047! **************************************************************************************************
1048 SUBROUTINE cp_fm_svd(matrix_a, matrix_eigvl, matrix_eigvr_t, eigval, info)
1049
1050 TYPE(cp_fm_type), INTENT(IN) :: matrix_a
1051 TYPE(cp_fm_type), INTENT(INOUT) :: matrix_eigvl, matrix_eigvr_t
1052 REAL(kind=dp), DIMENSION(:), POINTER, &
1053 INTENT(INOUT) :: eigval
1054 INTEGER, INTENT(OUT), OPTIONAL :: info
1055
1056 CHARACTER(LEN=*), PARAMETER :: routinen = 'cp_fm_svd'
1057
1058 INTEGER :: handle, n, m, myinfo, lwork
1059 REAL(kind=dp), DIMENSION(:, :), POINTER :: a
1060 TYPE(cp_fm_type) :: matrix_lu
1061 REAL(kind=dp), DIMENSION(:), POINTER :: work
1062 REAL(kind=dp), TARGET :: w(1)
1063#if defined(__parallel)
1064 INTEGER, DIMENSION(9) :: desca, descu, descvt
1065#endif
1066
1067 CALL timeset(routinen, handle)
1068
1069 CALL cp_fm_create(matrix=matrix_lu, &
1070 matrix_struct=matrix_a%matrix_struct, &
1071 name="A_lu"//trim(adjustl(cp_to_string(1)))//"MATRIX")
1072 CALL cp_fm_to_fm(matrix_a, matrix_lu)
1073 a => matrix_lu%local_data
1074 m = matrix_lu%matrix_struct%nrow_global
1075 n = matrix_lu%matrix_struct%ncol_global
1076 ! Assert that incoming matrix is quadratic
1077 cpassert(m == n)
1078
1079 ! Prepare for workspace queries
1080 myinfo = 0
1081 lwork = -1
1082 work => w
1083#if defined(__parallel)
1084 ! To do: That might need a redistribution check as in cp_fm_syevd
1085 desca(:) = matrix_lu%matrix_struct%descriptor(:)
1086 descu(:) = matrix_eigvl%matrix_struct%descriptor(:)
1087 descvt(:) = matrix_eigvr_t%matrix_struct%descriptor(:)
1088
1089 ! Workspace query
1090 CALL pdgesvd('V', 'V', m, m, matrix_lu%local_data, 1, 1, desca, eigval, matrix_eigvl%local_data, &
1091 1, 1, descu, matrix_eigvr_t%local_data, 1, 1, descvt, work, lwork, myinfo)
1092
1093 IF (matrix_lu%matrix_struct%para_env%is_source() .AND. (myinfo /= 0)) THEN
1094 CALL cp_fm_error("ERROR in PDGESVD: Work space query failed", myinfo, PRESENT(info))
1095 END IF
1096
1097 lwork = nint(work(1))
1098 ALLOCATE (work(lwork))
1099
1100 CALL pdgesvd('V', 'V', m, m, matrix_lu%local_data, 1, 1, desca, eigval, matrix_eigvl%local_data, &
1101 1, 1, descu, matrix_eigvr_t%local_data, 1, 1, descvt, work, lwork, myinfo)
1102
1103 IF (matrix_lu%matrix_struct%para_env%is_source() .AND. (myinfo /= 0)) THEN
1104 CALL cp_fm_error("ERROR in PDGESVD: Matrix diagonalization failed", myinfo, PRESENT(info))
1105 END IF
1106#else
1107 ! Workspace query
1108 CALL dgesvd('S', 'S', m, m, matrix_lu%local_data, m, eigval, matrix_eigvl%local_data, &
1109 m, matrix_eigvr_t%local_data, m, work, lwork, myinfo)
1110
1111 IF (myinfo /= 0) THEN
1112 CALL cp_fm_error("ERROR in DGESVD: Work space query failed", myinfo, PRESENT(info))
1113 END IF
1114
1115 lwork = nint(work(1))
1116 ALLOCATE (work(lwork))
1117
1118 CALL dgesvd('S', 'S', m, m, matrix_lu%local_data, m, eigval, matrix_eigvl%local_data, &
1119 m, matrix_eigvr_t%local_data, m, work, lwork, myinfo)
1120
1121 IF (myinfo /= 0) THEN
1122 CALL cp_fm_error("ERROR in DGESVD: Matrix diagonalization failed", myinfo, PRESENT(info))
1123 END IF
1124
1125#endif
1126 ! Release intermediary matrices
1127 DEALLOCATE (work)
1128 CALL cp_fm_release(matrix_lu)
1129
1130 IF (PRESENT(info)) info = myinfo
1131
1132 CALL timestop(handle)
1133 END SUBROUTINE cp_fm_svd
1134
1135! **************************************************************************************************
1136!> \brief ...
1137!> \param matrix ...
1138!> \param work ...
1139!> \param exponent ...
1140!> \param threshold ...
1141!> \param n_dependent ...
1142!> \param verbose ...
1143!> \param eigvals ...
1144! **************************************************************************************************
1145 SUBROUTINE cp_fm_power(matrix, work, exponent, threshold, n_dependent, verbose, eigvals)
1146
1147 ! Raise the real symmetric n by n matrix to the power given by
1148 ! the exponent. All eigenvectors with a corresponding eigenvalue lower
1149 ! than threshold are quenched. result in matrix
1150
1151 ! - Creation (29.03.1999, Matthias Krack)
1152 ! - Parallelised using BLACS and ScaLAPACK (06.06.2001,MK)
1153
1154 TYPE(cp_fm_type), INTENT(IN) :: matrix, work
1155 REAL(kind=dp), INTENT(IN) :: exponent, threshold
1156 INTEGER, INTENT(OUT) :: n_dependent
1157 LOGICAL, INTENT(IN), OPTIONAL :: verbose
1158 REAL(kind=dp), DIMENSION(2), INTENT(OUT), &
1159 OPTIONAL :: eigvals
1160
1161 CHARACTER(LEN=*), PARAMETER :: routinen = 'cp_fm_power'
1162
1163 INTEGER :: handle, icol_global, &
1164 mypcol, myprow, &
1165 ncol_global, nrow_global
1166 LOGICAL :: my_verbose
1167 REAL(kind=dp) :: condition_number, f, p
1168 REAL(kind=dp), DIMENSION(:), ALLOCATABLE :: eigenvalues
1169 REAL(kind=dp), DIMENSION(:, :), POINTER :: eigenvectors
1170 TYPE(cp_blacs_env_type), POINTER :: context
1171
1172#if defined(__parallel)
1173 INTEGER :: icol_local, ipcol, iprow, irow_global, &
1174 irow_local
1175#endif
1176
1177 CALL timeset(routinen, handle)
1178
1179 my_verbose = .false.
1180 IF (PRESENT(verbose)) my_verbose = verbose
1181
1182 context => matrix%matrix_struct%context
1183 myprow = context%mepos(1)
1184 mypcol = context%mepos(2)
1185 n_dependent = 0
1186 p = 0.5_dp*exponent
1187
1188 nrow_global = matrix%matrix_struct%nrow_global
1189 ncol_global = matrix%matrix_struct%ncol_global
1190
1191 ALLOCATE (eigenvalues(ncol_global))
1192 eigenvalues(:) = 0.0_dp
1193
1194 ! Compute the eigenvectors and eigenvalues
1195
1196 CALL choose_eigv_solver(matrix, work, eigenvalues)
1197
1198 IF (PRESENT(eigvals)) THEN
1199 eigvals(1) = eigenvalues(1)
1200 eigvals(2) = eigenvalues(ncol_global)
1201 END IF
1202
1203#if defined(__parallel)
1204 eigenvectors => work%local_data
1205
1206 ! Build matrix**exponent with eigenvector quenching
1207
1208 DO icol_global = 1, ncol_global
1209
1210 IF (eigenvalues(icol_global) < threshold) THEN
1211
1212 n_dependent = n_dependent + 1
1213
1214 ipcol = work%matrix_struct%g2p_col(icol_global)
1215
1216 IF (mypcol == ipcol) THEN
1217 icol_local = work%matrix_struct%g2l_col(icol_global)
1218 DO irow_global = 1, nrow_global
1219 iprow = work%matrix_struct%g2p_row(irow_global)
1220 IF (myprow == iprow) THEN
1221 irow_local = work%matrix_struct%g2l_row(irow_global)
1222 eigenvectors(irow_local, icol_local) = 0.0_dp
1223 END IF
1224 END DO
1225 END IF
1226
1227 ELSE
1228
1229 f = eigenvalues(icol_global)**p
1230
1231 ipcol = work%matrix_struct%g2p_col(icol_global)
1232
1233 IF (mypcol == ipcol) THEN
1234 icol_local = work%matrix_struct%g2l_col(icol_global)
1235 DO irow_global = 1, nrow_global
1236 iprow = work%matrix_struct%g2p_row(irow_global)
1237 IF (myprow == iprow) THEN
1238 irow_local = work%matrix_struct%g2l_row(irow_global)
1239 eigenvectors(irow_local, icol_local) = &
1240 f*eigenvectors(irow_local, icol_local)
1241 END IF
1242 END DO
1243 END IF
1244
1245 END IF
1246
1247 END DO
1248
1249#else
1250
1251 eigenvectors => work%local_data
1252
1253 ! Build matrix**exponent with eigenvector quenching
1254
1255 DO icol_global = 1, ncol_global
1256
1257 IF (eigenvalues(icol_global) < threshold) THEN
1258
1259 n_dependent = n_dependent + 1
1260 eigenvectors(1:nrow_global, icol_global) = 0.0_dp
1261
1262 ELSE
1263
1264 f = eigenvalues(icol_global)**p
1265 eigenvectors(1:nrow_global, icol_global) = &
1266 f*eigenvectors(1:nrow_global, icol_global)
1267
1268 END IF
1269
1270 END DO
1271
1272#endif
1273 CALL cp_fm_syrk("U", "N", ncol_global, 1.0_dp, work, 1, 1, 0.0_dp, matrix)
1274 CALL cp_fm_uplo_to_full(matrix, work)
1275
1276 ! Print some warnings/notes
1277 IF (matrix%matrix_struct%para_env%is_source() .AND. my_verbose) THEN
1278 condition_number = abs(eigenvalues(ncol_global)/eigenvalues(1))
1279 WRITE (unit=cp_logger_get_default_unit_nr(), fmt="(/,(T2,A,ES15.6))") &
1280 "CP_FM_POWER: smallest eigenvalue:", eigenvalues(1), &
1281 "CP_FM_POWER: largest eigenvalue: ", eigenvalues(ncol_global), &
1282 "CP_FM_POWER: condition number: ", condition_number
1283 IF (eigenvalues(1) <= 0.0_dp) THEN
1284 WRITE (unit=cp_logger_get_default_unit_nr(), fmt="(/,T2,A)") &
1285 "WARNING: matrix has a negative eigenvalue, tighten EPS_DEFAULT"
1286 END IF
1287 IF (condition_number > 1.0e12_dp) THEN
1288 WRITE (unit=cp_logger_get_default_unit_nr(), fmt="(/,T2,A)") &
1289 "WARNING: high condition number => possibly ill-conditioned matrix"
1290 END IF
1291 END IF
1292
1293 DEALLOCATE (eigenvalues)
1294
1295 CALL timestop(handle)
1296
1297 END SUBROUTINE cp_fm_power
1298
1299! **************************************************************************************************
1300!> \brief ...
1301!> \param matrix ...
1302!> \param eigenvectors ...
1303!> \param eigval ...
1304!> \param thresh ...
1305!> \param start_sec_block ...
1306! **************************************************************************************************
1307 SUBROUTINE cp_fm_block_jacobi(matrix, eigenvectors, eigval, thresh, start_sec_block)
1308
1309 ! Calculates block diagonalization of a full symmetric matrix
1310 ! It has its origin in cp_fm_syevx. This routine rotates only elements
1311 ! which are larger than a threshold values "thresh".
1312 ! start_sec_block is the start of the second block.
1313 ! IT DOES ONLY ONE SWEEP!
1314
1315 ! - Creation (07.10.2002, Martin Fengler)
1316 ! - Cosmetics (05.04.06, MK)
1317
1318 TYPE(cp_fm_type), INTENT(IN) :: eigenvectors, matrix
1319 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: eigval
1320 INTEGER, INTENT(IN) :: start_sec_block
1321 REAL(kind=dp), INTENT(IN) :: thresh
1322
1323 CHARACTER(LEN=*), PARAMETER :: routinen = 'cp_fm_block_jacobi'
1324
1325 INTEGER :: handle
1326 REAL(kind=dp), DIMENSION(:, :), POINTER :: a, ev
1327
1328 REAL(kind=dp) :: tan_theta, tau, c, s
1329 INTEGER :: q, p, n
1330 REAL(kind=dp), DIMENSION(:), ALLOCATABLE :: c_ip
1331
1332#if defined(__parallel)
1333 TYPE(cp_blacs_env_type), POINTER :: context
1334
1335 INTEGER :: nprow, npcol, block_dim_row, block_dim_col, info, &
1336 ev_row_block_size, iam, mynumrows, mype, npe, q_loc
1337 REAL(kind=dp), DIMENSION(:, :), ALLOCATABLE :: a_loc, ev_loc
1338 INTEGER, DIMENSION(9) :: desca, descz, &
1339 desc_a_block, &
1340 desc_ev_loc
1341 TYPE(mp_comm_type) :: allgrp
1342 TYPE(cp_blacs_type) :: ictxt_loc
1343 INTEGER, EXTERNAL :: numroc
1344#endif
1345
1346 ! -------------------------------------------------------------------------
1347
1348 CALL timeset(routinen, handle)
1349
1350#if defined(__parallel)
1351 context => matrix%matrix_struct%context
1352 allgrp = matrix%matrix_struct%para_env
1353
1354 nprow = context%num_pe(1)
1355 npcol = context%num_pe(2)
1356
1357 n = matrix%matrix_struct%nrow_global
1358
1359 a => matrix%local_data
1360 desca(:) = matrix%matrix_struct%descriptor(:)
1361 ev => eigenvectors%local_data
1362 descz(:) = eigenvectors%matrix_struct%descriptor(:)
1363
1364 ! Copy the block to be rotated to the master process firstly and broadcast to all processes
1365 ! start_sec_block defines where the second block starts!
1366 ! Block will be processed together with the OO block
1367
1368 block_dim_row = start_sec_block - 1
1369 block_dim_col = n - block_dim_row
1370 ALLOCATE (a_loc(block_dim_row, block_dim_col))
1371
1372 mype = matrix%matrix_struct%para_env%mepos
1373 npe = matrix%matrix_struct%para_env%num_pe
1374 ! Get a new context
1375 CALL ictxt_loc%gridinit(matrix%matrix_struct%para_env, 'R', nprow*npcol, 1)
1376
1377 CALL descinit(desc_a_block, block_dim_row, block_dim_col, block_dim_row, &
1378 block_dim_col, 0, 0, ictxt_loc%get_handle(), block_dim_row, info)
1379
1380 CALL pdgemr2d(block_dim_row, block_dim_col, a, 1, start_sec_block, desca, &
1381 a_loc, 1, 1, desc_a_block, context%get_handle())
1382 ! Only the master (root) process received data yet
1383 CALL allgrp%bcast(a_loc, 0)
1384
1385 ! Since each process owns now the upper block, the eigenvectors can be re-sorted in such a way that
1386 ! each process has a NN*1 grid, i.e. the process owns a bunch of rows which can be modified locally
1387
1388 ! Initialize distribution of the eigenvectors
1389 iam = mype
1390 ev_row_block_size = n/(nprow*npcol)
1391 mynumrows = numroc(n, ev_row_block_size, iam, 0, nprow*npcol)
1392
1393 ALLOCATE (ev_loc(mynumrows, n), c_ip(mynumrows))
1394
1395 CALL descinit(desc_ev_loc, n, n, ev_row_block_size, n, 0, 0, ictxt_loc%get_handle(), &
1396 mynumrows, info)
1397
1398 CALL pdgemr2d(n, n, ev, 1, 1, descz, ev_loc, 1, 1, desc_ev_loc, context%get_handle())
1399
1400 ! Start block diagonalization of matrix
1401
1402 q_loc = 0
1403
1404 DO q = start_sec_block, n
1405 q_loc = q_loc + 1
1406 DO p = 1, (start_sec_block - 1)
1407
1408 IF (abs(a_loc(p, q_loc)) > thresh) THEN
1409
1410 tau = (eigval(q) - eigval(p))/(2.0_dp*a_loc(p, q_loc))
1411
1412 tan_theta = sign(1.0_dp, tau)/(abs(tau) + sqrt(1.0_dp + tau*tau))
1413
1414 ! Cos(theta)
1415 c = 1.0_dp/sqrt(1.0_dp + tan_theta*tan_theta)
1416 s = tan_theta*c
1417
1418 ! Calculate eigenvectors: Q*J
1419 ! c_ip = c*EV_loc(:,p) - s*EV_loc(:,q)
1420 ! c_iq = s*EV_loc(:,p) + c*EV_loc(:,q)
1421 ! EV(:,p) = c_ip
1422 ! EV(:,q) = c_iq
1423 CALL dcopy(mynumrows, ev_loc(1, p), 1, c_ip(1), 1)
1424 CALL dscal(mynumrows, c, ev_loc(1, p), 1)
1425 CALL daxpy(mynumrows, -s, ev_loc(1, q), 1, ev_loc(1, p), 1)
1426 CALL dscal(mynumrows, c, ev_loc(1, q), 1)
1427 CALL daxpy(mynumrows, s, c_ip(1), 1, ev_loc(1, q), 1)
1428
1429 END IF
1430
1431 END DO
1432 END DO
1433
1434 ! Copy eigenvectors back to the original distribution
1435 CALL pdgemr2d(n, n, ev_loc, 1, 1, desc_ev_loc, ev, 1, 1, descz, context%get_handle())
1436
1437 ! Release work storage
1438 DEALLOCATE (a_loc, ev_loc, c_ip)
1439
1440 CALL ictxt_loc%gridexit()
1441
1442#else
1443
1444 n = matrix%matrix_struct%nrow_global
1445
1446 ALLOCATE (c_ip(n)) ! Local eigenvalue vector
1447
1448 a => matrix%local_data ! Contains the Matrix to be worked on
1449 ev => eigenvectors%local_data ! Contains the eigenvectors up to blocksize, rest is garbage
1450
1451 ! Start matrix diagonalization
1452
1453 tan_theta = 0.0_dp
1454 tau = 0.0_dp
1455
1456 DO q = start_sec_block, n
1457 DO p = 1, (start_sec_block - 1)
1458
1459 IF (abs(a(p, q)) > thresh) THEN
1460
1461 tau = (eigval(q) - eigval(p))/(2.0_dp*a(p, q))
1462
1463 tan_theta = sign(1.0_dp, tau)/(abs(tau) + sqrt(1.0_dp + tau*tau))
1464
1465 ! Cos(theta)
1466 c = 1.0_dp/sqrt(1.0_dp + tan_theta*tan_theta)
1467 s = tan_theta*c
1468
1469 ! Calculate eigenvectors: Q*J
1470 ! c_ip = c*EV(:,p) - s*EV(:,q)
1471 ! c_iq = s*EV(:,p) + c*EV(:,q)
1472 ! EV(:,p) = c_ip
1473 ! EV(:,q) = c_iq
1474 CALL dcopy(n, ev(1, p), 1, c_ip(1), 1)
1475 CALL dscal(n, c, ev(1, p), 1)
1476 CALL daxpy(n, -s, ev(1, q), 1, ev(1, p), 1)
1477 CALL dscal(n, c, ev(1, q), 1)
1478 CALL daxpy(n, s, c_ip(1), 1, ev(1, q), 1)
1479
1480 END IF
1481
1482 END DO
1483 END DO
1484
1485 ! Release work storage
1486
1487 DEALLOCATE (c_ip)
1488
1489#endif
1490
1491 CALL timestop(handle)
1492
1493 END SUBROUTINE cp_fm_block_jacobi
1494
1495! **************************************************************************************************
1496!> \brief General Eigenvalue Problem AX = BXE.
1497!> Use cuSOLVERMp directly when requested and large enough; otherwise
1498!> reduce the problem through a Cholesky decomposition of B.
1499!> \param amatrix ...
1500!> \param bmatrix ...
1501!> \param eigenvectors ...
1502!> \param eigenvalues ...
1503!> \param work ...
1504! **************************************************************************************************
1505 SUBROUTINE cp_fm_geeig(amatrix, bmatrix, eigenvectors, eigenvalues, work)
1506
1507 TYPE(cp_fm_type), INTENT(IN) :: amatrix, bmatrix, eigenvectors
1508 REAL(kind=dp), DIMENSION(:) :: eigenvalues
1509 TYPE(cp_fm_type), INTENT(IN) :: work
1510
1511 CHARACTER(LEN=*), PARAMETER :: routinen = 'cp_fm_geeig'
1512
1513 INTEGER :: handle, nao, nmo
1514 LOGICAL :: check_eigenvectors
1515 TYPE(cp_fm_type) :: overlap_check, scratch_check
1516
1517 CALL timeset(routinen, handle)
1518
1519 CALL cp_fm_get_info(amatrix, nrow_global=nao)
1520 nmo = SIZE(eigenvalues)
1521 check_eigenvectors = diag_check_requested()
1522
1524 nao >= cusolver_n_min) THEN
1525 ! Use cuSolverMP generalized eigenvalue solver without a CP2K-side
1526 ! Cholesky reduction.
1527 ! Use work as intermediate buffer since eigenvectors may be smaller (nao x nmo)
1528 IF (check_eigenvectors) THEN
1529 CALL cp_fm_create(overlap_check, bmatrix%matrix_struct)
1530 CALL cp_fm_create(scratch_check, bmatrix%matrix_struct)
1531 CALL cp_fm_to_fm(bmatrix, overlap_check)
1532 END IF
1533 CALL cp_fm_general_cusolver(amatrix, bmatrix, work, eigenvalues)
1534 IF (check_eigenvectors) THEN
1535 CALL check_generalized_diag(overlap_check, work, scratch_check, nmo)
1536 CALL cp_fm_release(scratch_check)
1537 CALL cp_fm_release(overlap_check)
1538 END IF
1539 CALL cp_fm_to_fm(work, eigenvectors, nmo)
1540#if defined(__parallel)
1542 ! Use ScaLAPACK generalized eigenvalue solver without a CP2K-side
1543 ! Cholesky reduction.
1544 IF (check_eigenvectors) THEN
1545 CALL cp_fm_create(overlap_check, bmatrix%matrix_struct)
1546 CALL cp_fm_create(scratch_check, bmatrix%matrix_struct)
1547 CALL cp_fm_to_fm(bmatrix, overlap_check)
1548 END IF
1549 CALL cp_fm_geeig_scalapack(amatrix, bmatrix, work, eigenvalues)
1550 IF (check_eigenvectors) THEN
1551 CALL check_generalized_diag(overlap_check, work, scratch_check, nmo)
1552 CALL cp_fm_release(scratch_check)
1553 CALL cp_fm_release(overlap_check)
1554 END IF
1555 CALL cp_fm_to_fm(work, eigenvectors, nmo)
1556#endif
1557#if defined(__DLAF)
1559 nao >= dlaf_neigvec_min) THEN
1560 ! Use DLA-Future generalized eigenvalue solver for large matrices
1561 IF (check_eigenvectors) THEN
1562 CALL cp_fm_create(overlap_check, bmatrix%matrix_struct)
1563 CALL cp_fm_create(scratch_check, bmatrix%matrix_struct)
1564 CALL cp_fm_to_fm(bmatrix, overlap_check)
1565 END IF
1566 CALL cp_fm_diag_gen_dlaf(amatrix, bmatrix, work, eigenvalues)
1567 IF (check_eigenvectors) THEN
1568 CALL check_generalized_diag(overlap_check, work, scratch_check, nmo)
1569 CALL cp_fm_release(scratch_check)
1570 CALL cp_fm_release(overlap_check)
1571 END IF
1572 CALL cp_fm_to_fm(work, eigenvectors, nmo)
1573#endif
1574 ELSE
1575 ! Cholesky decompose S=U(T)U
1576 CALL cp_fm_cholesky_decompose(bmatrix)
1577 ! Invert to get U^(-1)
1578 CALL cp_fm_triangular_invert(bmatrix)
1579 ! Reduce to get U^(-T) * H * U^(-1)
1580 CALL cp_fm_triangular_multiply(bmatrix, amatrix, side="R")
1581 CALL cp_fm_triangular_multiply(bmatrix, amatrix, transpose_tr=.true.)
1582 ! Diagonalize
1583 CALL choose_eigv_solver(matrix=amatrix, eigenvectors=work, &
1584 eigenvalues=eigenvalues)
1585 ! Restore vectors C = U^(-1) * C*
1586 CALL cp_fm_triangular_multiply(bmatrix, work)
1587 CALL cp_fm_to_fm(work, eigenvectors, nmo)
1588 END IF
1589
1590 CALL timestop(handle)
1591
1592 END SUBROUTINE cp_fm_geeig
1593
1594! **************************************************************************************************
1595!> \brief General Eigenvalue Problem AX = BXE using ScaLAPACK PDSYGVX.
1596!> \param amatrix ...
1597!> \param bmatrix ...
1598!> \param eigenvectors ...
1599!> \param eigenvalues ...
1600! **************************************************************************************************
1601 SUBROUTINE cp_fm_geeig_scalapack(amatrix, bmatrix, eigenvectors, eigenvalues)
1602
1603 TYPE(cp_fm_type), INTENT(IN) :: amatrix, bmatrix, eigenvectors
1604 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
1605
1606 CHARACTER(LEN=*), PARAMETER :: routinen = 'cp_fm_geeig_scalapack'
1607
1608#if defined(__parallel)
1609 REAL(kind=dp), PARAMETER :: orfac = -1.0_dp, &
1610 vl = 0.0_dp, &
1611 vu = 0.0_dp
1612
1613 INTEGER :: handle, info, liwork, lwork, m, n, nb, &
1614 neig, npcol, nprow, nz
1615 INTEGER, DIMENSION(9) :: desca, descb, descz
1616 INTEGER, DIMENSION(:), ALLOCATABLE :: iclustr, ifail, iwork
1617 REAL(kind=dp) :: abstol
1618 REAL(kind=dp), DIMENSION(:), ALLOCATABLE :: gap, w, work
1619 REAL(kind=dp), DIMENSION(:, :), POINTER :: a, b, z
1620
1621 INTEGER :: mq0, nn, np0, npe
1622 INTEGER, EXTERNAL :: iceil, numroc
1623 REAL(kind=dp), EXTERNAL :: dlamch
1624#if defined (__HAS_IEEE_EXCEPTIONS)
1625 LOGICAL, DIMENSION(5) :: halt
1626#endif
1627#else
1628 INTEGER :: handle
1629#endif
1630
1631 CALL timeset(routinen, handle)
1632
1633#if defined(__parallel)
1634 n = amatrix%matrix_struct%nrow_global
1635 neig = min(SIZE(eigenvalues), n)
1636
1637 IF (neig == 0) THEN
1638 CALL timestop(handle)
1639 RETURN
1640 END IF
1641
1642 IF (amatrix%matrix_struct%nrow_block /= amatrix%matrix_struct%ncol_block) THEN
1643 cpabort("ERROR in "//routinen//": Invalid blocksize (no square blocks) found")
1644 END IF
1645
1646 a => amatrix%local_data
1647 b => bmatrix%local_data
1648 z => eigenvectors%local_data
1649 desca(:) = amatrix%matrix_struct%descriptor(:)
1650 descb(:) = bmatrix%matrix_struct%descriptor(:)
1651 descz(:) = eigenvectors%matrix_struct%descriptor(:)
1652
1653 nprow = amatrix%matrix_struct%context%num_pe(1)
1654 npcol = amatrix%matrix_struct%context%num_pe(2)
1655 npe = nprow*npcol
1656 nb = amatrix%matrix_struct%nrow_block
1657 nn = max(n, nb, 2)
1658 np0 = numroc(nn, nb, 0, 0, nprow)
1659 mq0 = max(numroc(nn, nb, 0, 0, npcol), nb)
1660
1661 lwork = 5*n + max(5*nn, np0*mq0 + 2*nb*nb) + iceil(neig, npe)*nn + &
1662 max(0, neig - 1)*n
1663 liwork = 6*max(n, npe + 1, 4)
1664
1665 ALLOCATE (gap(npe))
1666 gap = 0.0_dp
1667 ALLOCATE (iclustr(2*npe))
1668 iclustr = 0
1669 ALLOCATE (ifail(n))
1670 ifail = 0
1671 ALLOCATE (iwork(liwork))
1672 ALLOCATE (w(n))
1673 ALLOCATE (work(lwork))
1674
1675 abstol = 2.0_dp*dlamch("S")
1676
1677#if defined (__HAS_IEEE_EXCEPTIONS)
1678 CALL ieee_get_halting_mode(ieee_all, halt)
1679 CALL ieee_set_halting_mode(ieee_all, .false.)
1680#endif
1681 CALL pdsygvx(1, "V", "I", "U", n, a(1, 1), 1, 1, desca, b(1, 1), 1, 1, descb, &
1682 vl, vu, 1, neig, abstol, m, nz, w(1), orfac, z(1, 1), 1, 1, descz, &
1683 work(1), lwork, iwork(1), liwork, ifail(1), iclustr(1), gap(1), info)
1684#if defined (__HAS_IEEE_EXCEPTIONS)
1685 CALL ieee_set_halting_mode(ieee_all, halt)
1686#endif
1687
1688 IF (info /= 0 .OR. m < neig .OR. nz < neig) THEN
1689 cpabort("ERROR in PDSYGVX (ScaLAPACK), info="//trim(cp_to_string(info)))
1690 END IF
1691
1692 eigenvalues(:) = 0.0_dp
1693 eigenvalues(1:neig) = w(1:neig)
1694
1695 DEALLOCATE (gap, iclustr, ifail, iwork, w, work)
1696#else
1697 mark_used(amatrix)
1698 mark_used(bmatrix)
1699 mark_used(eigenvectors)
1700 mark_used(eigenvalues)
1701 cpabort("ERROR in "//routinen//": PDSYGVX requested without ScaLAPACK support")
1702#endif
1703
1704 CALL timestop(handle)
1705
1706 END SUBROUTINE cp_fm_geeig_scalapack
1707
1708! **************************************************************************************************
1709!> \brief General Eigenvalue Problem AX = BXE
1710!> Use canonical diagonalization : U*s**(-1/2)
1711!> \param amatrix ...
1712!> \param bmatrix ...
1713!> \param eigenvectors ...
1714!> \param eigenvalues ...
1715!> \param work ...
1716!> \param epseig ...
1717!> \param nmo_retained ...
1718! **************************************************************************************************
1719 SUBROUTINE cp_fm_geeig_canon(amatrix, bmatrix, eigenvectors, eigenvalues, work, epseig, &
1720 nmo_retained)
1721
1722 TYPE(cp_fm_type), INTENT(IN) :: amatrix, bmatrix, eigenvectors
1723 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
1724 TYPE(cp_fm_type), INTENT(IN) :: work
1725 REAL(kind=dp), INTENT(IN) :: epseig
1726 INTEGER, INTENT(OUT), OPTIONAL :: nmo_retained
1727
1728 CHARACTER(LEN=*), PARAMETER :: routinen = 'cp_fm_geeig_canon'
1729
1730 INTEGER :: handle, i, icol, irow, nao, nc, ncol, &
1731 nmo, nx
1732 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: evals
1733
1734 CALL timeset(routinen, handle)
1735
1736 ! Test sizees
1737 CALL cp_fm_get_info(amatrix, nrow_global=nao)
1738 nmo = SIZE(eigenvalues)
1739 ALLOCATE (evals(nao))
1740
1741 ! Diagonalize -S matrix, this way the NULL space is at the end of the spectrum
1742 CALL cp_fm_scale(-1.0_dp, bmatrix)
1743 CALL choose_eigv_solver(matrix=bmatrix, eigenvectors=work, eigenvalues=evals)
1744 evals(:) = -evals(:)
1745 nc = nao
1746 DO i = 1, nao
1747 IF (evals(i) < epseig) THEN
1748 nc = i - 1
1749 EXIT
1750 END IF
1751 END DO
1752 cpassert(nc /= 0)
1753
1754 IF (nc /= nao) THEN
1755 IF (nc < nmo) THEN
1756 ! Copy NULL space definition to last vectors of eigenvectors (if needed)
1757 ncol = nmo - nc
1758 CALL cp_fm_to_fm(work, eigenvectors, ncol, nc + 1, nc + 1)
1759 END IF
1760 ! Set NULL space in eigenvector matrix of S to zero
1761 DO icol = nc + 1, nao
1762 DO irow = 1, nao
1763 CALL cp_fm_set_element(work, irow, icol, 0.0_dp)
1764 END DO
1765 END DO
1766 ! Set small eigenvalues to a dummy save value
1767 evals(nc + 1:nao) = 1.0_dp
1768 END IF
1769 ! Calculate U*s**(-1/2)
1770 evals(:) = 1.0_dp/sqrt(evals(:))
1771 CALL cp_fm_column_scale(work, evals)
1772 ! Reduce to get U^(-T) * H * U^(-1)
1773 CALL cp_fm_gemm("T", "N", nao, nao, nao, 1.0_dp, work, amatrix, 0.0_dp, bmatrix)
1774 CALL cp_fm_gemm("N", "N", nao, nao, nao, 1.0_dp, bmatrix, work, 0.0_dp, amatrix)
1775 IF (nc /= nao) THEN
1776 ! set diagonal values to save large value
1777 DO icol = nc + 1, nao
1778 CALL cp_fm_set_element(amatrix, icol, icol, set_removed_eigval_to)
1779 END DO
1780 END IF
1781 ! Diagonalize
1782 CALL choose_eigv_solver(matrix=amatrix, eigenvectors=bmatrix, eigenvalues=eigenvalues)
1783 nx = min(nc, nmo)
1784 ! Restore vectors C = U^(-1) * C*
1785 CALL cp_fm_gemm("N", "N", nao, nx, nc, 1.0_dp, work, bmatrix, 0.0_dp, eigenvectors)
1786
1787 ! Number of basis modes that survived the linear-dependency filter. The remaining
1788 ! nao - nc entries of eigenvalues(:) are the placeholders set above.
1789 IF (PRESENT(nmo_retained)) nmo_retained = nc
1790
1791 DEALLOCATE (evals)
1792
1793 CALL timestop(handle)
1794
1795 END SUBROUTINE cp_fm_geeig_canon
1796
1797END MODULE cp_fm_diag
static void dgemm(const char transa, const char transb, const int m, const int n, const int k, const double alpha, const double *a, const int lda, const double *b, const int ldb, const double beta, double *c, const int ldc)
Convenient wrapper to hide Fortran nature of dgemm_, swapping a and b.
methods related to the blacs parallel environment
wrappers for the actual blacs calls. all functionality needed in the code should actually be provide ...
Wrapper for ELPA (complex matrices, i.e. cp_cfm_type).
Definition cp_cfm_elpa.F:11
subroutine, public set_elpa_c_kernel(requested_kernel)
Sets the active ELPA kernel for complex matrices.
subroutine, public cp_dlaf_finalize()
Finalize DLA-Future and pika runtime.
subroutine, public cp_dlaf_initialize()
Initialize DLA-Future and pika runtime.
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_gemm(transa, transb, m, n, k, alpha, matrix_a, matrix_b, beta, matrix_c, a_first_col, a_first_row, b_first_col, b_first_row, c_first_col, c_first_row)
computes matrix_c = beta * matrix_c + alpha * ( matrix_a ** transa ) * ( matrix_b ** transb )
subroutine, public cp_fm_syrk(uplo, trans, k, alpha, matrix_a, ia, ja, beta, matrix_c)
performs a rank-k update of a symmetric matrix_c matrix_c = beta * matrix_c + alpha * matrix_a * tran...
subroutine, public cp_fm_uplo_to_full(matrix, work, uplo)
given a triangular matrix according to uplo, computes the corresponding full matrix
subroutine, public cp_fm_scale(alpha, matrix_a)
scales a matrix matrix_a = alpha * matrix_b
subroutine, public cp_fm_column_scale(matrixa, scaling, first_col, source)
Scales columns of the full matrix by corresponding factors.
subroutine, public cp_fm_triangular_invert(matrix_a, uplo_tr)
inverts a triangular matrix
subroutine, public cp_fm_triangular_multiply(triangular_matrix, matrix_b, side, transpose_tr, invert_tr, uplo_tr, unit_diag_tr, n_rows, n_cols, alpha)
multiplies in place by a triangular matrix: matrix_b = alpha op(triangular_matrix) matrix_b or (if si...
various cholesky decomposition related routines
subroutine, public cp_fm_cholesky_decompose(matrix, n, info_out)
used to replace a symmetric positive def. matrix M with its cholesky decomposition U: M = U^T * U,...
Wrapper for cuSOLVERMp.
subroutine, public cp_fm_general_cusolver(amatrix, bmatrix, eigenvectors, eigenvalues)
Driver routine to solve generalized eigenvalue problem A*x = lambda*B*x with cuSOLVERMp.
subroutine, public cp_fm_diag_cusolver(matrix, eigenvectors, eigenvalues)
Driver routine to diagonalize a FM matrix with the cuSOLVERMp library.
Auxiliary tools to redistribute cp_fm_type and cp_cfm_type matrices before and after diagonalization....
subroutine, public cp_fm_redistribute_end(matrix, eigenvectors, eig, matrix_new, eigenvectors_new)
Redistributes eigenvectors and eigenvalues back to the original communicator group.
subroutine, public cp_fm_redistribute_start(matrix, eigenvectors, matrix_new, eigenvectors_new, caller_is_elpa, redist_info)
Determines the optimal number of CPUs for matrix diagonalization and redistributes the input matrices...
used for collecting some of the diagonalization schemes available for cp_fm_type. cp_fm_power also mo...
Definition cp_fm_diag.F:17
subroutine, public cp_fm_block_jacobi(matrix, eigenvectors, eigval, thresh, start_sec_block)
...
real(kind=dp), parameter, public eps_check_diag_default
Definition cp_fm_diag.F:85
subroutine, public cp_fm_power(matrix, work, exponent, threshold, n_dependent, verbose, eigvals)
...
integer, parameter, public fm_diag_type_cusolver
Definition cp_fm_diag.F:112
real(kind=dp), parameter, public set_removed_eigval_to
Definition cp_fm_diag.F:89
subroutine, public cp_fm_svd(matrix_a, matrix_eigvl, matrix_eigvr_t, eigval, info)
decomposes a quadratic matrix into its singular value decomposition
subroutine, public cp_fm_geeig_canon(amatrix, bmatrix, eigenvectors, eigenvalues, work, epseig, nmo_retained)
General Eigenvalue Problem AX = BXE Use canonical diagonalization : U*s**(-1/2).
integer, parameter, public fm_diag_type_dlaf
Definition cp_fm_diag.F:112
integer, parameter, public fm_diag_type_scalapack
Definition cp_fm_diag.F:112
subroutine, public cp_fm_syevd(matrix, eigenvectors, eigenvalues, info)
Computes all eigenvalues and vectors of a real symmetric matrix significantly faster than syevx,...
Definition cp_fm_diag.F:592
subroutine, public diag_finalize()
Finalize the diagonalization library.
Definition cp_fm_diag.F:238
logical, save, public direct_generalized_diagonalization
Definition cp_fm_diag.F:106
integer, parameter, public fm_diag_type_default
Definition cp_fm_diag.F:121
integer, save, public elpa_neigvec_min
Definition cp_fm_diag.F:99
subroutine, public diag_init(diag_lib, fallback_applied, elpa_kernel, elpa_c_kernel, elpa_neigvec_min_input, elpa_qr, elpa_print, elpa_one_stage, dlaf_neigvec_min_input, eps_check_diag_input, direct_generalized_diagonalization_input, diag_lib_explicit_input)
Setup the diagonalization library to be used.
Definition cp_fm_diag.F:164
subroutine, public cp_fm_geeig(amatrix, bmatrix, eigenvectors, eigenvalues, work)
General Eigenvalue Problem AX = BXE. Use cuSOLVERMp directly when requested and large enough; otherwi...
subroutine, public choose_eigv_solver(matrix, eigenvectors, eigenvalues, info)
Choose the Eigensolver depending on which library is available ELPA seems to be unstable for small sy...
Definition cp_fm_diag.F:262
real(kind=dp) function, public diag_check_warning_threshold()
Return the warning threshold for diagonalization checks.
Definition cp_fm_diag.F:331
integer, save, public diag_type
Definition cp_fm_diag.F:93
integer, save, public dlaf_neigvec_min
Definition cp_fm_diag.F:105
integer, parameter, public fm_diag_type_elpa
Definition cp_fm_diag.F:112
integer, parameter, public cusolver_n_min
Definition cp_fm_diag.F:102
logical function, public diag_check_requested()
Return whether diagonalization checks should be performed.
Definition cp_fm_diag.F:316
subroutine, public cp_fm_syevx(matrix, eigenvectors, eigenvalues, neig, work_syevx)
compute eigenvalues and optionally eigenvectors of a real symmetric matrix using scalapack....
Definition cp_fm_diag.F:816
logical, save, public diag_lib_explicit
Definition cp_fm_diag.F:96
subroutine, public cp_fm_diag_dlaf(matrix, eigenvectors, eigenvalues)
...
subroutine, public cp_fm_diag_gen_dlaf(a_matrix, b_matrix, eigenvectors, eigenvalues)
...
Wrapper for ELPA.
Definition cp_fm_elpa.F:12
subroutine, public set_elpa_kernel(requested_kernel)
Sets the active ELPA kernel.
Definition cp_fm_elpa.F:247
logical, save, public elpa_qr
Definition cp_fm_elpa.F:161
subroutine, public cp_fm_diag_elpa(matrix, eigenvectors, eigenvalues)
Driver routine to diagonalize a FM matrix with the ELPA library.
Definition cp_fm_elpa.F:299
logical, save, public elpa_print
Definition cp_fm_elpa.F:161
subroutine, public finalize_elpa_library()
Finalize the ELPA library.
Definition cp_fm_elpa.F:235
logical, save, public elpa_one_stage
Definition cp_fm_elpa.F:168
subroutine, public initialize_elpa_library(one_stage, qr, should_print)
Initialize the ELPA library.
Definition cp_fm_elpa.F:214
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
logical function, public cp_fm_struct_equivalent(fmstruct1, fmstruct2)
returns true if the two matrix structures are equivalent, false otherwise.
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, nrow_locals, ncol_locals, matrix_struct, para_env)
returns all kind of information about the full matrix
subroutine, public cp_fm_to_fm_submat(msource, mtarget, nrow, ncol, s_firstrow, s_firstcol, t_firstrow, t_firstcol)
copy just a part ot the matrix
subroutine, public cp_fm_set_all(matrix, alpha, beta)
set all elements of a matrix to the same value, and optionally the diagonal to a different one
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
subroutine, public cp_fm_set_element(matrix, irow_global, icol_global, alpha)
sets an element of a matrix
various routines to log and control the output. The idea is that decisions about where to log should ...
recursive integer function, public cp_logger_get_default_unit_nr(logger, local, skip_not_ionode)
asks the default unit number of the given logger. try to use cp_logger_get_unit_nr
integer function, public cp_logger_get_unit_nr(logger, local)
returns the unit nr for the requested kind of log.
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
integer, parameter, public default_output_unit
Definition machine.F:49
subroutine, public m_memory(mem)
Returns the total amount of memory [bytes] in use, if known, zero otherwise.
Definition machine.F:445
Interface to the message passing library MPI.
basic linear algebra operations for full matrixes
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
keeps the information about the structure of a full matrix
represent a full matrix
type of a logger, at the moment it contains just a print level starting at which level it should be l...