9 USE iso_c_binding,
ONLY: c_associated, c_bool, c_char, c_double, c_f_pointer, c_funloc, c_funptr, &
10 c_int, c_int64_t, c_null_char, c_null_ptr, c_ptr
19#define DBM_VALIDATE_NBLOCKS_MATCH .TRUE.
20#define DBM_VALIDATE_THRESHOLD 5e-10_dp
22#if defined(DBM_VALIDATE_AGAINST_DBCSR)
23 USE dbcsr_block_access,
ONLY: dbcsr_get_block_p, &
26 USE dbcsr_dist_methods,
ONLY: dbcsr_distribution_col_dist, &
27 dbcsr_distribution_hold, &
28 dbcsr_distribution_new, &
29 dbcsr_distribution_release, &
30 dbcsr_distribution_row_dist
31 USE dbcsr_dist_operations,
ONLY: dbcsr_get_stored_coordinates
32 USE dbcsr_dist_util,
ONLY: dbcsr_checksum
33 USE dbcsr_iterator_operations,
ONLY: dbcsr_iterator_blocks_left, &
34 dbcsr_iterator_next_block, &
35 dbcsr_iterator_start, &
37 USE dbcsr_methods,
ONLY: dbcsr_col_block_sizes, &
38 dbcsr_get_num_blocks, &
43 USE dbcsr_mp_methods,
ONLY: dbcsr_mp_new
44 USE dbcsr_multiply_api,
ONLY: dbcsr_multiply
45 USE dbcsr_operations,
ONLY: dbcsr_add, &
53 USE dbcsr_transformations,
ONLY: dbcsr_redistribute
54 USE dbcsr_types,
ONLY: dbcsr_distribution_obj, &
60 dbcsr_type_no_symmetry, &
62 USE dbcsr_work_operations,
ONLY: dbcsr_create, &
64 USE dbcsr_data_methods,
ONLY: dbcsr_scalar
67#include "../base/base_uses.f90"
73 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'dbm_api'
125 TYPE(C_PTR) :: c_ptr = c_null_ptr
126#if defined(DBM_VALIDATE_AGAINST_DBCSR)
127 TYPE(dbcsr_distribution_obj) :: dbcsr
133 TYPE(C_PTR) :: c_ptr = c_null_ptr
134#if defined(DBM_VALIDATE_AGAINST_DBCSR)
135 TYPE(dbcsr_type) :: dbcsr
141 TYPE(C_PTR) :: c_ptr = c_null_ptr
146#if defined(DBM_VALIDATE_AGAINST_DBCSR)
153 TYPE(dbm_type),
INTENT(IN) :: matrix
155 INTEGER :: col, col_size, col_size_dbcsr, i, j, &
156 num_blocks, num_blocks_dbcsr, &
157 num_blocks_diff, row, row_size, &
159 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: local_cols, local_rows
160 LOGICAL :: transposed
161 REAL(dp) :: norm2, rel_diff
162 REAL(dp),
DIMENSION(:, :),
POINTER :: block, block_dbcsr
163 TYPE(C_PTR) :: block_c
164 TYPE(dbcsr_iterator) :: iter
166 SUBROUTINE dbm_get_block_p_c(matrix, row, col, block, row_size, col_size) &
167 BIND(C, name="dbm_get_block_p")
168 IMPORT :: c_ptr, c_int
169 TYPE(C_PTR),
VALUE :: matrix
170 INTEGER(kind=C_INT),
VALUE :: row
171 INTEGER(kind=C_INT),
VALUE :: col
173 INTEGER(kind=C_INT) :: row_size
174 INTEGER(kind=C_INT) :: col_size
175 END SUBROUTINE dbm_get_block_p_c
182 num_blocks_dbcsr = dbcsr_get_num_blocks(matrix%dbcsr)
184 num_blocks_diff = abs(num_blocks - num_blocks_dbcsr)
185 IF (num_blocks_diff /= 0)
THEN
186 WRITE (*, *)
"num_blocks mismatch dbcsr:", num_blocks_dbcsr,
"new:", num_blocks
187 IF (dbm_validate_nblocks_match)
THEN
188 cpabort(
"num_blocks mismatch")
192 IF (dbm_validate_nblocks_match)
THEN
193 cpassert(
dbm_get_nze(matrix) == dbcsr_get_nze(matrix%dbcsr))
198 CALL dbcsr_iterator_start(iter, matrix%dbcsr)
199 DO WHILE (dbcsr_iterator_blocks_left(iter))
200 CALL dbcsr_iterator_next_block(iter, row=row, column=col, block=block_dbcsr, &
201 transposed=transposed, &
202 row_size=row_size_dbcsr, col_size=col_size_dbcsr)
203 cpassert(.NOT. transposed)
204 CALL dbm_get_block_p_c(matrix=matrix%c_ptr, row=row - 1, col=col - 1, &
205 block=block_c, row_size=row_size, col_size=col_size)
207 cpassert(row_size == row_size_dbcsr .AND. col_size == col_size_dbcsr)
208 IF (
SIZE(block_dbcsr) == 0)
THEN
211 IF (.NOT. c_associated(block_c))
THEN
212 cpassert(maxval(abs(block_dbcsr)) < dbm_validate_threshold)
216 CALL c_f_pointer(block_c, block, shape=[row_size, col_size])
219 rel_diff = abs(block(i, j) - block_dbcsr(i, j))/max(1.0_dp, abs(block_dbcsr(i, j)))
220 IF (rel_diff > dbm_validate_threshold)
THEN
221 WRITE (*, *)
"row:", row,
"col:", col,
"i:", i,
"j:", j,
"rel_diff:", rel_diff
222 WRITE (*, *)
"values dbcsr:", block_dbcsr(i, j),
"new:", block(i, j)
223 cpabort(
"block value mismatch")
227 norm2 = norm2 + sum(block**2)
228 block_dbcsr(:, :) = block(:, :)
230 CALL dbcsr_iterator_stop(iter)
253 TYPE(
dbm_type),
INTENT(IN) :: matrix
267 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
268 CHARACTER(len=*),
INTENT(IN) :: name
269 TYPE(
dbm_type),
INTENT(IN) :: template
271 INTEGER,
CONTIGUOUS,
DIMENSION(:),
POINTER :: col_block_sizes, row_block_sizes
280 row_block_sizes=row_block_sizes, &
281 col_block_sizes=col_block_sizes)
294 SUBROUTINE dbm_create(matrix, name, dist, row_block_sizes, col_block_sizes)
295 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
296 CHARACTER(len=*),
INTENT(IN) :: name
298 INTEGER,
CONTIGUOUS,
DIMENSION(:),
INTENT(IN), &
299 POINTER :: row_block_sizes, col_block_sizes
302 SUBROUTINE dbm_create_c(matrix, dist, name, nrows, ncols, row_sizes, col_sizes) &
303 BIND(C, name="dbm_create")
304 IMPORT :: c_ptr, c_char, c_int
305 TYPE(c_ptr) :: matrix
306 TYPE(c_ptr),
VALUE :: dist
307 CHARACTER(kind=C_CHAR),
DIMENSION(*) :: name
308 INTEGER(kind=C_INT),
VALUE :: nrows
309 INTEGER(kind=C_INT),
VALUE :: ncols
310 INTEGER(kind=C_INT),
DIMENSION(*) :: row_sizes
311 INTEGER(kind=C_INT),
DIMENSION(*) :: col_sizes
312 END SUBROUTINE dbm_create_c
315 cpassert(.NOT. c_associated(matrix%c_ptr))
316 CALL dbm_create_c(matrix=matrix%c_ptr, &
318 name=trim(name)//c_null_char, &
319 nrows=
SIZE(row_block_sizes), &
320 ncols=
SIZE(col_block_sizes), &
321 row_sizes=row_block_sizes, &
322 col_sizes=col_block_sizes)
323 cpassert(c_associated(matrix%c_ptr))
325#if defined(DBM_VALIDATE_AGAINST_DBCSR)
326 CALL dbcsr_create(matrix%dbcsr, name=name, dist=dist%dbcsr, &
327 matrix_type=dbcsr_type_no_symmetry, &
328 row_blk_size=row_block_sizes, col_blk_size=col_block_sizes, &
329 data_type=dbcsr_type_real_8)
341 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
345#if defined(DBM_VALIDATE_AGAINST_DBCSR)
346 CALL dbcsr_finalize(matrix%dbcsr)
356 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
359 SUBROUTINE dbm_release_c(matrix) &
360 BIND(C, name="dbm_release")
362 TYPE(c_ptr),
VALUE :: matrix
363 END SUBROUTINE dbm_release_c
366 CALL dbm_release_c(matrix=matrix%c_ptr)
367 matrix%c_ptr = c_null_ptr
369#if defined(DBM_VALIDATE_AGAINST_DBCSR)
370 CALL dbcsr_release(matrix%dbcsr)
382 TYPE(
dbm_type),
INTENT(INOUT) :: matrix_a
383 TYPE(
dbm_type),
INTENT(IN) :: matrix_b
385 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_copy'
389 SUBROUTINE dbm_copy_c(matrix_a, matrix_b) &
390 BIND(C, name="dbm_copy")
392 TYPE(c_ptr),
VALUE :: matrix_a
393 TYPE(c_ptr),
VALUE :: matrix_b
394 END SUBROUTINE dbm_copy_c
397 CALL timeset(routinen, handle)
398 CALL dbm_copy_c(matrix_a=matrix_a%c_ptr, matrix_b=matrix_b%c_ptr)
400#if defined(DBM_VALIDATE_AGAINST_DBCSR)
401 CALL dbcsr_copy(matrix_a%dbcsr, matrix_b%dbcsr)
404 CALL timestop(handle)
414 TYPE(
dbm_type),
INTENT(IN) :: matrix
415 TYPE(
dbm_type),
INTENT(INOUT) :: redist
417 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_redistribute'
421 SUBROUTINE dbm_redistribute_c(matrix, redist) &
422 BIND(C, name="dbm_redistribute")
424 TYPE(c_ptr),
VALUE :: matrix
425 TYPE(c_ptr),
VALUE :: redist
426 END SUBROUTINE dbm_redistribute_c
429 CALL timeset(routinen, handle)
430 CALL dbm_redistribute_c(matrix=matrix%c_ptr, redist=redist%c_ptr)
432#if defined(DBM_VALIDATE_AGAINST_DBCSR)
433 CALL dbcsr_redistribute(matrix%dbcsr, redist%dbcsr)
436 CALL timestop(handle)
451 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
452 INTEGER,
INTENT(IN) :: row, col
453 REAL(
dp),
DIMENSION(:, :),
INTENT(OUT),
POINTER :: block
454 INTEGER,
INTENT(OUT),
OPTIONAL :: row_size, col_size
456 INTEGER :: my_col_size, my_row_size
457 TYPE(c_ptr) :: block_c
459 SUBROUTINE dbm_get_block_p_c(matrix, row, col, block, row_size, col_size) &
460 BIND(C, name="dbm_get_block_p")
461 IMPORT :: c_ptr, c_int
462 TYPE(c_ptr),
VALUE :: matrix
463 INTEGER(kind=C_INT),
VALUE :: row
464 INTEGER(kind=C_INT),
VALUE :: col
466 INTEGER(kind=C_INT) :: row_size
467 INTEGER(kind=C_INT) :: col_size
468 END SUBROUTINE dbm_get_block_p_c
471 CALL dbm_get_block_p_c(matrix=matrix%c_ptr, row=row - 1, col=col - 1, &
472 block=block_c, row_size=my_row_size, col_size=my_col_size)
473 IF (c_associated(block_c))
THEN
474 CALL c_f_pointer(block_c, block, shape=[my_row_size, my_col_size])
478 IF (
PRESENT(row_size)) row_size = my_row_size
479 IF (
PRESENT(col_size)) col_size = my_col_size
493 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
494 INTEGER,
INTENT(IN) :: row, col
495 REAL(
dp),
CONTIGUOUS,
DIMENSION(:, :),
INTENT(IN) :: block
496 LOGICAL,
INTENT(IN),
OPTIONAL :: summation
498 LOGICAL :: my_summation
500 SUBROUTINE dbm_put_block_c(matrix, row, col, summation, block) &
501 BIND(C, name="dbm_put_block")
502 IMPORT :: c_ptr, c_int, c_bool, c_double
503 TYPE(c_ptr),
VALUE :: matrix
504 INTEGER(kind=C_INT),
VALUE :: row
505 INTEGER(kind=C_INT),
VALUE :: col
506 LOGICAL(kind=C_BOOL),
VALUE :: summation
507 REAL(kind=c_double),
DIMENSION(*) :: block
508 END SUBROUTINE dbm_put_block_c
511 my_summation = .false.
512 IF (
PRESENT(summation)) my_summation = summation
514 CALL dbm_put_block_c(matrix=matrix%c_ptr, &
515 row=row - 1, col=col - 1, &
516 summation=
LOGICAL(my_summation, C_BOOL), &
519#if defined(DBM_VALIDATE_AGAINST_DBCSR)
520 CALL dbcsr_put_block(matrix%dbcsr, row, col, block, summation=summation)
531 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
534 SUBROUTINE dbm_clear_c(matrix) &
535 BIND(C, name="dbm_clear")
537 TYPE(c_ptr),
VALUE :: matrix
538 END SUBROUTINE dbm_clear_c
541 CALL dbm_clear_c(matrix=matrix%c_ptr)
543#if defined(DBM_VALIDATE_AGAINST_DBCSR)
544 CALL dbcsr_clear(matrix%dbcsr)
557 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
558 REAL(
dp),
INTENT(IN) :: eps
560 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_filter'
564 SUBROUTINE dbm_filter_c(matrix, eps) &
565 BIND(C, name="dbm_filter")
566 IMPORT :: c_ptr, c_double
567 TYPE(c_ptr),
VALUE :: matrix
568 REAL(kind=c_double),
VALUE :: eps
569 END SUBROUTINE dbm_filter_c
572 CALL timeset(routinen, handle)
574 CALL dbm_filter_c(matrix=matrix%c_ptr, eps=eps)
576#if defined(DBM_VALIDATE_AGAINST_DBCSR)
577 CALL dbcsr_filter(matrix%dbcsr, eps)
580 CALL timestop(handle)
591 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
592 INTEGER,
DIMENSION(:),
INTENT(IN) :: rows, cols
594 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_reserve_blocks'
597 INTEGER(kind=C_INT),
DIMENSION(SIZE(rows)) :: cols_c, rows_c
599 SUBROUTINE dbm_reserve_blocks_c(matrix, nblocks, rows, cols) &
600 BIND(C, name="dbm_reserve_blocks")
601 IMPORT :: c_ptr, c_int
602 TYPE(c_ptr),
VALUE :: matrix
603 INTEGER(kind=C_INT),
VALUE :: nblocks
604 INTEGER(kind=C_INT),
DIMENSION(*) :: rows
605 INTEGER(kind=C_INT),
DIMENSION(*) :: cols
606 END SUBROUTINE dbm_reserve_blocks_c
609 CALL timeset(routinen, handle)
610 cpassert(
SIZE(rows) ==
SIZE(cols))
614 CALL dbm_reserve_blocks_c(matrix=matrix%c_ptr, &
615 nblocks=
SIZE(rows), &
619#if defined(DBM_VALIDATE_AGAINST_DBCSR)
620 CALL dbcsr_reserve_blocks(matrix%dbcsr, rows, cols)
623 CALL timestop(handle)
634 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
635 TYPE(
dbm_type),
INTENT(IN) :: template
637 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_reserve_blocks_template'
640#if defined(DBM_VALIDATE_AGAINST_DBCSR)
641 INTEGER :: iblock, nblocks
642 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: rows, cols
646 SUBROUTINE dbm_reserve_blocks_template_c(matrix, template) &
647 BIND(C, name="dbm_reserve_blocks_template")
649 TYPE(c_ptr),
VALUE :: matrix, template
650 END SUBROUTINE dbm_reserve_blocks_template_c
653 CALL timeset(routinen, handle)
654 CALL dbm_reserve_blocks_template_c(matrix%c_ptr, template%c_ptr)
655#if defined(DBM_VALIDATE_AGAINST_DBCSR)
659 ALLOCATE (rows(nblocks), cols(nblocks))
660 DO iblock = 1, nblocks
664 CALL dbcsr_reserve_blocks(matrix%dbcsr, rows, cols)
665 DEALLOCATE (rows, cols)
669 CALL timestop(handle)
679 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
680 REAL(
dp),
INTENT(IN) :: alpha
682 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_scale'
686 SUBROUTINE dbm_scale_c(matrix, alpha) &
687 BIND(C, name="dbm_scale")
688 IMPORT :: c_ptr, c_double
689 TYPE(c_ptr),
VALUE :: matrix
690 REAL(kind=c_double),
VALUE :: alpha
691 END SUBROUTINE dbm_scale_c
694 CALL timeset(routinen, handle)
695 CALL dbm_scale_c(matrix=matrix%c_ptr, alpha=alpha)
697#if defined(DBM_VALIDATE_AGAINST_DBCSR)
698 CALL dbcsr_scale(matrix%dbcsr, alpha)
701 CALL timestop(handle)
710 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
712 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_zero'
716 SUBROUTINE dbm_zero_c(matrix) &
717 BIND(C, name="dbm_zero")
719 TYPE(c_ptr),
VALUE :: matrix
720 END SUBROUTINE dbm_zero_c
723 CALL timeset(routinen, handle)
724 CALL dbm_zero_c(matrix=matrix%c_ptr)
726#if defined(DBM_VALIDATE_AGAINST_DBCSR)
727 CALL dbcsr_zero(matrix%dbcsr)
730 CALL timestop(handle)
740 TYPE(
dbm_type),
INTENT(INOUT) :: matrix_a
741 TYPE(
dbm_type),
INTENT(IN) :: matrix_b
743 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_add'
747 SUBROUTINE dbm_add_c(matrix_a, matrix_b) &
748 BIND(C, name="dbm_add")
749 IMPORT :: c_ptr, c_double
750 TYPE(c_ptr),
VALUE :: matrix_a
751 TYPE(c_ptr),
VALUE :: matrix_b
752 END SUBROUTINE dbm_add_c
755 CALL timeset(routinen, handle)
758 CALL dbm_add_c(matrix_a=matrix_a%c_ptr, matrix_b=matrix_b%c_ptr)
760#if defined(DBM_VALIDATE_AGAINST_DBCSR)
761 CALL dbcsr_add(matrix_a%dbcsr, matrix_b%dbcsr)
764 CALL timestop(handle)
782 alpha, matrix_a, matrix_b, beta, matrix_c, &
783 retain_sparsity, filter_eps, flop)
784 LOGICAL,
INTENT(IN) :: transa, transb
785 REAL(kind=
dp),
INTENT(IN) :: alpha
786 TYPE(
dbm_type),
INTENT(IN) :: matrix_a, matrix_b
787 REAL(kind=
dp),
INTENT(IN) :: beta
788 TYPE(
dbm_type),
INTENT(INOUT) :: matrix_c
789 LOGICAL,
INTENT(IN),
OPTIONAL :: retain_sparsity
790 REAL(kind=
dp),
INTENT(IN),
OPTIONAL :: filter_eps
791 INTEGER(int_8),
INTENT(OUT),
OPTIONAL :: flop
793 CHARACTER(LEN=*),
PARAMETER :: routinen =
'dbm_multiply'
795 CHARACTER(LEN=1) :: transa_char, transb_char
797 INTEGER(int_8) :: flop_dbcsr, my_flop
798 LOGICAL :: my_retain_sparsity
799 REAL(kind=
dp) :: my_filter_eps
801 SUBROUTINE dbm_multiply_c(transa, transb, alpha, &
802 matrix_a, matrix_b, &
804 retain_sparsity, filter_eps, flop) &
805 BIND(C, name="dbm_multiply")
806 IMPORT :: c_ptr, c_double, c_bool, c_int64_t
807 LOGICAL(kind=C_BOOL),
VALUE :: transa
808 LOGICAL(kind=C_BOOL),
VALUE :: transb
809 REAL(kind=c_double),
VALUE :: alpha
810 TYPE(c_ptr),
VALUE :: matrix_a
811 TYPE(c_ptr),
VALUE :: matrix_b
812 REAL(kind=c_double),
VALUE :: beta
813 TYPE(c_ptr),
VALUE :: matrix_c
814 LOGICAL(kind=C_BOOL),
VALUE :: retain_sparsity
815 REAL(kind=c_double),
VALUE :: filter_eps
816 INTEGER(kind=C_INT64_T) :: flop
817 END SUBROUTINE dbm_multiply_c
820 CALL timeset(routinen, handle)
822 IF (
PRESENT(retain_sparsity))
THEN
823 my_retain_sparsity = retain_sparsity
825 my_retain_sparsity = .false.
828 IF (
PRESENT(filter_eps))
THEN
829 my_filter_eps = filter_eps
831 my_filter_eps = 0.0_dp
837 CALL dbm_multiply_c(transa=
LOGICAL(transa, C_BOOL), &
838 transb=logical(transb, c_bool), &
840 matrix_a=matrix_a%c_ptr, &
841 matrix_b=matrix_b%c_ptr, &
843 matrix_c=matrix_c%c_ptr, &
844 retain_sparsity=
LOGICAL(my_retain_sparsity, C_BOOL), &
845 filter_eps=my_filter_eps, &
848 IF (
PRESENT(flop))
THEN
852#if defined(DBM_VALIDATE_AGAINST_DBCSR)
854 transa_char = dbcsr_transpose
856 transa_char = dbcsr_no_transpose
859 transb_char = dbcsr_transpose
861 transb_char = dbcsr_no_transpose
863 CALL dbcsr_multiply(transa=transa_char, transb=transb_char, &
864 alpha=alpha, matrix_a=matrix_a%dbcsr, &
865 matrix_b=matrix_b%dbcsr, beta=beta, matrix_c=matrix_c%dbcsr, &
866 retain_sparsity=retain_sparsity, filter_eps=filter_eps, flop=flop_dbcsr)
867 cpassert(my_flop == flop_dbcsr)
871 mark_used(transa_char)
872 mark_used(transb_char)
873 mark_used(flop_dbcsr)
875 CALL timestop(handle)
886 TYPE(
dbm_type),
INTENT(IN) :: matrix
889 SUBROUTINE dbm_iterator_start_c(iterator, matrix) &
890 BIND(C, name="dbm_iterator_start")
892 TYPE(c_ptr) :: iterator
893 TYPE(c_ptr),
VALUE :: matrix
894 END SUBROUTINE dbm_iterator_start_c
897 cpassert(.NOT. c_associated(iterator%c_ptr))
898 CALL dbm_iterator_start_c(iterator=iterator%c_ptr, matrix=matrix%c_ptr)
899 cpassert(c_associated(iterator%c_ptr))
911 INTEGER :: num_blocks
914 FUNCTION dbm_iterator_num_blocks_c(iterator) &
915 BIND(C, name="dbm_iterator_num_blocks")
916 IMPORT :: c_ptr, c_int
917 TYPE(c_ptr),
VALUE :: iterator
918 INTEGER(kind=C_INT) :: dbm_iterator_num_blocks_c
919 END FUNCTION dbm_iterator_num_blocks_c
922 num_blocks = dbm_iterator_num_blocks_c(iterator%c_ptr)
933 LOGICAL :: blocks_left
936 FUNCTION dbm_iterator_blocks_left_c(iterator) &
937 BIND(C, name="dbm_iterator_blocks_left")
938 IMPORT :: c_ptr, c_bool
939 TYPE(c_ptr),
VALUE :: iterator
940 LOGICAL(C_BOOL) :: dbm_iterator_blocks_left_c
941 END FUNCTION dbm_iterator_blocks_left_c
944 blocks_left = dbm_iterator_blocks_left_c(iterator%c_ptr)
959 INTEGER,
INTENT(OUT) :: row, column
960 REAL(
dp),
DIMENSION(:, :),
INTENT(OUT),
OPTIONAL, &
962 INTEGER,
INTENT(OUT),
OPTIONAL :: row_size, col_size
964 INTEGER :: col0, my_col_size, my_row_size, row0
965 TYPE(c_ptr) :: block_c
967 SUBROUTINE dbm_iterator_next_block_c(iterator, row, col, block, row_size, col_size) &
968 BIND(C, name="dbm_iterator_next_block")
969 IMPORT :: c_ptr, c_int
970 TYPE(c_ptr),
VALUE :: iterator
971 INTEGER(kind=C_INT) :: row
972 INTEGER(kind=C_INT) :: col
974 INTEGER(kind=C_INT) :: row_size
975 INTEGER(kind=C_INT) :: col_size
976 END SUBROUTINE dbm_iterator_next_block_c
979 CALL dbm_iterator_next_block_c(iterator%c_ptr, row=row0, col=col0, block=block_c, &
980 row_size=my_row_size, col_size=my_col_size)
982 cpassert(c_associated(block_c))
983 IF (
PRESENT(block))
CALL c_f_pointer(block_c, block, shape=[my_row_size, my_col_size])
986 IF (
PRESENT(row_size)) row_size = my_row_size
987 IF (
PRESENT(col_size)) col_size = my_col_size
999 SUBROUTINE dbm_iterator_stop_c(iterator) &
1000 BIND(C, name="dbm_iterator_stop")
1002 TYPE(c_ptr),
VALUE :: iterator
1003 END SUBROUTINE dbm_iterator_stop_c
1006 CALL dbm_iterator_stop_c(iterator%c_ptr)
1007 iterator%c_ptr = c_null_ptr
1017 TYPE(
dbm_type),
INTENT(IN) :: matrix
1018 REAL(kind=
dp) :: res
1021 FUNCTION dbm_checksum_c(matrix) &
1022 BIND(C, name="dbm_checksum")
1023 IMPORT :: c_ptr, c_double
1024 TYPE(c_ptr),
VALUE :: matrix
1025 REAL(c_double) :: dbm_checksum_c
1026 END FUNCTION dbm_checksum_c
1030 res = dbm_checksum_c(matrix%c_ptr)
1032#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1033 cpassert(abs(res - dbcsr_checksum(matrix%dbcsr))/max(1.0_dp, abs(res)) < dbm_validate_threshold)
1044 TYPE(
dbm_type),
INTENT(INOUT) :: matrix
1045 REAL(kind=
dp) :: res
1048 FUNCTION dbm_maxabs_c(matrix) &
1049 BIND(C, name="dbm_maxabs")
1050 IMPORT :: c_ptr, c_double
1051 TYPE(c_ptr),
VALUE :: matrix
1052 REAL(c_double) :: dbm_maxabs_c
1053 END FUNCTION dbm_maxabs_c
1057 res = dbm_maxabs_c(matrix%c_ptr)
1059#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1060 cpassert(abs(res - dbcsr_maxabs(matrix%dbcsr))/max(1.0_dp, abs(res)) < dbm_validate_threshold)
1071 TYPE(
dbm_type),
INTENT(IN) :: matrix
1072 CHARACTER(len=default_string_length) :: res
1074 CHARACTER(LEN=1, KIND=C_CHAR),
DIMENSION(:), &
1077 TYPE(c_ptr) :: name_c
1079 FUNCTION dbm_get_name_c(matrix)
BIND(C, name="dbm_get_name")
1081 TYPE(c_ptr),
VALUE :: matrix
1082 TYPE(c_ptr) :: dbm_get_name_c
1083 END FUNCTION dbm_get_name_c
1086 name_c = dbm_get_name_c(matrix%c_ptr)
1092 IF (name_f(i) == c_null_char)
EXIT
1093 res(i:i) = name_f(i)
1105 TYPE(
dbm_type),
INTENT(IN) :: matrix
1109 PURE FUNCTION dbm_get_nze_c(matrix) &
1110 BIND(C, name="dbm_get_nze")
1111 IMPORT :: c_ptr, c_int
1112 TYPE(c_ptr),
VALUE,
INTENT(IN) :: matrix
1113 INTEGER(C_INT) :: dbm_get_nze_c
1114 END FUNCTION dbm_get_nze_c
1117 res = dbm_get_nze_c(matrix%c_ptr)
1128 TYPE(
dbm_type),
INTENT(IN) :: matrix
1132 PURE FUNCTION dbm_get_num_blocks_c(matrix) &
1133 BIND(C, name="dbm_get_num_blocks")
1134 IMPORT :: c_ptr, c_int
1135 TYPE(c_ptr),
VALUE,
INTENT(IN) :: matrix
1136 INTEGER(C_INT) :: dbm_get_num_blocks_c
1137 END FUNCTION dbm_get_num_blocks_c
1140 res = dbm_get_num_blocks_c(matrix%c_ptr)
1151 TYPE(
dbm_type),
INTENT(IN) :: matrix
1152 INTEGER,
CONTIGUOUS,
DIMENSION(:),
POINTER :: res
1155 TYPE(c_ptr) :: row_sizes
1157 SUBROUTINE dbm_get_row_sizes_c(matrix, nrows, row_sizes) &
1158 BIND(C, name="dbm_get_row_sizes")
1159 IMPORT :: c_ptr, c_int
1160 TYPE(c_ptr),
VALUE :: matrix
1161 INTEGER(C_INT) :: nrows
1162 TYPE(c_ptr) :: row_sizes
1163 END SUBROUTINE dbm_get_row_sizes_c
1166 CALL dbm_get_row_sizes_c(matrix%c_ptr, nrows, row_sizes)
1167 CALL c_f_pointer(row_sizes, res, shape=[nrows])
1178 TYPE(
dbm_type),
INTENT(IN) :: matrix
1179 INTEGER,
CONTIGUOUS,
DIMENSION(:),
POINTER :: res
1182 TYPE(c_ptr) :: col_sizes
1184 SUBROUTINE dbm_get_col_sizes_c(matrix, ncols, col_sizes) &
1185 BIND(C, name="dbm_get_col_sizes")
1186 IMPORT :: c_ptr, c_int
1187 TYPE(c_ptr),
VALUE :: matrix
1188 INTEGER(C_INT) :: ncols
1189 TYPE(c_ptr) :: col_sizes
1190 END SUBROUTINE dbm_get_col_sizes_c
1193 CALL dbm_get_col_sizes_c(matrix%c_ptr, ncols, col_sizes)
1194 CALL c_f_pointer(col_sizes, res, shape=[ncols])
1206 TYPE(
dbm_type),
INTENT(IN) :: matrix
1207 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: local_rows
1209 INTEGER :: nlocal_rows
1210 INTEGER,
DIMENSION(:),
POINTER :: local_rows_dbcsr, local_rows_ptr
1211 TYPE(c_ptr) :: local_rows_c
1213 SUBROUTINE dbm_get_local_rows_c(matrix, nlocal_rows, local_rows) &
1214 BIND(C, name="dbm_get_local_rows")
1215 IMPORT :: c_ptr, c_int
1216 TYPE(c_ptr),
VALUE :: matrix
1217 INTEGER(C_INT) :: nlocal_rows
1218 TYPE(c_ptr) :: local_rows
1219 END SUBROUTINE dbm_get_local_rows_c
1222 CALL dbm_get_local_rows_c(matrix%c_ptr, nlocal_rows, local_rows_c)
1223 CALL c_f_pointer(local_rows_c, local_rows_ptr, shape=[nlocal_rows])
1224 ALLOCATE (local_rows(nlocal_rows))
1225 local_rows(:) = local_rows_ptr(:) + 1
1227#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1228 CALL dbcsr_get_info(matrix%dbcsr, local_rows=local_rows_dbcsr)
1229 cpassert(all(local_rows == local_rows_dbcsr))
1231 mark_used(local_rows_dbcsr)
1243 TYPE(
dbm_type),
INTENT(IN) :: matrix
1244 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: local_cols
1246 INTEGER :: nlocal_cols
1247 INTEGER,
DIMENSION(:),
POINTER :: local_cols_dbcsr, local_cols_ptr
1248 TYPE(c_ptr) :: local_cols_c
1250 SUBROUTINE dbm_get_local_cols_c(matrix, nlocal_cols, local_cols) &
1251 BIND(C, name="dbm_get_local_cols")
1252 IMPORT :: c_ptr, c_int
1253 TYPE(c_ptr),
VALUE :: matrix
1254 INTEGER(C_INT) :: nlocal_cols
1255 TYPE(c_ptr) :: local_cols
1256 END SUBROUTINE dbm_get_local_cols_c
1259 CALL dbm_get_local_cols_c(matrix%c_ptr, nlocal_cols, local_cols_c)
1260 CALL c_f_pointer(local_cols_c, local_cols_ptr, shape=[nlocal_cols])
1261 ALLOCATE (local_cols(nlocal_cols))
1262 local_cols(:) = local_cols_ptr(:) + 1
1264#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1265 CALL dbcsr_get_info(matrix%dbcsr, local_cols=local_cols_dbcsr)
1266 cpassert(all(local_cols == local_cols_dbcsr))
1268 mark_used(local_cols_dbcsr)
1281 TYPE(
dbm_type),
INTENT(IN) :: matrix
1282 INTEGER,
INTENT(IN) :: row, column
1283 INTEGER,
INTENT(OUT) :: processor
1285 INTEGER :: processor_dbcsr
1287 PURE FUNCTION dbm_get_stored_coordinates_c(matrix, row, col) &
1288 BIND(C, name="dbm_get_stored_coordinates")
1289 IMPORT :: c_ptr, c_int
1290 TYPE(c_ptr),
VALUE,
INTENT(IN) :: matrix
1291 INTEGER(C_INT),
VALUE,
INTENT(IN) :: row
1292 INTEGER(C_INT),
VALUE,
INTENT(IN) :: col
1293 INTEGER(C_INT) :: dbm_get_stored_coordinates_c
1294 END FUNCTION dbm_get_stored_coordinates_c
1297 processor = dbm_get_stored_coordinates_c(matrix%c_ptr, row=row - 1, col=column - 1)
1299#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1300 CALL dbcsr_get_stored_coordinates(matrix%dbcsr, row, column, processor_dbcsr)
1301 cpassert(processor == processor_dbcsr)
1303 mark_used(processor_dbcsr)
1314 TYPE(
dbm_type),
INTENT(IN) :: matrix
1318 FUNCTION dbm_get_distribution_c(matrix)
BIND(C, name="dbm_get_distribution")
1320 TYPE(c_ptr),
VALUE :: matrix
1321 TYPE(c_ptr) :: dbm_get_distribution_c
1322 END FUNCTION dbm_get_distribution_c
1325 res%c_ptr = dbm_get_distribution_c(matrix%c_ptr)
1327#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1328 CALL dbcsr_get_info(matrix%dbcsr, distribution=res%dbcsr)
1343 res = c_associated(dist_a%c_ptr, dist_b%c_ptr)
1358 INTEGER,
CONTIGUOUS,
DIMENSION(:),
INTENT(IN), &
1359 POINTER :: row_dist_block, col_dist_block
1362 SUBROUTINE dbm_distribution_new_c(dist, fortran_comm, nrows, ncols, row_dist, col_dist) &
1363 BIND(C, name="dbm_distribution_new")
1364 IMPORT :: c_ptr, c_char, c_int
1366 INTEGER(kind=C_INT),
VALUE :: fortran_comm
1367 INTEGER(kind=C_INT),
VALUE :: nrows
1368 INTEGER(kind=C_INT),
VALUE :: ncols
1369 INTEGER(kind=C_INT),
DIMENSION(*) :: row_dist
1370 INTEGER(kind=C_INT),
DIMENSION(*) :: col_dist
1371 END SUBROUTINE dbm_distribution_new_c
1374 cpassert(.NOT. c_associated(dist%c_ptr))
1375 CALL dbm_distribution_new_c(dist=dist%c_ptr, &
1376 fortran_comm=mp_comm%get_handle(), &
1377 nrows=
SIZE(row_dist_block), &
1378 ncols=
SIZE(col_dist_block), &
1379 row_dist=row_dist_block, &
1380 col_dist=col_dist_block)
1381 cpassert(c_associated(dist%c_ptr))
1383#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1384 CALL dbcsr_distribution_new_wrapper(dist, mp_comm, row_dist_block, col_dist_block)
1396 SUBROUTINE dbcsr_distribution_new_wrapper(dist, mp_comm, row_dist_block, col_dist_block)
1399 INTEGER,
CONTIGUOUS,
DIMENSION(:),
INTENT(IN), &
1400 POINTER :: row_dist_block, col_dist_block
1402#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1403 INTEGER :: mynode, numnodes, pcol, prow
1404 INTEGER,
ALLOCATABLE,
DIMENSION(:, :) :: pgrid
1405 INTEGER,
DIMENSION(2) :: coord, mycoord, npdims
1406 TYPE(dbcsr_mp_obj) :: mp_env
1409 CALL mp_comm%get_info_cart(npdims, mycoord)
1410 CALL mp_comm%get_size(numnodes)
1411 CALL mp_comm%get_rank(mynode)
1412 ALLOCATE (pgrid(0:npdims(1) - 1, 0:npdims(2) - 1))
1413 DO prow = 0, npdims(1) - 1
1414 DO pcol = 0, npdims(2) - 1
1415 coord = [prow, pcol]
1416 CALL mp_comm%rank_cart(coord, pgrid(prow, pcol))
1419 cpassert(mynode == pgrid(mycoord(1), mycoord(2)))
1421 CALL dbcsr_mp_new(mp_env, mp_comm%get_handle(), pgrid, mynode, numnodes, mycoord(1), mycoord(2))
1422 CALL dbcsr_distribution_new(dist=dist%dbcsr, mp_env=mp_env, &
1423 row_dist_block=row_dist_block, col_dist_block=col_dist_block)
1424 CALL dbcsr_mp_release(mp_env)
1428 mark_used(row_dist_block)
1429 mark_used(col_dist_block)
1431 END SUBROUTINE dbcsr_distribution_new_wrapper
1442 SUBROUTINE dbm_distribution_hold_c(dist) &
1443 BIND(C, name="dbm_distribution_hold")
1445 TYPE(c_ptr),
VALUE :: dist
1446 END SUBROUTINE dbm_distribution_hold_c
1449 CALL dbm_distribution_hold_c(dist%c_ptr)
1451#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1452 CALL dbcsr_distribution_hold(dist%dbcsr)
1465 SUBROUTINE dbm_distribution_release_c(dist) &
1466 BIND(C, name="dbm_distribution_release")
1468 TYPE(c_ptr),
VALUE :: dist
1469 END SUBROUTINE dbm_distribution_release_c
1472 CALL dbm_distribution_release_c(dist%c_ptr)
1474#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1475 CALL dbcsr_distribution_release(dist%dbcsr)
1487 INTEGER,
CONTIGUOUS,
DIMENSION(:),
POINTER :: res
1490 TYPE(c_ptr) :: row_dist
1492 SUBROUTINE dbm_distribution_row_dist_c(dist, nrows, row_dist) &
1493 BIND(C, name="dbm_distribution_row_dist")
1494 IMPORT :: c_ptr, c_int
1495 TYPE(c_ptr),
VALUE :: dist
1496 INTEGER(C_INT) :: nrows
1497 TYPE(c_ptr) :: row_dist
1498 END SUBROUTINE dbm_distribution_row_dist_c
1501 CALL dbm_distribution_row_dist_c(dist%c_ptr, nrows, row_dist)
1502 CALL c_f_pointer(row_dist, res, shape=[nrows])
1504#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1505 cpassert(all(res == dbcsr_distribution_row_dist(dist%dbcsr)))
1517 INTEGER,
CONTIGUOUS,
DIMENSION(:),
POINTER :: res
1520 TYPE(c_ptr) :: col_dist
1522 SUBROUTINE dbm_distribution_col_dist_c(dist, ncols, col_dist) &
1523 BIND(C, name="dbm_distribution_col_dist")
1524 IMPORT :: c_ptr, c_int
1525 TYPE(c_ptr),
VALUE :: dist
1526 INTEGER(C_INT) :: ncols
1527 TYPE(c_ptr) :: col_dist
1528 END SUBROUTINE dbm_distribution_col_dist_c
1531 CALL dbm_distribution_col_dist_c(dist%c_ptr, ncols, col_dist)
1532 CALL c_f_pointer(col_dist, res, shape=[ncols])
1534#if defined(DBM_VALIDATE_AGAINST_DBCSR)
1535 cpassert(all(res == dbcsr_distribution_col_dist(dist%dbcsr)))
1545 SUBROUTINE dbm_library_init_c()
BIND(C, name="dbm_library_init")
1546 END SUBROUTINE dbm_library_init_c
1549 CALL dbm_library_init_c()
1559 SUBROUTINE dbm_library_finalize_c()
BIND(C, name="dbm_library_finalize")
1560 END SUBROUTINE dbm_library_finalize_c
1563 CALL dbm_library_finalize_c()
1575 INTEGER,
INTENT(IN) :: output_unit
1578 SUBROUTINE dbm_library_print_stats_c(mpi_comm, print_func, output_unit) &
1579 BIND(C, name="dbm_library_print_stats")
1580 IMPORT :: c_funptr, c_int
1581 INTEGER(KIND=C_INT),
VALUE :: mpi_comm
1582 TYPE(c_funptr),
VALUE :: print_func
1583 INTEGER(KIND=C_INT),
VALUE :: output_unit
1584 END SUBROUTINE dbm_library_print_stats_c
1588 CALL dbm_library_print_stats_c(mpi_comm=mpi_comm%get_handle(), &
1589 print_func=c_funloc(print_func), &
1590 output_unit=output_unit)
1601 SUBROUTINE print_func(msg, msglen, output_unit)
BIND(C, name="dbm_api_print_func")
1602 CHARACTER(KIND=C_CHAR),
INTENT(IN) :: msg(*)
1603 INTEGER(KIND=C_INT),
INTENT(IN),
VALUE :: msglen, output_unit
1605 IF (output_unit <= 0)
RETURN
1606 WRITE (output_unit, fmt=
"(100A)", advance=
"NO") msg(1:msglen)
1607 END SUBROUTINE print_func
subroutine, public dbm_multiply(transa, transb, alpha, matrix_a, matrix_b, beta, matrix_c, retain_sparsity, filter_eps, flop)
Computes matrix product: matrix_c = alpha * matrix_a * matrix_b + beta * matrix_c.
subroutine, public dbm_redistribute(matrix, redist)
Copies content of matrix_b into matrix_a. Matrices may have different distributions.
subroutine, public dbm_zero(matrix)
Sets all blocks in the given matrix to zero.
subroutine, public dbm_clear(matrix)
Remove all blocks from given matrix, but does not release the underlying memory.
real(kind=dp) function, public dbm_maxabs(matrix)
Returns the absolute value of the larges element of the entire given matrix.
subroutine, public dbm_create_from_template(matrix, name, template)
Creates a new matrix from given template, reusing dist and row/col_block_sizes.
subroutine validate(matrix)
Dummy for when DBM_VALIDATE_AGAINST_DBCSR is not defined.
pure integer function, public dbm_get_nze(matrix)
Returns the number of local Non-Zero Elements of the given matrix.
subroutine, public dbm_get_local_cols(matrix, local_cols)
Returns the local column block sizes of the given matrix.
subroutine, public dbm_scale(matrix, alpha)
Multiplies all entries in the given matrix by the given factor alpha.
subroutine, public dbm_distribution_release(dist)
Decreases the reference counter of the given distribution.
subroutine, public dbm_library_init()
Initialize DBM library.
subroutine, public dbm_iterator_next_block(iterator, row, column, block, row_size, col_size)
Returns the next block from the given iterator.
subroutine, public dbm_filter(matrix, eps)
Removes all blocks from the given matrix whose block norm is below the given threshold....
integer function, public dbm_iterator_num_blocks(iterator)
Returns number of blocks the iterator will provide to calling thread.
subroutine, public dbm_get_stored_coordinates(matrix, row, column, processor)
Returns the MPI rank on which the given block should be stored.
type(dbm_distribution_obj) function, public dbm_get_distribution(matrix)
Returns the distribution of the given matrix.
subroutine, public dbm_create(matrix, name, dist, row_block_sizes, col_block_sizes)
Creates a new matrix.
integer function, dimension(:), pointer, contiguous, public dbm_get_row_block_sizes(matrix)
Returns the row block sizes of the given matrix.
logical function, public dbm_iterator_blocks_left(iterator)
Tests whether the given iterator has any block left.
integer function, dimension(:), pointer, contiguous, public dbm_distribution_col_dist(dist)
Returns the columns of the given distribution.
subroutine, public dbm_reserve_blocks(matrix, rows, cols)
Adds given list of blocks efficiently. The blocks will be filled with zeros.
subroutine, public dbm_library_finalize()
Finalize DBM library.
subroutine, public dbm_iterator_stop(iterator)
Releases the given iterator.
subroutine, public dbm_get_local_rows(matrix, local_rows)
Returns the local row block sizes of the given matrix.
subroutine, public dbm_put_block(matrix, row, col, block, summation)
Adds a block to given matrix. This routine is thread-safe. If block already exist then it gets overwr...
character(len=default_string_length) function, public dbm_get_name(matrix)
Returns the name of the matrix of the given matrix.
real(kind=dp) function, public dbm_checksum(matrix)
Computes a checksum of the given matrix.
subroutine, public dbm_add(matrix_a, matrix_b)
Adds matrix_b to matrix_a.
subroutine, public dbm_finalize(matrix)
Needed to be called for DBCSR after blocks where inserted. For DBM it's a no-opt.
subroutine, public dbm_iterator_start(iterator, matrix)
Creates an iterator for the blocks of the given matrix. The iteration order is not stable.
pure integer function, public dbm_get_num_blocks(matrix)
Returns the number of local blocks of the given matrix.
subroutine, public dbm_library_print_stats(mpi_comm, output_unit)
Print DBM library statistics.
subroutine, public dbm_copy(matrix_a, matrix_b)
Copies content of matrix_b into matrix_a. Matrices must have the same row/col block sizes and distrib...
subroutine, public dbm_distribution_hold(dist)
Increases the reference counter of the given distribution.
subroutine, public dbm_reserve_blocks_template(matrix, template)
Reserves template blocks in a matrix sharing the distribution object. Block sizes may differ....
subroutine, public dbm_release(matrix)
Releases a matrix and all its ressources.
integer function, dimension(:), pointer, contiguous, public dbm_get_col_block_sizes(matrix)
Returns the column block sizes of the given matrix.
pure logical function, public dbm_distribution_is_same(dist_a, dist_b)
Returns whether two handles refer to the same distribution object.
integer function, dimension(:), pointer, contiguous, public dbm_distribution_row_dist(dist)
Returns the rows of the given distribution.
subroutine, public dbm_distribution_new(dist, mp_comm, row_dist_block, col_dist_block)
Creates a new two dimensional distribution.
subroutine, public dbm_get_block_p(matrix, row, col, block, row_size, col_size)
Looks up a block from given matrics. This routine is thread-safe. If the block is not found then a nu...
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp
integer, parameter, public default_string_length
Interface to the message passing library MPI.