Skip to content

Commit

Permalink
Merge pull request #1534 from IntelPython/make-some-offset-classes-co…
Browse files Browse the repository at this point in the history
…nstexpr

Make some offset indexer classes constexpr
  • Loading branch information
oleksandr-pavlyk authored Feb 7, 2024
2 parents 71b1640 + 2c7609a commit ad98b19
Showing 1 changed file with 53 additions and 48 deletions.
101 changes: 53 additions & 48 deletions dpctl/tensor/libtensor/include/utils/offset_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,15 @@ device_allocate_and_pack(sycl::queue &q,

struct NoOpIndexer
{
size_t operator()(size_t gid) const
constexpr NoOpIndexer() {}
constexpr size_t operator()(size_t gid) const
{
return gid;
}
};

using dpctl::tensor::ssize_t;

/* @brief Indexer with shape and strides arrays of same size are packed */
struct StridedIndexer
{
Expand Down Expand Up @@ -204,7 +207,7 @@ struct UnpackedStridedIndexer
using dpctl::tensor::strides::CIndexer_vector;

CIndexer_vector _ind(nd);
py::ssize_t relative_offset(0);
ssize_t relative_offset(0);
_ind.get_displacement<const ssize_t *, const ssize_t *>(
gid,
shape, // shape ptr
Expand Down Expand Up @@ -253,18 +256,18 @@ struct Strided1DCyclicIndexer

template <typename displacementT> struct TwoOffsets
{
TwoOffsets() : first_offset(0), second_offset(0) {}
TwoOffsets(const displacementT &first_offset_,
const displacementT &second_offset_)
constexpr TwoOffsets() : first_offset(0), second_offset(0) {}
constexpr TwoOffsets(const displacementT &first_offset_,
const displacementT &second_offset_)
: first_offset(first_offset_), second_offset(second_offset_)
{
}

displacementT get_first_offset() const
constexpr displacementT get_first_offset() const
{
return first_offset;
}
displacementT get_second_offset() const
constexpr displacementT get_second_offset() const
{
return second_offset;
}
Expand Down Expand Up @@ -323,9 +326,9 @@ struct TwoOffsets_StridedIndexer

struct TwoZeroOffsets_Indexer
{
TwoZeroOffsets_Indexer() {}
constexpr TwoZeroOffsets_Indexer() {}

TwoOffsets<ssize_t> operator()(ssize_t) const
constexpr TwoOffsets<ssize_t> operator()(ssize_t) const
{
return TwoOffsets<ssize_t>();
}
Expand All @@ -339,39 +342,41 @@ struct TwoOffsets_CombinedIndexer
SecondIndexerT second_indexer_;

public:
TwoOffsets_CombinedIndexer(const FirstIndexerT &first_indexer,
const SecondIndexerT &second_indexer)
constexpr TwoOffsets_CombinedIndexer(const FirstIndexerT &first_indexer,
const SecondIndexerT &second_indexer)
: first_indexer_(first_indexer), second_indexer_(second_indexer)
{
}

TwoOffsets<py::ssize_t> operator()(py::ssize_t gid) const
constexpr TwoOffsets<ssize_t> operator()(ssize_t gid) const
{
return TwoOffsets<py::ssize_t>(first_indexer_(gid),
second_indexer_(gid));
return TwoOffsets<ssize_t>(first_indexer_(gid), second_indexer_(gid));
}
};

template <typename displacementT> struct ThreeOffsets
{
ThreeOffsets() : first_offset(0), second_offset(0), third_offset(0) {}
ThreeOffsets(const displacementT &first_offset_,
const displacementT &second_offset_,
const displacementT &third_offset_)
constexpr ThreeOffsets()
: first_offset(0), second_offset(0), third_offset(0)
{
}
constexpr ThreeOffsets(const displacementT &first_offset_,
const displacementT &second_offset_,
const displacementT &third_offset_)
: first_offset(first_offset_), second_offset(second_offset_),
third_offset(third_offset_)
{
}

displacementT get_first_offset() const
constexpr displacementT get_first_offset() const
{
return first_offset;
}
displacementT get_second_offset() const
constexpr displacementT get_second_offset() const
{
return second_offset;
}
displacementT get_third_offset() const
constexpr displacementT get_third_offset() const
{
return third_offset;
}
Expand Down Expand Up @@ -438,11 +443,11 @@ struct ThreeOffsets_StridedIndexer

struct ThreeZeroOffsets_Indexer
{
ThreeZeroOffsets_Indexer() {}
constexpr ThreeZeroOffsets_Indexer() {}

ThreeOffsets<py::ssize_t> operator()(py::ssize_t) const
constexpr ThreeOffsets<ssize_t> operator()(ssize_t) const
{
return ThreeOffsets<py::ssize_t>();
return ThreeOffsets<ssize_t>();
}
};

Expand All @@ -457,15 +462,15 @@ struct ThreeOffsets_CombinedIndexer
ThirdIndexerT third_indexer_;

public:
ThreeOffsets_CombinedIndexer(const FirstIndexerT &first_indexer,
const SecondIndexerT &second_indexer,
const ThirdIndexerT &third_indexer)
constexpr ThreeOffsets_CombinedIndexer(const FirstIndexerT &first_indexer,
const SecondIndexerT &second_indexer,
const ThirdIndexerT &third_indexer)
: first_indexer_(first_indexer), second_indexer_(second_indexer),
third_indexer_(third_indexer)
{
}

ThreeOffsets<ssize_t> operator()(ssize_t gid) const
constexpr ThreeOffsets<ssize_t> operator()(ssize_t gid) const
{
return ThreeOffsets<ssize_t>(first_indexer_(gid), second_indexer_(gid),
third_indexer_(gid));
Expand All @@ -474,32 +479,32 @@ struct ThreeOffsets_CombinedIndexer

template <typename displacementT> struct FourOffsets
{
FourOffsets()
constexpr FourOffsets()
: first_offset(0), second_offset(0), third_offset(0), fourth_offset(0)
{
}
FourOffsets(const displacementT &first_offset_,
const displacementT &second_offset_,
const displacementT &third_offset_,
const displacementT &fourth_offset_)
constexpr FourOffsets(const displacementT &first_offset_,
const displacementT &second_offset_,
const displacementT &third_offset_,
const displacementT &fourth_offset_)
: first_offset(first_offset_), second_offset(second_offset_),
third_offset(third_offset_), fourth_offset(fourth_offset_)
{
}

displacementT get_first_offset() const
constexpr displacementT get_first_offset() const
{
return first_offset;
}
displacementT get_second_offset() const
constexpr displacementT get_second_offset() const
{
return second_offset;
}
displacementT get_third_offset() const
constexpr displacementT get_third_offset() const
{
return third_offset;
}
displacementT get_fourth_offset() const
constexpr displacementT get_fourth_offset() const
{
return fourth_offset;
}
Expand All @@ -513,12 +518,12 @@ template <typename displacementT> struct FourOffsets

struct FourOffsets_StridedIndexer
{
FourOffsets_StridedIndexer(int common_nd,
ssize_t first_offset_,
ssize_t second_offset_,
ssize_t third_offset_,
ssize_t fourth_offset_,
ssize_t const *_packed_shape_strides)
constexpr FourOffsets_StridedIndexer(int common_nd,
ssize_t first_offset_,
ssize_t second_offset_,
ssize_t third_offset_,
ssize_t fourth_offset_,
ssize_t const *_packed_shape_strides)
: nd(common_nd), starting_first_offset(first_offset_),
starting_second_offset(second_offset_),
starting_third_offset(third_offset_),
Expand All @@ -527,12 +532,12 @@ struct FourOffsets_StridedIndexer
{
}

FourOffsets<ssize_t> operator()(ssize_t gid) const
constexpr FourOffsets<ssize_t> operator()(ssize_t gid) const
{
return compute_offsets(gid);
}

FourOffsets<ssize_t> operator()(size_t gid) const
constexpr FourOffsets<ssize_t> operator()(size_t gid) const
{
return compute_offsets(static_cast<ssize_t>(gid));
}
Expand All @@ -545,7 +550,7 @@ struct FourOffsets_StridedIndexer
ssize_t starting_fourth_offset;
ssize_t const *shape_strides;

FourOffsets<py::ssize_t> compute_offsets(ssize_t gid) const
FourOffsets<ssize_t> compute_offsets(ssize_t gid) const
{
using dpctl::tensor::strides::CIndexer_vector;

Expand Down Expand Up @@ -573,9 +578,9 @@ struct FourOffsets_StridedIndexer

struct FourZeroOffsets_Indexer
{
FourZeroOffsets_Indexer() {}
constexpr FourZeroOffsets_Indexer() {}

FourOffsets<ssize_t> operator()(ssize_t) const
constexpr FourOffsets<ssize_t> operator()(ssize_t) const
{
return FourOffsets<ssize_t>();
}
Expand Down

0 comments on commit ad98b19

Please sign in to comment.