Skip to content

Commit

Permalink
DBT: Add lightweight fork of DBCSR tensor library from Patrick Seewald
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Jan 17, 2022
1 parent 82a440d commit 0410ae8
Show file tree
Hide file tree
Showing 26 changed files with 15,142 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/dbt/PACKAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description": "Distributed Block-sparse Tensors",
"requires": ["./tas", "../dbm", "../base", "../common", "../mpiwrap", "../offload"],
"public": ["dbt_api.F"],
}
8 changes: 8 additions & 0 deletions src/dbt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# DBT: Distributed Block-sparse Tensors

The DBT is a lightweight fork of [DBCSR Tensors](https://github.com/cp2k/dbcsr/tree/develop/src/tensors)
and the related [TAS layer](https://github.com/cp2k/dbcsr/tree/develop/src/tas)
for Tall-and-Skinny matrices.

The code was originally written by Patrick Seewald and is well documented in his
[PhD thesis](https://www.cp2k.org/_media/docs:phd_thesis_patrick_seewald.pdf).
82 changes: 82 additions & 0 deletions src/dbt/dbt_allocate_wrap.F
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2022 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!

! **************************************************************************************************
!> \brief Wrapper for allocating, copying and reshaping arrays.
!> \todo with fortran 2008 support, this should be replaced by plain ALLOCATE
!> \note in particular ALLOCATE(..., SOURCE=...) does not work in gcc 5.4.0, see also
!> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44672
!> \author Patrick Seewald
! **************************************************************************************************
MODULE dbt_allocate_wrap
#:include "dbt_macros.fypp"
#:set maxdim = fortran_max_ndim

USE kinds, ONLY: dp

#include "../base/base_uses.f90"
IMPLICIT NONE
PRIVATE

PUBLIC :: allocate_any

INTERFACE allocate_any
#:for dim in range(1, maxdim+1)
MODULE PROCEDURE allocate_${dim}$d
#:endfor
END INTERFACE

CONTAINS

#:for dim in range(1, maxdim+1)
! **************************************************************************************************
!> \brief Allocate array according to shape_spec. Possibly assign array from source.
!> \note this does not fully replace Fortran RESHAPE intrinsic since source and target array must
!> have same rank
!> \param array target array.
!> \param shape_spec shape of array to be allocated. If not specified, it is derived from source.
!> \param source source array to be copied to target array, must have same rank as target array.
!> \param order in which order to copy source to array (same convention as RESHAPE intrinsic).
!> \author Patrick Seewald
! **************************************************************************************************
SUBROUTINE allocate_${dim}$d(array, shape_spec, source, order)
REAL(dp), DIMENSION(${shape_colon(dim)}$), ALLOCATABLE, INTENT(OUT) :: array
INTEGER, DIMENSION(${dim}$), INTENT(IN), OPTIONAL :: shape_spec
REAL(dp), DIMENSION(${shape_colon(dim)}$), INTENT(IN), OPTIONAL :: source
INTEGER, DIMENSION(${dim}$), INTENT(IN), OPTIONAL :: order
INTEGER, DIMENSION(${dim}$) :: shape_prv

IF (PRESENT(shape_spec)) THEN
IF (PRESENT(order)) THEN
shape_prv(order) = shape_spec
ELSE
shape_prv = shape_spec
END IF
ELSEIF (PRESENT(source)) THEN
IF (PRESENT(order)) THEN
shape_prv(order) = SHAPE(source)
ELSE
shape_prv = SHAPE(source)
END IF
ELSE
CPABORT("either source or shape_spec must be present")
END IF

IF (PRESENT(source)) THEN
IF (PRESENT(order)) THEN
ALLOCATE (array(${arrlist("shape_prv", nmax=dim)}$))
array(${shape_colon(dim)}$) = RESHAPE(source, shape_prv, order=order)
ELSE
ALLOCATE (array(${arrlist("shape_prv", nmax=dim)}$), source=source)
END IF
ELSE
ALLOCATE (array(${arrlist("shape_prv", nmax=dim)}$))
END IF

END SUBROUTINE
#:endfor
END MODULE
111 changes: 111 additions & 0 deletions src/dbt/dbt_api.F
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2022 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!

! **************************************************************************************************
!> \brief This is the start of a dbt_api, all publically needed functions
!> are exported here. The others remain private to the library.
!> Currently, this is the CP2K used set.
!> Ultimately, a reduced subset and well defined api will remain,
!> possibly grouped in to standard and expert api.
!> Currently, this is work in progress.
!> \author Patrick Seewald
! **************************************************************************************************
MODULE dbt_api

USE dbt_block, ONLY: dbt_iterator_blocks_left,&
dbt_iterator_next_block,&
dbt_iterator_start,&
dbt_iterator_stop,&
dbt_iterator_type,&
dbt_reserved_block_indices
USE dbt_index, ONLY: dbt_get_mapping_info
USE dbt_io, ONLY: dbt_write_blocks,&
dbt_write_split_info,&
dbt_write_tensor_dist,&
dbt_write_tensor_info
USE dbt_methods, ONLY: &
dbt_batched_contract_finalize, dbt_batched_contract_init, dbt_contract, &
dbt_contract_index, dbt_copy, dbt_copy_matrix_to_tensor, dbt_copy_tensor_to_matrix, &
dbt_get_block, dbt_get_stored_coordinates, dbt_put_block, dbt_reserve_blocks
USE dbt_split, ONLY: dbt_split_blocks
USE dbt_test, ONLY: dbt_checksum,&
dbt_contract_test
USE dbt_types, ONLY: &
dbt_blk_size, dbt_blk_sizes, dbt_clear, dbt_create, dbt_default_distvec, dbt_destroy, &
dbt_dims => dims_tensor, dbt_distribution_destroy, dbt_distribution_new, &
dbt_distribution_type, dbt_filter, dbt_finalize, dbt_get_info, dbt_get_num_blocks, &
dbt_get_num_blocks_total, dbt_get_nze, dbt_get_nze_total, dbt_max_nblks_local, &
dbt_mp_dims_create, dbt_mp_environ_pgrid => mp_environ_pgrid, dbt_nblks_local, &
dbt_nblks_total, dbt_nd_mp_comm, dbt_nd_mp_free, dbt_ndims => ndims_tensor, &
dbt_ndims_matrix_column => ndims_matrix_column, dbt_ndims_matrix_row => ndims_matrix_row, &
dbt_pgrid_change_dims, dbt_pgrid_create, dbt_pgrid_create_expert, dbt_pgrid_destroy, &
dbt_pgrid_type, dbt_scale, dbt_type

IMPLICIT NONE

PRIVATE

CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'dbt_api'

PUBLIC :: dbt_contract, dbt_contract_test
PUBLIC :: dbt_get_block
PUBLIC :: dbt_get_stored_coordinates
PUBLIC :: dbt_put_block
PUBLIC :: dbt_reserve_blocks
PUBLIC :: dbt_create
PUBLIC :: dbt_destroy
PUBLIC :: dbt_distribution_destroy
PUBLIC :: dbt_distribution_new
PUBLIC :: dbt_distribution_type
PUBLIC :: dbt_nd_mp_comm
PUBLIC :: dbt_nd_mp_free
PUBLIC :: dbt_type
PUBLIC :: dbt_iterator_next_block
PUBLIC :: dbt_iterator_blocks_left
PUBLIC :: dbt_iterator_stop
PUBLIC :: dbt_iterator_start
PUBLIC :: dbt_iterator_type
PUBLIC :: dbt_split_blocks
PUBLIC :: dbt_pgrid_type
PUBLIC :: dbt_pgrid_create
PUBLIC :: dbt_pgrid_create_expert
PUBLIC :: dbt_pgrid_destroy
PUBLIC :: dbt_filter
PUBLIC :: dbt_mp_environ_pgrid
PUBLIC :: dbt_copy_matrix_to_tensor
PUBLIC :: dbt_blk_sizes
PUBLIC :: dbt_copy
PUBLIC :: dbt_copy_tensor_to_matrix
PUBLIC :: dbt_get_info
PUBLIC :: dbt_checksum
PUBLIC :: dbt_finalize
PUBLIC :: dbt_scale
PUBLIC :: dbt_get_num_blocks, dbt_get_num_blocks_total
PUBLIC :: dbt_get_nze, dbt_get_nze_total
PUBLIC :: dbt_clear
PUBLIC :: dbt_get_mapping_info
PUBLIC :: dbt_write_split_info
PUBLIC :: dbt_write_blocks
PUBLIC :: dbt_write_tensor_dist
PUBLIC :: dbt_write_tensor_info
PUBLIC :: dbt_mp_dims_create
PUBLIC :: dbt_batched_contract_init
PUBLIC :: dbt_batched_contract_finalize
PUBLIC :: dbt_ndims
PUBLIC :: dbt_dims
PUBLIC :: dbt_pgrid_change_dims
PUBLIC :: dbt_reserved_block_indices
PUBLIC :: dbt_contract_index
PUBLIC :: dbt_ndims_matrix_row
PUBLIC :: dbt_ndims_matrix_column
PUBLIC :: dbt_nblks_local
PUBLIC :: dbt_nblks_total
PUBLIC :: dbt_blk_size
PUBLIC :: dbt_max_nblks_local
PUBLIC :: dbt_default_distvec

END MODULE dbt_api

0 comments on commit 0410ae8

Please sign in to comment.