31#include <pybind11/pybind11.h>
34#include "utils/memory_overlap.hpp"
35#include "utils/output_validation.hpp"
36#include "utils/sycl_alloc_utils.hpp"
37#include "utils/type_dispatch.hpp"
39#include "common_helpers.hpp"
40#include "linalg_exceptions.hpp"
42namespace dpnp::extensions::lapack::gesv_utils
44namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
45namespace py = pybind11;
47inline void common_gesv_checks(sycl::queue &exec_q,
48 const dpctl::tensor::usm_ndarray &coeff_matrix,
49 const dpctl::tensor::usm_ndarray &dependent_vals,
50 const py::ssize_t *coeff_matrix_shape,
51 const py::ssize_t *dependent_vals_shape,
52 const int expected_coeff_matrix_ndim,
53 const int min_dependent_vals_ndim,
54 const int max_dependent_vals_ndim)
56 const int coeff_matrix_nd = coeff_matrix.get_ndim();
57 const int dependent_vals_nd = dependent_vals.get_ndim();
59 if (coeff_matrix_nd != expected_coeff_matrix_ndim) {
60 throw py::value_error(
"The coefficient matrix has ndim=" +
61 std::to_string(coeff_matrix_nd) +
", but a " +
62 std::to_string(expected_coeff_matrix_ndim) +
63 "-dimensional array is expected.");
66 if (dependent_vals_nd < min_dependent_vals_ndim ||
67 dependent_vals_nd > max_dependent_vals_ndim)
69 throw py::value_error(
"The dependent values array has ndim=" +
70 std::to_string(dependent_vals_nd) +
", but a " +
71 std::to_string(min_dependent_vals_ndim) +
72 "-dimensional or a " +
73 std::to_string(max_dependent_vals_ndim) +
74 "-dimensional array is expected.");
83 if (coeff_matrix_shape[0] != coeff_matrix_shape[1]) {
84 throw py::value_error(
"The coefficient matrix must be square,"
85 " but got a shape of (" +
86 std::to_string(coeff_matrix_shape[0]) +
", " +
87 std::to_string(coeff_matrix_shape[1]) +
").");
89 if (coeff_matrix_shape[0] != dependent_vals_shape[0]) {
90 throw py::value_error(
"The first dimension (n) of coeff_matrix and"
91 " dependent_vals must be the same, but got " +
92 std::to_string(coeff_matrix_shape[0]) +
" and " +
93 std::to_string(dependent_vals_shape[0]) +
".");
97 if (!dpctl::utils::queues_are_compatible(exec_q,
98 {coeff_matrix, dependent_vals}))
100 throw py::value_error(
101 "Execution queue is not compatible with allocation queues.");
104 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
105 if (overlap(coeff_matrix, dependent_vals)) {
106 throw py::value_error(
107 "The arrays of coefficients and dependent variables "
108 "are overlapping segments of memory.");
111 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(
114 const bool is_coeff_matrix_f_contig = coeff_matrix.is_f_contiguous();
115 if (!is_coeff_matrix_f_contig) {
116 throw py::value_error(
"The coefficient matrix "
117 "must be F-contiguous.");
120 const bool is_dependent_vals_f_contig = dependent_vals.is_f_contiguous();
121 if (!is_dependent_vals_f_contig) {
122 throw py::value_error(
"The array of dependent variables "
123 "must be F-contiguous.");
126 auto array_types = dpctl_td_ns::usm_ndarray_types();
127 const int coeff_matrix_type_id =
128 array_types.typenum_to_lookup_id(coeff_matrix.get_typenum());
129 const int dependent_vals_type_id =
130 array_types.typenum_to_lookup_id(dependent_vals.get_typenum());
132 if (coeff_matrix_type_id != dependent_vals_type_id) {
133 throw py::value_error(
"The types of the coefficient matrix and "
134 "dependent variables are mismatched.");
139inline void handle_lapack_exc(sycl::queue &exec_q,
140 const std::int64_t lda,
142 std::int64_t scratchpad_size,
145 const oneapi::mkl::lapack::exception &e,
146 std::stringstream &error_msg)
148 std::int64_t info = e.info();
150 error_msg <<
"Parameter number " << -info <<
" had an illegal value.";
152 else if (info == scratchpad_size && e.detail() != 0) {
153 error_msg <<
"Insufficient scratchpad size. Required size is at least "
158 exec_q.memcpy(&host_U, &a[(info - 1) * lda + info - 1],
sizeof(T))
161 using ThresholdType =
typename helper::value_type_of<T>::type;
163 const auto threshold =
164 std::numeric_limits<ThresholdType>::epsilon() * 100;
165 if (std::abs(host_U) < threshold) {
166 using dpctl::tensor::alloc_utils::sycl_free_noexcept;
168 if (scratchpad !=
nullptr)
169 sycl_free_noexcept(scratchpad, exec_q);
171 sycl_free_noexcept(ipiv, exec_q);
172 throw LinAlgError(
"The input coefficient matrix is singular.");
175 error_msg <<
"Unexpected MKL exception caught during gesv() "
177 << e.what() <<
"\ninfo: " << e.info();
182 <<
"Unexpected MKL exception caught during gesv() call:\nreason: "
183 << e.what() <<
"\ninfo: " << e.info();