28#include <pybind11/pybind11.h>
31#include "utils/memory_overlap.hpp"
32#include "utils/output_validation.hpp"
33#include "utils/type_dispatch.hpp"
35namespace dpnp::extensions::lapack::evd
37namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
38namespace py = pybind11;
40template <
typename dispatchT,
41 template <
typename fnT,
typename T,
typename RealT>
43void init_evd_dispatch_table(
44 dispatchT evd_dispatch_table[][dpctl_td_ns::num_types])
46 dpctl_td_ns::DispatchTableBuilder<dispatchT, factoryT,
47 dpctl_td_ns::num_types>
49 contig.populate_dispatch_table(evd_dispatch_table);
52inline void common_evd_checks(sycl::queue &exec_q,
53 const dpctl::tensor::usm_ndarray &eig_vecs,
54 const dpctl::tensor::usm_ndarray &eig_vals,
55 const py::ssize_t *eig_vecs_shape,
56 const int expected_eig_vecs_nd,
57 const int expected_eig_vals_nd)
59 const int eig_vecs_nd = eig_vecs.get_ndim();
60 const int eig_vals_nd = eig_vals.get_ndim();
62 if (eig_vecs_nd != expected_eig_vecs_nd) {
63 throw py::value_error(
"The output eigenvectors array has ndim=" +
64 std::to_string(eig_vecs_nd) +
", but a " +
65 std::to_string(expected_eig_vecs_nd) +
66 "-dimensional array is expected.");
68 else if (eig_vals_nd != expected_eig_vals_nd) {
69 throw py::value_error(
"The output eigenvalues array has ndim=" +
70 std::to_string(eig_vals_nd) +
", but a " +
71 std::to_string(expected_eig_vals_nd) +
72 "-dimensional array is expected.");
75 if (eig_vecs_shape[0] != eig_vecs_shape[1]) {
76 throw py::value_error(
"Output array with eigenvectors must be square");
79 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vecs);
80 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vals);
83 if (!dpctl::utils::queues_are_compatible(exec_q, {eig_vecs, eig_vals})) {
84 throw py::value_error(
85 "Execution queue is not compatible with allocation queues");
88 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
89 if (overlap(eig_vecs, eig_vals)) {
90 throw py::value_error(
"Arrays with eigenvectors and eigenvalues are "
91 "overlapping segments of memory");
94 const bool is_eig_vecs_f_contig = eig_vecs.is_f_contiguous();
95 const bool is_eig_vals_c_contig = eig_vals.is_c_contiguous();
96 if (!is_eig_vecs_f_contig) {
97 throw py::value_error(
98 "An array with input matrix / output eigenvectors "
99 "must be F-contiguous");
101 else if (!is_eig_vals_c_contig) {
102 throw py::value_error(
103 "An array with output eigenvalues must be C-contiguous");