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) {
68 throw py::value_error(
"The dependent values array has ndim=" +
69 std::to_string(dependent_vals_nd) +
", but a " +
70 std::to_string(min_dependent_vals_ndim) +
71 "-dimensional or a " +
72 std::to_string(max_dependent_vals_ndim) +
73 "-dimensional array is expected.");
82 if (coeff_matrix_shape[0] != coeff_matrix_shape[1]) {
83 throw py::value_error(
"The coefficient matrix must be square,"
84 " but got a shape of (" +
85 std::to_string(coeff_matrix_shape[0]) +
", " +
86 std::to_string(coeff_matrix_shape[1]) +
").");
88 if (coeff_matrix_shape[0] != dependent_vals_shape[0]) {
89 throw py::value_error(
"The first dimension (n) of coeff_matrix and"
90 " dependent_vals must be the same, but got " +
91 std::to_string(coeff_matrix_shape[0]) +
" and " +
92 std::to_string(dependent_vals_shape[0]) +
".");
96 if (!dpctl::utils::queues_are_compatible(exec_q,
97 {coeff_matrix, dependent_vals})) {
98 throw py::value_error(
99 "Execution queue is not compatible with allocation queues.");
102 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
103 if (overlap(coeff_matrix, dependent_vals)) {
104 throw py::value_error(
105 "The arrays of coefficients and dependent variables "
106 "are overlapping segments of memory.");
109 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(
112 const bool is_coeff_matrix_f_contig = coeff_matrix.is_f_contiguous();
113 if (!is_coeff_matrix_f_contig) {
114 throw py::value_error(
"The coefficient matrix "
115 "must be F-contiguous.");
118 const bool is_dependent_vals_f_contig = dependent_vals.is_f_contiguous();
119 if (!is_dependent_vals_f_contig) {
120 throw py::value_error(
"The array of dependent variables "
121 "must be F-contiguous.");
124 auto array_types = dpctl_td_ns::usm_ndarray_types();
125 const int coeff_matrix_type_id =
126 array_types.typenum_to_lookup_id(coeff_matrix.get_typenum());
127 const int dependent_vals_type_id =
128 array_types.typenum_to_lookup_id(dependent_vals.get_typenum());
130 if (coeff_matrix_type_id != dependent_vals_type_id) {
131 throw py::value_error(
"The types of the coefficient matrix and "
132 "dependent variables are mismatched.");
137inline void handle_lapack_exc(sycl::queue &exec_q,
138 const std::int64_t lda,
140 std::int64_t scratchpad_size,
143 const oneapi::mkl::lapack::exception &e,
144 std::stringstream &error_msg)
146 std::int64_t info = e.info();
148 error_msg <<
"Parameter number " << -info <<
" had an illegal value.";
150 else if (info == scratchpad_size && e.detail() != 0) {
151 error_msg <<
"Insufficient scratchpad size. Required size is at least "
156 exec_q.memcpy(&host_U, &a[(info - 1) * lda + info - 1],
sizeof(T))
159 using ThresholdType =
typename helper::value_type_of<T>::type;
161 const auto threshold =
162 std::numeric_limits<ThresholdType>::epsilon() * 100;
163 if (std::abs(host_U) < threshold) {
164 using dpctl::tensor::alloc_utils::sycl_free_noexcept;
166 if (scratchpad !=
nullptr)
167 sycl_free_noexcept(scratchpad, exec_q);
169 sycl_free_noexcept(ipiv, exec_q);
170 throw LinAlgError(
"The input coefficient matrix is singular.");
173 error_msg <<
"Unexpected MKL exception caught during gesv() "
175 << e.what() <<
"\ninfo: " << e.info();
180 <<
"Unexpected MKL exception caught during gesv() call:\nreason: "
181 << e.what() <<
"\ninfo: " << e.info();