|
ADS-IDAC Engine 5.0.0
|
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_ |
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.
|
explicit |
Creates an owning tensor filled with zeros.
| shape | Extent of each dimension. Must not be empty. |
| std::invalid_argument | if shape is empty. |
Definition at line 5 of file dense_tensor.cpp.
References compute_strides(), num_elements_, owned_data_, and shape().
| 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.
| shape | Extent of each dimension. Must not be empty. |
| preallocated_ptr | Buffer of at least size() doubles. It must stay valid for as long as the tensor is used. |
| std::invalid_argument | if shape is empty or the pointer is null. |
Definition at line 15 of file dense_tensor.cpp.
References compute_strides(), num_elements_, and shape().
| double & bncore::DenseTensor::at | ( | const std::vector< std::size_t > & | indices | ) |
Checked element access by one index per dimension.
| indices | One index per dimension, in shape order. |
| std::invalid_argument | if the number of indices differs from the number of dimensions. |
| std::out_of_range | if an index is not below its extent. |
Definition at line 39 of file dense_tensor.cpp.
References compute_flat_index(), and operator[]().
| double bncore::DenseTensor::at | ( | const std::vector< std::size_t > & | indices | ) | const |
Checked element read by one index per dimension.
| indices | One index per dimension, in shape order. |
| std::invalid_argument | if the number of indices differs from the number of dimensions. |
| std::out_of_range | if an index is not below its extent. |
Definition at line 43 of file dense_tensor.cpp.
References compute_flat_index(), and operator[]().
| 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.
| ptr | Buffer 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().
|
private |
|
private |
Definition at line 74 of file dense_tensor.cpp.
References shape_, and strides_.
Referenced by DenseTensor().
| 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().
| 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_.
| 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().
| void bncore::DenseTensor::multiply_inplace | ( | const DenseTensor & | other | ) |
Multiplies this tensor by another, element by element.
| other | Tensor of the same shape. |
| std::invalid_argument | if the shapes differ. |
Definition at line 57 of file dense_tensor.cpp.
References data(), num_elements_, and shape_.
| double & bncore::DenseTensor::operator[] | ( | std::size_t | flat_idx | ) |
Element access by flat row-major index.
| flat_idx | Index 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().
| double bncore::DenseTensor::operator[] | ( | std::size_t | flat_idx | ) | const |
Element read by flat row-major index.
| flat_idx | Index in [0, size()). It is not checked. |
Definition at line 35 of file dense_tensor.cpp.
References mapped_data_, and owned_data_.
| 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().
| 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().
|
private |
borrowed buffer; takes precedence when non-null
Definition at line 90 of file dense_tensor.hpp.
Referenced by bind_data(), data(), and operator[]().
|
private |
Definition at line 91 of file dense_tensor.hpp.
Referenced by DenseTensor(), fill(), multiply_inplace(), and size().
|
private |
empty for a tensor created over caller memory
Definition at line 89 of file dense_tensor.hpp.
Referenced by data(), DenseTensor(), and operator[]().
|
private |
Definition at line 86 of file dense_tensor.hpp.
Referenced by compute_flat_index(), compute_strides(), multiply_inplace(), and shape().
|
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().