← Documentation
ADS-IDAC Engine 5.0.0
dense_tensor.hpp
Go to the documentation of this file.
1#pragma once
9#include <algorithm>
10#include <cstdint>
11#include <numeric>
12#include <stdexcept>
13#include <vector>
14
15namespace bncore {
16
25public:
29 explicit DenseTensor(const std::vector<std::size_t> &shape);
30 // Borrows memory avoiding system allocation locks
37 DenseTensor(const std::vector<std::size_t> &shape, double *preallocated_ptr);
38
40 [[nodiscard]] const std::vector<std::size_t> &shape() const;
42 [[nodiscard]] std::size_t size() const;
43
46 [[nodiscard]] double &operator[](std::size_t flat_idx);
49 [[nodiscard]] double operator[](std::size_t flat_idx) const;
50
56 [[nodiscard]] double &at(const std::vector<std::size_t> &indices);
62 [[nodiscard]] double at(const std::vector<std::size_t> &indices) const;
63
66 double *data();
68 const double *data() const;
69
76 void bind_data(double *ptr);
77
81 void multiply_inplace(const DenseTensor &other);
83 void fill(double value);
84
85private:
86 std::vector<std::size_t> shape_;
87 std::vector<std::size_t> strides_;
88
89 std::vector<double> owned_data_;
90 double *mapped_data_ = nullptr;
91 std::size_t num_elements_;
92
93 void compute_strides();
95 [[nodiscard]] std::size_t
96 compute_flat_index(const std::vector<std::size_t> &indices) const;
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.