31#include <pybind11/pybind11.h>
33#include "dpnp4pybind11.hpp"
36#include "utils/memory_overlap.hpp"
37#include "utils/output_validation.hpp"
39namespace dpnp::extensions::lapack::evd
41namespace py = pybind11;
43inline void common_evd_checks(sycl::queue &exec_q,
44 const dpnp::tensor::usm_ndarray &eig_vecs,
45 const dpnp::tensor::usm_ndarray &eig_vals,
46 const py::ssize_t *eig_vecs_shape,
47 const int expected_eig_vecs_nd,
48 const int expected_eig_vals_nd)
50 const int eig_vecs_nd = eig_vecs.get_ndim();
51 const int eig_vals_nd = eig_vals.get_ndim();
53 if (eig_vecs_nd != expected_eig_vecs_nd) {
54 throw py::value_error(
"The output eigenvectors array has ndim=" +
55 std::to_string(eig_vecs_nd) +
", but a " +
56 std::to_string(expected_eig_vecs_nd) +
57 "-dimensional array is expected.");
59 else if (eig_vals_nd != expected_eig_vals_nd) {
60 throw py::value_error(
"The output eigenvalues array has ndim=" +
61 std::to_string(eig_vals_nd) +
", but a " +
62 std::to_string(expected_eig_vals_nd) +
63 "-dimensional array is expected.");
66 if (eig_vecs_shape[0] != eig_vecs_shape[1]) {
67 throw py::value_error(
"Output array with eigenvectors must be square");
70 dpnp::tensor::validation::CheckWritable::throw_if_not_writable(eig_vecs);
71 dpnp::tensor::validation::CheckWritable::throw_if_not_writable(eig_vals);
74 if (!dpnp::utils::queues_are_compatible(exec_q, {eig_vecs, eig_vals})) {
75 throw py::value_error(
76 "Execution queue is not compatible with allocation queues");
79 auto const &overlap = dpnp::tensor::overlap::MemoryOverlap();
80 if (overlap(eig_vecs, eig_vals)) {
81 throw py::value_error(
"Arrays with eigenvectors and eigenvalues are "
82 "overlapping segments of memory");
85 const bool is_eig_vecs_f_contig = eig_vecs.is_f_contiguous();
86 const bool is_eig_vals_c_contig = eig_vals.is_c_contiguous();
87 if (!is_eig_vecs_f_contig) {
88 throw py::value_error(
89 "An array with input matrix / output eigenvectors "
90 "must be F-contiguous");
92 else if (!is_eig_vals_c_contig) {
93 throw py::value_error(
94 "An array with output eigenvalues must be C-contiguous");