LCOV - code coverage report
Current view: top level - src/dbx - cp_dbcsr_io.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 92.4 % 330 305
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 2 2

            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 Binary file I/O for DBCSR/DBM matrices built on cp_dbcsr_api (DBCSRv_1.0 format)
      10              : ! **************************************************************************************************
      11              : MODULE cp_dbcsr_io
      12              :    USE cp_dbcsr_api,                    ONLY: &
      13              :         dbcsr_create, dbcsr_distribution_get, dbcsr_distribution_hold, dbcsr_distribution_release, &
      14              :         dbcsr_distribution_type, dbcsr_finalize, dbcsr_get_info, dbcsr_get_stored_coordinates, &
      15              :         dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_readonly_start, &
      16              :         dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_put_block, dbcsr_release, dbcsr_type, &
      17              :         dbcsr_valid_index, dbcsr_work_create
      18              :    USE kinds,                           ONLY: default_string_length,&
      19              :                                               dp,&
      20              :                                               int_8,&
      21              :                                               real_4,&
      22              :                                               real_4_size,&
      23              :                                               real_8_size
      24              :    USE message_passing,                 ONLY: &
      25              :         file_amode_create, file_amode_rdonly, file_amode_wronly, file_offset, mp_comm_type, &
      26              :         mp_file_delete, mp_file_type, mpi_character_size, mpi_integer_size
      27              :    USE util,                            ONLY: sort
      28              : #include "../base/base_uses.f90"
      29              : 
      30              :    IMPLICIT NONE
      31              :    PRIVATE
      32              : 
      33              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_dbcsr_io'
      34              : 
      35              :    INTEGER, PARAMETER, PRIVATE :: version_len = 10
      36              :    INTEGER, PARAMETER, PRIVATE :: name_v_1_0_len = 80
      37              :    INTEGER, PARAMETER, PRIVATE :: char_count = version_len + name_v_1_0_len + 1
      38              :    CHARACTER(LEN=version_len), PARAMETER, PRIVATE :: version_v_1_0 = "DBCSRv_1.0"
      39              : 
      40              :    INTEGER, PARAMETER, PRIVATE :: dbcsr_type_real_4_id = 1
      41              :    INTEGER, PARAMETER, PRIVATE :: dbcsr_type_real_8_id = 3
      42              : 
      43              :    PUBLIC :: dbcsr_binary_write
      44              :    PUBLIC :: dbcsr_binary_read
      45              : 
      46              : CONTAINS
      47              : 
      48              : ! **************************************************************************************************
      49              : !> \brief Write a DBCSR matrix to a binary file in DBCSRv_1.0 format
      50              : !> \param matrix ...
      51              : !> \param filepath ...
      52              : ! **************************************************************************************************
      53          282 :    SUBROUTINE dbcsr_binary_write(matrix, filepath)
      54              :       TYPE(dbcsr_type), INTENT(IN)                       :: matrix
      55              :       CHARACTER(LEN=*), INTENT(IN)                       :: filepath
      56              : 
      57              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_binary_write'
      58              :       INTEGER(KIND=file_offset), PARAMETER               :: bof = 0_file_offset
      59              : 
      60              :       CHARACTER                                          :: matrix_type
      61              :       CHARACTER(LEN=default_string_length)               :: matrix_name
      62              :       CHARACTER(LEN=name_v_1_0_len)                      :: matrix_name_v_1_0
      63              :       INTEGER :: bcol, brow, cnt, csize, cur_off, data_area_size, handle, k, linfo_size, &
      64              :          mp_handle, my_off, mynode, nblkcols_total, nblkrows_total, nblks, numnodes, nze, &
      65              :          orig_slot, r, rsize, s_end, s_start, slot
      66              :       INTEGER(KIND=file_offset) :: bdata_my_offset, bdata_start, prev_da, prev_linfo, &
      67              :          subh1_chars_bytes, subh2_my_offset, subh2_start, subh3_my_offset, subh3_start, tot_linfo
      68          282 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: blk_p, col_i, col_unsorted, da_sizes, &
      69          282 :                                                             hdr1_ints, linfo_sizes, linfo_vec, &
      70          282 :                                                             perm, raw_off, row_cnt, row_p, row_pos
      71          282 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: col_blk_size, row_blk_size
      72              :       INTEGER, DIMENSION(2)                              :: subh2_pair
      73              :       LOGICAL                                            :: tr
      74          282 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: r_dp, raw_data
      75          282 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: blk_ptr
      76              :       TYPE(dbcsr_distribution_type)                      :: distribution
      77              :       TYPE(dbcsr_iterator_type)                          :: iter
      78              :       TYPE(mp_comm_type)                                 :: mp_group
      79              :       TYPE(mp_file_type)                                 :: thefile
      80              : 
      81          282 :       CALL timeset(routineN, handle)
      82              : 
      83          282 :       IF (.NOT. dbcsr_valid_index(matrix)) THEN
      84            0 :          CPABORT("Invalid DBCSR matrix in dbcsr_binary_write")
      85              :       END IF
      86              : 
      87          282 :       NULLIFY (row_blk_size, col_blk_size, blk_ptr)
      88              :       CALL dbcsr_get_info(matrix, &
      89              :                           nblkrows_total=nblkrows_total, &
      90              :                           nblkcols_total=nblkcols_total, &
      91              :                           row_blk_size=row_blk_size, &
      92              :                           col_blk_size=col_blk_size, &
      93              :                           matrix_type=matrix_type, &
      94              :                           name=matrix_name, &
      95          282 :                           distribution=distribution)
      96              : 
      97          282 :       CALL dbcsr_distribution_get(distribution, group=mp_handle, mynode=mynode, numnodes=numnodes)
      98          282 :       CALL mp_group%set_handle(mp_handle)
      99              : 
     100              :       ! Pass 1: count local blocks per row and total local element count
     101          846 :       ALLOCATE (row_cnt(nblkrows_total))
     102          282 :       row_cnt(:) = 0
     103          282 :       nblks = 0
     104              :       data_area_size = 0
     105              : 
     106              : !$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, row_cnt) &
     107              : !$OMP PRIVATE(iter, brow, bcol, blk_ptr, rsize, csize, tr) &
     108          282 : !$OMP REDUCTION(+:nblks, data_area_size)
     109              :       NULLIFY (blk_ptr)
     110              :       CALL dbcsr_iterator_readonly_start(iter, matrix)
     111              :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     112              :          CALL dbcsr_iterator_next_block(iter, brow, bcol, blk_ptr, &
     113              :                                         row_size=rsize, col_size=csize, transposed=tr)
     114              :          nblks = nblks + 1
     115              : !$OMP ATOMIC UPDATE
     116              :          row_cnt(brow) = row_cnt(brow) + 1
     117              :          data_area_size = data_area_size + rsize*csize
     118              :       END DO
     119              :       CALL dbcsr_iterator_stop(iter)
     120              : !$OMP END PARALLEL
     121              : 
     122          846 :       ALLOCATE (row_p(nblkrows_total + 1))
     123          282 :       row_p(1) = 0
     124         1256 :       DO r = 1, nblkrows_total
     125         1256 :          row_p(r + 1) = row_p(r) + row_cnt(r)
     126              :       END DO
     127              : 
     128              :       ! Pass 2: collect block columns and untranposed block data, then sort columns per row
     129         1604 :       ALLOCATE (col_unsorted(nblks), raw_off(nblks), col_i(nblks), blk_p(nblks))
     130         1128 :       ALLOCATE (raw_data(MAX(1, data_area_size)), r_dp(MAX(1, data_area_size)))
     131          282 :       raw_data(:) = 0.0_dp
     132          282 :       r_dp(:) = 0.0_dp
     133          564 :       ALLOCATE (row_pos(nblkrows_total))
     134         1256 :       IF (nblkrows_total > 0) row_pos(:) = row_p(1:nblkrows_total)
     135              : 
     136          282 :       cur_off = 1
     137              : !$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, row_pos, col_unsorted, raw_off, raw_data, cur_off) &
     138          282 : !$OMP PRIVATE(iter, brow, bcol, blk_ptr, rsize, csize, tr, nze, slot, my_off)
     139              :       NULLIFY (blk_ptr)
     140              :       CALL dbcsr_iterator_readonly_start(iter, matrix)
     141              :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     142              :          CALL dbcsr_iterator_next_block(iter, brow, bcol, blk_ptr, &
     143              :                                         row_size=rsize, col_size=csize, transposed=tr)
     144              :          nze = rsize*csize
     145              : !$OMP ATOMIC CAPTURE
     146              :          row_pos(brow) = row_pos(brow) + 1
     147              :          slot = row_pos(brow)
     148              : !$OMP END ATOMIC
     149              : !$OMP ATOMIC CAPTURE
     150              :          my_off = cur_off
     151              :          cur_off = cur_off + nze
     152              : !$OMP END ATOMIC
     153              :          col_unsorted(slot) = bcol
     154              :          raw_off(slot) = my_off
     155              :          IF (nze > 0) THEN
     156              :             IF (tr) THEN
     157              :                raw_data(my_off:my_off + nze - 1) = RESHAPE(TRANSPOSE(blk_ptr), [nze])
     158              :             ELSE
     159              :                raw_data(my_off:my_off + nze - 1) = RESHAPE(blk_ptr, [nze])
     160              :             END IF
     161              :          END IF
     162              :       END DO
     163              :       CALL dbcsr_iterator_stop(iter)
     164              : !$OMP END PARALLEL
     165              : 
     166          282 :       IF (nblkrows_total > 0) THEN
     167         1820 :          ALLOCATE (perm(MAX(1, MAXVAL(row_cnt))))
     168          282 :          cur_off = 1
     169         1256 :          DO r = 1, nblkrows_total
     170          974 :             cnt = row_cnt(r)
     171         1256 :             IF (cnt > 0) THEN
     172          467 :                s_start = row_p(r) + 1
     173          467 :                s_end = row_p(r + 1)
     174         2400 :                col_i(s_start:s_end) = col_unsorted(s_start:s_end)
     175          467 :                IF (cnt > 1) THEN
     176          457 :                   CALL sort(col_i(s_start:s_end), cnt, perm(1:cnt))
     177              :                ELSE
     178           10 :                   perm(1) = 1
     179              :                END IF
     180          467 :                rsize = row_blk_size(r)
     181         2400 :                DO k = 1, cnt
     182         1933 :                   orig_slot = s_start + perm(k) - 1
     183         1933 :                   bcol = col_i(s_start + k - 1)
     184         1933 :                   nze = rsize*col_blk_size(bcol)
     185         1933 :                   blk_p(s_start + k - 1) = cur_off
     186         1933 :                   IF (nze > 0) THEN
     187       189284 :                      r_dp(cur_off:cur_off + nze - 1) = raw_data(raw_off(orig_slot):raw_off(orig_slot) + nze - 1)
     188              :                   END IF
     189         2400 :                   cur_off = cur_off + nze
     190              :                END DO
     191              :             END IF
     192              :          END DO
     193          282 :          DEALLOCATE (perm)
     194              :       END IF
     195          282 :       DEALLOCATE (row_cnt, row_pos, col_unsorted, raw_off, raw_data)
     196              : 
     197          282 :       linfo_size = 1 + nblkrows_total + 2*nblks
     198          846 :       ALLOCATE (linfo_vec(linfo_size))
     199         1538 :       linfo_vec(1:nblkrows_total + 1) = row_p(1:nblkrows_total + 1)
     200          282 :       IF (nblks > 0) THEN
     201         2193 :          linfo_vec(nblkrows_total + 2:nblkrows_total + 1 + nblks) = col_i(1:nblks)
     202         2193 :          linfo_vec(nblkrows_total + 2 + nblks:linfo_size) = blk_p(1:nblks)
     203              :       END IF
     204          282 :       DEALLOCATE (row_p, col_i, blk_p)
     205              : 
     206         1128 :       ALLOCATE (linfo_sizes(numnodes), da_sizes(numnodes))
     207          282 :       CALL mp_group%allgather(linfo_size, linfo_sizes)
     208          282 :       CALL mp_group%allgather(data_area_size, da_sizes)
     209              : 
     210          282 :       subh1_chars_bytes = INT(char_count, KIND=file_offset)*INT(mpi_character_size, KIND=file_offset)
     211              :       subh2_start = subh1_chars_bytes + &
     212              :                     INT(4 + nblkrows_total + nblkcols_total, KIND=file_offset)* &
     213          282 :                     INT(mpi_integer_size, KIND=file_offset)
     214          282 :       subh2_my_offset = subh2_start + INT(2*mynode, KIND=file_offset)*INT(mpi_integer_size, KIND=file_offset)
     215              : 
     216          282 :       subh3_start = subh2_start + INT(2*numnodes, KIND=file_offset)*INT(mpi_integer_size, KIND=file_offset)
     217          423 :       prev_linfo = SUM(INT(linfo_sizes(1:mynode), KIND=file_offset))
     218          846 :       tot_linfo = SUM(INT(linfo_sizes(1:numnodes), KIND=file_offset))
     219          282 :       subh3_my_offset = subh3_start + prev_linfo*INT(mpi_integer_size, KIND=file_offset)
     220              : 
     221          282 :       bdata_start = subh3_start + tot_linfo*INT(mpi_integer_size, KIND=file_offset)
     222          423 :       prev_da = SUM(INT(da_sizes(1:mynode), KIND=file_offset))
     223          282 :       bdata_my_offset = bdata_start + prev_da*INT(real_8_size, KIND=file_offset)
     224              : 
     225          282 :       DEALLOCATE (linfo_sizes, da_sizes)
     226              : 
     227          282 :       IF (mynode == 0) CALL mp_file_delete(filepath)
     228          282 :       CALL mp_group%sync()
     229              : 
     230          282 :       CALL thefile%open(mp_group, filepath, file_amode_create + file_amode_wronly)
     231              : 
     232          282 :       IF (mynode == 0) THEN
     233          141 :          matrix_name_v_1_0 = matrix_name
     234          141 :          CALL thefile%write_at(bof, version_v_1_0)
     235          141 :          CALL thefile%write_at(bof + INT(version_len*mpi_character_size, KIND=file_offset), matrix_name_v_1_0)
     236              :          CALL thefile%write_at(bof + INT((version_len + name_v_1_0_len)*mpi_character_size, KIND=file_offset), &
     237          141 :                                matrix_type)
     238              : 
     239          423 :          ALLOCATE (hdr1_ints(4 + nblkrows_total + nblkcols_total))
     240          705 :          hdr1_ints(1:4) = [numnodes, dbcsr_type_real_8_id, nblkrows_total, nblkcols_total]
     241          141 :          IF (nblkrows_total > 0) THEN
     242          628 :             hdr1_ints(5:4 + nblkrows_total) = row_blk_size(1:nblkrows_total)
     243              :          END IF
     244          141 :          IF (nblkcols_total > 0) THEN
     245          628 :             hdr1_ints(5 + nblkrows_total:4 + nblkrows_total + nblkcols_total) = col_blk_size(1:nblkcols_total)
     246              :          END IF
     247          141 :          CALL thefile%write_at(bof + subh1_chars_bytes, hdr1_ints)
     248          141 :          DEALLOCATE (hdr1_ints)
     249              :       END IF
     250              : 
     251          846 :       subh2_pair = [nblks, data_area_size]
     252          282 :       CALL thefile%write_at_all(subh2_my_offset, subh2_pair)
     253          282 :       CALL thefile%write_at_all(subh3_my_offset, linfo_vec)
     254          282 :       CALL thefile%write_at_all(bdata_my_offset, r_dp, msglen=data_area_size)
     255              : 
     256          282 :       CALL thefile%close()
     257              : 
     258          282 :       DEALLOCATE (linfo_vec, r_dp)
     259              : 
     260          282 :       CALL timestop(handle)
     261          846 :    END SUBROUTINE dbcsr_binary_write
     262              : 
     263              : ! **************************************************************************************************
     264              : !> \brief Read a DBCSR matrix from a binary DBCSRv_1.0 file into the given distribution
     265              : !> \param filepath ...
     266              : !> \param distribution ...
     267              : !> \param matrix_new ...
     268              : ! **************************************************************************************************
     269           10 :    SUBROUTINE dbcsr_binary_read(filepath, distribution, matrix_new)
     270              :       CHARACTER(len=*), INTENT(IN)                       :: filepath
     271              :       TYPE(dbcsr_distribution_type), INTENT(IN)          :: distribution
     272              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix_new
     273              : 
     274              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'dbcsr_binary_read'
     275              :       INTEGER(KIND=file_offset), PARAMETER               :: bof = 0_file_offset
     276              :       INTEGER, PARAMETER                                 :: metalen = 2
     277              : 
     278              :       CHARACTER                                          :: matrix_type
     279              :       CHARACTER(LEN=default_string_length)               :: matrix_name
     280              :       CHARACTER(LEN=name_v_1_0_len)                      :: matrix_name_v_1_0
     281              :       CHARACTER(LEN=version_len)                         :: version
     282              :       INTEGER :: bcol, blk, blk_size, blockdata_length, brow, csize, cur_blks, darea_size, &
     283              :          data_type, dst_p, end_index, globalinfo_size, group_list_size, handle, i, job_count, k, &
     284              :          linfo_length, localinfo_length, m_pos, mp_handle, nblkcols_total, nblkrows_total, nblks, &
     285              :          order, proc_idx, rsize, share_size, size_of_pgrid, start_index, total_recv_blks, &
     286              :          total_recv_vals, total_send_blks, total_send_vals, type_size, v_pos, val_pos, worker_id
     287              :       INTEGER(KIND=file_offset) :: bdata_start, blockdata_offset, localinfo_offset, offset, &
     288              :          subh1_chars_bytes, subh2_start, subh3_length, subh3_start, sum_nblks
     289              :       INTEGER(KIND=file_offset), ALLOCATABLE, &
     290           10 :          DIMENSION(:)                                    :: bdata_offsets, subh3_offsets
     291              :       INTEGER(KIND=int_8)                                :: actual_blk, blkp_abs, end_ind, start_ind
     292           10 :       INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:)     :: extra_darea_size, extra_nblks
     293           10 :       INTEGER, ALLOCATABLE, DIMENSION(:) :: bdata_lens, blkp, coli, ginfo_vec, linfo_lens, &
     294           10 :          linfo_vec, recv_meta, rmeta_cnt, rmeta_displ, rowp, rval_cnt, rval_displ, send_meta, &
     295           10 :          smeta_cnt, smeta_displ, smeta_pos, sval_cnt, sval_displ, sval_pos
     296           10 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: recv_counts, send_counts, val_data
     297           10 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: col_blk_size, row_blk_size
     298              :       INTEGER, DIMENSION(2)                              :: subh2_pair
     299              :       INTEGER, DIMENSION(4)                              :: hdr1_vals
     300           10 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: rdp, recv_data, send_data
     301           10 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: blk2d
     302           10 :       REAL(KIND=real_4), ALLOCATABLE, DIMENSION(:)       :: rsp
     303              :       TYPE(dbcsr_distribution_type)                      :: dist_local
     304              :       TYPE(mp_comm_type)                                 :: mp_group
     305              :       TYPE(mp_file_type)                                 :: thefile
     306              : 
     307           10 :       CALL timeset(routineN, handle)
     308              : 
     309              :       CALL dbcsr_distribution_get(distribution, group=mp_handle, &
     310           10 :                                   mynode=worker_id, numnodes=group_list_size)
     311           10 :       CALL mp_group%set_handle(mp_handle)
     312              : 
     313           10 :       CALL thefile%open(mp_group, filepath, file_amode_rdonly)
     314              : 
     315           10 :       CALL thefile%read_at_all(bof, version)
     316           10 :       IF (version /= version_v_1_0) THEN
     317            0 :          CPABORT("Unsupported DBCSR binary file version in "//TRIM(filepath))
     318              :       END IF
     319              : 
     320              :       CALL thefile%read_at_all(bof + INT(version_len*mpi_character_size, KIND=file_offset), &
     321           10 :                                matrix_name_v_1_0)
     322           10 :       matrix_name = matrix_name_v_1_0
     323              : 
     324              :       CALL thefile%read_at_all(bof + INT((version_len + name_v_1_0_len)*mpi_character_size, KIND=file_offset), &
     325           10 :                                matrix_type)
     326              : 
     327           10 :       subh1_chars_bytes = INT(char_count, KIND=file_offset)*INT(mpi_character_size, KIND=file_offset)
     328           10 :       CALL thefile%read_at_all(bof + subh1_chars_bytes, hdr1_vals)
     329           10 :       size_of_pgrid = hdr1_vals(1)
     330           10 :       data_type = hdr1_vals(2)
     331           10 :       nblkrows_total = hdr1_vals(3)
     332           10 :       nblkcols_total = hdr1_vals(4)
     333              : 
     334           10 :       IF (data_type /= dbcsr_type_real_8_id .AND. data_type /= dbcsr_type_real_4_id) THEN
     335            0 :          CPABORT("Unsupported data_type in DBCSR binary file: "//TRIM(filepath))
     336              :       END IF
     337              : 
     338           10 :       globalinfo_size = nblkrows_total + nblkcols_total
     339           30 :       ALLOCATE (ginfo_vec(MAX(1, globalinfo_size)))
     340           10 :       IF (globalinfo_size > 0) THEN
     341              :          CALL thefile%read_at_all(bof + subh1_chars_bytes + INT(4*mpi_integer_size, KIND=file_offset), &
     342           10 :                                   ginfo_vec(1:globalinfo_size), msglen=globalinfo_size)
     343              :       END IF
     344              : 
     345           50 :       ALLOCATE (row_blk_size(nblkrows_total), col_blk_size(nblkcols_total))
     346          100 :       IF (nblkrows_total > 0) row_blk_size(:) = ginfo_vec(1:nblkrows_total)
     347           64 :       IF (nblkcols_total > 0) col_blk_size(:) = ginfo_vec(nblkrows_total + 1:globalinfo_size)
     348           10 :       DEALLOCATE (ginfo_vec)
     349              : 
     350           10 :       subh2_start = subh1_chars_bytes + INT(4 + globalinfo_size, KIND=file_offset)*INT(mpi_integer_size, KIND=file_offset)
     351           10 :       subh3_start = subh2_start + INT(2*size_of_pgrid, KIND=file_offset)*INT(mpi_integer_size, KIND=file_offset)
     352              : 
     353           10 :       share_size = CEILING(REAL(size_of_pgrid, KIND=dp)/REAL(group_list_size, KIND=dp))
     354           30 :       ALLOCATE (val_data(3, share_size))
     355           10 :       val_data(:, :) = 0
     356              : 
     357           20 :       DO i = 1, share_size
     358           10 :          proc_idx = worker_id + (i - 1)*group_list_size
     359           20 :          IF (proc_idx < size_of_pgrid) THEN
     360           10 :             offset = subh2_start + INT(2*proc_idx, KIND=file_offset)*INT(mpi_integer_size, KIND=file_offset)
     361           10 :             CALL thefile%read_at_all(offset, subh2_pair, msglen=2)
     362           10 :             val_data(1, i) = subh2_pair(1)
     363           10 :             val_data(2, i) = subh2_pair(2)
     364           10 :             val_data(3, i) = proc_idx + 1
     365              :          ELSE
     366            0 :             CALL thefile%read_at_all(bof, subh2_pair, msglen=0)
     367              :          END IF
     368              :       END DO
     369              : 
     370           20 :       nblks = SUM(val_data(1, :))
     371           20 :       darea_size = SUM(val_data(2, :))
     372           20 :       job_count = COUNT(val_data(3, :) /= 0)
     373              : 
     374           50 :       ALLOCATE (linfo_lens(size_of_pgrid), subh3_offsets(size_of_pgrid))
     375           10 :       linfo_lens(:) = 0
     376           20 :       DO i = 1, share_size
     377           10 :          order = val_data(3, i)
     378           20 :          IF (order > 0) THEN
     379           10 :             linfo_lens(order) = 1 + nblkrows_total + 2*val_data(1, i)
     380              :          END IF
     381              :       END DO
     382           10 :       CALL mp_group%sum(linfo_lens)
     383              : 
     384           10 :       offset = subh3_start
     385           30 :       DO i = 1, size_of_pgrid
     386           20 :          subh3_offsets(i) = offset
     387           30 :          offset = offset + INT(linfo_lens(i), KIND=file_offset)*INT(mpi_integer_size, KIND=file_offset)
     388              :       END DO
     389              : 
     390           10 :       sum_nblks = INT(nblks, KIND=file_offset)
     391           10 :       CALL mp_group%sum(sum_nblks)
     392           10 :       subh3_length = INT(size_of_pgrid, KIND=file_offset)*INT(1 + nblkrows_total, KIND=file_offset) + 2_file_offset*sum_nblks
     393              : 
     394           20 :       linfo_length = nblkrows_total + 1 + 2*MAX(0, MAXVAL(val_data(1, :)))
     395           30 :       ALLOCATE (linfo_vec(MAX(1, linfo_length)))
     396           30 :       ALLOCATE (rowp((nblkrows_total + 1)*job_count))
     397           40 :       ALLOCATE (coli(nblks), blkp(nblks))
     398              : 
     399           20 :       DO i = 1, share_size
     400           10 :          order = val_data(3, i)
     401           10 :          cur_blks = val_data(1, i)
     402           20 :          IF (order == 0) THEN
     403            0 :             CALL thefile%read_at_all(bof, linfo_vec, msglen=0)
     404              :          ELSE
     405           10 :             localinfo_offset = subh3_offsets(order)
     406           10 :             localinfo_length = linfo_lens(order)
     407           10 :             CALL thefile%read_at_all(localinfo_offset, linfo_vec(1:localinfo_length), msglen=localinfo_length)
     408          110 :             rowp((i - 1)*(nblkrows_total + 1) + 1:i*(nblkrows_total + 1)) = linfo_vec(1:nblkrows_total + 1)
     409           10 :             IF (cur_blks > 0) THEN
     410           10 :                start_index = SUM(val_data(1, 1:i - 1)) + 1
     411           10 :                end_index = start_index + cur_blks - 1
     412          253 :                coli(start_index:end_index) = linfo_vec(nblkrows_total + 2:nblkrows_total + 1 + cur_blks)
     413          253 :                blkp(start_index:end_index) = linfo_vec(nblkrows_total + 2 + cur_blks:nblkrows_total + 1 + 2*cur_blks)
     414              :             END IF
     415              :          END IF
     416              :       END DO
     417           10 :       DEALLOCATE (linfo_lens, subh3_offsets, linfo_vec)
     418              : 
     419           30 :       ALLOCATE (bdata_lens(size_of_pgrid), bdata_offsets(size_of_pgrid))
     420           10 :       bdata_lens(:) = 0
     421           20 :       DO i = 1, share_size
     422           10 :          order = val_data(3, i)
     423           20 :          IF (order > 0) bdata_lens(order) = val_data(2, i)
     424              :       END DO
     425           10 :       CALL mp_group%sum(bdata_lens)
     426              : 
     427           10 :       IF (data_type == dbcsr_type_real_8_id) THEN
     428              :          type_size = real_8_size
     429              :       ELSE
     430            0 :          type_size = real_4_size
     431              :       END IF
     432              : 
     433           10 :       bdata_start = subh3_start + subh3_length*INT(mpi_integer_size, KIND=file_offset)
     434           10 :       offset = bdata_start
     435           30 :       DO i = 1, size_of_pgrid
     436           20 :          bdata_offsets(i) = offset
     437           30 :          offset = offset + INT(bdata_lens(i), KIND=file_offset)*INT(type_size, KIND=file_offset)
     438              :       END DO
     439              : 
     440           30 :       ALLOCATE (rdp(MAX(1, darea_size)))
     441           10 :       rdp(:) = 0.0_dp
     442           10 :       IF (data_type == dbcsr_type_real_8_id) THEN
     443           20 :          DO i = 1, share_size
     444           10 :             order = val_data(3, i)
     445           20 :             IF (order == 0) THEN
     446            0 :                CALL thefile%read_at_all(bof, rdp, msglen=0)
     447              :             ELSE
     448           10 :                blockdata_length = bdata_lens(order)
     449           10 :                IF (blockdata_length > 0) THEN
     450           10 :                   start_index = SUM(val_data(2, 1:i - 1)) + 1
     451           10 :                   end_index = start_index + blockdata_length - 1
     452           10 :                   blockdata_offset = bdata_offsets(order)
     453           10 :                   CALL thefile%read_at_all(blockdata_offset, rdp(start_index:end_index), msglen=blockdata_length)
     454              :                ELSE
     455            0 :                   CALL thefile%read_at_all(bof, rdp, msglen=0)
     456              :                END IF
     457              :             END IF
     458              :          END DO
     459              :       ELSE
     460            0 :          ALLOCATE (rsp(MAX(1, darea_size)))
     461            0 :          rsp(:) = 0.0_real_4
     462            0 :          DO i = 1, share_size
     463            0 :             order = val_data(3, i)
     464            0 :             IF (order == 0) THEN
     465            0 :                CALL thefile%read_at_all(bof, rsp, msglen=0)
     466              :             ELSE
     467            0 :                blockdata_length = bdata_lens(order)
     468            0 :                IF (blockdata_length > 0) THEN
     469            0 :                   start_index = SUM(val_data(2, 1:i - 1)) + 1
     470            0 :                   end_index = start_index + blockdata_length - 1
     471            0 :                   blockdata_offset = bdata_offsets(order)
     472            0 :                   CALL thefile%read_at_all(blockdata_offset, rsp(start_index:end_index), msglen=blockdata_length)
     473              :                ELSE
     474            0 :                   CALL thefile%read_at_all(bof, rsp, msglen=0)
     475              :                END IF
     476              :             END IF
     477              :          END DO
     478            0 :          IF (darea_size > 0) rdp(1:darea_size) = REAL(rsp(1:darea_size), KIND=dp)
     479            0 :          DEALLOCATE (rsp)
     480              :       END IF
     481              : 
     482           10 :       CALL thefile%close()
     483           10 :       DEALLOCATE (bdata_lens, bdata_offsets)
     484              : 
     485              :       ! Hold distribution before releasing any pre-existing matrix_new that might own it
     486           10 :       dist_local = distribution
     487           10 :       CALL dbcsr_distribution_hold(dist_local)
     488           10 :       IF (dbcsr_valid_index(matrix_new)) THEN
     489            8 :          CALL dbcsr_release(matrix_new)
     490              :       END IF
     491              :       CALL dbcsr_create(matrix_new, name=matrix_name, dist=dist_local, matrix_type=matrix_type, &
     492           10 :                         row_blk_size=row_blk_size, col_blk_size=col_blk_size)
     493           10 :       CALL dbcsr_distribution_release(dist_local)
     494              : 
     495              :       ! Redistribute blocks read by this rank to their target owners in matrix_new
     496           40 :       ALLOCATE (extra_nblks(job_count), extra_darea_size(job_count))
     497           20 :       DO i = 1, job_count
     498           10 :          extra_nblks(i) = SUM(INT(val_data(1, 1:i - 1), KIND=int_8))
     499           20 :          extra_darea_size(i) = SUM(INT(val_data(2, 1:i - 1), KIND=int_8))
     500              :       END DO
     501              : 
     502           40 :       ALLOCATE (send_counts(2, group_list_size), recv_counts(2, group_list_size))
     503           10 :       send_counts(:, :) = 0
     504           10 :       recv_counts(:, :) = 0
     505              : 
     506           20 :       DO i = 1, job_count
     507          110 :          DO brow = 1, nblkrows_total
     508           90 :             rsize = row_blk_size(brow)
     509          333 :             DO blk = rowp((i - 1)*(nblkrows_total + 1) + brow) + 1, &
     510          100 :                rowp((i - 1)*(nblkrows_total + 1) + brow + 1)
     511          243 :                actual_blk = INT(blk, KIND=int_8) + extra_nblks(i)
     512          243 :                IF (blkp(actual_blk) == 0) CYCLE
     513          243 :                bcol = coli(actual_blk)
     514          243 :                csize = col_blk_size(bcol)
     515          243 :                CALL dbcsr_get_stored_coordinates(matrix_new, brow, bcol, dst_p)
     516          243 :                send_counts(1, dst_p + 1) = send_counts(1, dst_p + 1) + 1
     517          333 :                send_counts(2, dst_p + 1) = send_counts(2, dst_p + 1) + rsize*csize
     518              :             END DO
     519              :          END DO
     520              :       END DO
     521              : 
     522           10 :       CALL mp_group%alltoall(send_counts, recv_counts, 2)
     523              : 
     524           50 :       ALLOCATE (smeta_cnt(group_list_size), smeta_displ(group_list_size), smeta_pos(group_list_size))
     525           30 :       ALLOCATE (rmeta_cnt(group_list_size), rmeta_displ(group_list_size))
     526           40 :       ALLOCATE (sval_cnt(group_list_size), sval_displ(group_list_size), sval_pos(group_list_size))
     527           30 :       ALLOCATE (rval_cnt(group_list_size), rval_displ(group_list_size))
     528              : 
     529           30 :       DO i = 1, group_list_size
     530           20 :          smeta_cnt(i) = metalen*send_counts(1, i)
     531           20 :          rmeta_cnt(i) = metalen*recv_counts(1, i)
     532           20 :          sval_cnt(i) = send_counts(2, i)
     533           30 :          rval_cnt(i) = recv_counts(2, i)
     534              :       END DO
     535              : 
     536           10 :       smeta_displ(1) = 0
     537           10 :       rmeta_displ(1) = 0
     538           10 :       sval_displ(1) = 0
     539           10 :       rval_displ(1) = 0
     540           20 :       DO i = 2, group_list_size
     541           10 :          smeta_displ(i) = smeta_displ(i - 1) + smeta_cnt(i - 1)
     542           10 :          rmeta_displ(i) = rmeta_displ(i - 1) + rmeta_cnt(i - 1)
     543           10 :          sval_displ(i) = sval_displ(i - 1) + sval_cnt(i - 1)
     544           20 :          rval_displ(i) = rval_displ(i - 1) + rval_cnt(i - 1)
     545              :       END DO
     546           30 :       smeta_pos(:) = smeta_displ(:)
     547           30 :       sval_pos(:) = sval_displ(:)
     548              : 
     549           30 :       total_send_blks = SUM(send_counts(1, :))
     550           30 :       total_send_vals = SUM(send_counts(2, :))
     551           30 :       total_recv_blks = SUM(recv_counts(1, :))
     552           30 :       total_recv_vals = SUM(recv_counts(2, :))
     553              : 
     554           50 :       ALLOCATE (send_meta(MAX(1, metalen*total_send_blks)), recv_meta(MAX(1, metalen*total_recv_blks)))
     555           50 :       ALLOCATE (send_data(MAX(1, total_send_vals)), recv_data(MAX(1, total_recv_vals)))
     556              : 
     557           20 :       DO i = 1, job_count
     558          110 :          DO brow = 1, nblkrows_total
     559           90 :             rsize = row_blk_size(brow)
     560          333 :             DO blk = rowp((i - 1)*(nblkrows_total + 1) + brow) + 1, &
     561          100 :                rowp((i - 1)*(nblkrows_total + 1) + brow + 1)
     562          243 :                actual_blk = INT(blk, KIND=int_8) + extra_nblks(i)
     563          243 :                IF (blkp(actual_blk) == 0) CYCLE
     564          243 :                bcol = coli(actual_blk)
     565          243 :                csize = col_blk_size(bcol)
     566          243 :                blk_size = rsize*csize
     567          243 :                blkp_abs = INT(ABS(blkp(actual_blk)), KIND=int_8)
     568          243 :                start_ind = blkp_abs + extra_darea_size(i)
     569          243 :                end_ind = start_ind + INT(blk_size, KIND=int_8) - 1_int_8
     570              : 
     571          243 :                CALL dbcsr_get_stored_coordinates(matrix_new, brow, bcol, dst_p)
     572          243 :                m_pos = smeta_pos(dst_p + 1)
     573          243 :                send_meta(m_pos + 1) = brow
     574          243 :                send_meta(m_pos + 2) = bcol
     575          243 :                smeta_pos(dst_p + 1) = m_pos + metalen
     576              : 
     577          333 :                IF (blk_size > 0) THEN
     578          243 :                   v_pos = sval_pos(dst_p + 1)
     579          243 :                   IF (blkp(actual_blk) < 0) THEN
     580              :                      send_data(v_pos + 1:v_pos + blk_size) = &
     581            0 :                         RESHAPE(TRANSPOSE(RESHAPE(rdp(start_ind:end_ind), [csize, rsize])), [blk_size])
     582              :                   ELSE
     583        23111 :                      send_data(v_pos + 1:v_pos + blk_size) = rdp(start_ind:end_ind)
     584              :                   END IF
     585          243 :                   sval_pos(dst_p + 1) = v_pos + blk_size
     586              :                END IF
     587              :             END DO
     588              :          END DO
     589              :       END DO
     590              : 
     591           10 :       DEALLOCATE (val_data, extra_nblks, extra_darea_size, rowp, coli, blkp, rdp)
     592              : 
     593           10 :       CALL mp_group%alltoall(send_meta, smeta_cnt, smeta_displ, recv_meta, rmeta_cnt, rmeta_displ)
     594           10 :       CALL mp_group%alltoall(send_data, sval_cnt, sval_displ, recv_data, rval_cnt, rval_displ)
     595              : 
     596           10 :       DEALLOCATE (send_counts, recv_counts, send_meta, send_data)
     597           10 :       DEALLOCATE (smeta_cnt, smeta_displ, smeta_pos, rmeta_cnt, rmeta_displ)
     598           10 :       DEALLOCATE (sval_cnt, sval_displ, sval_pos, rval_cnt, rval_displ)
     599              : 
     600           10 :       CALL dbcsr_work_create(matrix_new, nblks_guess=total_recv_blks, sizedata_guess=total_recv_vals)
     601           10 :       val_pos = 1
     602          253 :       DO k = 1, total_recv_blks
     603          243 :          brow = recv_meta(metalen*(k - 1) + 1)
     604          243 :          bcol = recv_meta(metalen*(k - 1) + 2)
     605          243 :          rsize = row_blk_size(brow)
     606          243 :          csize = col_blk_size(bcol)
     607          243 :          blk_size = rsize*csize
     608          972 :          ALLOCATE (blk2d(rsize, csize))
     609          243 :          IF (blk_size > 0) THEN
     610          729 :             blk2d(:, :) = RESHAPE(recv_data(val_pos:val_pos + blk_size - 1), [rsize, csize])
     611              :          ELSE
     612            0 :             blk2d(:, :) = 0.0_dp
     613              :          END IF
     614          243 :          CALL dbcsr_put_block(matrix_new, brow, bcol, blk2d)
     615          243 :          DEALLOCATE (blk2d)
     616          253 :          val_pos = val_pos + blk_size
     617              :       END DO
     618           10 :       CALL dbcsr_finalize(matrix_new)
     619              : 
     620           10 :       DEALLOCATE (recv_meta, recv_data, row_blk_size, col_blk_size)
     621              : 
     622           10 :       CALL timestop(handle)
     623           20 :    END SUBROUTINE dbcsr_binary_read
     624              : 
     625              : END MODULE cp_dbcsr_io
        

Generated by: LCOV version 2.0-1