Skip to content

Commit

Permalink
HFX: Do not rely on block order of tensor iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Jan 1, 2022
1 parent 37ab475 commit 26f8803
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 53 deletions.
25 changes: 8 additions & 17 deletions src/hfx_ri.F
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ MODULE hfx_ri
dbcsr_t_batched_contract_finalize, dbcsr_t_batched_contract_init, dbcsr_t_clear, &
dbcsr_t_contract, dbcsr_t_copy, dbcsr_t_copy_matrix_to_tensor, &
dbcsr_t_copy_tensor_to_matrix, dbcsr_t_create, dbcsr_t_destroy, dbcsr_t_filter, &
dbcsr_t_get_block, dbcsr_t_get_info, dbcsr_t_get_num_blocks, dbcsr_t_get_num_blocks_total, &
dbcsr_t_get_block, dbcsr_t_get_info, dbcsr_t_get_num_blocks_total, &
dbcsr_t_iterator_blocks_left, dbcsr_t_iterator_next_block, dbcsr_t_iterator_start, &
dbcsr_t_iterator_stop, dbcsr_t_iterator_type, dbcsr_t_mp_environ_pgrid, &
dbcsr_t_nd_mp_comm, dbcsr_t_pgrid_create, dbcsr_t_pgrid_destroy, dbcsr_t_pgrid_type, &
dbcsr_t_reserved_block_indices, dbcsr_t_type
dbcsr_t_type
USE distribution_2d_types, ONLY: distribution_2d_type
USE hfx_types, ONLY: alloc_containers,&
block_ind_type,&
Expand Down Expand Up @@ -774,11 +774,8 @@ SUBROUTINE hfx_ri_pre_scf_Pmat(qs_env, ri_data)
CALL get_tensor_occupancy(ri_data%t_3c_int_ctr_1(1, 1), nze, occ)
nze_O = nze_O + nze

IF (ALLOCATED(ri_data%blk_indices(j_mem, i_mem)%ind)) DEALLOCATE (ri_data%blk_indices(j_mem, i_mem)%ind)
ALLOCATE (ri_data%blk_indices(j_mem, i_mem)%ind(dbcsr_t_get_num_blocks(ri_data%t_3c_int_ctr_1(1, 1)), 3))
CALL dbcsr_t_reserved_block_indices(ri_data%t_3c_int_ctr_1(1, 1), ri_data%blk_indices(j_mem, i_mem)%ind)
CALL compress_tensor(ri_data%t_3c_int_ctr_1(1, 1), ri_data%store_3c(j_mem, i_mem), ri_data%filter_eps_storage, &
memory_3c)
CALL compress_tensor(ri_data%t_3c_int_ctr_1(1, 1), ri_data%blk_indices(j_mem, i_mem)%ind, &
ri_data%store_3c(j_mem, i_mem), ri_data%filter_eps_storage, memory_3c)

CALL timestop(handle2)
END DO
Expand Down Expand Up @@ -1576,14 +1573,9 @@ SUBROUTINE hfx_ri_update_ks_Pmat(qs_env, ri_data, ks_matrix, rho_ao, &
ri_data%store_3c(i_mem, j_mem), ri_data%filter_eps_storage)
CALL dbcsr_t_copy(tensor_old, t_3c, move_data=.TRUE.)

DEALLOCATE (blk_indices%ind)
ALLOCATE (blk_indices%ind(dbcsr_t_get_num_blocks(t_3c), 3))

CALL dbcsr_t_reserved_block_indices(t_3c, blk_indices%ind)

