31#include <pybind11/pybind11.h>
34#include "utils/memory_overlap.hpp"
35#include "utils/output_validation.hpp"
37namespace dpnp::extensions::lapack::evd
39namespace py = pybind11;
41inline void common_evd_checks(sycl::queue &exec_q,
42 const dpctl::tensor::usm_ndarray &eig_vecs,
43 const dpctl::tensor::usm_ndarray &eig_vals,
44 const py::ssize_t *eig_vecs_shape,
45 const int expected_eig_vecs_nd,
46 const int expected_eig_vals_nd)
48 const int eig_vecs_nd = eig_vecs.get_ndim();
49 const int eig_vals_nd = eig_vals.get_ndim();
51 if (eig_vecs_nd != expected_eig_vecs_nd) {
52 throw py::value_error(
"The output eigenvectors array has ndim=" +
53 std::to_string(eig_vecs_nd) +
", but a " +
54 std::to_string(expected_eig_vecs_nd) +
55 "-dimensional array is expected.");
57 else if (eig_vals_nd != expected_eig_vals_nd) {
58 throw py::value_error(
"The output eigenvalues array has ndim=" +
59 std::to_string(eig_vals_nd) +
", but a " +
60 std::to_string(expected_eig_vals_nd) +
61 "-dimensional array is expected.");
64 if (eig_vecs_shape[0] != eig_vecs_shape[1]) {
65 throw py::value_error(
"Output array with eigenvectors must be square");
68 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vecs);
69 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vals);
72 if (!dpctl::utils::queues_are_compatible(exec_q, {eig_vecs, eig_vals})) {
73 throw py::value_error(
74 "Execution queue is not compatible with allocation queues");
77 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
78 if (overlap(eig_vecs, eig_vals)) {
79 throw py::value_error(
"Arrays with eigenvectors and eigenvalues are "
80 "overlapping segments of memory");
83 const bool is_eig_vecs_f_contig = eig_vecs.is_f_contiguous();
84 const bool is_eig_vals_c_contig = eig_vals.is_c_contiguous();
85 if (!is_eig_vecs_f_contig) {
86 throw py::value_error(
87 "An array with input matrix / output eigenvectors "
88 "must be F-contiguous");
90 else if (!is_eig_vals_c_contig) {
91 throw py::value_error(
92 "An array with output eigenvalues must be C-contiguous");