← Documentation
ADS-IDAC Engine 5.0.0
dense_tensor.cpp
Go to the documentation of this file.
2
3namespace bncore {
4
5DenseTensor::DenseTensor(const std::vector<std::size_t> &shape)
6 : shape_(shape), mapped_data_(nullptr) {
7 if (shape.empty())
8 throw std::invalid_argument("Tensor shape cannot be empty.");
11 std::accumulate(shape.begin(), shape.end(), 1ULL, std::multiplies<>());
12 owned_data_.resize(num_elements_, 0.0);
13}
14
15DenseTensor::DenseTensor(const std::vector<std::size_t> &shape,
16 double *preallocated_ptr)
17 : shape_(shape), mapped_data_(preallocated_ptr) {
18 if (shape.empty())
19 throw std::invalid_argument("Tensor shape cannot be empty.");
20 if (!preallocated_ptr)
21 throw std::invalid_argument("Preallocated pointer is null.");
24 std::accumulate(shape.begin(), shape.end(), 1ULL, std::multiplies<>());
25}
26
27const std::vector<std::size_t> &DenseTensor::shape() const { return shape_; }
28
29std::size_t DenseTensor::size() const { return num_elements_; }
30
31double &DenseTensor::operator[](std::size_t flat_idx) {
32 return mapped_data_ ? mapped_data_[flat_idx] : owned_data_[flat_idx];
33}
34
35double DenseTensor::operator[](std::size_t flat_idx) const {
36 return mapped_data_ ? mapped_data_[flat_idx] : owned_data_[flat_idx];
37}
38
39double &DenseTensor::at(const std::vector<std::size_t> &indices) {
40 return this->operator[](compute_flat_index(indices));
41}
42
43double DenseTensor::at(const std::vector<std::size_t> &indices) const {
44 return this->operator[](compute_flat_index(indices));
45}
46
48 return mapped_data_ ? mapped_data_ : owned_data_.data();
49}
50
51const double *DenseTensor::data() const {
52 return mapped_data_ ? mapped_data_ : owned_data_.data();
53}
54
55void DenseTensor::bind_data(double *ptr) { mapped_data_ = ptr; }
56
58 if (shape_ != other.shape_) {
59 throw std::invalid_argument(
60 "Shapes must match for inplace multiplication.");
61 }
62 double *my_data = data();
63 const double *other_data = other.data();
64 for (std::size_t i = 0; i < num_elements_; ++i) {
65 my_data[i] *= other_data[i];
66 }
67}
68
69void DenseTensor::fill(double value) {
70 double *my_data = data();
71 std::fill(my_data, my_data + num_elements_, value);
72}
73
75 strides_.resize(shape_.size());
76 std::size_t stride = 1;
77 for (int i = static_cast<int>(shape_.size()) - 1; i >= 0; --i) {
78 strides_[i] = stride;
79 stride *= shape_[i];
80 }
81}
82
83std::size_t
84DenseTensor::compute_flat_index(const std::vector<std::size_t> &indices) const {
85 if (indices.size() != shape_.size()) {
86 throw std::invalid_argument(
87 "Number of indices must match tensor dimensionality.");
88 }
89 std::size_t flat_idx = 0;
90 for (std::size_t i = 0; i < indices.size(); ++i) {
91 if (indices[i] >= shape_[i]) {
92 throw std::out_of_range("Index out of bounds for tensor dimension.");
93 }
94 flat_idx += indices[i] * strides_[i];
95 }
96 return flat_idx;
97}
98
99} // namespace bncore
Dense row-major array of doubles with a fixed shape.
double * mapped_data_
borrowed buffer; takes precedence when non-null
std::vector< std::size_t > shape_
DenseTensor(const std::vector< std::size_t > &shape)
Creates an owning tensor filled with zeros.
Definition: dense_tensor.cpp:5
double & at(const std::vector< std::size_t > &indices)
Checked element access by one index per dimension.
double * data()
Pointer to the first element: the borrowed buffer when one is bound, otherwise the owned storage.
const std::vector< std::size_t > & shape() const
Extent of each dimension, as given at construction.
double & operator[](std::size_t flat_idx)
Element access by flat row-major index.
std::vector< double > owned_data_
empty for a tensor created over caller memory
void multiply_inplace(const DenseTensor &other)
Multiplies this tensor by another, element by element.
std::size_t compute_flat_index(const std::vector< std::size_t > &indices) const
Checked conversion used by at(). Throws as documented there.
void bind_data(double *ptr)
Redirects the tensor to a caller buffer.
void fill(double value)
Sets every element to value.
std::size_t num_elements_
std::vector< std::size_t > strides_
row-major strides, same length as shape_
std::size_t size() const
Total number of elements, the product of the shape.
bncore::DenseTensor, a row-major multi-dimensional array of doubles that either owns its storage or b...