LCOV - code coverage report
Current view: top level - src - submatrix_dissection.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 86.9 % 397 345
Test Date: 2026-11-24 01:22:08 Functions: 80.0 % 10 8

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \note
      10              : !> This module implements a modified version of the submatrix method, proposed in
      11              : !>   M. Lass, S. Mohr, H. Wiebeler, T. Kuehne, C. Plessl
      12              : !>   A Massively Parallel Algorithm for the Approximate Calculation of Inverse p-th Roots of Large Sparse Matrices
      13              : !>   Proc. Platform for Advanced Scientific Computing (PASC) Conference, ACM, 2018
      14              : !>
      15              : !> The method is extended to minimize the required data transfers and floating-point operations under the assumption that non-zero
      16              : !> blocks of the matrix are close to its diagonal.
      17              : !>
      18              : !> Submatrices can be constructed not for single columns of the matrix but for a set of w consecutive submatrices. The underlying
      19              : !> assumption is that columns next to each other have relatively similar occupation patterns, i.e., constructing a submatrix from
      20              : !> columns x and x+1 should not lead to a much bigger submatrix than contructing it only from column x.
      21              : !>
      22              : !> The construction of the submatrices requires communication between all ranks. It is crucial to implement this communication as
      23              : !> efficient as possible, i.e., data should only ever be transferred once between two ranks and message sizes need to be
      24              : !> sufficiently large to utilize the communication bandwidth. To achieve this, we communicate the required blocks for all
      25              : !> submatrices at once and copy them to large buffers before transmitting them via MPI.
      26              : !>
      27              : !> Note on multi-threading:
      28              : !> Submatrices can be constructed and processed in parallel by multiple threads. However, generate_submatrix, get_sm_ids_for_rank
      29              : !> and copy_resultcol are the only thread-safe routines in this module. All other routines involve MPI communication or operate on
      30              : !> common, non-protected data and are hence not thread-safe.
      31              : !>
      32              : !> TODO:
      33              : !> * generic types (for now only dp supported)
      34              : !> * optimization of threaded initialization
      35              : !> * sanity checks at the beginning of all methods
      36              : !>
      37              : !> \author Michael Lass
      38              : ! **************************************************************************************************
      39              : 
      40              : MODULE submatrix_dissection
      41              : 
      42              :    USE bibliography,                    ONLY: Lass2018,&
      43              :                                               cite_reference
      44              :    USE cp_dbcsr_api,                    ONLY: &
      45              :         dbcsr_distribution_get, dbcsr_distribution_type, dbcsr_finalize, dbcsr_get_block_p, &
      46              :         dbcsr_get_info, dbcsr_get_stored_coordinates, dbcsr_iterator_blocks_left, &
      47              :         dbcsr_iterator_next_block, dbcsr_iterator_readonly_start, dbcsr_iterator_stop, &
      48              :         dbcsr_iterator_type, dbcsr_put_block, dbcsr_type
      49              :    USE kinds,                           ONLY: dp
      50              :    USE message_passing,                 ONLY: mp_comm_type
      51              :    USE submatrix_types,                 ONLY: buffer_type,&
      52              :                                               bufptr_type,&
      53              :                                               intBuffer_type,&
      54              :                                               set_type,&
      55              :                                               setarray_type
      56              : 
      57              : !$ USE omp_lib, ONLY: omp_get_max_threads, omp_get_thread_num
      58              : 
      59              : #include "./base/base_uses.f90"
      60              : 
      61              :    IMPLICIT NONE
      62              :    PRIVATE
      63              : 
      64              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'submatrix_dissection'
      65              : 
      66              :    TYPE, PUBLIC :: submatrix_dissection_type
      67              :       TYPE(dbcsr_type)                                :: dbcsr_mat
      68              :       TYPE(dbcsr_distribution_type)                   :: dist
      69              :       LOGICAL                                         :: initialized = .FALSE.
      70              :       TYPE(mp_comm_type)                              :: group = mp_comm_type()
      71              :       INTEGER                                         :: numnodes = -1, myrank = -1, nblkcols = -1, &
      72              :                                                          nblkrows = -1, nblks = -1, local_blocks = -1, &
      73              :                                                          cols_per_sm = -1, number_of_submatrices = -1
      74              :       INTEGER, DIMENSION(:), POINTER                  :: row_blk_size => NULL(), col_blk_size => NULL()
      75              :       INTEGER, DIMENSION(:), ALLOCATABLE              :: coo_cols, coo_rows, coo_col_offsets, coo_cols_local, coo_rows_local, &
      76              :                                                          coo_col_offsets_local, submatrix_owners, submatrix_sizes
      77              :       TYPE(buffer_type), DIMENSION(:), ALLOCATABLE    :: recvbufs, result_sendbufs ! Indexing starts with 0 to match rank ids!
      78              :       TYPE(set_type), DIMENSION(:), ALLOCATABLE       :: result_blocks_for_rank, result_blocks_from_rank
      79              :       TYPE(bufptr_type), DIMENSION(:), ALLOCATABLE    :: coo_dptr
      80              :       TYPE(intBuffer_type), DIMENSION(:), ALLOCATABLE :: result_blocks_for_rank_buf_offsets
      81              :    CONTAINS
      82              :       PROCEDURE :: init => submatrix_dissection_init
      83              :       PROCEDURE :: final => submatrix_dissection_final
      84              :       PROCEDURE :: get_sm_ids_for_rank => submatrix_get_sm_ids_for_rank
      85              :       PROCEDURE :: generate_submatrix => submatrix_generate_sm
      86              :       PROCEDURE :: copy_resultcol => submatrix_cpy_resultcol
      87              :       PROCEDURE :: communicate_results => submatrix_communicate_results
      88              :       PROCEDURE :: get_relevant_sm_columns => submatrix_get_relevant_sm_columns
      89              :    END TYPE submatrix_dissection_type
      90              : 
      91              : CONTAINS
      92              : 
      93              : ! **************************************************************************************************
      94              : !> \brief determine which columns of the submatrix are relevant for the result matrix
      95              : !> \param this - object of class submatrix_dissection_type
      96              : !> \param sm_id - id of the submatrix
      97              : !> \param first - first column of submatrix that is relevant
      98              : !> \param last - last column of submatrix that is relevant
      99              : ! **************************************************************************************************
     100           74 :    SUBROUTINE submatrix_get_relevant_sm_columns(this, sm_id, first, last)
     101              :       CLASS(submatrix_dissection_type), INTENT(IN)     :: this
     102              :       INTEGER, INTENT(IN)                              :: sm_id
     103              :       INTEGER, INTENT(OUT)                             :: first, last
     104              :       INTEGER                                          :: i, j, blkid
     105        19092 :       TYPE(set_type)                                   :: nonzero_rows
     106              : 
     107              :       ! TODO: Should we buffer the list of non-zero rows for each submatrix instead of recalculating it each time?
     108          148 :       DO i = (sm_id - 1)*this%cols_per_sm + 1, sm_id*this%cols_per_sm     ! all colums that determine submatrix sm_id
     109          222 :          DO j = this%coo_col_offsets(i), this%coo_col_offsets(i + 1) - 1  ! all blocks that are within this column
     110          148 :             CALL nonzero_rows%insert(this%coo_rows(j))
     111              :          END DO
     112              :       END DO
     113              : 
     114           74 :       first = 1
     115           74 :       DO i = 1, nonzero_rows%elements
     116           74 :          blkid = nonzero_rows%get(i)
     117           74 :          IF (blkid == (sm_id - 1)*this%cols_per_sm + 1) THEN
     118              :             ! We just found the nonzero row that corresponds to the first inducing column of our submatrix
     119              :             ! Now add up block sizes to determine the last one as well
     120           74 :             last = first - 1
     121           74 :             DO j = i, nonzero_rows%elements
     122           74 :                blkid = nonzero_rows%get(j)
     123           74 :                last = last + this%col_blk_size(blkid)
     124           74 :                IF (blkid == (sm_id)*this%cols_per_sm) EXIT
     125              :             END DO
     126              :             EXIT
     127              :          END IF
     128            0 :          first = first + this%col_blk_size(blkid)
     129              :       END DO
     130              : 
     131           74 :       CALL nonzero_rows%reset
     132              : 
     133        19092 :    END SUBROUTINE submatrix_get_relevant_sm_columns
     134              : 
     135              : ! **************************************************************************************************
     136              : !> \brief initialize submatrix dissection and communicate, needs to be called before constructing any submatrices.
     137              : !> \param this - object of class submatrix_dissection_type
     138              : !> \param matrix_p - dbcsr input matrix
     139              : !> \par History
     140              : !>       2020.02 created [Michael Lass]
     141              : !>       2020.05 add time measurements [Michael Lass]
     142              : ! **************************************************************************************************
     143           86 :    SUBROUTINE submatrix_dissection_init(this, matrix_p) ! Should be PURE but the iterator routines are not
     144              :       CLASS(submatrix_dissection_type), INTENT(INOUT)  :: this
     145              :       TYPE(dbcsr_type), INTENT(IN)                     :: matrix_p
     146              : 
     147              :       INTEGER                                          :: cur_row, cur_col, i, j, k, l, m, l_limit_left, l_limit_right, &
     148              :                                                           bufsize, bufsize_next, my_i, nblks_local
     149          172 :       INTEGER, DIMENSION(:), ALLOCATABLE               :: blocks_per_rank, coo_dsplcmnts, num_blockids_send, num_blockids_recv
     150              :       TYPE(dbcsr_iterator_type)                        :: iter
     151        22188 :       TYPE(set_type)                                   :: nonzero_rows
     152              :       REAL(KIND=dp)                                    :: flops_total, flops_per_rank, flops_per_sm, flops_threshold, &
     153              :                                                           flops_current, flops_remaining
     154              : 
     155              :       ! Indexing for the following arrays starts with 0 to match rank ids
     156           86 :       TYPE(set_type), DIMENSION(:), ALLOCATABLE        :: blocks_from_rank
     157           86 :       TYPE(buffer_type), DIMENSION(:), ALLOCATABLE     :: sendbufs
     158           86 :       TYPE(intBuffer_type), DIMENSION(:), ALLOCATABLE  :: blocks_for_rank
     159              : 
     160              :       ! Additional structures for threaded parts
     161              :       INTEGER                                          :: numthreads, mytid
     162              :       ! Indexing starts at 0 to match thread ids
     163           86 :       TYPE(setarray_type), DIMENSION(:), ALLOCATABLE   :: nonzero_rows_t
     164           86 :       TYPE(setarray_type), DIMENSION(:), ALLOCATABLE   :: result_blocks_from_rank_t, result_blocks_for_rank_t, &
     165           86 :                                                           blocks_from_rank_t
     166              : 
     167              :       LOGICAL                                          :: valid
     168           86 :       REAL(KIND=dp), DIMENSION(:, :), POINTER           :: blockp
     169              : 
     170              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'submatrix_dissection_init'
     171              :       INTEGER :: handle
     172              : 
     173           86 :       CALL timeset(routineN, handle)
     174           86 :       CALL cite_reference(Lass2018)
     175              : 
     176           86 :       this%dbcsr_mat = matrix_p
     177              :       CALL dbcsr_get_info(matrix=this%dbcsr_mat, nblkcols_total=this%nblkcols, nblkrows_total=this%nblkrows, &
     178           86 :                           row_blk_size=this%row_blk_size, col_blk_size=this%col_blk_size, group=this%group, distribution=this%dist)
     179           86 :       CALL dbcsr_distribution_get(dist=this%dist, mynode=this%myrank, numnodes=this%numnodes)
     180              : 
     181           86 :       IF (this%nblkcols /= this%nblkrows) THEN
     182            0 :          CPABORT("Number of block rows and cols need to be identical")
     183              :       END IF
     184              : 
     185          172 :       DO i = 1, this%nblkcols
     186          172 :          IF (this%col_blk_size(i) /= this%row_blk_size(i)) THEN
     187            0 :             CPABORT("Block row sizes and col sizes need to be identical")
     188              :          END IF
     189              :       END DO
     190              : 
     191              :       ! TODO: We could do even more sanity checks here, e.g., the matrix must not be stored symmetrically
     192              : 
     193              :       ! For the submatrix method, we need global knwoledge about which blocks are actually used. Therefore, we create a COO
     194              :       ! representation of the blocks (without their contents) on all ranks.
     195              : 
     196              :       ! TODO: Right now, the COO contains all blocks. Also we transfer all blocks. We could skip half of them due to the matrix
     197              :       ! being symmetric (however, we need to transpose the blocks). This can increase performance only by a factor of 2 and is
     198              :       ! therefore deferred.
     199              : 
     200              :       ! Determine number of locally stored blocks
     201           86 :       nblks_local = 0
     202              : !$OMP PARALLEL DEFAULT(NONE) SHARED(this) PRIVATE(iter, cur_row, cur_col) &
     203           86 : !$OMP REDUCTION(+:nblks_local)
     204              :       CALL dbcsr_iterator_readonly_start(iter, this%dbcsr_mat)
     205              :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     206              :          CALL dbcsr_iterator_next_block(iter, cur_row, cur_col)
     207              :          nblks_local = nblks_local + 1
     208              :       END DO
     209              :       CALL dbcsr_iterator_stop(iter)
     210              : !$OMP END PARALLEL
     211           86 :       this%local_blocks = nblks_local
     212              : 
     213            0 :       ALLOCATE (this%coo_cols_local(this%local_blocks), this%coo_rows_local(this%local_blocks), blocks_per_rank(this%numnodes), &
     214            0 :                 coo_dsplcmnts(this%numnodes), this%coo_col_offsets_local(this%nblkcols + 1), &
     215              :                 blocks_for_rank(0:this%numnodes - 1), blocks_from_rank(0:this%numnodes - 1), &
     216            0 :                 sendbufs(0:this%numnodes - 1), this%recvbufs(0:this%numnodes - 1), this%result_sendbufs(0:this%numnodes - 1), &
     217            0 :                 this%result_blocks_for_rank(0:this%numnodes - 1), this%result_blocks_from_rank(0:this%numnodes - 1), &
     218       202616 :                 this%result_blocks_for_rank_buf_offsets(0:this%numnodes - 1))
     219              : 
     220           86 :       i = 1
     221           86 : !$OMP PARALLEL DEFAULT(NONE) SHARED(this, i) PRIVATE(iter, cur_row, cur_col, my_i)
     222              :       CALL dbcsr_iterator_readonly_start(iter, this%dbcsr_mat)
     223              :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     224              :          CALL dbcsr_iterator_next_block(iter, cur_row, cur_col)
     225              : !$OMP ATOMIC CAPTURE
     226              :          my_i = i
     227              :          i = i + 1
     228              : !$OMP END ATOMIC
     229              :          this%coo_cols_local(my_i) = cur_col
     230              :          this%coo_rows_local(my_i) = cur_row
     231              :       END DO
     232              :       CALL dbcsr_iterator_stop(iter)
     233              : !$OMP END PARALLEL
     234              : 
     235              :       ! We only know how many blocks we own. What's with the other ranks?
     236           86 :       CALL this%group%allgather(msgout=this%local_blocks, msgin=blocks_per_rank)
     237           86 :       coo_dsplcmnts(1) = 0
     238          172 :       DO i = 1, this%numnodes - 1
     239          172 :          coo_dsplcmnts(i + 1) = coo_dsplcmnts(i) + blocks_per_rank(i)
     240              :       END DO
     241              : 
     242              :       ! Get a global view on the matrix
     243          258 :       this%nblks = SUM(blocks_per_rank)
     244            0 :       ALLOCATE (this%coo_cols(this%nblks), this%coo_rows(this%nblks), this%coo_col_offsets(this%nblkcols + 1), &
     245          860 :                 this%coo_dptr(this%nblks))
     246              :       CALL this%group%allgatherv(msgout=this%coo_rows_local, msgin=this%coo_rows, rcount=blocks_per_rank, &
     247           86 :                                  rdispl=coo_dsplcmnts)
     248              :       CALL this%group%allgatherv(msgout=this%coo_cols_local, msgin=this%coo_cols, rcount=blocks_per_rank, &
     249           86 :                                  rdispl=coo_dsplcmnts)
     250              : 
     251           86 :       DEALLOCATE (blocks_per_rank, coo_dsplcmnts)
     252              : 
     253              :       ! Sort COO arrays according to their columns
     254           86 :       CALL qsort_two(this%coo_cols_local, this%coo_rows_local, 1, this%local_blocks)
     255           86 :       CALL qsort_two(this%coo_cols, this%coo_rows, 1, this%nblks)
     256              : 
     257              :       ! Get COO array offsets for columns to accelerate lookups
     258           86 :       this%coo_col_offsets(this%nblkcols + 1) = this%nblks + 1
     259           86 :       j = 1
     260          172 :       DO i = 1, this%nblkcols
     261           86 :          DO WHILE ((j <= this%nblks))
     262           86 :             IF (this%coo_cols(j) >= i) EXIT
     263           86 :             j = j + 1
     264              :          END DO
     265          172 :          this%coo_col_offsets(i) = j
     266              :       END DO
     267              : 
     268              :       ! Same for local COO
     269           86 :       this%coo_col_offsets_local(this%nblkcols + 1) = this%local_blocks + 1
     270           86 :       j = 1
     271          172 :       DO i = 1, this%nblkcols
     272           86 :          DO WHILE ((j <= this%local_blocks))
     273           43 :             IF (this%coo_cols_local(j) >= i) EXIT
     274           43 :             j = j + 1
     275              :          END DO
     276          172 :          this%coo_col_offsets_local(i) = j
     277              :       END DO
     278              : 
     279              :       ! We could combine multiple columns to generate a single submatrix. For now, we have not found a practical use-case for this
     280              :       ! so we only look at single columns for now.
     281           86 :       this%cols_per_sm = 1
     282              : 
     283              :       ! Determine sizes of all submatrices. This is required in order to assess the computational effort that is required to process
     284              :       ! the submatrices.
     285           86 :       this%number_of_submatrices = this%nblkcols/this%cols_per_sm
     286          258 :       ALLOCATE (this%submatrix_sizes(this%number_of_submatrices))
     287          172 :       this%submatrix_sizes = 0
     288           86 :       flops_total = 0.0D0
     289          172 :       DO i = 1, this%number_of_submatrices
     290           86 :          CALL nonzero_rows%reset
     291          172 :          DO j = (i - 1)*this%cols_per_sm + 1, i*this%cols_per_sm            ! all colums that determine submatrix i
     292          258 :             DO k = this%coo_col_offsets(j), this%coo_col_offsets(j + 1) - 1 ! all blocks that are within this column
     293          172 :                CALL nonzero_rows%insert(this%coo_rows(k))
     294              :             END DO
     295              :          END DO
     296          172 :          DO j = 1, nonzero_rows%elements
     297          172 :             this%submatrix_sizes(i) = this%submatrix_sizes(i) + this%col_blk_size(nonzero_rows%get(j))
     298              :          END DO
     299          172 :          flops_total = flops_total + 2.0D0*this%submatrix_sizes(i)*this%submatrix_sizes(i)*this%submatrix_sizes(i)
     300              :       END DO
     301              : 
     302              :       ! Create mapping from submatrices to ranks. Since submatrices can be of different sizes, we need to perform some load
     303              :       ! balancing here. For that we assume that arithmetic operations performed on the submatrices scale cubically.
     304          258 :       ALLOCATE (this%submatrix_owners(this%number_of_submatrices))
     305           86 :       flops_per_sm = flops_total/this%number_of_submatrices
     306           86 :       flops_per_rank = flops_total/this%numnodes
     307           86 :       flops_current = 0.0D0
     308           86 :       j = 0
     309          172 :       DO i = 1, this%number_of_submatrices
     310           86 :          this%submatrix_owners(i) = j
     311           86 :          flops_current = flops_current + 2.0D0*this%submatrix_sizes(i)*this%submatrix_sizes(i)*this%submatrix_sizes(i)
     312           86 :          flops_remaining = flops_total - flops_current
     313           86 :          flops_threshold = (this%numnodes - j - 1)*flops_per_rank
     314              :          IF ((j < (this%numnodes - 1)) &
     315           86 :              .AND. ((flops_remaining <= flops_threshold &
     316           86 :                      .OR. (this%number_of_submatrices - i) < (this%numnodes - j)))) THEN
     317           86 :             j = j + 1
     318           86 :             flops_total = flops_total - flops_current
     319           86 :             flops_current = 0.0D0
     320              :          END IF
     321              :       END DO
     322              : 
     323              :       ! Prepare data structures for multithreaded loop
     324           86 :       numthreads = 1
     325           86 : !$    numthreads = omp_get_max_threads()
     326              : 
     327            0 :       ALLOCATE (result_blocks_from_rank_t(0:numthreads - 1), &
     328            0 :                 result_blocks_for_rank_t(0:numthreads - 1), &
     329            0 :                 blocks_from_rank_t(0:numthreads - 1), &
     330          860 :                 nonzero_rows_t(0:numthreads - 1))
     331              : 
     332              :       ! Figure out which blocks we need to receive. Blocks are identified here as indices into our COO representation.
     333              :       ! TODO: This currently shows limited parallel efficiency. Investigate further.
     334              : 
     335              :       !$OMP PARALLEL DEFAULT(NONE) &
     336              :       !$OMP          NUM_THREADS(numthreads) &
     337              :       !$OMP          PRIVATE(i,j,k,l,m,l_limit_left,l_limit_right,cur_col,cur_row,mytid) &
     338              :       !$OMP          SHARED(result_blocks_from_rank_t,result_blocks_for_rank_t,blocks_from_rank_t,this,numthreads,nonzero_rows_t)
     339              :       mytid = 0
     340              : !$    mytid = omp_get_thread_num()
     341              : 
     342              :       ALLOCATE (nonzero_rows_t(mytid)%sets(1), &
     343              :                 result_blocks_from_rank_t(mytid)%sets(0:this%numnodes - 1), &
     344              :                 result_blocks_for_rank_t(mytid)%sets(0:this%numnodes - 1), &
     345              :                 blocks_from_rank_t(mytid)%sets(0:this%numnodes - 1))
     346              : 
     347           86 :       !$OMP DO schedule(guided)
     348              :       DO i = 1, this%number_of_submatrices
     349              :          CALL nonzero_rows_t(mytid)%sets(1)%reset
     350              :          DO j = (i - 1)*this%cols_per_sm + 1, i*this%cols_per_sm            ! all colums that determine submatrix i
     351              :             DO k = this%coo_col_offsets(j), this%coo_col_offsets(j + 1) - 1 ! all blocks that are within this column
     352              :                ! This block will be required to assemble the final block matrix as it is within an inducing column for submatrix i.
     353              :                ! Figure out who stores it and insert it into the result_blocks_* sets.
     354              :                CALL dbcsr_get_stored_coordinates(matrix=this%dbcsr_mat, row=this%coo_rows(k), column=j, processor=m)
     355              :                IF (m == this%myrank) THEN
     356              :                   CALL result_blocks_from_rank_t(mytid)%sets(this%submatrix_owners(i))%insert(k)
     357              :                END IF
     358              :                IF (this%submatrix_owners(i) == this%myrank) THEN
     359              :                   CALL nonzero_rows_t(mytid)%sets(1)%insert(this%coo_rows(k))
     360              :                   CALL result_blocks_for_rank_t(mytid)%sets(m)%insert(k)
     361              :                END IF
     362              :             END DO
     363              :          END DO
     364              : 
     365              :          IF (this%submatrix_owners(i) /= this%myrank) CYCLE
     366              : 
     367              :          ! In the following, we determine all blocks required to build the submatrix. We interpret nonzero_rows_t(mytid)(j) as
     368              :          ! column and nonzero_rows_t(mytid)(k) as row.
     369              :          DO j = 1, nonzero_rows_t(mytid)%sets(1)%elements
     370              :             cur_col = nonzero_rows_t(mytid)%sets(1)%get(j)
     371              :             l_limit_left = this%coo_col_offsets(cur_col)
     372              :             l_limit_right = this%coo_col_offsets(cur_col + 1) - 1
     373              :             DO k = 1, nonzero_rows_t(mytid)%sets(1)%elements
     374              :                cur_row = nonzero_rows_t(mytid)%sets(1)%get(k)
     375              :                l = l_limit_left
     376              :                DO WHILE (l <= l_limit_right)
     377              :                   IF (this%coo_rows(l) >= cur_row) EXIT
     378              :                   l = l + 1
     379              :                END DO
     380              :                l_limit_left = l
     381              :                IF (l <= l_limit_right) THEN
     382              :                   IF (this%coo_rows(l) == cur_row) THEN
     383              :                      ! We found a valid block. Figure out what to do with it.
     384              :                      CALL dbcsr_get_stored_coordinates(matrix=this%dbcsr_mat, row=this%coo_rows(l), &
     385              :                                                        column=this%coo_cols(l), processor=m)
     386              :                      CALL blocks_from_rank_t(mytid)%sets(m)%insert(l)
     387              :                   END IF
     388              :                END IF
     389              :             END DO
     390              :          END DO
     391              :       END DO
     392              :       !$OMP END DO
     393              :       !$OMP END PARALLEL
     394              : 
     395              :       ! Merge partial results from threads
     396          258 :       DO i = 0, this%numnodes - 1
     397          430 :          DO j = 0, numthreads - 1
     398          215 :             DO k = 1, result_blocks_from_rank_t(j)%sets(i)%elements
     399          215 :                CALL this%result_blocks_from_rank(i)%insert(result_blocks_from_rank_t(j)%sets(i)%get(k))
     400              :             END DO
     401          172 :             CALL result_blocks_from_rank_t(j)%sets(i)%reset
     402          215 :             DO k = 1, result_blocks_for_rank_t(j)%sets(i)%elements
     403          215 :                CALL this%result_blocks_for_rank(i)%insert(result_blocks_for_rank_t(j)%sets(i)%get(k))
     404              :             END DO
     405          172 :             CALL result_blocks_for_rank_t(j)%sets(i)%reset
     406          215 :             DO k = 1, blocks_from_rank_t(j)%sets(i)%elements
     407          215 :                CALL blocks_from_rank(i)%insert(blocks_from_rank_t(j)%sets(i)%get(k))
     408              :             END DO
     409          344 :             CALL blocks_from_rank_t(j)%sets(i)%reset
     410              :          END DO
     411              :       END DO
     412          172 :       DO i = 0, numthreads - 1
     413           86 :          CALL nonzero_rows_t(i)%sets(1)%reset
     414            0 :          DEALLOCATE (result_blocks_from_rank_t(i)%sets, result_blocks_for_rank_t(i)%sets, blocks_from_rank_t(i)%sets, &
     415       155488 :                      nonzero_rows_t(i)%sets)
     416              :       END DO
     417          430 :       DEALLOCATE (result_blocks_from_rank_t, result_blocks_for_rank_t, blocks_from_rank_t, nonzero_rows_t)
     418              : 
     419              :       ! Make other ranks aware of our needs
     420          344 :       ALLOCATE (num_blockids_send(0:this%numnodes - 1), num_blockids_recv(0:this%numnodes - 1))
     421          258 :       DO i = 0, this%numnodes - 1
     422          258 :          num_blockids_send(i) = blocks_from_rank(i)%elements
     423              :       END DO
     424           86 :       CALL this%group%alltoall(num_blockids_send, num_blockids_recv, 1)
     425          258 :       DO i = 0, this%numnodes - 1
     426          258 :          CALL blocks_for_rank(i)%alloc(num_blockids_recv(i))
     427              :       END DO
     428           86 :       DEALLOCATE (num_blockids_send, num_blockids_recv)
     429              : 
     430           86 :       IF (this%numnodes > 1) THEN
     431          258 :          DO i = 1, this%numnodes
     432          172 :             k = MODULO(this%myrank - i, this%numnodes) ! rank to receive from
     433          258 :             CALL this%group%irecv(msgout=blocks_for_rank(k)%data, source=k, request=blocks_for_rank(k)%mpi_request)
     434              :          END DO
     435          258 :          DO i = 1, this%numnodes
     436          172 :             k = MODULO(this%myrank + i, this%numnodes) ! rank to send to
     437          258 :             CALL this%group%send(blocks_from_rank(k)%getall(), k, 0)
     438              :          END DO
     439          258 :          DO i = 0, this%numnodes - 1
     440          258 :             CALL blocks_for_rank(i)%mpi_request%wait()
     441              :          END DO
     442              :       ELSE
     443            0 :          blocks_for_rank(0)%data = blocks_from_rank(0)%getall()
     444              :       END IF
     445              : 
     446              :       ! Free memory allocated in nonzero_rows
     447           86 :       CALL nonzero_rows%reset
     448              : 
     449              :       ! Make get calls on this%result_blocks_for_rank(...) threadsafe in other routines by updating the internal sorted list
     450          258 :       DO m = 0, this%numnodes - 1
     451          258 :          IF ((.NOT. this%result_blocks_for_rank(m)%sorted_up_to_date) .AND. (this%result_blocks_for_rank(m)%elements > 0)) THEN
     452           43 :             CALL this%result_blocks_for_rank(m)%update_sorted
     453              :          END IF
     454              :       END DO
     455              : 
     456              :       ! Create and fill send buffers
     457          258 :       DO i = 0, this%numnodes - 1
     458          172 :          bufsize = 0
     459          215 :          DO j = 1, blocks_for_rank(i)%size
     460           43 :             k = blocks_for_rank(i)%data(j)
     461          215 :             bufsize = bufsize + this%col_blk_size(this%coo_cols(k))*this%col_blk_size(this%coo_rows(k))
     462              :          END DO
     463          172 :          CALL sendbufs(i)%alloc(bufsize)
     464              : 
     465          172 :          bufsize = 0
     466          172 :          CALL this%result_blocks_for_rank_buf_offsets(i)%alloc(this%result_blocks_for_rank(i)%elements)
     467          215 :          DO j = 1, this%result_blocks_for_rank(i)%elements
     468           43 :             k = this%result_blocks_for_rank(i)%get(j)
     469           43 :             this%result_blocks_for_rank_buf_offsets(i)%data(j) = bufsize
     470          215 :             bufsize = bufsize + this%col_blk_size(this%coo_cols(k))*this%col_blk_size(this%coo_rows(k))
     471              :          END DO
     472          172 :          CALL this%result_sendbufs(i)%alloc(bufsize)
     473              : 
     474          172 :          bufsize = 0
     475          301 :          DO j = 1, blocks_for_rank(i)%size
     476           43 :             k = blocks_for_rank(i)%data(j)
     477           43 :             CALL dbcsr_get_block_p(this%dbcsr_mat, row=this%coo_rows(k), col=this%coo_cols(k), block=blockp, found=valid)
     478           43 :             IF (.NOT. valid) THEN
     479            0 :                CPABORT("Block included in our COO and placed on our rank could not be fetched!")
     480              :             END IF
     481          129 :             bufsize_next = bufsize + SIZE(blockp)
     482         1302 :             sendbufs(i)%data(bufsize + 1:bufsize_next) = RESHAPE(blockp, [SIZE(blockp)])
     483          258 :             bufsize = bufsize_next
     484              :          END DO
     485              :       END DO
     486              : 
     487              :       ! Create receive buffers and mapping from blocks to memory locations
     488          258 :       DO i = 0, this%numnodes - 1
     489          172 :          bufsize = 0
     490          215 :          DO j = 1, blocks_from_rank(i)%elements
     491           43 :             k = blocks_from_rank(i)%get(j)
     492          215 :             bufsize = bufsize + this%col_blk_size(this%coo_cols(k))*this%col_blk_size(this%coo_rows(k))
     493              :          END DO
     494          172 :          CALL this%recvbufs(i)%alloc(bufsize)
     495          172 :          bufsize = 0
     496          301 :          DO j = 1, blocks_from_rank(i)%elements
     497           43 :             k = blocks_from_rank(i)%get(j)
     498           43 :             bufsize_next = bufsize + this%col_blk_size(this%coo_cols(k))*this%col_blk_size(this%coo_rows(k))
     499           43 :             this%coo_dptr(k)%target => this%recvbufs(i)%data(bufsize + 1:bufsize_next)
     500          215 :             bufsize = bufsize_next
     501              :          END DO
     502              :       END DO
     503              : 
     504          258 :       DO i = 0, this%numnodes - 1
     505          172 :          CALL blocks_for_rank(i)%dealloc
     506          258 :          CALL blocks_from_rank(i)%reset
     507              :       END DO
     508        44548 :       DEALLOCATE (blocks_for_rank, blocks_from_rank)
     509              : 
     510           86 :       IF (this%numnodes > 1) THEN
     511              :          ! Communicate. We attempt to balance communication load in the network here by starting our sends with our right neighbor
     512              :          ! and first trying to receive from our left neighbor.
     513          258 :          DO i = 1, this%numnodes
     514          172 :             k = MODULO(this%myrank - i, this%numnodes) ! rank to receive from
     515          172 :             CALL this%group%irecv(msgout=this%recvbufs(k)%data, source=k, request=this%recvbufs(k)%mpi_request)
     516          172 :             k = MODULO(this%myrank + i, this%numnodes) ! rank to send to
     517          258 :             CALL this%group%isend(msgin=sendbufs(k)%data, dest=k, request=sendbufs(k)%mpi_request)
     518              :          END DO
     519          258 :          DO i = 0, this%numnodes - 1
     520          172 :             CALL sendbufs(i)%mpi_request%wait()
     521          258 :             CALL this%recvbufs(i)%mpi_request%wait()
     522              :          END DO
     523              :       ELSE
     524            0 :          this%recvbufs(0)%data = sendbufs(0)%data
     525              :       END IF
     526              : 
     527          258 :       DO i = 0, this%numnodes - 1
     528          258 :          CALL sendbufs(i)%dealloc
     529              :       END DO
     530           86 :       DEALLOCATE (sendbufs)
     531              : 
     532           86 :       this%initialized = .TRUE.
     533              : 
     534           86 :       CALL timestop(handle)
     535        22446 :    END SUBROUTINE submatrix_dissection_init
     536              : 
     537              : ! **************************************************************************************************
     538              : !> \brief free all associated memory, afterwards submatrix_dissection_init needs to be called again
     539              : !> \param this - object of class submatrix_dissection_type
     540              : ! **************************************************************************************************
     541           86 :    PURE SUBROUTINE submatrix_dissection_final(this)
     542              :       CLASS(submatrix_dissection_type), INTENT(INOUT) :: this
     543              :       INTEGER                                         :: i
     544              : 
     545           86 :       this%initialized = .FALSE.
     546              : 
     547           86 :       IF (ALLOCATED(this%submatrix_sizes)) DEALLOCATE (this%submatrix_sizes)
     548           86 :       IF (ALLOCATED(this%coo_cols_local)) DEALLOCATE (this%coo_cols_local)
     549           86 :       IF (ALLOCATED(this%coo_rows_local)) DEALLOCATE (this%coo_rows_local)
     550           86 :       IF (ALLOCATED(this%coo_col_offsets_local)) DEALLOCATE (this%coo_col_offsets_local)
     551           86 :       IF (ALLOCATED(this%result_blocks_for_rank_buf_offsets)) THEN
     552          258 :          DO i = 0, this%numnodes - 1
     553          258 :             CALL this%result_blocks_for_rank_buf_offsets(i)%dealloc
     554              :          END DO
     555           86 :          DEALLOCATE (this%result_blocks_for_rank_buf_offsets)
     556              :       END IF
     557           86 :       IF (ALLOCATED(this%recvbufs)) THEN
     558          258 :          DO i = 0, this%numnodes - 1
     559          258 :             CALL this%recvbufs(i)%dealloc
     560              :          END DO
     561           86 :          DEALLOCATE (this%recvbufs)
     562              :       END IF
     563           86 :       IF (ALLOCATED(this%result_sendbufs)) THEN
     564          258 :          DO i = 0, this%numnodes - 1
     565          258 :             CALL this%result_sendbufs(i)%dealloc
     566              :          END DO
     567           86 :          DEALLOCATE (this%result_sendbufs)
     568              :       END IF
     569           86 :       IF (ALLOCATED(this%result_blocks_for_rank)) THEN
     570          258 :          DO i = 0, this%numnodes - 1
     571          258 :             CALL this%result_blocks_for_rank(i)%reset
     572              :          END DO
     573        44462 :          DEALLOCATE (this%result_blocks_for_rank)
     574              :       END IF
     575           86 :       IF (ALLOCATED(this%result_blocks_from_rank)) THEN
     576          258 :          DO i = 0, this%numnodes - 1
     577          258 :             CALL this%result_blocks_from_rank(i)%reset
     578              :          END DO
     579        44462 :          DEALLOCATE (this%result_blocks_from_rank)
     580              :       END IF
     581           86 :       IF (ALLOCATED(this%coo_cols)) DEALLOCATE (this%coo_cols)
     582           86 :       IF (ALLOCATED(this%coo_rows)) DEALLOCATE (this%coo_rows)
     583           86 :       IF (ALLOCATED(this%coo_col_offsets)) DEALLOCATE (this%coo_col_offsets)
     584           86 :       IF (ALLOCATED(this%coo_dptr)) DEALLOCATE (this%coo_dptr)
     585           86 :       IF (ALLOCATED(this%submatrix_owners)) DEALLOCATE (this%submatrix_owners)
     586              : 
     587           86 :    END SUBROUTINE submatrix_dissection_final
     588              : 
     589              : ! **************************************************************************************************
     590              : !> \brief generate a specific submatrix
     591              : !> \param this - object of class submatrix_dissection_type
     592              : !> \param sm_id - id of the submatrix to generate
     593              : !> \param sm - generated submatrix
     594              : ! **************************************************************************************************
     595           44 :    SUBROUTINE submatrix_generate_sm(this, sm_id, sm)
     596              :       CLASS(submatrix_dissection_type), INTENT(IN)             :: this
     597              :       INTEGER, INTENT(IN)                                      :: sm_id
     598              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE, INTENT(OUT) :: sm
     599              : 
     600              :       INTEGER                                                  :: sm_dim, i, j, k, offset_x1, offset_x2, offset_y1, &
     601              :                                                                   offset_y2, k_limit_left, k_limit_right
     602        11352 :       TYPE(set_type)                                           :: nonzero_rows
     603              : 
     604           44 :       IF (.NOT. this%initialized) THEN
     605            0 :          CPABORT("Submatrix dissection not initialized")
     606              :       END IF
     607              : 
     608           44 :       IF (this%myrank /= this%submatrix_owners(sm_id)) THEN
     609            0 :          CPABORT("This rank is not supposed to construct this submatrix")
     610              :       END IF
     611              : 
     612              :       ! TODO: Should we buffer the list of non-zero rows for each submatrix instead of recalculating it each time?
     613           44 :       CALL nonzero_rows%reset
     614           88 :       DO i = (sm_id - 1)*this%cols_per_sm + 1, sm_id*this%cols_per_sm     ! all colums that determine submatrix sm_id
     615          132 :          DO j = this%coo_col_offsets(i), this%coo_col_offsets(i + 1) - 1  ! all blocks that are within this column
     616           88 :             CALL nonzero_rows%insert(this%coo_rows(j))
     617              :          END DO
     618              :       END DO
     619           44 :       sm_dim = 0
     620           88 :       DO i = 1, nonzero_rows%elements
     621           88 :          sm_dim = sm_dim + this%col_blk_size(nonzero_rows%get(i))
     622              :       END DO
     623              : 
     624          176 :       ALLOCATE (sm(sm_dim, sm_dim))
     625           44 :       sm = 0
     626              : 
     627           44 :       offset_x1 = 0
     628           88 :       DO j = 1, nonzero_rows%elements
     629           44 :          offset_x2 = offset_x1 + this%col_blk_size(nonzero_rows%get(j))
     630           44 :          offset_y1 = 0
     631           44 :          k_limit_left = this%coo_col_offsets(nonzero_rows%get(j))
     632           44 :          k_limit_right = this%coo_col_offsets(nonzero_rows%get(j) + 1) - 1
     633           88 :          DO i = 1, nonzero_rows%elements
     634           44 :             offset_y2 = offset_y1 + this%col_blk_size(nonzero_rows%get(i))
     635              :             ! Copy block nonzero_rows(i),nonzero_rows(j) to sm(i,j) (if the block actually exists)
     636           44 :             k = k_limit_left
     637           44 :             DO WHILE (k <= k_limit_right)
     638           44 :                IF (this%coo_rows(k) >= nonzero_rows%get(i)) EXIT
     639            0 :                k = k + 1
     640              :             END DO
     641           44 :             k_limit_left = k
     642           44 :             IF (this%coo_rows(k) == nonzero_rows%get(i)) THEN ! it does exist and k is our block id
     643              :                sm(offset_y1 + 1:offset_y2, offset_x1 + 1:offset_x2) = RESHAPE(this%coo_dptr(k)%target, &
     644         1524 :                                                                               [offset_y2 - offset_y1, offset_x2 - offset_x1])
     645              :             END IF
     646           88 :             offset_y1 = offset_y2
     647              :          END DO
     648           88 :          offset_x1 = offset_x2
     649              :       END DO
     650              : 
     651              :       ! Free memory allocated in nonzero_rows
     652           44 :       CALL nonzero_rows%reset
     653              : 
     654        11352 :    END SUBROUTINE submatrix_generate_sm
     655              : 
     656              : ! **************************************************************************************************
     657              : !> \brief determine submatrix ids that are handled by a specific rank
     658              : !> \param this - object of class submatrix_dissection_type
     659              : !> \param rank - rank id of interest
     660              : !> \param sm_ids - list of submatrix ids handled by that rank
     661              : ! **************************************************************************************************
     662           86 :    SUBROUTINE submatrix_get_sm_ids_for_rank(this, rank, sm_ids)
     663              :       CLASS(submatrix_dissection_type), INTENT(IN)    :: this
     664              :       INTEGER, INTENT(IN)                             :: rank
     665              :       INTEGER, DIMENSION(:), ALLOCATABLE, INTENT(OUT) :: sm_ids
     666              :       INTEGER                                         :: count, i
     667              : 
     668           86 :       IF (.NOT. this%initialized) THEN
     669            0 :          CPABORT("Submatrix dissection not initialized")
     670              :       END IF
     671              : 
     672           86 :       count = 0
     673          172 :       DO i = 1, this%number_of_submatrices
     674          172 :          IF (this%submatrix_owners(i) == rank) count = count + 1
     675              :       END DO
     676              : 
     677          215 :       ALLOCATE (sm_ids(count))
     678              : 
     679           86 :       count = 0
     680          172 :       DO i = 1, this%number_of_submatrices
     681          172 :          IF (this%submatrix_owners(i) == rank) THEN
     682           43 :             count = count + 1
     683           43 :             sm_ids(count) = i
     684              :          END IF
     685              :       END DO
     686              : 
     687           86 :    END SUBROUTINE submatrix_get_sm_ids_for_rank
     688              : 
     689              : ! **************************************************************************************************
     690              : !> \brief copy result columns from a submatrix into result buffer
     691              : !> \param this - object of class submatrix_dissection_type
     692              : !> \param sm_id - id of the submatrix
     693              : !> \param sm - result-submatrix
     694              : ! **************************************************************************************************
     695           44 :    SUBROUTINE submatrix_cpy_resultcol(this, sm_id, sm)
     696              :       CLASS(submatrix_dissection_type), INTENT(INOUT)         :: this
     697              :       INTEGER, INTENT(in)                                     :: sm_id
     698              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE, INTENT(IN) :: sm
     699              : 
     700        11352 :       TYPE(set_type)                                          :: nonzero_rows
     701              :       INTEGER                                                 :: i, j, k, m, sm_col_offset, offset_x1, offset_x2, offset_y1, &
     702              :                                                                  offset_y2, bufsize, bufsize_next, k_limit_left, k_limit_right
     703           44 :       INTEGER, DIMENSION(:), ALLOCATABLE                      :: buf_offset_idxs
     704              : 
     705           44 :       IF (.NOT. this%initialized) THEN
     706            0 :          CPABORT("Submatrix dissection is not initizalized")
     707              :       END IF
     708              : 
     709           44 :       IF (this%myrank /= this%submatrix_owners(sm_id)) THEN
     710            0 :          CPABORT("This rank is not supposed to operate on this submatrix")
     711              :       END IF
     712              : 
     713          132 :       ALLOCATE (buf_offset_idxs(0:this%numnodes - 1))
     714          132 :       buf_offset_idxs = 1
     715              : 
     716              :       ! TODO: Should we buffer the list of non-zero rows for each submatrix instead of recalculating it each time?
     717           44 :       sm_col_offset = 0
     718           88 :       DO i = (sm_id - 1)*this%cols_per_sm + 1, sm_id*this%cols_per_sm     ! all colums that determine submatrix sm_id
     719          132 :          DO j = this%coo_col_offsets(i), this%coo_col_offsets(i + 1) - 1  ! all blocks that are within this column
     720           88 :             CALL nonzero_rows%insert(this%coo_rows(j))
     721              :          END DO
     722              :       END DO
     723              : 
     724           44 :       sm_col_offset = 0
     725           44 :       DO i = 1, nonzero_rows%elements
     726           44 :          IF (nonzero_rows%get(i) == (sm_id - 1)*this%cols_per_sm + 1) THEN
     727              :             ! We just found the nonzero row that corresponds to the first inducing column of our submatrix
     728           44 :             sm_col_offset = i
     729           44 :             EXIT
     730              :          END IF
     731              :       END DO
     732           44 :       IF (sm_col_offset == 0) THEN
     733            0 :          CPABORT("Could not determine relevant result columns of submatrix")
     734              :       END IF
     735              : 
     736           44 :       offset_x1 = 0
     737           88 :       DO j = 1, nonzero_rows%elements
     738           44 :          offset_x2 = offset_x1 + this%col_blk_size(nonzero_rows%get(j))
     739              :          ! We only want to copy the blocks from the result columns
     740           44 :          IF ((j >= sm_col_offset) .AND. (j < sm_col_offset + this%cols_per_sm)) THEN
     741           44 :             offset_y1 = 0
     742           44 :             k_limit_left = this%coo_col_offsets(nonzero_rows%get(j))
     743           44 :             k_limit_right = this%coo_col_offsets(nonzero_rows%get(j) + 1) - 1
     744           88 :             DO i = 1, nonzero_rows%elements
     745           44 :                offset_y2 = offset_y1 + this%col_blk_size(nonzero_rows%get(i))
     746              :                ! Check if sm(i,j), i.e., (nonzero_rows(i),nonzero_rows(j)) exists in the original matrix and if so, copy it into the
     747              :                ! correct send buffer.
     748           44 :                k = k_limit_left
     749           44 :                DO WHILE (k <= k_limit_right)
     750           44 :                   IF (this%coo_rows(k) >= nonzero_rows%get(i)) EXIT
     751            0 :                   k = k + 1
     752              :                END DO
     753           44 :                k_limit_left = k
     754           44 :                IF (this%coo_rows(k) == nonzero_rows%get(i)) THEN ! it does exist and k is our block id
     755              :                   CALL dbcsr_get_stored_coordinates(matrix=this%dbcsr_mat, row=this%coo_rows(k), column=this%coo_cols(k), &
     756           44 :                                                     processor=m)
     757           44 :                   DO WHILE (buf_offset_idxs(m) <= this%result_blocks_for_rank(m)%elements)
     758           44 :                      IF (this%result_blocks_for_rank(m)%get(buf_offset_idxs(m)) >= k) EXIT
     759            0 :                      buf_offset_idxs(m) = buf_offset_idxs(m) + 1
     760              :                   END DO
     761           44 :                   IF (this%result_blocks_for_rank(m)%get(buf_offset_idxs(m)) /= k) THEN
     762            0 :                      CPABORT("Could not determine buffer offset for block")
     763              :                   END IF
     764           44 :                   bufsize = this%result_blocks_for_rank_buf_offsets(m)%data(buf_offset_idxs(m))
     765           44 :                   bufsize_next = bufsize + this%col_blk_size(this%coo_cols(k))*this%col_blk_size(this%coo_rows(k))
     766              :                   this%result_sendbufs(m)%data(bufsize + 1:bufsize_next) = RESHAPE( &
     767              :                                                                            sm(offset_y1 + 1:offset_y2, offset_x1 + 1:offset_x2), &
     768         1254 :                                                                            [bufsize_next - bufsize])
     769              :                END IF
     770           88 :                offset_y1 = offset_y2
     771              :             END DO
     772              :          END IF
     773           88 :          offset_x1 = offset_x2
     774              :       END DO
     775              : 
     776           44 :       DEALLOCATE (buf_offset_idxs)
     777              : 
     778              :       ! Free memory in set
     779           44 :       CALL nonzero_rows%reset
     780              : 
     781        11352 :    END SUBROUTINE submatrix_cpy_resultcol
     782              : 
     783              : ! **************************************************************************************************
     784              : !> \brief finalize results back into a dbcsr matrix
     785              : !> \param this - object of class submatrix_dissection_type
     786              : !> \param resultmat - result dbcsr matrix
     787              : !> \par History
     788              : !>       2020.02 created [Michael Lass]
     789              : !>       2020.05 add time measurements [Michael Lass]
     790              : ! **************************************************************************************************
     791           86 :    SUBROUTINE submatrix_communicate_results(this, resultmat)
     792              :       CLASS(submatrix_dissection_type), INTENT(INOUT)                 :: this
     793              :       TYPE(dbcsr_type), INTENT(INOUT)                                 :: resultmat
     794              : 
     795              :       INTEGER                                                         :: i, j, k, cur_row, cur_col, cur_sm, cur_buf, last_buf, &
     796              :                                                                          bufsize, bufsize_next, row_size, col_size
     797           86 :       REAL(kind=dp), DIMENSION(:), POINTER                            :: vector
     798           86 :       TYPE(buffer_type), DIMENSION(:), ALLOCATABLE                    :: result_recvbufs
     799              : 
     800              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'submatrix_communicate_results'
     801              :       INTEGER :: handle
     802              : 
     803           86 :       CALL timeset(routineN, handle)
     804              : 
     805          516 :       ALLOCATE (result_recvbufs(0:this%numnodes - 1))
     806          258 :       DO i = 0, this%numnodes - 1
     807          172 :          bufsize = 0
     808          215 :          DO j = 1, this%result_blocks_from_rank(i)%elements
     809           43 :             k = this%result_blocks_from_rank(i)%get(j)
     810          215 :             bufsize = bufsize + this%col_blk_size(this%coo_cols(k))*this%col_blk_size(this%coo_rows(k))
     811              :          END DO
     812          258 :          CALL result_recvbufs(i)%alloc(bufsize)
     813              :       END DO
     814              : 
     815              :       ! Communicate. We attempt to balance communication load in the network here by starting our sends with our right neighbor
     816              :       ! and first trying to receive from our left neighbor.
     817           86 :       IF (this%numnodes > 1) THEN
     818          258 :          DO i = 1, this%numnodes
     819          172 :             k = MODULO(this%myrank - i, this%numnodes) ! rank to receive from
     820          172 :             CALL this%group%irecv(msgout=result_recvbufs(k)%data, source=k, request=result_recvbufs(k)%mpi_request)
     821          172 :             k = MODULO(this%myrank + i, this%numnodes) ! rank to send to
     822          258 :             CALL this%group%isend(msgin=this%result_sendbufs(k)%data, dest=k, request=this%result_sendbufs(k)%mpi_request)
     823              :          END DO
     824          258 :          DO i = 0, this%numnodes - 1
     825          172 :             CALL this%result_sendbufs(i)%mpi_request%wait()
     826          258 :             CALL result_recvbufs(i)%mpi_request%wait()
     827              :          END DO
     828              :       ELSE
     829            0 :          result_recvbufs(0)%data = this%result_sendbufs(0)%data
     830              :       END IF
     831              : 
     832           86 :       last_buf = -1
     833           86 :       bufsize = 0
     834          129 :       DO i = 1, this%local_blocks
     835           43 :          cur_col = this%coo_cols_local(i)
     836           43 :          cur_row = this%coo_rows_local(i)
     837           43 :          cur_sm = (cur_col - 1)/this%cols_per_sm + 1
     838           43 :          cur_buf = this%submatrix_owners(cur_sm)
     839           43 :          IF (cur_buf > last_buf) bufsize = 0
     840           43 :          row_size = this%row_blk_size(cur_row)
     841           43 :          col_size = this%col_blk_size(cur_col)
     842           43 :          bufsize_next = bufsize + row_size*col_size
     843           43 :          vector => result_recvbufs(cur_buf)%data(bufsize + 1:bufsize_next)
     844              :          CALL dbcsr_put_block(matrix=resultmat, row=cur_row, col=cur_col, &
     845          129 :                               block=RESHAPE(vector, [row_size, col_size]))
     846           43 :          bufsize = bufsize_next
     847          129 :          last_buf = cur_buf
     848              :       END DO
     849              : 
     850          258 :       DO i = 0, this%numnodes - 1
     851          258 :          CALL result_recvbufs(i)%dealloc
     852              :       END DO
     853           86 :       DEALLOCATE (result_recvbufs)
     854              : 
     855           86 :       CALL dbcsr_finalize(resultmat)
     856              : 
     857           86 :       CALL timestop(handle)
     858           86 :    END SUBROUTINE submatrix_communicate_results
     859              : 
     860              : ! **************************************************************************************************
     861              : !> \brief sort two integer arrays using quicksort, using the second list as second-level sorting criterion
     862              : !> \param arr_a - first input array
     863              : !> \param arr_b - second input array
     864              : !> \param left - left boundary of region to be sorted
     865              : !> \param right - right boundary of region to be sorted
     866              : ! **************************************************************************************************
     867          172 :    RECURSIVE PURE SUBROUTINE qsort_two(arr_a, arr_b, left, right)
     868              : 
     869              :       INTEGER, DIMENSION(:), INTENT(inout)               :: arr_a, arr_b
     870              :       INTEGER, INTENT(in)                                :: left, right
     871              : 
     872              :       INTEGER                                            :: i, j, pivot_a, pivot_b, tmp
     873              : 
     874          172 :       IF (right - left < 1) RETURN
     875              : 
     876            0 :       i = left
     877            0 :       j = right - 1
     878            0 :       pivot_a = arr_a(right)
     879            0 :       pivot_b = arr_b(right)
     880              : 
     881            0 :       DO
     882            0 :          DO WHILE ((arr_a(i) < pivot_a) .OR. ((arr_a(i) == pivot_a) .AND. (arr_b(i) < pivot_b)))
     883            0 :             i = i + 1
     884              :          END DO
     885            0 :          DO WHILE ((j > i) .AND. ((arr_a(j) > pivot_a) .OR. ((arr_a(j) == pivot_a) .AND. (arr_b(j) >= pivot_b))))
     886            0 :             j = j - 1
     887              :          END DO
     888            0 :          IF (i < j) THEN
     889            0 :             tmp = arr_a(i)
     890            0 :             arr_a(i) = arr_a(j)
     891            0 :             arr_a(j) = tmp
     892            0 :             tmp = arr_b(i)
     893            0 :             arr_b(i) = arr_b(j)
     894            0 :             arr_b(j) = tmp
     895              :          ELSE
     896              :             EXIT
     897              :          END IF
     898              :       END DO
     899              : 
     900            0 :       IF ((arr_a(i) > pivot_a) .OR. (arr_a(i) == pivot_a .AND. arr_b(i) > pivot_b)) THEN
     901            0 :          tmp = arr_a(i)
     902            0 :          arr_a(i) = arr_a(right)
     903            0 :          arr_a(right) = tmp
     904            0 :          tmp = arr_b(i)
     905            0 :          arr_b(i) = arr_b(right)
     906            0 :          arr_b(right) = tmp
     907              :       END IF
     908              : 
     909            0 :       IF (i - 1 > left) CALL qsort_two(arr_a, arr_b, left, i - 1)
     910            0 :       IF (i + 1 < right) CALL qsort_two(arr_a, arr_b, i + 1, right)
     911              : 
     912              :    END SUBROUTINE qsort_two
     913              : 
     914            0 : END MODULE submatrix_dissection
        

Generated by: LCOV version 2.0-1