14namespace Experimental {
30 if (shape.size() == 0)
49 const auto size = shape.size();
51 if (layout == MemoryLayout::RowMajor) {
52 for (std::size_t i = 0; i < size; i++) {
54 strides[size - 1 - i] = 1;
56 strides[size - 1 - i] = strides[size - 1 - i + 1] * shape[size - 1 - i + 1];
59 }
else if (layout == MemoryLayout::ColumnMajor) {
60 for (std::size_t i = 0; i < size; i++) {
64 strides[i] = strides[i - 1] * shape[i - 1];
69 ss <<
"Memory layout type is not valid for calculating strides.";
70 throw std::runtime_error(ss.str());
83 const auto size = shape.size();
87 for (std::size_t i = 0; i < size; i++) {
88 indices[i] =
int(
r / strides[i]);
98template <
typename U,
typename V>
101 std::size_t globalIndex = 0;
102 const auto size = idx.size();
103 for (std::size_t i = 0; i < size; i++) {
104 globalIndex += strides[size - 1 - i] * idx[size - 1 - i];
110template <
class... Ts>
114template <
class T0,
class... Ts>
115struct and_types<T0, Ts...> : std::integral_constant<bool, T0() && and_types<Ts...>()> {
130 const std::vector<std::size_t> &mins,
const std::vector<std::size_t> &maxs,
131 std::vector<std::size_t> idx, std::size_t active)
133 const auto size = idx.size();
134 for (std::size_t i = mins[active]; i < maxs[active]; i++) {
136 if (active == size - 1) {
138 for (std::size_t j = 0; j < size; j++) {
139 idxThere[j] -= mins[j];
141 there(idxThere) = here(idx);
161template <
typename V,
typename C = std::vector<V>>
217 fData = &(*container->begin());
263 class Iterator :
public std::iterator<std::random_access_iterator_tag, Value_t> {
268 using difference_type =
typename std::iterator<std::random_access_iterator_tag, Value_t>::difference_type;
311template <
typename Value_t,
typename Container_t>
316 std::stringstream ss;
317 ss <<
"Cannot reshape tensor with size " <<
fSize <<
" into shape { ";
318 for (std::size_t i = 0; i < shape.size(); i++) {
319 if (i != shape.size() - 1) {
320 ss << shape[i] <<
", ";
322 ss << shape[i] <<
" }.";
325 throw std::runtime_error(ss.str());
338template <
typename Value_t,
typename Container_t>
342 return fData[globalIndex];
348template <
typename Value_t,
typename Container_t>
352 return fData[globalIndex];
358template <
typename Value_t,
typename Container_t>
359template <
typename... Idx>
363 "Indices are not convertible to std::size_t.");
364 return operator()({
static_cast<std::size_t
>(idx)...});
370template <
typename Value_t,
typename Container_t>
371template <
typename... Idx>
375 "Indices are not convertible to std::size_t.");
376 return operator()({
static_cast<std::size_t
>(idx)...});
384template <
typename Value_t,
typename Container_t>
389 if (fLayout == MemoryLayout::RowMajor) {
390 layout = MemoryLayout::ColumnMajor;
391 }
else if (fLayout == MemoryLayout::ColumnMajor) {
392 layout = MemoryLayout::RowMajor;
394 throw std::runtime_error(
"Memory layout is not known.");
401 std::reverse(
x.fShape.begin(),
x.fShape.end());
404 std::reverse(
x.fStrides.begin(),
x.fStrides.end());
412template <
typename Value_t,
typename Container_t>
418 for (std::size_t i = 0; i < fShape.size(); i++) {
419 if (fShape[i] != 1) {
420 shape.emplace_back(fShape[i]);
421 strides.emplace_back(fStrides[i]);
428 if (shape.size() == 0 && fShape.size() != 0) {
429 shape.emplace_back(1);
430 strides.emplace_back(1);
436 x.fStrides = strides;
444template <
typename Value_t,
typename Container_t>
448 const int len = fShape.size();
450 auto strides = fStrides;
452 if (len + idx + 1 < 0) {
453 throw std::runtime_error(
"Given negative index is invalid.");
455 shape.insert(shape.end() + 1 + idx, 1);
456 strides.insert(strides.begin() + 1 + idx, 1);
459 throw std::runtime_error(
"Given index is invalid.");
461 shape.insert(shape.begin() + idx, 1);
462 strides.insert(strides.begin() + idx, 1);
468 x.fStrides = strides;
476template <
typename Value_t,
typename Container_t>
481 x.ReshapeInplace(shape);
489template <
typename Value_t,
typename Container_t>
493 const auto sliceSize = slice.size();
494 const auto shapeSize = fShape.size();
495 if (sliceSize != shapeSize) {
496 std::stringstream ss;
497 ss <<
"Size of slice (" << sliceSize <<
") is unequal number of dimensions (" << shapeSize <<
").";
498 throw std::runtime_error(ss.str());
513 for (std::size_t i = 0; i < sliceSize; i++) {
514 shape[i] = slice[i][1] - slice[i][0];
521 for (std::size_t i = 0; i < sliceSize; i++) {
522 idx[i] = slice[i][0];
542template <
typename Value_t,
typename Container_t>
549 const auto mins =
Shape_t(fShape.size());
550 const auto maxs = fShape;
564 const auto shapeSize =
x.GetShape().size();
565 if (shapeSize == 1) {
567 const auto size =
x.GetSize();
568 for (std::size_t i = 0; i < size; i++) {
574 }
else if (shapeSize == 2) {
576 const auto shape =
x.GetShape();
577 for (std::size_t i = 0; i < shape[0]; i++) {
579 for (std::size_t j = 0; j < shape[1]; j++) {
581 if (j < shape[1] - 1) {
591 os <<
"{ printing not yet implemented for this rank }";
603 std::stringstream ss;
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
TRObject operator()(const T1 &t1) const
bool operator>=(const Iterator &rhs) const
bool operator==(const Iterator &rhs) const
Iterator(RTensor< Value_t, Container_t > &x, typename Index_t::value_type idx)
bool operator!=(const Iterator &rhs) const
difference_type operator-(const Iterator &rhs)
Iterator operator+(difference_type rhs) const
bool operator<(const Iterator &rhs) const
Index_t::value_type fGlobalIndex
Iterator & operator+=(difference_type rhs)
Iterator & operator-=(difference_type rhs)
typename std::iterator< std::random_access_iterator_tag, Value_t >::difference_type difference_type
RTensor< Value_t, Container_t > & fTensor
bool operator>(const Iterator &rhs) const
Iterator operator-(difference_type rhs) const
Index_t::value_type GetGlobalIndex() const
bool operator<=(const Iterator &rhs) const
RTensor is a container with contiguous memory and shape information.
void ReshapeInplace(const Shape_t &shape)
Reshape tensor in place.
RTensor< Value_t, Container_t > Squeeze() const
Squeeze dimensions.
const std::shared_ptr< Container_t > GetContainer() const
Value_t & operator()(const Index_t &idx)
Access elements.
RTensor(Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor owning data initialized with new container.
MemoryLayout GetMemoryLayout() const
std::vector< Shape_t > Slice_t
RTensor(Value_t *data, Shape_t shape, Shape_t strides, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor as view on data.
RTensor< Value_t, Container_t > ExpandDims(int idx) const
Expand dimensions.
RTensor< Value_t, Container_t > Transpose() const
Transpose.
std::shared_ptr< Container_t > GetContainer()
Value_t & operator()(Idx... idx)
Access elements.
std::shared_ptr< Container_t > fContainer
RTensor(std::shared_ptr< Container_t > container, Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor owning externally provided data.
RTensor< Value_t, Container_t > Copy(MemoryLayout layout=MemoryLayout::RowMajor) const
Copy RTensor to new object.
const Shape_t & GetStrides() const
std::size_t GetSize() const
RTensor< Value_t, Container_t > Reshape(const Shape_t &shape) const
Reshape tensor.
RTensor(Value_t *data, Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor as view on data.
RTensor< Value_t, Container_t > Slice(const Slice_t &slice)
Create a slice of the tensor.
const Value_t * GetData() const
Iterator begin() noexcept
const Shape_t & GetShape() const
std::vector< std::size_t > Shape_t
void RecursiveCopy(const T &here, T &there, const std::vector< std::size_t > &mins, const std::vector< std::size_t > &maxs, std::vector< std::size_t > idx, std::size_t active)
Copy slice of a tensor recursively from here to there.
std::vector< std::size_t > ComputeStridesFromShape(const T &shape, MemoryLayout layout)
Compute strides from shape vector.
T ComputeIndicesFromGlobalIndex(const T &shape, MemoryLayout layout, const typename T::value_type idx)
Compute indices from global index.
std::size_t GetSizeFromShape(const T &shape)
Get size of tensor from shape vector.
std::size_t ComputeGlobalIndex(const U &strides, const V &idx)
Compute global index from indices.
MemoryLayout
Memory layout type (copy from RTensor.hxx)
create variable transformations
Type checking for all types of a parameter pack, e.g., used in combination with std::is_convertible.