← Documentation
ADS-IDAC Engine 5.0.0
bncore::DenseTensor Class Reference

Dense row-major array of doubles with a fixed shape. More...

#include <dense_tensor.hpp>

Public Member Functions

 DenseTensor (const std::vector< std::size_t > &shape)
 Creates an owning tensor filled with zeros. More...
 
 DenseTensor (const std::vector< std::size_t > &shape, double *preallocated_ptr)
 Creates a tensor over caller memory. Nothing is allocated and the buffer contents are left as they are. More...
 
const std::vector< std::size_t > & shape () const
 Extent of each dimension, as given at construction. More...
 
std::size_t size () const
 Total number of elements, the product of the shape. More...
 
double & operator[] (std::size_t flat_idx)
 Element access by flat row-major index. More...
 
double operator[] (std::size_t flat_idx) const
 Element read by flat row-major index. More...
 
double & at (const std::vector< std::size_t > &indices)
 Checked element access by one index per dimension. More...
 
double at (const std::vector< std::size_t > &indices) const
 Checked element read by one index per dimension. More...
 
double * data ()
 Pointer to the first element: the borrowed buffer when one is bound, otherwise the owned storage. More...
 
const double * data () const
 Const pointer to the first element. See data(). More...
 
void bind_data (double *ptr)
 Redirects the tensor to a caller buffer. More...
 
void multiply_inplace (const DenseTensor &other)
 Multiplies this tensor by another, element by element. More...
 
void fill (double value)
 Sets every element to value. More...
 

Private Member Functions

void compute_strides ()
 
std::size_t compute_flat_index (const std::vector< std::size_t > &indices) const
 Checked conversion used by at(). Throws as documented there. More...
 

Private Attributes

std::vector< std::size_t > shape_
 
std::vector< std::size_t > strides_
 row-major strides, same length as shape_ More...
 
std::vector< double > owned_data_
 empty for a tensor created over caller memory More...
 
double * mapped_data_ = nullptr
 borrowed buffer; takes precedence when non-null More...
 
std::size_t num_elements_
 

Detailed Description

Dense row-major array of doubles with a fixed shape.

The last dimension varies fastest. A tensor built from a shape alone owns zero-initialized storage. A tensor built with a pointer, or redirected with bind_data(), reads and writes the caller's buffer and never frees it. Copying a borrowing tensor copies the pointer, so both copies refer to the same buffer.

Definition at line 24 of file dense_tensor.hpp.

Constructor & Destructor Documentation

◆ DenseTensor() [1/2]

bncore::DenseTensor::DenseTensor ( const std::vector< std::size_t > &  shape)
explicit

Creates an owning tensor filled with zeros.

Parameters
shapeExtent of each dimension. Must not be empty.
Exceptions
std::invalid_argumentif shape is empty.

Definition at line 5 of file dense_tensor.cpp.

References compute_strides(), num_elements_, owned_data_, and shape().

Here is the call graph for this function:

◆ DenseTensor() [2/2]

bncore::DenseTensor::DenseTensor ( const std::vector< std::size_t > &  shape,
double *  preallocated_ptr 
)

Creates a tensor over caller memory. Nothing is allocated and the buffer contents are left as they are.

Parameters
shapeExtent of each dimension. Must not be empty.
preallocated_ptrBuffer of at least size() doubles. It must stay valid for as long as the tensor is used.
Exceptions
std::invalid_argumentif shape is empty or the pointer is null.

Definition at line 15 of file dense_tensor.cpp.

References compute_strides(), num_elements_, and shape().

Here is the call graph for this function:

Member Function Documentation

◆ at() [1/2]

double & bncore::DenseTensor::at ( const std::vector< std::size_t > &  indices)

Checked element access by one index per dimension.

Parameters
indicesOne index per dimension, in shape order.
Exceptions
std::invalid_argumentif the number of indices differs from the number of dimensions.
std::out_of_rangeif an index is not below its extent.

Definition at line 39 of file dense_tensor.cpp.

References compute_flat_index(), and operator[]().

Here is the call graph for this function:

◆ at() [2/2]

double bncore::DenseTensor::at ( const std::vector< std::size_t > &  indices) const

Checked element read by one index per dimension.

Parameters
indicesOne index per dimension, in shape order.
Exceptions
std::invalid_argumentif the number of indices differs from the number of dimensions.
std::out_of_rangeif an index is not below its extent.

Definition at line 43 of file dense_tensor.cpp.

References compute_flat_index(), and operator[]().

Here is the call graph for this function:

◆ bind_data()

void bncore::DenseTensor::bind_data ( double *  ptr)

Redirects the tensor to a caller buffer.

Owned storage, if any, is kept but not used while a buffer is bound. A null pointer makes the tensor use its owned storage again, which is empty for a tensor created with the borrowing constructor.

Parameters
ptrBuffer of at least size() doubles, or nullptr.

Definition at line 55 of file dense_tensor.cpp.

References mapped_data_.

