31#include <pybind11/pybind11.h>
33#include "dpnp4pybind11.hpp"
36#include "utils/memory_overlap.hpp"
37#include "utils/output_validation.hpp"
38#include "utils/sycl_alloc_utils.hpp"
39#include "utils/type_dispatch.hpp"
41#include "common_helpers.hpp"
42#include "linalg_exceptions.hpp"
44namespace dpnp::extensions::lapack::gesv_utils
46namespace dpnp_td_ns = dpnp::tensor::type_dispatch;
47namespace py = pybind11;
49inline void common_gesv_checks(sycl::queue &exec_q,
50 const dpnp::tensor::usm_ndarray &coeff_matrix,
51 const dpnp::tensor::usm_ndarray &dependent_vals,
52 const py::ssize_t *coeff_matrix_shape,
53 const py::ssize_t *dependent_vals_shape,
54 const int expected_coeff_matrix_ndim,
55 const int min_dependent_vals_ndim,
56 const int max_dependent_vals_ndim)
58 const int coeff_matrix_nd = coeff_matrix.get_ndim();
59 const int dependent_vals_nd = dependent_vals.get_ndim();
61 if (coeff_matrix_nd != expected_coeff_matrix_ndim) {
62 throw py::value_error(
"The coefficient matrix has ndim=" +
63 std::to_string(coeff_matrix_nd) +
", but a " +
64 std::to_string(expected_coeff_matrix_ndim) +
65 "-dimensional array is expected.");
68 if (dependent_vals_nd < min_dependent_vals_ndim ||
69 dependent_vals_nd > max_dependent_vals_ndim) {
70 throw py::value_error(
"The dependent values array has ndim=" +
71 std::to_string(dependent_vals_nd) +
", but a " +
72 std::to_string(min_dependent_vals_ndim) +
73 "-dimensional or a " +
74 std::to_string(max_dependent_vals_ndim) +
75 "-dimensional array is expected.");
84 if (coeff_matrix_shape[0] != coeff_matrix_shape[1]) {
85 throw py::value_error(
"The coefficient matrix must be square,"
86 " but got a shape of (" +
87 std::to_string(coeff_matrix_shape[0]) +
", " +
88 std::to_string(coeff_matrix_shape[1]) +
").");
90 if (coeff_matrix_shape[0] != dependent_vals_shape[0]) {
91 throw py::value_error(
"The first dimension (n) of coeff_matrix and"
92 " dependent_vals must be the same, but got " +
93 std::to_string(coeff_matrix_shape[0]) +
" and " +
94 std::to_string(dependent_vals_shape[0]) +
".");
98 if (!dpnp::utils::queues_are_compatible(exec_q,
99 {coeff_matrix, dependent_vals})) {
100 throw py::value_error(
101 "Execution queue is not compatible with allocation queues.");
104 auto const &overlap = dpnp::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 dpnp::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 = dpnp_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 dpnp::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();