LCOV - code coverage report
Current view: top level - src - cp_dbcsr_redistribution.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 98.8 % 243 240
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 4 4

            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              : !> \brief Redistribution of DBCSR blocks and distributed matrix slices.
      10              : ! **************************************************************************************************
      11              : MODULE cp_dbcsr_redistribution
      12              :    USE cp_dbcsr_api,                    ONLY: &
      13              :         dbcsr_clear, dbcsr_filter, dbcsr_finalize, dbcsr_get_block_p, dbcsr_get_info, &
      14              :         dbcsr_get_readonly_block_p, dbcsr_get_stored_coordinates, dbcsr_iterator_blocks_left, &
      15              :         dbcsr_iterator_next_block, dbcsr_iterator_readonly_start, dbcsr_iterator_start, &
      16              :         dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_p_type, dbcsr_reserve_blocks, dbcsr_set, &
      17              :         dbcsr_type, dbcsr_type_no_symmetry
      18              :    USE cp_dbcsr_contrib,                ONLY: dbcsr_reserve_all_blocks
      19              :    USE cp_dbcsr_operations,             ONLY: cp_dbcsr_m_by_n_from_template
      20              :    USE kinds,                           ONLY: dp,&
      21              :                                               int_8
      22              :    USE matrix_communication,            ONLY: allocate_matrix_buffers,&
      23              :                                               exchange_matrix_buffers,&
      24              :                                               matrix_buffer_type
      25              :    USE message_passing,                 ONLY: mp_comm_type,&
      26              :                                               mp_para_env_type
      27              :    USE util,                            ONLY: sort
      28              : #include "./base/base_uses.f90"
      29              : 
      30              :    IMPLICIT NONE
      31              :    PRIVATE
      32              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_dbcsr_redistribution'
      33              :    PUBLIC :: cp_dbcsr_to_subgroups, cp_dbcsr_from_subgroups, cp_array_to_dbcsr_slices, cp_array_to_dbcsr_blocks
      34              : 
      35              : CONTAINS
      36              : 
      37              : ! **************************************************************************************************
      38              : !> \brief ...
      39              : !> \param mat_global ...
      40              : !> \param mat_local ...
      41              : !> \param para_env ...
      42              : !> \param num_pe_sub ...
      43              : !> \param block_row_ranges ...
      44              : ! **************************************************************************************************
      45        12848 :    SUBROUTINE cp_dbcsr_to_subgroups(mat_global, mat_local, para_env, num_pe_sub, block_row_ranges)
      46              :       TYPE(dbcsr_type)                                   :: mat_global, mat_local
      47              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
      48              :       INTEGER                                            :: num_pe_sub
      49              :       INTEGER, DIMENSION(:, :), OPTIONAL                 :: block_row_ranges
      50              : 
      51              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_dbcsr_to_subgroups'
      52              : 
      53              :       INTEGER :: block_offset, block_size, col, col_size, handle, handle2, i_block, igroup, imep, &
      54              :          imep_sub, msg_offset, ngroup, offset, row, row_size
      55        12848 :       INTEGER, ALLOCATABLE                               :: counts_priv(:, :), counts_rec(:, :), &
      56        12848 :                                                             counts_send(:, :)
      57        12848 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: blk_counter, entry_counter
      58              :       LOGICAL                                            :: found
      59        12848 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: data_block
      60              :       TYPE(dbcsr_iterator_type)                          :: iter
      61        12848 :       TYPE(matrix_buffer_type)                           :: buffer_rec, buffer_send
      62              : 
      63        12848 :       CALL timeset(routineN, handle)
      64              : 
      65        12848 :       CALL timeset(routineN//"_count", handle2)
      66              : 
      67        51392 :       ALLOCATE (counts_send(2, 0:para_env%num_pe - 1), counts_rec(2, 0:para_env%num_pe - 1))
      68        12848 :       counts_send = 0
      69              : 
      70        12848 :       ngroup = para_env%num_pe/num_pe_sub
      71              : 
      72              : !$OMP PARALLEL DEFAULT(NONE) &
      73              : !$OMP SHARED(mat_global, mat_local, para_env, ngroup, num_pe_sub, block_row_ranges, counts_send) &
      74        12848 : !$OMP PRIVATE(iter, row, col, data_block, row_size, col_size, imep_sub, igroup, imep, counts_priv)
      75              :       NULLIFY (data_block)
      76              :       ALLOCATE (counts_priv(2, 0:para_env%num_pe - 1))
      77              :       counts_priv = 0
      78              :       CALL dbcsr_iterator_start(iter, mat_global)
      79              :       DO WHILE (dbcsr_iterator_blocks_left(iter))
      80              : 
      81              :          CALL dbcsr_iterator_next_block(iter, row, col, data_block, &
      82              :                                         row_size=row_size, col_size=col_size)
      83              : 
      84              :          CALL dbcsr_get_stored_coordinates(mat_local, row, col, imep_sub)
      85              : 
      86              :          DO igroup = 0, ngroup - 1
      87              : 
      88              :             IF (PRESENT(block_row_ranges)) THEN
      89              :                IF (row < block_row_ranges(1, igroup + 1) .OR. row > block_row_ranges(2, igroup + 1)) CYCLE
      90              :             END IF
      91              :             imep = imep_sub + igroup*num_pe_sub
      92              : 
      93              :             counts_priv(1, imep) = counts_priv(1, imep) + row_size*col_size
      94              :             counts_priv(2, imep) = counts_priv(2, imep) + 1
      95              : 
      96              :          END DO
      97              : 
      98              :       END DO
      99              : 
     100              :       CALL dbcsr_iterator_stop(iter)
     101              : !$OMP CRITICAL(dbcsr_to_subgroups_count_crit)
     102              :       counts_send(:, :) = counts_send(:, :) + counts_priv(:, :)
     103              : !$OMP END CRITICAL(dbcsr_to_subgroups_count_crit)
     104              :       DEALLOCATE (counts_priv)
     105              : !$OMP END PARALLEL
     106              : 
     107        12848 :       CALL timestop(handle2)
     108              : 
     109        12848 :       CALL timeset(routineN//"_exchange_counts", handle2)
     110              : 
     111        12848 :       IF (para_env%num_pe > 1) THEN
     112        12848 :          CALL para_env%alltoall(counts_send, counts_rec, 2)
     113              :       ELSE
     114            0 :          counts_rec(:, :) = counts_send
     115              :       END IF
     116              : 
     117        12848 :       CALL timestop(handle2)
     118              : 
     119        12848 :       CALL timeset(routineN//"_pack", handle2)
     120              : 
     121              :       CALL allocate_matrix_buffers(counts_send(1, :), counts_rec(1, :), 3, buffer_send, buffer_rec, &
     122        12848 :                                    counts_send(2, :), counts_rec(2, :))
     123              : 
     124        38544 :       ALLOCATE (entry_counter(0:para_env%num_pe - 1))
     125        38544 :       entry_counter(:) = buffer_send%msg_offset(:para_env%num_pe - 1)
     126              : 
     127        25696 :       ALLOCATE (blk_counter(0:para_env%num_pe - 1))
     128        38544 :       blk_counter(:) = buffer_send%indx_offset(:para_env%num_pe - 1)
     129              : 
     130              : !$OMP PARALLEL DEFAULT(NONE) &
     131              : !$OMP SHARED(mat_global, mat_local, ngroup, num_pe_sub, block_row_ranges, entry_counter, &
     132              : !$OMP        blk_counter, buffer_send) &
     133              : !$OMP PRIVATE(iter, row, col, data_block, row_size, col_size, imep_sub, igroup, imep, &
     134        12848 : !$OMP         msg_offset, block_size, block_offset)
     135              :       NULLIFY (data_block)
     136              :       CALL dbcsr_iterator_start(iter, mat_global)
     137              :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     138              : 
     139              :          CALL dbcsr_iterator_next_block(iter, row, col, data_block, &
     140              :                                         row_size=row_size, col_size=col_size)
     141              : 
     142              :          CALL dbcsr_get_stored_coordinates(mat_local, row, col, imep_sub)
     143              : 
     144              :          DO igroup = 0, ngroup - 1
     145              : 
     146              :             IF (PRESENT(block_row_ranges)) THEN
     147              :                IF (row < block_row_ranges(1, igroup + 1) .OR. row > block_row_ranges(2, igroup + 1)) CYCLE
     148              :             END IF
     149              : 
     150              :             imep = imep_sub + igroup*num_pe_sub
     151              :             block_size = row_size*col_size
     152              : 
     153              : !$OMP ATOMIC CAPTURE
     154              :             msg_offset = entry_counter(imep)
     155              :             entry_counter(imep) = entry_counter(imep) + block_size
     156              : !$OMP END ATOMIC
     157              : 
     158              : !$OMP ATOMIC CAPTURE
     159              :             blk_counter(imep) = blk_counter(imep) + 1
     160              :             block_offset = blk_counter(imep)
     161              : !$OMP END ATOMIC
     162              : 
     163              :             buffer_send%msg(msg_offset + 1:msg_offset + block_size) = &
     164              :                RESHAPE(data_block(1:row_size, 1:col_size), [block_size])
     165              : 
     166              :             ! The payload offset in each record is relative to its peer's segment.
     167              :             buffer_send%indx(:, block_offset) = [row, col, msg_offset - buffer_send%msg_offset(imep)]
     168              : 
     169              :          END DO
     170              : 
     171              :       END DO
     172              : 
     173              :       CALL dbcsr_iterator_stop(iter)
     174              : !$OMP END PARALLEL
     175              : 
     176        12848 :       CALL timestop(handle2)
     177              : 
     178        12848 :       CALL timeset(routineN//"_exchange_data", handle2)
     179              : 
     180        12848 :       CALL exchange_matrix_buffers(para_env, buffer_rec, buffer_send)
     181              : 
     182        12848 :       CALL timestop(handle2)
     183              : 
     184        12848 :       CALL timeset(routineN//"_reserve", handle2)
     185              : 
     186        12848 :       CALL dbcsr_set(mat_local, 0.0_dp)
     187        12848 :       CALL dbcsr_filter(mat_local, 1.0_dp)
     188        12848 :       CALL dbcsr_reserve_blocks(mat_local, rows=buffer_rec%indx(1, :), cols=buffer_rec%indx(2, :))
     189        12848 :       CALL dbcsr_finalize(mat_local)
     190        12848 :       CALL dbcsr_set(mat_local, 0.0_dp)
     191              : 
     192        12848 :       CALL timestop(handle2)
     193              : 
     194        12848 :       CALL timeset(routineN//"_unpack", handle2)
     195              : 
     196        38544 :       DO imep = 0, para_env%num_pe - 1
     197        78098 :          DO i_block = buffer_rec%indx_offset(imep) + 1, buffer_rec%indx_offset(imep + 1)
     198        39554 :             row = buffer_rec%indx(1, i_block)
     199        39554 :             col = buffer_rec%indx(2, i_block)
     200        39554 :             offset = buffer_rec%msg_offset(imep) + buffer_rec%indx(3, i_block)
     201              :             CALL dbcsr_get_block_p(mat_local, row, col, data_block, found=found, &
     202        39554 :                                    row_size=row_size, col_size=col_size)
     203        39554 :             CPASSERT(found)
     204              :             data_block(:, :) = RESHAPE(buffer_rec%msg(offset + 1:offset + row_size*col_size), &
     205       183912 :                                        [row_size, col_size])
     206              :          END DO
     207              :       END DO
     208              : 
     209        12848 :       CALL timestop(handle2)
     210              : 
     211        12848 :       CALL timestop(handle)
     212              : 
     213        89936 :    END SUBROUTINE cp_dbcsr_to_subgroups
     214              : 
     215              : ! **************************************************************************************************
     216              : !> \brief Sum subgroup blocks into the global layout, or copy mapped blocks.
     217              : !> \param mat_local ...
     218              : !> \param mat_global ...
     219              : !> \param para_env communicator covering all source and destination ranks
     220              : !> \param row_mapping optional source-to-destination block-row permutation
     221              : !> \param copy_only copy without summation, filtering, or consuming the source (default: false)
     222              : ! **************************************************************************************************
     223         7446 :    SUBROUTINE cp_dbcsr_from_subgroups(mat_local, mat_global, para_env, row_mapping, copy_only)
     224              : 
     225              :       TYPE(dbcsr_type)                                   :: mat_local, mat_global
     226              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
     227              :       INTEGER, DIMENSION(:), INTENT(IN), OPTIONAL        :: row_mapping
     228              :       LOGICAL, INTENT(IN), OPTIONAL                      :: copy_only
     229              : 
     230              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_dbcsr_from_subgroups'
     231              : 
     232              :       INTEGER                                            :: block_size, col, col_size, handle, &
     233              :                                                             handle2, i_block, imep, mapped_row, &
     234              :                                                             offset, row, row_size
     235         7446 :       INTEGER, ALLOCATABLE                               :: counts_priv(:, :), counts_rec(:, :), &
     236         7446 :                                                             counts_send(:, :)
     237         7446 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: block_counter, entry_counter
     238              :       LOGICAL                                            :: found, my_copy_only
     239         7446 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: data_block
     240              :       TYPE(dbcsr_iterator_type)                          :: iter
     241         7446 :       TYPE(matrix_buffer_type)                           :: buffer_rec, buffer_send
     242              : 
     243         7446 :       CALL timeset(routineN, handle)
     244         7446 :       my_copy_only = .FALSE.
     245         7446 :       IF (PRESENT(copy_only)) my_copy_only = copy_only
     246              : 
     247         7446 :       CALL timeset(routineN//"_prepare", handle2)
     248              : 
     249         7446 :       IF (.NOT. my_copy_only) THEN
     250         7444 :          CALL dbcsr_clear(mat_global)
     251         7444 :          CALL dbcsr_reserve_all_blocks(mat_global)
     252              :       END IF
     253              : 
     254        29784 :       ALLOCATE (counts_send(2, 0:para_env%num_pe - 1), counts_rec(2, 0:para_env%num_pe - 1))
     255         7446 :       counts_send = 0
     256              : 
     257              : !$OMP PARALLEL DEFAULT(NONE) &
     258              : !$OMP SHARED(mat_local, mat_global, para_env, row_mapping, counts_send) &
     259         7446 : !$OMP PRIVATE(iter, row, col, data_block, row_size, col_size, mapped_row, imep, counts_priv)
     260              :       NULLIFY (data_block)
     261              :       ALLOCATE (counts_priv(2, 0:para_env%num_pe - 1))
     262              :       counts_priv = 0
     263              :       CALL dbcsr_iterator_readonly_start(iter, mat_local)
     264              :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     265              : 
     266              :          CALL dbcsr_iterator_next_block(iter, row, col, data_block, &
     267              :                                         row_size=row_size, col_size=col_size)
     268              : 
     269              :          mapped_row = row
     270              :          IF (PRESENT(row_mapping)) mapped_row = row_mapping(row)
     271              :          CALL dbcsr_get_stored_coordinates(mat_global, mapped_row, col, imep)
     272              : 
     273              :          counts_priv(1, imep) = counts_priv(1, imep) + row_size*col_size
     274              :          counts_priv(2, imep) = counts_priv(2, imep) + 1
     275              : 
     276              :       END DO
     277              : 
     278              :       CALL dbcsr_iterator_stop(iter)
     279              : !$OMP CRITICAL(dbcsr_from_subgroups_count_crit)
     280              :       counts_send(:, :) = counts_send(:, :) + counts_priv(:, :)
     281              : !$OMP END CRITICAL(dbcsr_from_subgroups_count_crit)
     282              :       DEALLOCATE (counts_priv)
     283              : !$OMP END PARALLEL
     284              : 
     285         7446 :       CALL timestop(handle2)
     286              : 
     287         7446 :       CALL timeset(routineN//"_exchange_counts", handle2)
     288              : 
     289         7446 :       IF (para_env%num_pe > 1) THEN
     290         7446 :          CALL para_env%alltoall(counts_send, counts_rec, 2)
     291              :       ELSE
     292            0 :          counts_rec(:, :) = counts_send
     293              :       END IF
     294              : 
     295         7446 :       CALL timestop(handle2)
     296              : 
     297         7446 :       CALL timeset(routineN//"_pack", handle2)
     298              : 
     299              :       CALL allocate_matrix_buffers(counts_send(1, :), counts_rec(1, :), 3, buffer_send, buffer_rec, &
     300         7446 :                                    counts_send(2, :), counts_rec(2, :))
     301              : 
     302        22338 :       ALLOCATE (block_counter(0:para_env%num_pe - 1))
     303        22338 :       block_counter(:) = buffer_send%indx_offset(:para_env%num_pe - 1)
     304              : 
     305        14892 :       ALLOCATE (entry_counter(0:para_env%num_pe - 1))
     306        22338 :       entry_counter(:) = buffer_send%msg_offset(:para_env%num_pe - 1)
     307              : 
     308              :       ! fill buffer_send
     309              : !$OMP PARALLEL DEFAULT(NONE) &
     310              : !$OMP SHARED(mat_local, mat_global, row_mapping, entry_counter, block_counter, buffer_send) &
     311              : !$OMP PRIVATE(iter, row, col, data_block, row_size, col_size, mapped_row, imep, block_size, &
     312         7446 : !$OMP         offset, i_block)
     313              :       NULLIFY (data_block)
     314              :       CALL dbcsr_iterator_readonly_start(iter, mat_local)
     315              :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     316              : 
     317              :          CALL dbcsr_iterator_next_block(iter, row, col, data_block, &
     318              :                                         row_size=row_size, col_size=col_size)
     319              : 
     320              :          mapped_row = row
     321              :          IF (PRESENT(row_mapping)) mapped_row = row_mapping(row)
     322              :          CALL dbcsr_get_stored_coordinates(mat_global, mapped_row, col, imep)
     323              : 
     324              :          block_size = row_size*col_size
     325              : 
     326              : !$OMP ATOMIC CAPTURE
     327              :          offset = entry_counter(imep)
     328              :          entry_counter(imep) = entry_counter(imep) + block_size
     329              : !$OMP END ATOMIC
     330              : 
     331              : !$OMP ATOMIC CAPTURE
     332              :          block_counter(imep) = block_counter(imep) + 1
     333              :          i_block = block_counter(imep)
     334              : !$OMP END ATOMIC
     335              : 
     336              :          buffer_send%msg(offset + 1:offset + block_size) = &
     337              :             RESHAPE(data_block(1:row_size, 1:col_size), [block_size])
     338              : 
     339              :          ! The receiver adds its own segment start to this peer-relative offset.
     340              :          buffer_send%indx(:, i_block) = [mapped_row, col, offset - buffer_send%msg_offset(imep)]
     341              : 
     342              :       END DO
     343              : 
     344              :       CALL dbcsr_iterator_stop(iter)
     345              : !$OMP END PARALLEL
     346              : 
     347         7446 :       CALL timestop(handle2)
     348              : 
     349         7446 :       CALL timeset(routineN//"_exchange_data", handle2)
     350              : 
     351         7446 :       CALL exchange_matrix_buffers(para_env, buffer_rec, buffer_send)
     352              : 
     353         7446 :       CALL timestop(handle2)
     354              : 
     355         7446 :       CALL timeset(routineN//"_unpack", handle2)
     356              : 
     357              :       ! Keep the rank and receive-buffer order of the block-wise accumulation.
     358        22338 :       DO imep = 0, para_env%num_pe - 1
     359        37612 :          DO i_block = buffer_rec%indx_offset(imep) + 1, buffer_rec%indx_offset(imep + 1)
     360        15274 :             row = buffer_rec%indx(1, i_block)
     361        15274 :             col = buffer_rec%indx(2, i_block)
     362        15274 :             offset = buffer_rec%msg_offset(imep) + buffer_rec%indx(3, i_block)
     363              :             CALL dbcsr_get_block_p(mat_global, row, col, data_block, found=found, &
     364        15274 :                                    row_size=row_size, col_size=col_size)
     365        15274 :             CPASSERT(found)
     366        45440 :             IF (my_copy_only) THEN
     367              :                data_block(:, :) = RESHAPE(buffer_rec%msg(offset + 1:offset + row_size*col_size), &
     368           24 :                                           [row_size, col_size])
     369              :             ELSE
     370              :                data_block(:, :) = data_block + &
     371              :                                   RESHAPE(buffer_rec%msg(offset + 1:offset + row_size*col_size), &
     372      1980401 :                                           [row_size, col_size])
     373              :             END IF
     374              :          END DO
     375              :       END DO
     376              : 
     377         7446 :       IF (.NOT. my_copy_only) THEN
     378              :          ! Remove numerically zero blocks from the summed matrix.
     379         7444 :          CALL dbcsr_filter(mat_global, 1.0E-30_dp)
     380              :       END IF
     381              : 
     382              :       IF (.NOT. my_copy_only) THEN
     383         7444 :          CALL dbcsr_set(mat_local, 0.0_dp)
     384         7444 :          CALL dbcsr_filter(mat_local, 1.0_dp)
     385              :       END IF
     386              : 
     387         7446 :       CALL timestop(handle2)
     388              : 
     389         7446 :       CALL timestop(handle)
     390              : 
     391        52122 :    END SUBROUTINE cp_dbcsr_from_subgroups
     392              : 
     393              : ! **************************************************************************************************
     394              : !> \brief Consume a distributed flattened array and create a DBCSR matrix per slice.
     395              : !> \param array local flattened rows; all ranks have the same number of slices; deallocated on return
     396              : !> \param nrows number of rows in each output matrix
     397              : !> \param ncols number of columns in each output matrix
     398              : !> \param para_env_sub communicator for the output matrices
     399              : !> \param first_row first flattened row held locally
     400              : !> \param last_row last flattened row held locally
     401              : !> \param template DBCSR template defining output distribution and block sizes
     402              : !> \param matrices preallocated array of output matrices
     403              : !> \param matrix_ptrs preallocated array of output pointers; pointees are allocated here
     404              : !> \note Supply exactly one output representation. Flattened rows use (i-1)*ncols+j.
     405              : ! **************************************************************************************************
     406          360 :    SUBROUTINE cp_array_to_dbcsr_slices(array, nrows, ncols, para_env_sub, &
     407          360 :                                        first_row, last_row, template, matrices, matrix_ptrs)
     408              :       REAL(KIND=dp), ALLOCATABLE, INTENT(INOUT), TARGET  :: array(:, :)
     409              :       INTEGER, INTENT(IN)                                :: nrows, ncols
     410              :       TYPE(mp_para_env_type), INTENT(IN), POINTER        :: para_env_sub
     411              :       INTEGER, INTENT(IN)                                :: first_row, last_row
     412              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: template
     413              :       TYPE(dbcsr_type), INTENT(INOUT), OPTIONAL, TARGET  :: matrices(:)
     414              :       TYPE(dbcsr_p_type), INTENT(INOUT), OPTIONAL        :: matrix_ptrs(:)
     415              : 
     416              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_array_to_dbcsr_slices'
     417              : 
     418              :       INTEGER                                            :: handle, i, islice, j, nblock, nslice
     419          360 :       INTEGER, ALLOCATABLE                               :: blocks(:, :)
     420          360 :       INTEGER, POINTER                                   :: local_cols(:), local_rows(:)
     421          360 :       REAL(KIND=dp), POINTER                             :: values(:, :, :)
     422          360 :       TYPE(dbcsr_p_type), ALLOCATABLE                    :: outputs(:)
     423              : 
     424          360 :       CALL timeset(routineN, handle)
     425          360 :       CPASSERT(PRESENT(matrices) .NEQV. PRESENT(matrix_ptrs))
     426          360 :       nslice = SIZE(array, 2)
     427        17003 :       ALLOCATE (outputs(nslice))
     428        16283 :       DO islice = 1, nslice
     429        15923 :          IF (PRESENT(matrices)) THEN
     430           83 :             outputs(islice)%matrix => matrices(islice)
     431              :          ELSE
     432        15840 :             ALLOCATE (matrix_ptrs(islice)%matrix)
     433        15840 :             outputs(islice)%matrix => matrix_ptrs(islice)%matrix
     434              :          END IF
     435              :          CALL cp_dbcsr_m_by_n_from_template(outputs(islice)%matrix, template=template, &
     436        15923 :                                             m=nrows, n=ncols, sym=dbcsr_type_no_symmetry)
     437        15923 :          CALL dbcsr_reserve_all_blocks(outputs(islice)%matrix)
     438        16283 :          CALL dbcsr_finalize(outputs(islice)%matrix)
     439              :       END DO
     440          360 :       IF (nslice > 0) THEN
     441          360 :          CALL dbcsr_get_info(outputs(1)%matrix, local_rows=local_rows, local_cols=local_cols)
     442         1072 :          ALLOCATE (blocks(3, SIZE(local_rows)*SIZE(local_cols)*nslice))
     443          360 :          nblock = 0
     444        16283 :          DO islice = 1, nslice
     445        32206 :             DO j = 1, SIZE(local_cols)
     446        47051 :                DO i = 1, SIZE(local_rows)
     447        15205 :                   nblock = nblock + 1
     448        76743 :                   blocks(:, nblock) = [local_rows(i), local_cols(j), islice]
     449              :                END DO
     450              :             END DO
     451              :          END DO
     452              :          ! A rank-remapped view preserves the row-major flattened ia convention without copying.
     453          360 :          values(1:SIZE(array, 1), 1:1, 1:nslice) => array
     454              :          CALL cp_array_to_dbcsr_blocks(values, first_row, last_row, para_env_sub, outputs, blocks, &
     455          360 :                                        row_major=.TRUE.)
     456              :       END IF
     457          360 :       DEALLOCATE (array)
     458          360 :       CALL timestop(handle)
     459          720 :    END SUBROUTINE cp_array_to_dbcsr_slices
     460              : 
     461              : ! **************************************************************************************************
     462              : !> \brief Fill existing DBCSR blocks directly from distributed flattened array slices.
     463              : !>        Requests split contiguous block fragments at source-rank boundaries. Sources combine
     464              : !>        components before replying, so each target element transfers only one real value.
     465              : !>        Slice tiles bound request/reply storage; existing sparsity is preserved.
     466              : !> \param array local flattened entries, components, slices; borrowed, never deallocated
     467              : !> \param first_entry first flattened entry held locally
     468              : !> \param last_entry last flattened entry held locally; may precede first_entry on empty ranks
     469              : !> \param comm communicator covering sources and output matrices, in any source-range order
     470              : !> \param matrices existing output slices with identical dimensions and block sizes
     471              : !> \param blocks requested (block row, block column, slice) columns; absent local blocks are skipped
     472              : !> \param factors optional integer component multiplicities per requested block; default one component
     473              : !> \param row_major flatten with column index fastest; default Fortran column-major ordering
     474              : !> \note Source ranges must partition the whole flattened matrix. Requested blocks must be unique.
     475              : !>       Unrequested output entries are zeroed. The 64 MiB workspace target includes the borrowed
     476              : !>       array and descriptors; an irreducible single-slice exchange may exceed this target.
     477              : ! **************************************************************************************************
     478          372 :    SUBROUTINE cp_array_to_dbcsr_blocks(array, first_entry, last_entry, comm, matrices, blocks, factors, row_major)
     479              :       REAL(KIND=dp), INTENT(IN)                          :: array(:, :, :)
     480              :       INTEGER, INTENT(IN)                                :: first_entry, last_entry, blocks(:, :)
     481              : 
     482              :       CLASS(mp_comm_type), INTENT(IN)                    :: comm
     483              :       TYPE(dbcsr_p_type), INTENT(INOUT)                  :: matrices(:)
     484              :       INTEGER, INTENT(IN), OPTIONAL                      :: factors(:, :)
     485              :       LOGICAL, INTENT(IN), OPTIONAL                      :: row_major
     486              : 
     487              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_array_to_dbcsr_blocks'
     488              : 
     489              :       INTEGER(KIND=int_8), PARAMETER                     :: max_bytes = 64_int_8*1024**2
     490              : 
     491              :       INTEGER :: a, b, col, component, element, first, handle, i, image, last, length, lo, &
     492              :                  lower, middle, nc, nfast, nprocs, pass, peer, pos, rank, record, row, upper, width
     493              :       INTEGER(KIND=int_8)                                :: fixed_bytes, nel
     494          372 :       INTEGER, ALLOCATABLE                               :: counter(:), counts_rec(:, :), &
     495          372 :                                                             counts_send(:, :), ends(:), order(:), &
     496          372 :                                                             ranges(:, :), targets(:, :)
     497          372 :       INTEGER, POINTER                                   :: fast_offset(:), fast_size(:), &
     498          372 :                                                             slow_offset(:), slow_size(:)
     499              :       LOGICAL                                            :: found, row_order
     500          372 :       REAL(KIND=dp), POINTER                             :: block(:, :)
     501          372 :       TYPE(matrix_buffer_type)                           :: requests_rec, requests_send
     502              : 
     503          372 :       CALL timeset(routineN, handle)
     504          372 :       IF (SIZE(matrices) == 0) THEN
     505            0 :          CALL timestop(handle)
     506              :          RETURN
     507              :       END IF
     508          372 :       row_order = .FALSE.
     509          372 :       IF (PRESENT(row_major)) row_order = row_major
     510              :       CALL dbcsr_get_info(matrices(1)%matrix, row_blk_size=fast_size, col_blk_size=slow_size, &
     511          372 :                           row_blk_offset=fast_offset, col_blk_offset=slow_offset)
     512          372 :       IF (row_order) THEN
     513              :          CALL dbcsr_get_info(matrices(1)%matrix, row_blk_size=slow_size, col_blk_size=fast_size, &
     514          360 :                              row_blk_offset=slow_offset, col_blk_offset=fast_offset)
     515              :       END IF
     516          768 :       nfast = SUM(fast_size)
     517          768 :       nel = INT(nfast, int_8)*SUM(slow_size)
     518          372 :       nc = SIZE(array, 2)
     519          372 :       CPASSERT(nel > 0 .AND. nel < HUGE(element))
     520          372 :       CPASSERT(nc >= 1)
     521          372 :       CPASSERT(SIZE(array, 1) == MAX(0, last_entry - first_entry + 1))
     522          372 :       CPASSERT(SIZE(array, 3) == SIZE(matrices) .AND. SIZE(blocks, 1) == 3)
     523          372 :       IF (PRESENT(factors)) THEN
     524           12 :          CPASSERT(SIZE(factors, 1) == nc .AND. SIZE(factors, 2) == SIZE(blocks, 2))
     525              :       ELSE
     526          360 :          CPASSERT(nc == 1)
     527              :       END IF
     528          372 :       nprocs = comm%num_pe
     529              :       ALLOCATE (ends(nprocs), order(nprocs), ranges(2, nprocs), counts_send(2, 0:nprocs - 1), &
     530         4092 :                 counts_rec(2, 0:nprocs - 1), counter(0:nprocs - 1))
     531         1116 :       CALL comm%allgather([first_entry, last_entry], ranges)
     532          772 :       ends(:) = ranges(2, :)
     533          772 :       WHERE (ranges(2, :) < ranges(1, :)) ends = HUGE(element)
     534          372 :       CALL sort(ends, nprocs, order)
     535              :       ! Validate coverage, independently of communicator rank ordering and empty ranges.
     536          372 :       element = 0
     537          772 :       DO rank = 1, nprocs
     538          400 :          IF (ends(rank) == HUGE(element)) EXIT
     539          400 :          CPASSERT(ranges(1, order(rank)) == element + 1)
     540          772 :          element = ends(rank)
     541              :       END DO
     542          372 :       CPASSERT(INT(element, int_8) == nel)
     543         2232 :       fixed_bytes = 8_int_8*SIZE(array, KIND=int_8) + 4_int_8*SIZE(blocks, KIND=int_8) + 64_int_8*nprocs
     544          396 :       IF (PRESENT(factors)) fixed_bytes = fixed_bytes + 4_int_8*SIZE(factors, KIND=int_8)
     545              :       width = INT(MIN(INT(SIZE(matrices), int_8), &
     546          372 :                       MAX(1_int_8, (max_bytes - fixed_bytes)/((80_int_8 + 8*nc)*nel))))
     547          372 :       CALL comm%min(width)
     548        16691 :       DO image = 1, SIZE(matrices)
     549        16691 :          CALL dbcsr_set(matrices(image)%matrix, 0.0_dp)
     550              :       END DO
     551          372 :       DO first = 1, SIZE(matrices), width
     552          372 :          last = MIN(SIZE(matrices), first + width - 1)
     553          372 :          counts_send(:, :) = 0
     554         1116 :          DO pass = 1, 2
     555          744 :             IF (pass == 2) THEN
     556          372 :                CALL comm%alltoall(counts_send, counts_rec, 2)
     557              :                CALL allocate_matrix_buffers(counts_send(1, :), counts_rec(1, :), 3 + nc, requests_send, requests_rec, &
     558          372 :                                             send_blocks=counts_send(2, :), recv_blocks=counts_rec(2, :))
     559         1108 :                ALLOCATE (targets(3, SIZE(requests_send%indx, 2)))
     560          772 :                counter(:) = requests_send%indx_offset(:nprocs - 1)
     561              :             END IF
     562        33014 :             DO i = 1, SIZE(blocks, 2)
     563        31898 :                image = blocks(3, i)
     564        31898 :                IF (image < first .OR. image > last) CYCLE
     565        31898 :                CALL dbcsr_get_readonly_block_p(matrices(image)%matrix, blocks(1, i), blocks(2, i), block, found=found)
     566        31898 :                IF (.NOT. found) CYCLE
     567        31898 :                a = blocks(1, i)
     568        31898 :                b = blocks(2, i)
     569        31898 :                IF (row_order) THEN
     570        30410 :                   a = blocks(2, i)
     571        30410 :                   b = blocks(1, i)
     572              :                END IF
     573       196980 :                DO col = 1, slow_size(b)
     574              :                   row = 1
     575       297446 :                   DO WHILE (row <= fast_size(a))
     576       133108 :                      element = (slow_offset(b) + col - 2)*nfast + fast_offset(a) + row - 1
     577              :                      ! Find the first nonempty source range whose upper bound covers this entry.
     578       133108 :                      lower = 1
     579       133108 :                      upper = nprocs
     580       159744 :                      DO WHILE (lower < upper)
     581        26636 :                         middle = (lower + upper)/2
     582       159744 :                         IF (ends(middle) < element) THEN
     583        17284 :                            lower = middle + 1
     584              :                         ELSE
     585              :                            upper = middle
     586              :                         END IF
     587              :                      END DO
     588       133108 :                      peer = order(lower) - 1
     589       133108 :                      length = MIN(fast_size(a) - row + 1, ends(lower) - element + 1)
     590       133108 :                      IF (pass == 1) THEN
     591       199662 :                         counts_send(:, peer) = counts_send(:, peer) + [length, 1]
     592              :                      ELSE
     593        66554 :                         counter(peer) = counter(peer) + 1
     594        66554 :                         record = counter(peer)
     595       266216 :                         requests_send%indx(1:3, record) = [element, length, image]
     596       142894 :                         requests_send%indx(4:, record) = 1
     597        86126 :                         IF (PRESENT(factors)) requests_send%indx(4:, record) = factors(:, i)
     598       266216 :                         targets(:, record) = [i, row, col]
     599              :                      END IF
     600       133108 :                      row = row + length
     601              :                   END DO
     602              :                END DO
     603              :             END DO
     604              :          END DO
     605          372 :          CALL exchange_matrix_buffers(comm, requests_rec, requests_send, do_msg=.FALSE.)
     606          772 :          DO peer = 0, nprocs - 1
     607          400 :             pos = requests_rec%msg_offset(peer)
     608        67326 :             DO record = requests_rec%indx_offset(peer) + 1, requests_rec%indx_offset(peer + 1)
     609        66554 :                lo = requests_rec%indx(1, record) - first_entry + 1
     610        66554 :                length = requests_rec%indx(2, record)
     611        66554 :                image = requests_rec%indx(3, record)
     612        66554 :                CPASSERT(lo >= 1 .AND. lo + length - 1 <= SIZE(array, 1))
     613              :                requests_rec%msg(pos + 1:pos + length) = &
     614      1084462 :                   REAL(requests_rec%indx(4, record), dp)*array(lo:lo + length - 1, 1, image)
     615        76340 :                DO component = 2, nc
     616              :                   requests_rec%msg(pos + 1:pos + length) = requests_rec%msg(pos + 1:pos + length) + &
     617       202076 :                                       REAL(requests_rec%indx(3 + component, record), dp)*array(lo:lo + length - 1, component, image)
     618              :                END DO
     619        66954 :                pos = pos + length
     620              :             END DO
     621              :          END DO
     622          372 :          CALL exchange_matrix_buffers(comm, requests_send, requests_rec, do_indx=.FALSE.)
     623          772 :          DO peer = 0, nprocs - 1
     624          400 :             pos = requests_send%msg_offset(peer)
     625        67326 :             DO record = requests_send%indx_offset(peer) + 1, requests_send%indx_offset(peer + 1)
     626        66554 :                i = targets(1, record)
     627        66554 :                row = targets(2, record)
     628        66554 :                col = targets(3, record)
     629        66554 :                length = requests_send%indx(2, record)
     630        66554 :                CALL dbcsr_get_block_p(matrices(blocks(3, i))%matrix, blocks(1, i), blocks(2, i), block, found=found)
     631        66554 :                CPASSERT(found)
     632        66554 :                IF (row_order) THEN
     633       948940 :                   block(col, row:row + length - 1) = requests_send%msg(pos + 1:pos + length)
     634              :                ELSE
     635       135522 :                   block(row:row + length - 1, col) = requests_send%msg(pos + 1:pos + length)
     636              :                END IF
     637       133508 :                pos = pos + length
     638              :             END DO
     639              :          END DO
     640          372 :          DEALLOCATE (targets)
     641              :       END DO
     642          372 :       CALL timestop(handle)
     643          744 :    END SUBROUTINE cp_array_to_dbcsr_blocks
     644              : 
     645              : END MODULE cp_dbcsr_redistribution
        

Generated by: LCOV version 2.0-1