-
#include "tensorstore/strided_layout.h" -
template <DimensionIndex Rank,
ArrayOriginKind OriginKind,
ContainerKind CKind>
class tensorstore::StridedLayout; A
StridedLayoutspecifies the layout of a multi-dimensional array in terms of anoriginvector, ashapevector, and abyte_stridesvector, all of length equal to therankof the multi-dimensional array. Theoriginandshapevectors specify the domain of the array, and thebyte_stridesvector specifies the offset in bytes between consecutive elements in each dimension.Specifying the strides in units of bytes, rather than
sizeof(T)bytes, is consistent with NumPy, and is useful for representing an “array of structs” as a “struct of arrays” in the case thatalignof(T) <= sizeof(T).The rank may be specified at compile time using the
Ranktemplate parameter, or at run time by specifying aRankofdynamic_rank.If
OriginKind == zero_origin(the default), theoriginvector is implicitly an all-zero vector and is not stored explicitly. To use an explicitoriginvector, specifyOriginKind == offset_origin.If
CKind == container(the default), this type has value semantics with respect to the contents of theorigin,shapeandbyte_stridesvectors, and non-const instances provide mutable access to them (except that only const access to theoriginvector is provided ifOriginKind == zero_origin). If theRankis specified at compile time, these vectors are stored inline, while in the case ofdynamic_rankthey stored in a single heap allocation, making copying more expensive.If
CKind == view(or if using theStridedLayoutViewconvenience alias), this type represents a const, unowned view of a layout, and stores only pointers to theorigin,shape, andbyte_stridesvectors. In this case, the user is responsible for ensuring that theorigin,shape, andbyte_stridesvectors remain valid as long as they are referenced.Example usage:
tensorstore::StridedLayout<2> x(tensorstore::c_order, sizeof(int), {3, 4}); EXPECT_EQ(3, x.rank()); // Indexing with `operator()` requires all dimensions be specified. EXPECT_EQ((4 + 2) * sizeof(int), x(1, 2)); EXPECT_EQ((4 + 2) * sizeof(int), x({1, 2})); // Indexing with `operator[]` supports partial indexing. EXPECT_EQ(4 * sizeof(int), x[1]); EXPECT_EQ((4 + 2) * sizeof(int), x[{1, 2}]); // Create a StridedLayoutView that refers to x. tensorstore::StridedLayoutView<2> x_ref = x;- Template Parameters:¶
- DimensionIndex Rank¶
The compile time rank (must be
>= 0),dynamic_rankto specify a rank at run time, or ifCKind == container,dynamic_rank(n)forn >= 0to specify a rank at run time with inline storage for ranks<= n.- ArrayOriginKind OriginKind¶
Either
zero_origin(to indicate a constant all-zero origin vector) oroffset_origin(to allow an arbitrary origin vector).- ContainerKind CKind¶
Either
container(for value semantics) orview(for unowned view semantics).
Data members¶
- constexpr const ArrayOriginKind array_origin_kind = OriginKind;
Origin kind of the layout.
- constexpr const ContainerKind container_kind = CKind;
Indicates whether this represents a layout by value (
container) or by unowned reference (view).
-
constexpr const DimensionIndex static_rank =
RankConstraint::FromInlineRank(Rank); Rank of the layout, or
dynamic_rankif specified at run time.
Types¶
-
using RankType =
StaticOrDynamicRank<RankConstraint::FromInlineRank(Rank)>; Representation of static or dynamic rank value.
- type MaybeConstIndex;
Conditionally const-qualified element type of
shapevector.
- type MaybeConstOriginIndex;
Conditionally const-qualified element type of
originvector.
Methods¶
Constructors¶
- StridedLayout();
Constructs a default layout of rank equal to
static_rank(ifstatic_rank >= 0) or rank0(ifstatic_rank == dynamic_rank).
- explicit StridedLayout(RankType rank);
Constructs an uninitialized layout of the specified
rank.
-
explicit StridedLayout(
span<const Index, RankConstraint::FromInlineRank(Rank)> shape,
span<const Index, RankConstraint::FromInlineRank(Rank)>
byte_strides); -
explicit StridedLayout(const Index (&shape)[N],
const Index (&byte_strides)[N]); Constructs from the specified
shapeandbyte_strides.
-
explicit StridedLayout(
span<const Index, RankConstraint::FromInlineRank(Rank)> origin,
span<const Index, RankConstraint::FromInlineRank(Rank)> shape,
span<const Index, RankConstraint::FromInlineRank(Rank)>
byte_strides); -
explicit StridedLayout(const Index (&origin)[N],
const Index (&shape)[N],
const Index (&byte_strides)[N]); Constructs from the specified
origin,shapeandbyte_strides.
-
explicit StridedLayout(
BoxView<RankConstraint::FromInlineRank(Rank)> domain,
span<const Index, RankConstraint::FromInlineRank(Rank)>
byte_strides); Constructs from the specified
domainandbyte_strides.
- explicit(...) StridedLayout(const StridedLayout<R, O, C>& source);
Constructs from a layout with a compatible
static_rankandarray_origin_kind.
-
explicit StridedLayout(unchecked_t,
const StridedLayout<R, O, C>& source); - explicit StridedLayout(unchecked_t, StridedLayout&& source);
Unchecked conversion.
-
explicit StridedLayout(RankType rank,
const Index* shape,
const Index* byte_strides); -
explicit StridedLayout(RankType rank,
const Index* origin,
const Index* shape,
const Index* byte_strides); Constructs from the specified rank and origin/shape/byte_strides arrays.
-
explicit StridedLayout(
LayoutOrder order,
Index element_stride,
BoxView<RankConstraint::FromInlineRank(Rank)> domain); -
explicit StridedLayout(
LayoutOrder order,
Index element_stride,
span<const Index, RankConstraint::FromInlineRank(Rank)> shape); -
explicit StridedLayout(LayoutOrder order,
Index element_stride,
const Index (&shape)[R]); Constructs a contiguous layout with the specified domain and element stride.
Assignment¶
- StridedLayout& operator=(const StridedLayout<R, O, C>& other);
Assigns from a layout with a compatible
static_rankandarray_origin_kind.
Accessors¶
-
span<const Index, RankConstraint::FromInlineRank(Rank)>
origin() const; -
span<MaybeConstOriginIndex, RankConstraint::FromInlineRank(Rank)>
origin(); Returns the origin vector of size
rank().
-
span<const Index, RankConstraint::FromInlineRank(Rank)>
byte_strides() const; -
span<MaybeConstIndex, RankConstraint::FromInlineRank(Rank)>
byte_strides(); Returns the byte strides vector of size
rank().
- span<const Index, RankConstraint::FromInlineRank(Rank)> shape() const;
- span<MaybeConstIndex, RankConstraint::FromInlineRank(Rank)> shape();
Returns the shape vector of size
rank().
- Index origin_byte_offset() const;
Returns the byte offset of the origin.
- Index num_elements() const;
Returns the total number of elements (product of extents in
shape()).
- BoxView<RankConstraint::FromInlineRank(Rank)> domain() const;
Returns the domain of the layout.
Indexing¶
- Index operator[](const Indices& indices) const;
- Index operator[](const IndexType (&indices)[N]) const;
Returns the byte offset corresponding to a list of
N <= rank()indices into the layout.
- Index operator()(const Indices& indices) const;
- Index operator()(const Index (&indices)[N]) const;
Returns the byte offset corresponding to a list of
rank()indices into the layout.
- Index operator()(IndexType... index) const;
Returns
(*this)({index...}), or0ifindexis an empty pack.
Friend functions¶
-
friend std::ostream&
operator<<(std::ostream& os, const StridedLayout& layout); Writes a string representation to
os.
-
friend bool
operator==(const StridedLayout& a, const StridedLayout<R, O, C>& b); -
friend bool
operator!=(const StridedLayout& a, const StridedLayout<R, O, C>& b); Compares the
domainandbyte_strides.
Related Types¶
- enum class tensorstore::ContiguousLayoutOrder;
Specifies a C-order or Fortran-order contiguous array layout.
-
struct tensorstore::ContiguousLayoutPermutation<Rank>
: public span<const DimensionIndex, Rank>; Specifies a contiguous layout order as a permutation.
-
using tensorstore::StridedLayoutView<Rank, OriginKind> =
StridedLayout<Rank, OriginKind, view>; Specifies an unowned strided layout.
Related Constants¶
- constexpr bool tensorstore::IsContiguousLayoutOrder<T, Rank>;
Bool-valued metafunction that is true if
TisContiguousLayoutOrderorContiguousLayoutPermutationcompatible withRank.
- constexpr bool tensorstore::IsStridedLayout<X>;
Metafunction that checks whether a given type is convertible to StridedLayoutView.
Related Functions¶
-
void tensorstore::ComputeStrides(ContiguousLayoutOrder order,
ptrdiff_t element_stride,
span<const Index> shape,
span<Index> strides); -
void tensorstore::ComputeStrides(
ContiguousLayoutPermutation<> permutation,
ptrdiff_t element_stride,
span<const Index> shape,
span<Index> strides); Computes strides for the specified layout order.
-
Index tensorstore::IndexInnerProduct(DimensionIndex n,
const T0* a,
const T1* b); - Index tensorstore::IndexInnerProduct(const T0* a, const T1* b);
-
Index tensorstore::IndexInnerProduct(span<T0, Rank> a,
span<T1, Rank> b);
-
void tensorstore::InitializeContiguousLayout(
LayoutOrder order,
Index element_stride,
StridedLayout<Rank, OriginKind>* layout); Assigns
layout->byte_strides()to correspond to a contiguous layout that matches the existing value oflayout->shape().
-
void tensorstore::InitializeContiguousLayout(
LayoutOrder order,
Index element_stride,
BoxView<RankConstraint::FromInlineRank(Rank)> domain,
StridedLayout<Rank, offset_origin>* layout); Initializes
*layoutto a contiguous layout over the specifieddomain.
-
void tensorstore::InitializeContiguousLayout(
LayoutOrder order,
Index element_stride,
std::type_identity_t<
span<const Index, RankConstraint::FromInlineRank(Rank)>>
shape,
StridedLayout<Rank, OriginKind>* layout); Initializes
*layoutto a contiguous layout with the specifiedshape.
-
StridedLayoutView<
RankConstraint::Subtract(RankConstraint::FromInlineRank(R),
SubRank),
O>
tensorstore::GetSubLayoutView(const StridedLayout<R, O, C>& layout,
DimensionIndex sub_rank); -
StridedLayoutView<
RankConstraint::Subtract(RankConstraint::FromInlineRank(R),
SubRank),
O>
tensorstore::GetSubLayoutView(
const StridedLayout<R, O, C>& layout,
std::integral_constant<DimensionIndex, SubRank> sub_rank = {}); Returns a view with the leading
sub_rankdimensions oflayoutremoved.
-
BoxView<Rank> tensorstore::GetBoxDomainOf(
const StridedLayout<Rank, OriginKind, CKind>& layout); Implements the HasBoxDomain concept for
StridedLayout.
-
bool tensorstore::IsContiguousLayout(
const StridedLayout<Rank, OriginKind, CKind>& layout,
LayoutOrder order,
Index element_size); Checks if
layoutis a contiguous layout with the specified order and element size.
-
bool tensorstore::IsBroadcastScalar(
const StridedLayout<Rank, OriginKind, CKind>& layout); Checks if
layoutcontains at most a single distinct element.
-
Index tensorstore::GetByteExtent(
const StridedLayout<Rank, OriginKind, CKind>& layout,
Index element_size); Returns smallest number of contiguous bytes into which the specified layout fits.
Broadcasting¶
-
absl::Status
tensorstore::ValidateShapeBroadcast(span<const Index> source_shape,
span<const Index> target_shape); Validates that
source_shapecan be broadcast totarget_shape.
-
absl::Status
tensorstore::BroadcastStridedLayout(StridedLayoutView<> source,
span<const Index> target_shape,
Index* target_byte_strides); -
absl::Status
tensorstore::BroadcastStridedLayout(StridedLayoutView<> source,
span<const Index> target_shape,
StridedLayout<>& target); -
Result<Index> tensorstore::BroadcastStridedLayout(
StridedLayoutView<dynamic_rank, offset_origin> source,
BoxView<> target_domain,
StridedLayout<dynamic_rank, offset_origin>& target); Broadcasts
sourcetotarget_shape.