30#include <pybind11/pybind11.h> 
   31#include <sycl/sycl.hpp> 
   38#include "utils/sycl_alloc_utils.hpp" 
   40namespace dpnp::extensions::lapack::helper
 
   42namespace py = pybind11;
 
   57template <
typename intT>
 
   58inline intT round_up_mult(intT value, intT mult)
 
   60    intT q = (value + (mult - 1)) / mult;
 
   65inline bool check_zeros_shape(
int ndim, 
const py::ssize_t *shape)
 
   69    for (
int i = 0; i < ndim; ++i) {
 
   70        src_nelems *= 
static_cast<size_t>(shape[i]);
 
   72    return src_nelems == 0;
 
   76inline std::int64_t *alloc_ipiv(
const std::int64_t n, sycl::queue &exec_q)
 
   78    std::int64_t *ipiv = 
nullptr;
 
   81        ipiv = sycl::malloc_device<std::int64_t>(n, exec_q);
 
   83            throw std::runtime_error(
"Device allocation for ipiv failed");
 
   85    } 
catch (sycl::exception 
const &e) {
 
   87            dpctl::tensor::alloc_utils::sycl_free_noexcept(ipiv, exec_q);
 
   88        throw std::runtime_error(
 
   90                "Unexpected SYCL exception caught during ipiv allocation: ") +
 
  100inline std::int64_t *alloc_ipiv_batch(
const std::int64_t n,
 
  101                                      std::int64_t n_linear_streams,
 
  106    const std::int64_t padding = 256 / 
sizeof(T);
 
  110    size_t alloc_ipiv_size = round_up_mult(n_linear_streams * n, padding);
 
  112    return alloc_ipiv(alloc_ipiv_size, exec_q);
 
  117inline T *alloc_scratchpad(std::int64_t scratchpad_size, sycl::queue &exec_q)
 
  119    T *scratchpad = 
nullptr;
 
  122        if (scratchpad_size > 0) {
 
  123            scratchpad = sycl::malloc_device<T>(scratchpad_size, exec_q);
 
  125                throw std::runtime_error(
 
  126                    "Device allocation for scratchpad failed");
 
  129    } 
catch (sycl::exception 
const &e) {
 
  130        if (scratchpad != 
nullptr) {
 
  131            dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
 
  133        throw std::runtime_error(std::string(
"Unexpected SYCL exception caught " 
  134                                             "during scratchpad allocation: ") +
 
  144inline T *alloc_scratchpad_batch(std::int64_t scratchpad_size,
 
  145                                 std::int64_t n_linear_streams,
 
  150    const std::int64_t padding = 256 / 
sizeof(T);
 
  154    const size_t alloc_scratch_size =
 
  155        round_up_mult(n_linear_streams * scratchpad_size, padding);
 
  157    return alloc_scratchpad<T>(alloc_scratch_size, exec_q);