Referenced by bncore::Factor::bind_data().

Here is the caller graph for this function:

◆ compute_flat_index()

std::size_t bncore::DenseTensor::compute_flat_index ( const std::vector< std::size_t > &  indices) const
private

Checked conversion used by at(). Throws as documented there.

Definition at line 84 of file dense_tensor.cpp.

References shape_, and strides_.

Referenced by at().

Here is the caller graph for this function:

◆ compute_strides()

void bncore::DenseTensor::compute_strides ( )
private

Definition at line 74 of file dense_tensor.cpp.

References shape_, and strides_.

Referenced by DenseTensor().

Here is the caller graph for this function:

◆ data() [1/2]

double * bncore::DenseTensor::data ( )

Pointer to the first element: the borrowed buffer when one is bound, otherwise the owned storage.

Definition at line 47 of file dense_tensor.cpp.

References mapped_data_, and owned_data_.

Referenced by bncore::BatchWorkspace::apply_soft_evidence_to_clique(), fill(), bncore::Factor::marginalize(), bncore::Factor::multiply(), multiply_inplace(), bncore::BatchWorkspace::query_marginal(), bncore::BatchWorkspace::rebuild_clique_potentials(), and bncore::BatchWorkspace::snapshot_base_potentials().

Here is the caller graph for this function:

◆ data() [2/2]

const double * bncore::DenseTensor::data ( ) const

Const pointer to the first element. See data().

Definition at line 51 of file dense_tensor.cpp.

References mapped_data_, and owned_data_.

◆ fill()

void bncore::DenseTensor::fill ( double  value)

Sets every element to value.

Definition at line 69 of file dense_tensor.cpp.

References data(), and num_elements_.

Referenced by bncore::Factor::marginalize(), bncore::BatchWorkspace::query_marginal(), and bncore::BatchWorkspace::rebuild_clique_potentials().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ multiply_inplace()

void bncore::DenseTensor::multiply_inplace ( const DenseTensor &  other)

Multiplies this tensor by another, element by element.

Parameters
otherTensor of the same shape.
Exceptions
std::invalid_argumentif the shapes differ.

Definition at line 57 of file dense_tensor.cpp.

References data(), num_elements_, and shape_.

Here is the call graph for this function:

◆ operator[]() [1/2]

double & bncore::DenseTensor::operator[] ( std::size_t  flat_idx)

Element access by flat row-major index.

Parameters
flat_idxIndex in [0, size()). It is not checked.

Definition at line 31 of file dense_tensor.cpp.

References mapped_data_, and owned_data_.

Referenced by at().

Here is the caller graph for this function:

◆ operator[]() [2/2]

double bncore::DenseTensor::operator[] ( std::size_t  flat_idx) const

Element read by flat row-major index.

Parameters
flat_idxIndex in [0, size()). It is not checked.

Definition at line 35 of file dense_tensor.cpp.

References mapped_data_, and owned_data_.

◆ shape()

const std::vector< std::size_t > & bncore::DenseTensor::shape ( ) const

Extent of each dimension, as given at construction.

Definition at line 27 of file dense_tensor.cpp.

References shape_.

Referenced by bncore::BatchWorkspace::apply_soft_evidence_to_clique(), DenseTensor(), bncore::Factor::marginalize(), bncore::Factor::multiply(), and bncore::BatchWorkspace::rebuild_clique_potentials().

Here is the caller graph for this function:

◆ size()

std::size_t bncore::DenseTensor::size ( ) const

Total number of elements, the product of the shape.

Definition at line 29 of file dense_tensor.cpp.

References num_elements_.

Referenced by bncore::Factor::marginalize(), and bncore::BatchWorkspace::rebuild_clique_potentials().

Here is the caller graph for this function:

Member Data Documentation

◆ mapped_data_

double* bncore::DenseTensor::mapped_data_ = nullptr
private

borrowed buffer; takes precedence when non-null

Definition at line 90 of file dense_tensor.hpp.

Referenced by bind_data(), data(), and operator[]().

◆ num_elements_

std::size_t bncore::DenseTensor::num_elements_
private

Definition at line 91 of file dense_tensor.hpp.

Referenced by DenseTensor(), fill(), multiply_inplace(), and size().

◆ owned_data_

std::vector<double> bncore::DenseTensor::owned_data_
private

empty for a tensor created over caller memory

Definition at line 89 of file dense_tensor.hpp.

Referenced by data(), DenseTensor(), and operator[]().

◆ shape_

std::vector<std::size_t> bncore::DenseTensor::shape_
private

Definition at line 86 of file dense_tensor.hpp.

Referenced by compute_flat_index(), compute_strides(), multiply_inplace(), and shape().

◆ strides_

std::vector<std::size_t> bncore::DenseTensor::strides_
private

row-major strides, same length as shape_

Definition at line 87 of file dense_tensor.hpp.

Referenced by compute_flat_index(), and compute_strides().


The documentation for this class was generated from the following files: