33#include <pybind11/pybind11.h>
34#include <sycl/sycl.hpp>
41#include "utils/sycl_alloc_utils.hpp"
43namespace dpnp::extensions::lapack::helper
45namespace py = pybind11;
60template <
typename intT>
61inline intT round_up_mult(intT value, intT mult)
63 intT q = (value + (mult - 1)) / mult;
68inline bool check_zeros_shape(
int ndim,
const py::ssize_t *shape)
72 for (
int i = 0; i < ndim; ++i) {
73 src_nelems *=
static_cast<size_t>(shape[i]);
75 return src_nelems == 0;
79inline std::int64_t *alloc_ipiv(
const std::int64_t n, sycl::queue &exec_q)
81 std::int64_t *ipiv =
nullptr;
84 ipiv = sycl::malloc_device<std::int64_t>(n, exec_q);
86 throw std::runtime_error(
"Device allocation for ipiv failed");
88 }
catch (sycl::exception
const &e) {
90 dpctl::tensor::alloc_utils::sycl_free_noexcept(ipiv, exec_q);
91 throw std::runtime_error(
93 "Unexpected SYCL exception caught during ipiv allocation: ") +
103inline std::int64_t *alloc_ipiv_batch(
const std::int64_t n,
104 std::int64_t n_linear_streams,
109 const std::int64_t padding = 256 /
sizeof(T);
113 size_t alloc_ipiv_size = round_up_mult(n_linear_streams * n, padding);
115 return alloc_ipiv(alloc_ipiv_size, exec_q);
120inline T *alloc_scratchpad(std::int64_t scratchpad_size, sycl::queue &exec_q)
122 T *scratchpad =
nullptr;
125 if (scratchpad_size > 0) {
126 scratchpad = sycl::malloc_device<T>(scratchpad_size, exec_q);
128 throw std::runtime_error(
129 "Device allocation for scratchpad failed");
132 }
catch (sycl::exception
const &e) {
133 if (scratchpad !=
nullptr) {
134 dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
136 throw std::runtime_error(std::string(
"Unexpected SYCL exception caught "
137 "during scratchpad allocation: ") +
147inline T *alloc_scratchpad_batch(std::int64_t scratchpad_size,
148 std::int64_t n_linear_streams,
153 const std::int64_t padding = 256 /
sizeof(T);
157 const size_t alloc_scratch_size =
158 round_up_mult(n_linear_streams * scratchpad_size, padding);
160 return alloc_scratchpad<T>(alloc_scratch_size, exec_q);