|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- ! BSD 2-Clause License
- !
- ! Copyright (c) 2021-2022, Hewlett Packard Enterprise
- ! All rights reserved.
- !
- ! Redistribution and use in source and binary forms, with or without
- ! modification, are permitted provided that the following conditions are met:
- !
- ! 1. Redistributions of source code must retain the above copyright notice, this
- ! list of conditions and the following disclaimer.
- !
- ! 2. Redistributions in binary form must reproduce the above copyright notice,
- ! this list of conditions and the following disclaimer in the documentation
- ! and/or other materials provided with the distribution.
- !
- ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- ! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- ! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- ! DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- ! FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- ! DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- ! SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- ! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- ! OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- ! OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- module smartredis_dataset
-
- use iso_c_binding, only : c_ptr, c_bool, c_null_ptr, c_char, c_int
- use iso_c_binding, only : c_int8_t, c_int16_t, c_int32_t, c_int64_t, c_float, c_double, c_size_t
- use iso_c_binding, only : c_loc, c_f_pointer
- use fortran_c_interop, only : enum_kind
-
- implicit none; private
-
- include 'enum_fortran.inc'
- include 'dataset/dataset_interfaces.inc'
- include 'dataset/add_tensor_interfaces.inc'
- include 'dataset/unpack_dataset_tensor_interfaces.inc'
- include 'dataset/metadata_interfaces.inc'
-
- public :: enum_kind !< The kind of integer equivalent to a C enum. According to C an Fortran
- !! standards this should be c_int, but is renamed here to ensure that
- !! users do not have to import the iso_c_binding module into their
- !! programs
-
- !> Contains multiple tensors and metadata used to describe an entire set of data
- type, public :: dataset_type
- type(c_ptr) :: dataset_ptr !< A pointer to the initialized dataset object
-
- contains
-
- !> Initialize a new dataset with a given name
- procedure :: initialize => initialize_dataset
- !> Add metadata to the dataset with a given field and string
- procedure :: add_meta_string
- ! procedure :: get_meta_strings ! Not supported currently
- !> Add a tensor to be included as part of the dataset
- generic :: add_tensor => add_tensor_i8, add_tensor_i16, add_tensor_i32, add_tensor_i64, &
- add_tensor_float, add_tensor_double
- !> Unpack a tensor that has previously been added to the dataset
- generic :: unpack_dataset_tensor => unpack_dataset_tensor_i8, unpack_dataset_tensor_i16, &
- unpack_dataset_tensor_i32, unpack_dataset_tensor_i64, &
- unpack_dataset_tensor_float, unpack_dataset_tensor_double
- !> Add metadata of type 'scalar' into a given field
- generic :: add_meta_scalar => add_meta_scalar_double, add_meta_scalar_float, add_meta_scalar_i32, add_meta_scalar_i64
- !> Retrieve scalar-type metadata as a vector
- generic :: get_meta_scalars => get_meta_scalars_double, get_meta_scalars_float, get_meta_scalars_i32, &
- get_meta_scalars_i64
-
- ! Private procedures
- procedure, private :: add_tensor_i8
- procedure, private :: add_tensor_i16
- procedure, private :: add_tensor_i32
- procedure, private :: add_tensor_i64
- procedure, private :: add_tensor_float
- procedure, private :: add_tensor_double
- procedure, private :: unpack_dataset_tensor_i8
- procedure, private :: unpack_dataset_tensor_i16
- procedure, private :: unpack_dataset_tensor_i32
- procedure, private :: unpack_dataset_tensor_i64
- procedure, private :: unpack_dataset_tensor_float
- procedure, private :: unpack_dataset_tensor_double
- procedure, private :: add_meta_scalar_double
- procedure, private :: add_meta_scalar_float
- procedure, private :: add_meta_scalar_i32
- procedure, private :: add_meta_scalar_i64
- procedure, private :: get_meta_scalars_double
- procedure, private :: get_meta_scalars_float
- procedure, private :: get_meta_scalars_i32
- procedure, private :: get_meta_scalars_i64
- end type dataset_type
-
- contains
-
-
- !> Initialize the dataset
- function initialize_dataset(self, name) result(code)
- class(dataset_type), intent(inout) :: self !< Receives the dataset
- character(len=*), intent(in) :: name !< Name of the dataset
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! Local variables
- integer(kind=c_size_t) :: name_length
- character(kind=c_char, len=len_trim(name)) :: c_name
-
- name_length = len_trim(name)
- c_name = trim(name)
-
- code = dataset_constructor(c_name, name_length, self%dataset_ptr)
- end function initialize_dataset
-
- !> Add a tensor to a dataset whose Fortran type is the equivalent 'int8' C-type
- function add_tensor_i8(self, name, data, dims) result(code)
- integer(kind=c_int8_t), dimension(..), target, intent(in) :: data !< Data to be sent
- class(dataset_type), intent(in) :: self !< Fortran SmartRedis dataset
- character(len=*), intent(in) :: name !< The unique name used to store in the database
- integer, dimension(:), intent(in) :: dims !< The length of each dimension
- integer(kind=enum_kind) :: code !< Result of the operation
-
- include 'dataset/add_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_int8
- code = add_tensor_c(self%dataset_ptr, c_name, name_length, data_ptr, &
- c_dims_ptr, c_n_dims, data_type, c_fortran_contiguous)
- end function add_tensor_i8
-
- !> Add a tensor to a dataset whose Fortran type is the equivalent 'int16' C-type
- function add_tensor_i16(self, name, data, dims) result(code)
- integer(kind=c_int16_t), dimension(..), target, intent(in) :: data !< Data to be sent
- class(dataset_type), intent(in) :: self !< Fortran SmartRedis dataset
- character(len=*), intent(in) :: name !< The unique name used to store in the database
- integer, dimension(:), intent(in) :: dims !< The length of each dimension
- integer(kind=enum_kind) :: code !< Result of the operation
-
- include 'dataset/add_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_int16
- code = add_tensor_c(self%dataset_ptr, c_name, name_length, data_ptr, &
- c_dims_ptr, c_n_dims, data_type, c_fortran_contiguous)
- end function add_tensor_i16
-
- !> Add a tensor to a dataset whose Fortran type is the equivalent 'int32' C-type
- function add_tensor_i32(self, name, data, dims) result(code)
- integer(kind=c_int32_t), dimension(..), target, intent(in) :: data !< Data to be sent
- class(dataset_type), intent(in) :: self !< Fortran SmartRedis dataset
- character(len=*), intent(in) :: name !< The unique name used to store in the database
- integer, dimension(:), intent(in) :: dims !< The length of each dimension
- integer(kind=enum_kind) :: code !< Result of the operation
-
- include 'dataset/add_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_int32
- code = add_tensor_c(self%dataset_ptr, c_name, name_length, data_ptr, &
- c_dims_ptr, c_n_dims, data_type, c_fortran_contiguous)
- end function add_tensor_i32
-
- !> Add a tensor to a dataset whose Fortran type is the equivalent 'int64' C-type
- function add_tensor_i64(self, name, data, dims) result(code)
- integer(kind=c_int64_t), dimension(..), target, intent(in) :: data !< Data to be sent
- class(dataset_type), intent(in) :: self !< Fortran SmartRedis dataset
- character(len=*), intent(in) :: name !< The unique name used to store in the database
- integer, dimension(:), intent(in) :: dims !< The length of each dimension
- integer(kind=enum_kind) :: code !< Result of the operation
-
- include 'dataset/add_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_int64
- code = add_tensor_c(self%dataset_ptr, c_name, name_length, data_ptr, &
- c_dims_ptr, c_n_dims, data_type, c_fortran_contiguous)
- end function add_tensor_i64
-
- !> Add a tensor to a dataset whose Fortran type is the equivalent 'float' C-type
- function add_tensor_float(self, name, data, dims) result(code)
- real(kind=c_float), dimension(..), target, intent(in) :: data !< Data to be sent
- class(dataset_type), intent(in) :: self !< Fortran SmartRedis dataset
- character(len=*), intent(in) :: name !< The unique name used to store in the database
- integer, dimension(:), intent(in) :: dims !< The length of each dimension
- integer(kind=enum_kind) :: code !< Result of the operation
-
- include 'dataset/add_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_flt
- code = add_tensor_c(self%dataset_ptr, c_name, name_length, data_ptr, &
- c_dims_ptr, c_n_dims, data_type, c_fortran_contiguous)
- end function add_tensor_float
-
- !> Add a tensor to a dataset whose Fortran type is the equivalent 'double' C-type
- function add_tensor_double(self, name, data, dims) result(code)
- real(kind=c_double), dimension(..), target, intent(in) :: data !< Data to be sent
- class(dataset_type), intent(in) :: self !< Fortran SmartRedis dataset
- character(len=*), intent(in) :: name !< The unique name used to store in the database
- integer, dimension(:), intent(in) :: dims !< The length of each dimension
- integer(kind=enum_kind) :: code !< Result of the operation
-
- include 'dataset/add_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_dbl
- code = add_tensor_c(self%dataset_ptr, c_name, name_length, data_ptr, &
- c_dims_ptr, c_n_dims, data_type, c_fortran_contiguous)
- end function add_tensor_double
-
-
- !> Unpack a tensor into already allocated memory whose Fortran type is the equivalent 'int8' C-type
- function unpack_dataset_tensor_i8(self, name, result, dims) result(code)
- integer(kind=c_int8_t), dimension(..), target, intent(out) :: result !< Array to be populated with data
- class(dataset_type), intent(in) :: self !< Pointer to the initialized dataset
- character(len=*), intent(in) :: name !< The name to use to place the tensor
- integer, dimension(:), intent(in) :: dims !< Length along each dimension of the tensor
- integer(kind=enum_kind) :: code
-
- include 'dataset/unpack_dataset_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_int8
- code = unpack_dataset_tensor_c(self%dataset_ptr, c_name, name_length, &
- data_ptr, c_dims_ptr, c_n_dims, data_type, mem_layout)
- end function unpack_dataset_tensor_i8
-
- !> Unpack a tensor into already allocated memory whose Fortran type is the equivalent 'int16' C-type
- function unpack_dataset_tensor_i16(self, name, result, dims) result(code)
- integer(kind=c_int16_t), dimension(..), target, intent(out) :: result !< Array to be populated with data
- class(dataset_type), intent(in) :: self !< Pointer to the initialized dataset
- character(len=*), intent(in) :: name !< The name to use to place the tensor
- integer, dimension(:), intent(in) :: dims !< Length along each dimension of the tensor
- integer(kind=enum_kind) :: code
-
- include 'dataset/unpack_dataset_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_int16
- code = unpack_dataset_tensor_c(self%dataset_ptr, c_name, name_length, &
- data_ptr, c_dims_ptr, c_n_dims, data_type, mem_layout)
- end function unpack_dataset_tensor_i16
-
- !> Unpack a tensor into already allocated memory whose Fortran type is the equivalent 'int32' C-type
- function unpack_dataset_tensor_i32(self, name, result, dims) result(code)
- integer(kind=c_int32_t), dimension(..), target, intent(out) :: result !< Array to be populated with data
- class(dataset_type), intent(in) :: self !< Pointer to the initialized dataset
- character(len=*), intent(in) :: name !< The name to use to place the tensor
- integer, dimension(:), intent(in) :: dims !< Length along each dimension of the tensor
- integer(kind=enum_kind) :: code
-
- include 'dataset/unpack_dataset_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_int32
- code = unpack_dataset_tensor_c(self%dataset_ptr, c_name, name_length, &
- data_ptr, c_dims_ptr, c_n_dims, data_type, mem_layout)
- end function unpack_dataset_tensor_i32
-
- !> Unpack a tensor into already allocated memory whose Fortran type is the equivalent 'int64' C-type
- function unpack_dataset_tensor_i64(self, name, result, dims) result(code)
- integer(kind=c_int64_t), dimension(..), target, intent(out) :: result !< Array to be populated with data
- class(dataset_type), intent(in) :: self !< Pointer to the initialized dataset
- character(len=*), intent(in) :: name !< The name to use to place the tensor
- integer, dimension(:), intent(in) :: dims !< Length along each dimension of the tensor
- integer(kind=enum_kind) :: code
-
- include 'dataset/unpack_dataset_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_int64
- code = unpack_dataset_tensor_c(self%dataset_ptr, c_name, name_length, &
- data_ptr, c_dims_ptr, c_n_dims, data_type, mem_layout)
- end function unpack_dataset_tensor_i64
-
- !> Unpack a tensor into already allocated memory whose Fortran type is the equivalent 'float' C-type
- function unpack_dataset_tensor_float(self, name, result, dims) result(code)
- real(kind=c_float), dimension(..), target, intent(out) :: result !< Array to be populated with data
- class(dataset_type), intent(in) :: self !< Pointer to the initialized dataset
- character(len=*), intent(in) :: name !< The name to use to place the tensor
- integer, dimension(:), intent(in) :: dims !< Length along each dimension of the tensor
- integer(kind=enum_kind) :: code
-
- include 'dataset/unpack_dataset_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_flt
- code = unpack_dataset_tensor_c(self%dataset_ptr, c_name, name_length, &
- data_ptr, c_dims_ptr, c_n_dims, data_type, mem_layout)
- end function unpack_dataset_tensor_float
-
- !> Unpack a tensor into already allocated memory whose Fortran type is the equivalent 'double' C-type
- function unpack_dataset_tensor_double(self, name, result, dims) result(code)
- real(kind=c_double), dimension(..), target, intent(out) :: result !< Array to be populated with data
- class(dataset_type), intent(in) :: self !< Pointer to the initialized dataset
- character(len=*), intent(in) :: name !< The name to use to place the tensor
- integer, dimension(:), intent(in) :: dims !< Length along each dimension of the tensor
- integer(kind=enum_kind) :: code
-
- include 'dataset/unpack_dataset_tensor_methods_common.inc'
-
- ! Define the type and call the C-interface
- data_type = tensor_dbl
- code = unpack_dataset_tensor_c(self%dataset_ptr, c_name, name_length, &
- data_ptr, c_dims_ptr, c_n_dims, data_type, mem_layout)
- end function unpack_dataset_tensor_double
-
-
- !> Get scalar metadata whose Fortran type is the equivalent 'int32' C-type
- function get_meta_scalars_i32(self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- integer(kind=c_int32_t), dimension(:), pointer :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- integer(kind=enum_kind) :: expected_data_type = meta_int32
- include 'dataset/get_meta_scalars_common.inc'
- end function get_meta_scalars_i32
-
- !> Get scalar metadata whose Fortran type is the equivalent 'int64' C-type
- function get_meta_scalars_i64(self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- integer(kind=c_int64_t), dimension(:), pointer :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- integer(kind=enum_kind) :: expected_data_type = meta_int64
- include 'dataset/get_meta_scalars_common.inc'
- end function get_meta_scalars_i64
-
- !> Get scalar metadata whose Fortran type is the equivalent 'float' C-type
- function get_meta_scalars_float(self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- real(kind=c_float), dimension(:), pointer :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- integer(kind=enum_kind) :: expected_data_type = meta_flt
- include 'dataset/get_meta_scalars_common.inc'
- end function get_meta_scalars_float
-
- !> Get scalar metadata whose Fortran type is the equivalent 'double' C-type
- function get_meta_scalars_double(self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- real(kind=c_double), dimension(:), pointer :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- integer(kind=enum_kind) :: expected_data_type = meta_dbl
- include 'dataset/get_meta_scalars_common.inc'
- end function get_meta_scalars_double
-
- !> Add scalar metadata whose Fortran type is the equivalent 'int32' C-type
- function add_meta_scalar_i32(self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- integer(kind=c_int32_t), target, intent(in) :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- integer(kind=enum_kind), parameter :: meta_type = meta_int32
- include 'dataset/add_meta_scalar_common.inc'
- end function add_meta_scalar_i32
-
- !> Add scalar metadata whose Fortran type is the equivalent 'int64' C-type
- function add_meta_scalar_i64(self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- integer(kind=c_int64_t), target, intent(in) :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- integer(kind=enum_kind), parameter :: meta_type = meta_int64
- include 'dataset/add_meta_scalar_common.inc'
- end function add_meta_scalar_i64
-
- !> Add scalar metadata whose Fortran type is the equivalent 'float' C-type
- function add_meta_scalar_float(self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- real(kind=c_float), target, intent(in) :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- integer(kind=enum_kind), parameter :: meta_type = meta_flt
- include 'dataset/add_meta_scalar_common.inc'
- end function add_meta_scalar_float
-
- !> Add scalar metadata whose Fortran type is the equivalent 'double' C-type
- function add_meta_scalar_double(self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- real(kind=c_double), target, intent(in) :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- integer(kind=enum_kind), parameter :: meta_type = meta_dbl
- include 'dataset/add_meta_scalar_common.inc'
- end function add_meta_scalar_double
-
- !> Add string-like metadata to the dataset
- function add_meta_string( self, name, meta) result(code)
- class(dataset_type), intent(in) :: self !< The dataset
- character(len=*), intent(in) :: name !< The name of the metadata field
- character(len=*), intent(in) :: meta !< The actual metadata
- integer(kind=enum_kind) :: code !< Result of the operation
-
- ! local variables
- character(kind=c_char, len=len_trim(meta)) :: c_meta
- character(kind=c_char, len=len_trim(name)) :: c_name
-
- integer(kind=c_size_t) :: meta_length, name_length
-
- c_name = trim(name)
- c_meta = trim(meta)
-
- meta_length = len_trim(c_meta)
- name_length = len_trim(c_name)
-
- code = add_meta_string_c(self%dataset_ptr, c_name, name_length, c_meta, meta_length)
- end function add_meta_string
-
- end module smartredis_dataset
|