Skip to content

[cleanup/tensor, part 4] Made contract_iter and contract_iter2 functions templated to avoid needing to include pybind11 headers #933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions dpctl/tensor/libtensor/include/utils/strided_iters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,16 +499,16 @@ int simplify_iteration_two_strides(const int nd,
return nd_;
}

using vecT = std::vector<py::ssize_t>;
std::tuple<vecT, vecT, py::size_t> contract_iter(vecT shape, vecT strides)
template <typename T, class Error, typename vecT = std::vector<T>>
std::tuple<vecT, vecT, T> contract_iter(vecT shape, vecT strides)
{
const size_t dim = shape.size();
if (dim != strides.size()) {
throw py::value_error("Shape and strides must be of equal size.");
throw Error("Shape and strides must be of equal size.");
}
vecT out_shape = shape;
vecT out_strides = strides;
py::ssize_t disp(0);
T disp(0);

int nd = simplify_iteration_stride(dim, out_shape.data(),
out_strides.data(), disp);
Expand All @@ -517,18 +517,19 @@ std::tuple<vecT, vecT, py::size_t> contract_iter(vecT shape, vecT strides)
return std::make_tuple(out_shape, out_strides, disp);
}

std::tuple<vecT, vecT, py::size_t, vecT, py::ssize_t>
template <typename T, class Error, typename vecT = std::vector<T>>
std::tuple<vecT, vecT, T, vecT, T>
contract_iter2(vecT shape, vecT strides1, vecT strides2)
{
const size_t dim = shape.size();
if (dim != strides1.size() || dim != strides2.size()) {
throw py::value_error("Shape and strides must be of equal size.");
throw Error("Shape and strides must be of equal size.");
}
vecT out_shape = shape;
vecT out_strides1 = strides1;
vecT out_strides2 = strides2;
py::ssize_t disp1(0);
py::ssize_t disp2(0);
T disp1(0);
T disp2(0);

int nd = simplify_iteration_two_strides(dim, out_shape.data(),
out_strides1.data(),
Expand Down
4 changes: 2 additions & 2 deletions dpctl/tensor/libtensor/source/tensor_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ PYBIND11_MODULE(_tensor_impl, m)
import_dpctl();

m.def(
"_contract_iter", &contract_iter,
"_contract_iter", &contract_iter<py::ssize_t, py::value_error>,
"Simplifies iteration of array of given shape & stride. Returns "
"a triple: shape, stride and offset for the new iterator of possible "
"smaller dimension, which traverses the same elements as the original "
Expand All @@ -1464,7 +1464,7 @@ PYBIND11_MODULE(_tensor_impl, m)
py::arg("depends") = py::list());

m.def(
"_contract_iter2", &contract_iter2,
"_contract_iter2", &contract_iter2<py::ssize_t, py::value_error>,
"Simplifies iteration over elements of pair of arrays of given shape "
"with strides stride1 and stride2. Returns "
"a 5-tuple: shape, stride and offset for the new iterator of possible "
Expand Down