unused = 0
CALL compress_tensor(t_3c, ri_data%store_3c(i_mem, j_mem), ri_data%filter_eps_storage, &
unused)
CALL compress_tensor(t_3c, blk_indices%ind, ri_data%store_3c(i_mem, j_mem), &
ri_data%filter_eps_storage, unused)
END ASSOCIATE
END DO
END DO
Expand Down Expand Up @@ -2597,9 +2589,8 @@ SUBROUTINE hfx_ri_forces_Pmat(qs_env, ri_data, nspins, hf_fraction, rho_ao, rho_
CALL dbcsr_t_copy(t_3c_3, t_3c_int_1, order=[2, 1, 3], summation=.TRUE., move_data=.TRUE.)
END DO

ALLOCATE (blk_indices(j_mem, i_mem)%ind(dbcsr_t_get_num_blocks(t_3c_int_1), 3))
CALL dbcsr_t_reserved_block_indices(t_3c_int_1, blk_indices(j_mem, i_mem)%ind)
CALL compress_tensor(t_3c_int_1, store_3c(j_mem, i_mem), ri_data%filter_eps_storage, memory)
CALL compress_tensor(t_3c_int_1, blk_indices(j_mem, i_mem)%ind, &
store_3c(j_mem, i_mem), ri_data%filter_eps_storage, memory)

END DO
END DO
Expand Down
12 changes: 5 additions & 7 deletions src/mp2_integrals.F
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ MODULE mp2_integrals
USE dbcsr_tensor_api, ONLY: &
dbcsr_t_clear, dbcsr_t_contract, dbcsr_t_copy, dbcsr_t_create, dbcsr_t_destroy, &
dbcsr_t_distribution_destroy, dbcsr_t_distribution_new, dbcsr_t_distribution_type, &
dbcsr_t_filter, dbcsr_t_get_block, dbcsr_t_get_info, dbcsr_t_get_num_blocks, &
dbcsr_t_get_stored_coordinates, dbcsr_t_mp_environ_pgrid, dbcsr_t_pgrid_create, &
dbcsr_t_pgrid_destroy, dbcsr_t_pgrid_type, dbcsr_t_put_block, dbcsr_t_reserve_blocks, &
dbcsr_t_reserved_block_indices, dbcsr_t_split_blocks, dbcsr_t_type
dbcsr_t_filter, dbcsr_t_get_block, dbcsr_t_get_info, dbcsr_t_get_stored_coordinates, &
dbcsr_t_mp_environ_pgrid, dbcsr_t_pgrid_create, dbcsr_t_pgrid_destroy, dbcsr_t_pgrid_type, &
dbcsr_t_put_block, dbcsr_t_reserve_blocks, dbcsr_t_split_blocks, dbcsr_t_type
USE group_dist_types, ONLY: create_group_dist,&
get_group_dist,&
group_dist_d1_type
Expand Down Expand Up @@ -857,10 +856,9 @@ SUBROUTINE mp2_ri_gpw_compute_in(BIb_C, BIb_C_gw, BIb_C_bse_ij, BIb_C_bse_ab, gd
bounds(:, 2) = [starts_array_mc(i_mem), ends_array_mc(i_mem)]
CALL dbcsr_t_copy(t_3c_O(i, j), t_3c_tmp, bounds=bounds)

ALLOCATE (t_3c_O_ind(i, j, i_mem)%ind(dbcsr_t_get_num_blocks(t_3c_tmp), 3))
CALL dbcsr_t_reserved_block_indices(t_3c_tmp, t_3c_O_ind(i, j, i_mem)%ind)
CALL alloc_containers(t_3c_O_compressed(i, j, i_mem), 1)
CALL compress_tensor(t_3c_tmp, t_3c_O_compressed(i, j, i_mem), &
CALL compress_tensor(t_3c_tmp, t_3c_O_ind(i, j, i_mem)%ind, &
t_3c_O_compressed(i, j, i_mem), &
qs_env%mp2_env%ri_rpa_im_time%eps_compress, memory_3c)
END DO
CALL dbcsr_t_clear(t_3c_O(i, j))
Expand Down
45 changes: 23 additions & 22 deletions src/qs_tensors.F
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ MODULE qs_tensors
dbcsr_type_no_symmetry
USE dbcsr_tensor_api, ONLY: &
dbcsr_t_blk_sizes, dbcsr_t_clear, dbcsr_t_copy, dbcsr_t_create, dbcsr_t_destroy, &
dbcsr_t_filter, dbcsr_t_get_block, dbcsr_t_get_info, dbcsr_t_get_nze_total, &
dbcsr_t_get_stored_coordinates, dbcsr_t_iterator_blocks_left, dbcsr_t_iterator_next_block, &
dbcsr_t_iterator_start, dbcsr_t_iterator_stop, dbcsr_t_iterator_type, dbcsr_t_ndims, &
dbcsr_t_put_block, dbcsr_t_reserve_blocks, dbcsr_t_type
dbcsr_t_filter, dbcsr_t_get_block, dbcsr_t_get_info, dbcsr_t_get_num_blocks, &
dbcsr_t_get_nze_total, dbcsr_t_get_stored_coordinates, dbcsr_t_ndims, dbcsr_t_put_block, &
dbcsr_t_reserve_blocks, dbcsr_t_reserved_block_indices, dbcsr_t_type
USE distribution_1d_types, ONLY: distribution_1d_type
USE distribution_2d_types, ONLY: distribution_2d_type
USE gamma, ONLY: init_md_ftable
Expand Down Expand Up @@ -2740,27 +2739,28 @@ END SUBROUTINE build_2c_integrals
! **************************************************************************************************
!> \brief ...
!> \param tensor tensor with data. Data is cleared after compression.
!> \param blk_indices ...
!> \param compressed compressed tensor data
!> \param eps all entries < eps are discarded
!> \param memory ...
! **************************************************************************************************
SUBROUTINE compress_tensor(tensor, compressed, eps, memory)
SUBROUTINE compress_tensor(tensor, blk_indices, compressed, eps, memory)
TYPE(dbcsr_t_type), INTENT(INOUT) :: tensor
INTEGER, ALLOCATABLE, DIMENSION(:, :), &
INTENT(INOUT) :: blk_indices
TYPE(hfx_compression_type), INTENT(INOUT) :: compressed
REAL(dp), INTENT(IN) :: eps
REAL(dp), INTENT(INOUT) :: memory

INTEGER :: blk, buffer_left, buffer_size, &
buffer_start, i, memory_usage, nbits, &
nints
INTEGER :: buffer_left, buffer_size, buffer_start, &
i, memory_usage, nbits, nints
INTEGER(int_8) :: estimate_to_store_int, &
storage_counter_integrals
INTEGER, DIMENSION(3) :: ind
LOGICAL :: found
REAL(dp) :: spherical_estimate
REAL(dp), ALLOCATABLE, DIMENSION(:, :, :), TARGET :: blk_data
REAL(dp), DIMENSION(:), POINTER :: blk_data_1d
TYPE(dbcsr_t_iterator_type) :: iter
TYPE(hfx_cache_type), DIMENSION(:), POINTER :: integral_caches
TYPE(hfx_cache_type), POINTER :: maxval_cache
TYPE(hfx_container_type), DIMENSION(:), POINTER :: integral_containers
Expand All @@ -2780,9 +2780,13 @@ SUBROUTINE compress_tensor(tensor, compressed, eps, memory)
maxval_cache => compressed%maxval_cache(1)
integral_caches => compressed%integral_caches(:, 1)

CALL dbcsr_t_iterator_start(iter, tensor)
DO WHILE (dbcsr_t_iterator_blocks_left(iter))
CALL dbcsr_t_iterator_next_block(iter, ind, blk)
IF (ALLOCATED(blk_indices)) DEALLOCATE (blk_indices)
ALLOCATE (blk_indices(dbcsr_t_get_num_blocks(tensor), 3))
CALL dbcsr_t_reserved_block_indices(tensor, blk_indices)

! Can not use the tensor iterator here because the order of the blocks is not guaranteed.
DO i = 1, SIZE(blk_indices, 1)
ind = blk_indices(i, :)
CALL dbcsr_t_get_block(tensor, ind, blk_data, found)
CPASSERT(found)
nints = SIZE(blk_data)
Expand Down Expand Up @@ -2822,7 +2826,6 @@ SUBROUTINE compress_tensor(tensor, compressed, eps, memory)

NULLIFY (blk_data_1d); DEALLOCATE (blk_data)
END DO
CALL dbcsr_t_iterator_stop(iter)

CALL dbcsr_t_clear(tensor)

Expand Down Expand Up @@ -2860,15 +2863,13 @@ SUBROUTINE decompress_tensor(tensor, blk_indices, compressed, eps)
TYPE(hfx_compression_type), INTENT(INOUT) :: compressed
REAL(dp), INTENT(IN) :: eps

INTEGER :: blk, buffer_left, buffer_size, &
buffer_start, i, memory_usage, nbits, &
nints
INTEGER :: buffer_left, buffer_size, buffer_start, &
i, memory_usage, nbits, nints
INTEGER(int_8) :: estimate_to_store_int
INTEGER, DIMENSION(3) :: blk_size, ind
REAL(dp) :: spherical_estimate
REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET :: blk_data
REAL(dp), DIMENSION(:, :, :), POINTER :: blk_data_3d
TYPE(dbcsr_t_iterator_type) :: iter
TYPE(hfx_cache_type), DIMENSION(:), POINTER :: integral_caches
TYPE(hfx_cache_type), POINTER :: maxval_cache
TYPE(hfx_container_type), DIMENSION(:), POINTER :: integral_containers
Expand All @@ -2889,9 +2890,11 @@ SUBROUTINE decompress_tensor(tensor, blk_indices, compressed, eps)
END DO

CALL dbcsr_t_reserve_blocks(tensor, blk_indices)
CALL dbcsr_t_iterator_start(iter, tensor)
DO WHILE (dbcsr_t_iterator_blocks_left(iter))
CALL dbcsr_t_iterator_next_block(iter, ind, blk, blk_size=blk_size)

! Can not use the tensor iterator here because the order of the blocks is not guaranteed.
DO i = 1, SIZE(blk_indices, 1)
ind = blk_indices(i, :)
CALL dbcsr_t_blk_sizes(tensor, ind, blk_size)
nints = PRODUCT(blk_size)
CALL hfx_get_single_cache_element( &
estimate_to_store_int, 6, &
Expand Down Expand Up @@ -2924,8 +2927,6 @@ SUBROUTINE decompress_tensor(tensor, blk_indices, compressed, eps)
NULLIFY (blk_data_3d); DEALLOCATE (blk_data)
END DO

CALL dbcsr_t_iterator_stop(iter)

CALL hfx_reset_cache_and_container(maxval_cache, maxval_container, memory_usage, .FALSE.)
DO i = 1, 64
CALL hfx_reset_cache_and_container(integral_caches(i), integral_containers(i), &
Expand Down
13 changes: 6 additions & 7 deletions src/rpa_gw_im_time_util.F
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ MODULE rpa_gw_im_time_util
dbcsr_scalar, dbcsr_set_diag, dbcsr_type, dbcsr_type_no_symmetry
USE dbcsr_tensor_api, ONLY: &
dbcsr_t_contract, dbcsr_t_copy, dbcsr_t_copy_matrix_to_tensor, dbcsr_t_create, &
dbcsr_t_default_distvec, dbcsr_t_destroy, dbcsr_t_get_info, dbcsr_t_get_num_blocks, &
dbcsr_t_pgrid_create, dbcsr_t_pgrid_destroy, dbcsr_t_pgrid_type, &
dbcsr_t_reserved_block_indices, dbcsr_t_type
dbcsr_t_default_distvec, dbcsr_t_destroy, dbcsr_t_get_info, dbcsr_t_pgrid_create, &
dbcsr_t_pgrid_destroy, dbcsr_t_pgrid_type, dbcsr_t_type
USE hfx_types, ONLY: alloc_containers,&
block_ind_type,&
hfx_compression_type
Expand Down Expand Up @@ -409,12 +408,12 @@ SUBROUTINE get_tensor_3c_overl_int_gw(t_3c_overl_int, t_3c_O_compressed, t_3c_O_
END IF

CALL alloc_containers(t_3c_O_mo_compressed, 1)
ALLOCATE (t_3c_O_mo_ind(dbcsr_t_get_num_blocks(t_3c_overl_int_ao_mo), 3))
CALL dbcsr_t_reserved_block_indices(t_3c_overl_int_ao_mo, t_3c_O_mo_ind)

CALL get_tensor_occupancy(t_3c_overl_int_ao_mo, nze, occ)
memory_3c = 0.0_dp
CALL compress_tensor(t_3c_overl_int_ao_mo, t_3c_O_mo_compressed, qs_env%mp2_env%ri_rpa_im_time%eps_compress, memory_3c)

CALL compress_tensor(t_3c_overl_int_ao_mo, t_3c_O_mo_ind, t_3c_O_mo_compressed, &
qs_env%mp2_env%ri_rpa_im_time%eps_compress, memory_3c)

CALL mp_sum(memory_3c, para_env%group)
compression_factor = REAL(nze, dp)*1.0E-06*8.0_dp/memory_3c

Expand Down

0 comments on commit 26f8803

Please sign in to comment.