(git:3d5d2e5)
Loading...
Searching...
No Matches
cp_dbcsr_contrib.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
9 USE omp_lib, ONLY: omp_get_num_threads
10 USE cp_dbcsr_api, ONLY: &
19 USE kinds, ONLY: default_string_length,&
20 dp
23#include "../base/base_uses.f90"
24
25 IMPLICIT NONE
26 PRIVATE
27
29 PUBLIC :: dbcsr_maxabs
30 PUBLIC :: dbcsr_frobenius_norm
31 PUBLIC :: dbcsr_gershgorin_norm
32 PUBLIC :: dbcsr_init_random
35 PUBLIC :: dbcsr_add_on_diag
36 PUBLIC :: dbcsr_dot, dbcsr_dot_batch
37 PUBLIC :: dbcsr_trace
38 PUBLIC :: dbcsr_get_block_diag
39 PUBLIC :: dbcsr_scale_by_vector
40 PUBLIC :: dbcsr_get_diag
41 PUBLIC :: dbcsr_set_diag
42 PUBLIC :: dbcsr_checksum
43 PUBLIC :: dbcsr_print
44
45CONTAINS
46
47! **************************************************************************************************
48!> \brief Hadamard product: C = A . B (C needs to be different from A and B)
49!> \param matrix_a ...
50!> \param matrix_b ...
51!> \param matrix_c ...
52! **************************************************************************************************
53 SUBROUTINE dbcsr_hadamard_product(matrix_a, matrix_b, matrix_c)
54 TYPE(dbcsr_type), INTENT(IN) :: matrix_a, matrix_b
55 TYPE(dbcsr_type), INTENT(INOUT) :: matrix_c
56
57 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_hadamard_product'
58
59 INTEGER :: col, handle, nblkrows_tot_a, &
60 nblkrows_tot_b, nblkrows_tot_c, row
61 LOGICAL :: found_b
62 REAL(kind=dp), DIMENSION(:, :), POINTER :: block_a, block_b
63 TYPE(dbcsr_iterator_type) :: iter
64
65 CALL timeset(routinen, handle)
66 cpassert(omp_get_num_threads() == 1)
67
68 CALL dbcsr_get_info(matrix_a, nblkrows_total=nblkrows_tot_a)
69 CALL dbcsr_get_info(matrix_b, nblkrows_total=nblkrows_tot_b)
70 CALL dbcsr_get_info(matrix_c, nblkrows_total=nblkrows_tot_c)
71 IF (nblkrows_tot_a /= nblkrows_tot_b .OR. nblkrows_tot_a /= nblkrows_tot_c) THEN
72 cpabort("matrices not consistent")
73 END IF
74
75 CALL dbcsr_clear(matrix_c)
76 CALL dbcsr_work_create(matrix_c, work_mutable=.true.)
77!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix_a, matrix_b, matrix_c) &
78!$OMP PRIVATE(iter, row, col, block_a, block_b, found_b)
79 CALL dbcsr_iterator_readonly_start(iter, matrix_a)
80 DO WHILE (dbcsr_iterator_blocks_left(iter))
81 CALL dbcsr_iterator_next_block(iter, row, col, block_a)
82 CALL dbcsr_get_readonly_block_p(matrix_b, row, col, block_b, found_b)
83 IF (found_b) THEN
84 CALL dbcsr_put_block(matrix_c, row, col, block_a*block_b)
85 END IF
86 END DO
87 CALL dbcsr_iterator_stop(iter)
88!$OMP END PARALLEL
89 CALL dbcsr_finalize(matrix_c)
90 CALL timestop(handle)
91 END SUBROUTINE dbcsr_hadamard_product
92
93! **************************************************************************************************
94!> \brief Compute the maxabs norm of a dbcsr matrix
95!> \param matrix ...
96!> \return ...
97! **************************************************************************************************
98 FUNCTION dbcsr_maxabs(matrix) RESULT(norm)
99 TYPE(dbcsr_type), INTENT(IN) :: matrix
100 REAL(dp) :: norm
101
102 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_maxabs'
103
104 INTEGER :: handle
105 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
106 TYPE(dbcsr_iterator_type) :: iter
107 TYPE(mp_comm_type) :: group
108
109 CALL timeset(routinen, handle)
110 cpassert(omp_get_num_threads() == 1)
111
112 norm = 0.0_dp
113!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix) PRIVATE(iter, block) REDUCTION(MAX:norm)
114 CALL dbcsr_iterator_readonly_start(iter, matrix)
115 DO WHILE (dbcsr_iterator_blocks_left(iter))
116 CALL dbcsr_iterator_next_block(iter, block=block)
117 norm = max(norm, maxval(abs(block)))
118 END DO
119 CALL dbcsr_iterator_stop(iter)
120!$OMP END PARALLEL
121
122 CALL dbcsr_get_info(matrix, group=group)
123 CALL group%max(norm)
124
125 CALL timestop(handle)
126 END FUNCTION dbcsr_maxabs
127
128! **************************************************************************************************
129!> \brief Compute the frobenius norm of a dbcsr matrix
130!> \param matrix ...
131!> \return ...
132! **************************************************************************************************
133 FUNCTION dbcsr_frobenius_norm(matrix) RESULT(norm)
134 TYPE(dbcsr_type), INTENT(IN) :: matrix
135 REAL(dp) :: norm
136
137 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_frobenius_norm'
138
139 INTEGER :: col, handle, row
140 LOGICAL :: has_symmetry
141 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
142 TYPE(dbcsr_iterator_type) :: iter
143 TYPE(mp_comm_type) :: group
144
145 CALL timeset(routinen, handle)
146 cpassert(omp_get_num_threads() == 1)
147
148 has_symmetry = dbcsr_has_symmetry(matrix)
149 norm = 0.0_dp
150!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, has_symmetry) &
151!$OMP PRIVATE(iter, row, col, block) REDUCTION(+:norm)
152 CALL dbcsr_iterator_readonly_start(iter, matrix)
153 DO WHILE (dbcsr_iterator_blocks_left(iter))
154 CALL dbcsr_iterator_next_block(iter, row, col, block)
155 IF (has_symmetry .AND. row /= col) THEN
156 norm = norm + 2.0_dp*sum(block**2)
157 ELSE
158 norm = norm + sum(block**2)
159 END IF
160 END DO
161 CALL dbcsr_iterator_stop(iter)
162!$OMP END PARALLEL
163
164 CALL dbcsr_get_info(matrix, group=group)
165 CALL group%sum(norm)
166 norm = sqrt(norm)
167
168 CALL timestop(handle)
169 END FUNCTION dbcsr_frobenius_norm
170
171! **************************************************************************************************
172!> \brief Compute the gershgorin norm of a dbcsr matrix
173!> \param matrix ...
174!> \return ...
175! **************************************************************************************************
176 FUNCTION dbcsr_gershgorin_norm(matrix) RESULT(norm)
177 TYPE(dbcsr_type), INTENT(IN) :: matrix
178 REAL(dp) :: norm
179
180 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_gershgorin_norm'
181
182 INTEGER :: col, col_offset, handle, i, j, ncol, &
183 nrow, row, row_offset
184 LOGICAL :: has_symmetry
185 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: buffer, buffer_priv
186 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
187 TYPE(dbcsr_iterator_type) :: iter
188 TYPE(mp_comm_type) :: group
189
190 CALL timeset(routinen, handle)
191 cpassert(omp_get_num_threads() == 1)
192
193 has_symmetry = dbcsr_has_symmetry(matrix)
194 CALL dbcsr_get_info(matrix, nfullrows_total=nrow, nfullcols_total=ncol)
195 cpassert(nrow == ncol)
196 ALLOCATE (buffer(nrow))
197 buffer = 0.0_dp
198
199!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, has_symmetry, nrow, buffer) &
200!$OMP PRIVATE(iter, row, col, block, row_offset, col_offset, i, j, buffer_priv)
201 ALLOCATE (buffer_priv(nrow))
202 buffer_priv(:) = 0.0_dp
203 CALL dbcsr_iterator_readonly_start(iter, matrix)
204 DO WHILE (dbcsr_iterator_blocks_left(iter))
205 CALL dbcsr_iterator_next_block(iter, row, col, block, row_offset=row_offset, col_offset=col_offset)
206 DO j = 1, SIZE(block, 2)
207 DO i = 1, SIZE(block, 1)
208 buffer_priv(row_offset + i - 1) = buffer_priv(row_offset + i - 1) + abs(block(i, j))
209 IF (has_symmetry .AND. row /= col) THEN
210 buffer_priv(col_offset + j - 1) = buffer_priv(col_offset + j - 1) + abs(block(i, j))
211 END IF
212 END DO
213 END DO
214 END DO
215 CALL dbcsr_iterator_stop(iter)
216!$OMP CRITICAL(dbcsr_gershgorin_crit)
217 buffer(:) = buffer(:) + buffer_priv(:)
218!$OMP END CRITICAL(dbcsr_gershgorin_crit)
219 DEALLOCATE (buffer_priv)
220!$OMP END PARALLEL
221
222 CALL dbcsr_get_info(matrix, group=group)
223 CALL group%sum(buffer)
224 norm = maxval(buffer)
225 DEALLOCATE (buffer)
226
227 CALL timestop(handle)
228 END FUNCTION dbcsr_gershgorin_norm
229
230! **************************************************************************************************
231!> \brief Fills the given matrix with random numbers.
232!> \param matrix ...
233!> \param keep_sparsity ...
234! **************************************************************************************************
235 SUBROUTINE dbcsr_init_random(matrix, keep_sparsity)
236 TYPE(dbcsr_type), INTENT(INOUT) :: matrix
237 LOGICAL, OPTIONAL :: keep_sparsity
238
239 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_init_random'
240
241 INTEGER :: col, col_size, handle, ncol, nrow, row, &
242 row_size
243 INTEGER, DIMENSION(4) :: iseed
244 LOGICAL :: my_keep_sparsity
245 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
246 TYPE(dbcsr_iterator_type) :: iter
247
248 CALL timeset(routinen, handle)
249 cpassert(omp_get_num_threads() == 1)
250
251 my_keep_sparsity = .false.
252 IF (PRESENT(keep_sparsity)) my_keep_sparsity = keep_sparsity
253 IF (.NOT. my_keep_sparsity) CALL dbcsr_reserve_all_blocks(matrix)
254 CALL dbcsr_get_info(matrix, nblkrows_total=nrow, nblkcols_total=ncol)
255
256!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, nrow, ncol) &
257!$OMP PRIVATE(iter, row, col, block, row_size, col_size, iseed)
258 CALL dbcsr_iterator_start(iter, matrix)
259 DO WHILE (dbcsr_iterator_blocks_left(iter))
260 CALL dbcsr_iterator_next_block(iter, row, col, block, row_size=row_size, col_size=col_size)
261 ! set the seed for dlarnv, is here to guarantee same value of the random numbers
262 ! for all layouts (and block distributions)
263 iseed = generate_larnv_seed(row, nrow, col, ncol, 1)
264 CALL dlarnv(1, iseed, row_size*col_size, block(1, 1))
265 END DO
266 CALL dbcsr_iterator_stop(iter)
267!$OMP END PARALLEL
268
269 CALL timestop(handle)
270 END SUBROUTINE dbcsr_init_random
271
272! **************************************************************************************************
273!> \brief Reserves all diagonal blocks.
274!> \param matrix ...
275! **************************************************************************************************
276 SUBROUTINE dbcsr_reserve_diag_blocks(matrix)
277 TYPE(dbcsr_type), INTENT(INOUT) :: matrix
278
279 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_reserve_diag_blocks'
280
281 INTEGER :: handle, i, k, mynode, nblkcols_total, &
282 nblkrows_total, owner
283 INTEGER, ALLOCATABLE, DIMENSION(:) :: local_diag
284 INTEGER, DIMENSION(:), POINTER :: local_rows
285 TYPE(dbcsr_distribution_type) :: dist
286
287 CALL timeset(routinen, handle)
288 cpassert(omp_get_num_threads() == 1)
289
290 CALL dbcsr_get_info(matrix, nblkrows_total=nblkrows_total, nblkcols_total=nblkcols_total)
291 cpassert(nblkrows_total == nblkcols_total)
292
293 CALL dbcsr_get_info(matrix, local_rows=local_rows, distribution=dist)
294 CALL dbcsr_distribution_get(dist, mynode=mynode)
295 ALLOCATE (local_diag(SIZE(local_rows)))
296
297 k = 0
298 DO i = 1, SIZE(local_rows)
299 CALL dbcsr_get_stored_coordinates(matrix, row=local_rows(i), column=local_rows(i), processor=owner)
300 IF (owner == mynode) THEN
301 k = k + 1
302 local_diag(k) = local_rows(i)
303 END IF
304 END DO
305
306 CALL dbcsr_reserve_blocks(matrix, rows=local_diag(1:k), cols=local_diag(1:k))
307 DEALLOCATE (local_diag)
308
309 CALL timestop(handle)
310 END SUBROUTINE dbcsr_reserve_diag_blocks
311
312! **************************************************************************************************
313!> \brief Reserves all blocks.
314!> \param matrix ...
315! **************************************************************************************************
316 SUBROUTINE dbcsr_reserve_all_blocks(matrix)
317 TYPE(dbcsr_type), INTENT(INOUT) :: matrix
318
319 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_reserve_all_blocks'
320
321 INTEGER :: handle, i, j, k, n
322 INTEGER, ALLOCATABLE, DIMENSION(:) :: cols, rows
323 INTEGER, DIMENSION(:), POINTER :: local_cols, local_rows
324
325 CALL timeset(routinen, handle)
326 cpassert(omp_get_num_threads() == 1)
327
328 CALL dbcsr_get_info(matrix, local_rows=local_rows, local_cols=local_cols)
329 n = SIZE(local_rows)*SIZE(local_cols)
330 ALLOCATE (rows(n), cols(n))
331
332 k = 0
333 DO i = 1, SIZE(local_rows)
334 DO j = 1, SIZE(local_cols)
335 k = k + 1
336 rows(k) = local_rows(i)
337 cols(k) = local_cols(j)
338 END DO
339 END DO
340
341 CALL dbcsr_reserve_blocks(matrix, rows=rows(1:k), cols=cols(1:k))
342 DEALLOCATE (rows, cols)
343
344 CALL timestop(handle)
345 END SUBROUTINE dbcsr_reserve_all_blocks
346
347! **************************************************************************************************
348!> \brief Adds the given scalar to the diagonal of the matrix. Reserves any missing diagonal blocks.
349!> \param matrix ...
350!> \param alpha ...
351! **************************************************************************************************
352 SUBROUTINE dbcsr_add_on_diag(matrix, alpha)
353 TYPE(dbcsr_type), INTENT(INOUT) :: matrix
354 REAL(kind=dp), INTENT(IN) :: alpha
355
356 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_add_on_diag'
357
358 INTEGER :: col, col_size, handle, i, row, row_size
359 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
360 TYPE(dbcsr_iterator_type) :: iter
361
362 CALL timeset(routinen, handle)
363 cpassert(omp_get_num_threads() == 1)
364
365 CALL dbcsr_reserve_diag_blocks(matrix)
366
367!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, alpha) &
368!$OMP PRIVATE(iter, row, col, block, row_size, col_size, i)
369 CALL dbcsr_iterator_start(iter, matrix)
370 DO WHILE (dbcsr_iterator_blocks_left(iter))
371 CALL dbcsr_iterator_next_block(iter, row, col, block, row_size=row_size, col_size=col_size)
372 IF (row == col) THEN
373 cpassert(row_size == col_size)
374 DO i = 1, row_size
375 block(i, i) = block(i, i) + alpha
376 END DO
377 END IF
378 END DO
379 CALL dbcsr_iterator_stop(iter)
380!$OMP END PARALLEL
381
382 CALL timestop(handle)
383 END SUBROUTINE dbcsr_add_on_diag
384
385! **************************************************************************************************
386!> \brief Computes the dot product of two matrices, also known as the trace of their matrix product.
387!> \param matrix_a ...
388!> \param matrix_b ...
389!> \param trace ...
390! **************************************************************************************************
391 SUBROUTINE dbcsr_dot(matrix_a, matrix_b, trace)
392 TYPE(dbcsr_type), INTENT(IN), TARGET :: matrix_a, matrix_b
393 REAL(kind=dp), INTENT(OUT) :: trace
394
395 REAL(kind=dp) :: values(1)
396 TYPE(dbcsr_p_type) :: a(1), b(1)
397
398 a(1)%matrix => matrix_a
399 b(1)%matrix => matrix_b
400 CALL dbcsr_dot_batch(a, b, values)
401 trace = values(1)
402 END SUBROUTINE dbcsr_dot
403
404! **************************************************************************************************
405!> \brief Evaluate several dot products in one OpenMP region; MPI reductions remain outside it.
406!> \param matrix_a first operands, in reduction order
407!> \param matrix_b second operands
408!> \param trace dot products
409!> \param comm optional common communicator; all operands must use this exact communicator
410! **************************************************************************************************
411 SUBROUTINE dbcsr_dot_batch(matrix_a, matrix_b, trace, comm)
412 TYPE(dbcsr_p_type), INTENT(IN) :: matrix_a(:), matrix_b(:)
413 REAL(kind=dp), INTENT(OUT) :: trace(SIZE(matrix_a))
414 CLASS(mp_comm_type), INTENT(IN), OPTIONAL :: comm
415
416 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_dot_batch'
417
418 INTEGER :: col, handle, i, row
419 LOGICAL :: found_b, has_symmetry
420 REAL(kind=dp), DIMENSION(:, :), POINTER :: block_a, block_b
421 TYPE(dbcsr_iterator_type) :: iter
422 TYPE(mp_comm_type) :: group
423
424 CALL timeset(routinen, handle)
425 cpassert(omp_get_num_threads() == 1)
426
427 cpassert(SIZE(matrix_a) == SIZE(matrix_b))
428 DO i = 1, SIZE(matrix_a)
429 has_symmetry = dbcsr_has_symmetry(matrix_a(i)%matrix)
430 cpassert(has_symmetry .EQV. dbcsr_has_symmetry(matrix_b(i)%matrix))
431 IF (PRESENT(comm)) THEN
432 CALL dbcsr_get_info(matrix_a(i)%matrix, group=group)
433 cpassert(group == comm)
434 END IF
435 END DO
436
437 trace = 0.0_dp
438!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix_a, matrix_b) &
439!$OMP PRIVATE(i, iter, row, col, block_a, block_b, found_b, has_symmetry) REDUCTION(+:trace)
440 DO i = 1, SIZE(matrix_a)
441 has_symmetry = dbcsr_has_symmetry(matrix_a(i)%matrix)
442 CALL dbcsr_iterator_readonly_start(iter, matrix_a(i)%matrix)
443 DO WHILE (dbcsr_iterator_blocks_left(iter))
444 CALL dbcsr_iterator_next_block(iter, row, col, block_a)
445 IF (SIZE(block_a) == 0) cycle ! Skip zero-sized blocks.
446 CALL dbcsr_get_readonly_block_p(matrix_b(i)%matrix, row, col, block_b, found_b)
447 IF (found_b) THEN
448 IF (has_symmetry .AND. row /= col) THEN
449 trace(i) = trace(i) + 2.0_dp*sum(block_a*block_b)
450 ELSE
451 trace(i) = trace(i) + sum(block_a*block_b)
452 END IF
453 END IF
454 END DO
455 CALL dbcsr_iterator_stop(iter)
456 END DO
457!$OMP END PARALLEL
458
459 IF (PRESENT(comm)) THEN
460 IF (SIZE(trace) > 0 .AND. comm%num_pe > 1) CALL comm%sum(trace)
461 ELSE
462 DO i = 1, SIZE(matrix_a)
463 CALL dbcsr_get_info(matrix_a(i)%matrix, group=group)
464 IF (group%num_pe > 1) CALL group%sum(trace(i))
465 END DO
466 END IF
467
468 CALL timestop(handle)
469 END SUBROUTINE dbcsr_dot_batch
470
471! **************************************************************************************************
472!> \brief Computes the trace of the given matrix, also known as the sum of its diagonal elements.
473!> \param matrix ...
474!> \param trace ...
475! **************************************************************************************************
476 SUBROUTINE dbcsr_trace(matrix, trace)
477 TYPE(dbcsr_type), INTENT(IN) :: matrix
478 REAL(kind=dp), INTENT(OUT) :: trace
479
480 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_trace'
481
482 INTEGER :: col, col_size, handle, i, row, row_size
483 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
484 TYPE(dbcsr_iterator_type) :: iter
485 TYPE(mp_comm_type) :: group
486
487 CALL timeset(routinen, handle)
488 cpassert(omp_get_num_threads() == 1)
489
490 trace = 0.0_dp
491!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix) &
492!$OMP PRIVATE(iter, row, col, block, row_size, col_size, i) REDUCTION(+:trace)
493 CALL dbcsr_iterator_readonly_start(iter, matrix)
494 DO WHILE (dbcsr_iterator_blocks_left(iter))
495 CALL dbcsr_iterator_next_block(iter, row, col, block, row_size=row_size, col_size=col_size)
496 IF (row == col) THEN
497 cpassert(row_size == col_size)
498 DO i = 1, row_size
499 trace = trace + block(i, i)
500 END DO
501 END IF
502 END DO
503 CALL dbcsr_iterator_stop(iter)
504!$OMP END PARALLEL
505
506 CALL dbcsr_get_info(matrix, group=group)
507 CALL group%sum(trace)
508
509 CALL timestop(handle)
510 END SUBROUTINE dbcsr_trace
511
512! **************************************************************************************************
513!> \brief Copies the diagonal blocks of matrix into diag.
514!> \param matrix ...
515!> \param diag ...
516! **************************************************************************************************
517 SUBROUTINE dbcsr_get_block_diag(matrix, diag)
518 TYPE(dbcsr_type), INTENT(IN) :: matrix
519 TYPE(dbcsr_type), INTENT(INOUT) :: diag
520
521 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_get_block_diag'
522
523 CHARACTER(LEN=default_string_length) :: name
524 INTEGER :: col, handle, row
525 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
526 TYPE(dbcsr_iterator_type) :: iter
527
528 CALL timeset(routinen, handle)
529 cpassert(omp_get_num_threads() == 1)
530
531 CALL dbcsr_get_info(matrix, name=name)
532 CALL dbcsr_create(diag, template=matrix, name='diag of '//name)
533 CALL dbcsr_work_create(diag, work_mutable=.true.)
534
535!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, diag) PRIVATE(iter, row, col, block)
536 CALL dbcsr_iterator_readonly_start(iter, matrix)
537 DO WHILE (dbcsr_iterator_blocks_left(iter))
538 CALL dbcsr_iterator_next_block(iter, row, col, block)
539 IF (row == col) THEN
540 CALL dbcsr_put_block(diag, row, col, block)
541 END IF
542 END DO
543 CALL dbcsr_iterator_stop(iter)
544!$OMP END PARALLEL
545 CALL dbcsr_finalize(diag)
546
547 CALL timestop(handle)
548 END SUBROUTINE dbcsr_get_block_diag
549
550! **************************************************************************************************
551!> \brief Scales the rows/columns of given matrix.
552!> \param matrix Matrix to be scaled in-place.
553!> \param alpha Vector with scaling factors.
554!> \param side Side from which to apply the vector. Allowed values are 'right' and 'left'.
555! **************************************************************************************************
556 SUBROUTINE dbcsr_scale_by_vector(matrix, alpha, side)
557 TYPE(dbcsr_type), INTENT(INOUT) :: matrix
558 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: alpha
559 CHARACTER(LEN=*), INTENT(IN) :: side
560
561 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_scale_by_vector'
562
563 INTEGER :: col_offset, col_size, handle, i, &
564 nfullcols_total, nfullrows_total, &
565 row_offset, row_size
566 LOGICAL :: right
567 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
568 TYPE(dbcsr_iterator_type) :: iter
569
570 CALL timeset(routinen, handle)
571 cpassert(omp_get_num_threads() == 1)
572
573 IF (side == 'right') THEN
574 right = .true.
575 ELSE IF (side == 'left') THEN
576 right = .false.
577 ELSE
578 cpabort("Unknown side: "//trim(side))
579 END IF
580
581 ! Check that alpha and matrix have matching sizes.
582 CALL dbcsr_get_info(matrix, nfullrows_total=nfullrows_total, nfullcols_total=nfullcols_total)
583 IF (right) THEN
584 cpassert(nfullcols_total == SIZE(alpha))
585 ELSE
586 cpassert(nfullrows_total == SIZE(alpha))
587 END IF
588
589!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, alpha, right) &
590!$OMP PRIVATE(iter, block, row_size, col_size, row_offset, col_offset, i)
591 CALL dbcsr_iterator_start(iter, matrix)
592 DO WHILE (dbcsr_iterator_blocks_left(iter))
593 CALL dbcsr_iterator_next_block(iter, block=block, row_size=row_size, col_size=col_size, &
594 row_offset=row_offset, col_offset=col_offset)
595 IF (SIZE(block) == 0) cycle ! Skip zero-sized blocks.
596 IF (right) THEN
597 DO i = 1, col_size
598 block(:, i) = block(:, i)*alpha(col_offset + i - 1)
599 END DO
600 ELSE
601 DO i = 1, row_size
602 block(i, :) = block(i, :)*alpha(row_offset + i - 1)
603 END DO
604 END IF
605 END DO
606 CALL dbcsr_iterator_stop(iter)
607!$OMP END PARALLEL
608
609 CALL timestop(handle)
610 END SUBROUTINE dbcsr_scale_by_vector
611
612! **************************************************************************************************
613!> \brief Copies the diagonal elements from the given matrix into the given array.
614!> \param matrix ...
615!> \param diag ...
616! **************************************************************************************************
617 SUBROUTINE dbcsr_get_diag(matrix, diag)
618 TYPE(dbcsr_type), INTENT(IN) :: matrix
619 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: diag
620
621 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_get_diag'
622
623 INTEGER :: col, col_size, handle, i, &
624 nfullcols_total, nfullrows_total, row, &
625 row_offset, row_size
626 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
627 TYPE(dbcsr_iterator_type) :: iter
628
629 CALL timeset(routinen, handle)
630 cpassert(omp_get_num_threads() == 1)
631
632 CALL dbcsr_get_info(matrix, nfullrows_total=nfullrows_total, nfullcols_total=nfullcols_total)
633 cpassert(nfullrows_total == nfullcols_total)
634 cpassert(nfullrows_total == SIZE(diag))
635
636 diag(:) = 0.0_dp
637!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, diag) &
638!$OMP PRIVATE(iter, row, col, block, row_size, col_size, row_offset, i)
639 CALL dbcsr_iterator_readonly_start(iter, matrix)
640 DO WHILE (dbcsr_iterator_blocks_left(iter))
641 CALL dbcsr_iterator_next_block(iter, row, col, block, row_size=row_size, &
642 col_size=col_size, row_offset=row_offset)
643 IF (row == col) THEN
644 cpassert(row_size == col_size)
645 DO i = 1, row_size
646 diag(row_offset + i - 1) = block(i, i)
647 END DO
648 END IF
649 END DO
650 CALL dbcsr_iterator_stop(iter)
651!$OMP END PARALLEL
652
653 CALL timestop(handle)
654 END SUBROUTINE dbcsr_get_diag
655
656! **************************************************************************************************
657!> \brief Copies the diagonal elements from the given array into the given matrix.
658!> \param matrix ...
659!> \param diag ...
660! **************************************************************************************************
661 SUBROUTINE dbcsr_set_diag(matrix, diag)
662 TYPE(dbcsr_type), INTENT(INOUT) :: matrix
663 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: diag
664
665 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_set_diag'
666
667 INTEGER :: col, col_size, handle, i, &
668 nfullcols_total, nfullrows_total, row, &
669 row_offset, row_size
670 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
671 TYPE(dbcsr_iterator_type) :: iter
672
673 CALL timeset(routinen, handle)
674 cpassert(omp_get_num_threads() == 1)
675
676 CALL dbcsr_get_info(matrix, nfullrows_total=nfullrows_total, nfullcols_total=nfullcols_total)
677 cpassert(nfullrows_total == nfullcols_total)
678 cpassert(nfullrows_total == SIZE(diag))
679
680 CALL dbcsr_reserve_diag_blocks(matrix)
681
682!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, diag) &
683!$OMP PRIVATE(iter, row, col, block, row_size, col_size, row_offset, i)
684 CALL dbcsr_iterator_start(iter, matrix)
685 DO WHILE (dbcsr_iterator_blocks_left(iter))
686 CALL dbcsr_iterator_next_block(iter, row, col, block, row_size=row_size, &
687 col_size=col_size, row_offset=row_offset)
688 IF (row == col) THEN
689 cpassert(row_size == col_size)
690 DO i = 1, row_size
691 block(i, i) = diag(row_offset + i - 1)
692 END DO
693 END IF
694 END DO
695 CALL dbcsr_iterator_stop(iter)
696!$OMP END PARALLEL
697
698 CALL timestop(handle)
699 END SUBROUTINE dbcsr_set_diag
700
701! **************************************************************************************************
702!> \brief Calculates the checksum of a DBCSR matrix.
703!> \param matrix ...
704!> \param pos Enable position-dependent checksum.
705!> \return ...
706! **************************************************************************************************
707 FUNCTION dbcsr_checksum(matrix, pos) RESULT(checksum)
708
709 TYPE(dbcsr_type), INTENT(IN) :: matrix
710 LOGICAL, INTENT(IN), OPTIONAL :: pos
711 REAL(kind=dp) :: checksum
712
713 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_checksum'
714
715 INTEGER :: col_offset, col_size, handle, i, j, &
716 row_offset, row_size
717 LOGICAL :: my_pos
718 REAL(kind=dp) :: position_factor
719 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
720 TYPE(dbcsr_iterator_type) :: iter
721 TYPE(mp_comm_type) :: group
722
723 CALL timeset(routinen, handle)
724 cpassert(omp_get_num_threads() == 1)
725
726 my_pos = .false.
727 IF (PRESENT(pos)) THEN
728 my_pos = pos
729 END IF
730
731 checksum = 0.0_dp
732!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, my_pos) &
733!$OMP PRIVATE(iter, block, row_size, col_size, row_offset, col_offset, i, j, position_factor) &
734!$OMP REDUCTION(+:checksum)
735 CALL dbcsr_iterator_readonly_start(iter, matrix)
736 DO WHILE (dbcsr_iterator_blocks_left(iter))
737 CALL dbcsr_iterator_next_block(iter, block=block, row_size=row_size, col_size=col_size, &
738 row_offset=row_offset, col_offset=col_offset)
739 IF (my_pos) THEN
740 DO i = 1, row_size
741 DO j = 1, col_size
742 position_factor = log(real((row_offset + i - 1)*(col_offset + j - 1), kind=dp))
743 checksum = checksum + block(i, j)*position_factor
744 END DO
745 END DO
746 ELSE
747 checksum = checksum + sum(block**2)
748 END IF
749 END DO
750 CALL dbcsr_iterator_stop(iter)
751!$OMP END PARALLEL
752
753 CALL dbcsr_get_info(matrix, group=group)
754 CALL group%sum(checksum)
755
756 CALL timestop(handle)
757 END FUNCTION dbcsr_checksum
758
759! **************************************************************************************************
760!> \brief Prints given matrix in matlab format (only present blocks).
761!> \param matrix ...
762!> \param variable_name ...
763!> \param unit_nr ...
764! **************************************************************************************************
765 SUBROUTINE dbcsr_print(matrix, variable_name, unit_nr)
766 TYPE(dbcsr_type), INTENT(IN) :: matrix
767 CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: variable_name
768 INTEGER, INTENT(IN), OPTIONAL :: unit_nr
769
770 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbcsr_print'
771
772 CHARACTER(LEN=default_string_length) :: my_variable_name, name
773 INTEGER :: col_offset, col_size, handle, i, iw, j, nblkcols_total, nblkrows_total, &
774 nfullcols_total, nfullrows_total, row_offset, row_size
775 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
776 TYPE(dbcsr_iterator_type) :: iter
777
778 CALL timeset(routinen, handle)
779 cpassert(omp_get_num_threads() == 1)
780
782 IF (PRESENT(unit_nr)) iw = unit_nr
783
784 my_variable_name = 'a'
785 IF (PRESENT(variable_name)) my_variable_name = variable_name
786
787 ! Print matrix properties.
788 CALL dbcsr_get_info(matrix, name=name, &
789 nblkrows_total=nblkrows_total, nblkcols_total=nblkcols_total, &
790 nfullrows_total=nfullrows_total, nfullcols_total=nfullcols_total)
791 WRITE (iw, *) "===", routinen, "==="
792 WRITE (iw, *) "Name:", name
793 WRITE (iw, *) "Symmetry:", dbcsr_has_symmetry(matrix)
794 WRITE (iw, *) "Number of blocks:", dbcsr_get_num_blocks(matrix)
795 WRITE (iw, *) "Data size:", dbcsr_get_data_size(matrix)
796 WRITE (iw, *) "Occupation:", dbcsr_get_occupation(matrix)
797 WRITE (iw, *) "Full size:", nfullrows_total, "x", nfullcols_total
798 WRITE (iw, *) "Blocked size:", nblkrows_total, "x", nblkcols_total
799
800 ! Print matrix blocks.
801!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, iw, my_variable_name) &
802!$OMP PRIVATE(iter, block, row_size, col_size, row_offset, col_offset, i, j)
803 CALL dbcsr_iterator_readonly_start(iter, matrix)
804 DO WHILE (dbcsr_iterator_blocks_left(iter))
805 CALL dbcsr_iterator_next_block(iter, block=block, row_size=row_size, col_size=col_size, &
806 row_offset=row_offset, col_offset=col_offset)
807!$OMP CRITICAL(dbcsr_print_crit)
808 DO i = 1, row_size
809 DO j = 1, col_size
810 WRITE (iw, '(A,I4,A,I4,A,E23.16,A)') trim(my_variable_name)//'(', &
811 row_offset + i - 1, ',', col_offset + j - 1, ')=', block(i, j), ';'
812 END DO
813 END DO
814!$OMP END CRITICAL(dbcsr_print_crit)
815 END DO
816 CALL dbcsr_iterator_stop(iter)
817!$OMP END PARALLEL
818
819 CALL timestop(handle)
820 END SUBROUTINE dbcsr_print
821
822END MODULE cp_dbcsr_contrib
logical function, public dbcsr_has_symmetry(matrix)
...
integer function, public dbcsr_get_data_size(matrix)
...
subroutine, public dbcsr_get_readonly_block_p(matrix, row, col, block, found, row_size, col_size)
Like dbcsr_get_block_p() but with matrix being INTENT(IN). When invoking this routine,...
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
subroutine, public dbcsr_reserve_blocks(matrix, rows, cols)
...
subroutine, public dbcsr_get_stored_coordinates(matrix, row, column, processor)
...
subroutine, public dbcsr_work_create(matrix, nblks_guess, sizedata_guess, n, work_mutable)
...
subroutine, public dbcsr_iterator_next_block(iterator, row, column, block, block_number_argument_has_been_removed, row_size, col_size, row_offset, col_offset, transposed)
...
real(kind=dp) function, public dbcsr_get_occupation(matrix)
...
subroutine, public dbcsr_finalize(matrix)
...
subroutine, public dbcsr_iterator_start(iterator, matrix, shared, dynamic, dynamic_byrows)
...
integer function, public dbcsr_get_num_blocks(matrix)
...
subroutine, public dbcsr_iterator_readonly_start(iterator, matrix, shared, dynamic, dynamic_byrows)
Like dbcsr_iterator_start() but with matrix being INTENT(IN). When invoking this routine,...
subroutine, public dbcsr_clear(matrix)
...
subroutine, public dbcsr_put_block(matrix, row, col, block, summation)
...
subroutine, public dbcsr_distribution_get(dist, row_dist, col_dist, nrows, ncols, has_threads, group, mynode, numnodes, nprows, npcols, myprow, mypcol, pgrid, subgroups_defined, prow_group, pcol_group)
...
subroutine, public dbcsr_set_diag(matrix, diag)
Copies the diagonal elements from the given array into the given matrix.
real(dp) function, public dbcsr_gershgorin_norm(matrix)
Compute the gershgorin norm of a dbcsr matrix.
real(kind=dp) function, public dbcsr_checksum(matrix, pos)
Calculates the checksum of a DBCSR matrix.
subroutine, public dbcsr_get_diag(matrix, diag)
Copies the diagonal elements from the given matrix into the given array.
subroutine, public dbcsr_add_on_diag(matrix, alpha)
Adds the given scalar to the diagonal of the matrix. Reserves any missing diagonal blocks.
subroutine, public dbcsr_print(matrix, variable_name, unit_nr)
Prints given matrix in matlab format (only present blocks).
real(dp) function, public dbcsr_maxabs(matrix)
Compute the maxabs norm of a dbcsr matrix.
subroutine, public dbcsr_trace(matrix, trace)
Computes the trace of the given matrix, also known as the sum of its diagonal elements.
subroutine, public dbcsr_reserve_all_blocks(matrix)
Reserves all blocks.
subroutine, public dbcsr_init_random(matrix, keep_sparsity)
Fills the given matrix with random numbers.
real(dp) function, public dbcsr_frobenius_norm(matrix)
Compute the frobenius norm of a dbcsr matrix.
subroutine, public dbcsr_dot(matrix_a, matrix_b, trace)
Computes the dot product of two matrices, also known as the trace of their matrix product.
subroutine, public dbcsr_hadamard_product(matrix_a, matrix_b, matrix_c)
Hadamard product: C = A . B (C needs to be different from A and B).
subroutine, public dbcsr_reserve_diag_blocks(matrix)
Reserves all diagonal blocks.
subroutine, public dbcsr_scale_by_vector(matrix, alpha, side)
Scales the rows/columns of given matrix.
subroutine, public dbcsr_dot_batch(matrix_a, matrix_b, trace, comm)
Evaluate several dot products in one OpenMP region; MPI reductions remain outside it.
subroutine, public dbcsr_get_block_diag(matrix, diag)
Copies the diagonal blocks of matrix into diag.
integer function, dimension(4), public generate_larnv_seed(irow, nrow, icol, ncol, ival)
Generate a seed respecting the lapack constraints,.
Definition dbm_tests.F:432
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
Interface to the message passing library MPI.