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