DPNP C++ backend kernel library 0.19.0dev6
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
evd_common_utils.hpp
1//*****************************************************************************
2// Copyright (c) 2024-2025, Intel Corporation
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7// - Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// - Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23// THE POSSIBILITY OF SUCH DAMAGE.
24//*****************************************************************************
25
26#pragma once
27
28#include <pybind11/pybind11.h>
29
30// dpctl tensor headers
31#include "utils/memory_overlap.hpp"
32#include "utils/output_validation.hpp"
33
34namespace dpnp::extensions::lapack::evd
35{
36namespace py = pybind11;
37
38inline void common_evd_checks(sycl::queue &exec_q,
39 const dpctl::tensor::usm_ndarray &eig_vecs,
40 const dpctl::tensor::usm_ndarray &eig_vals,
41 const py::ssize_t *eig_vecs_shape,
42 const int expected_eig_vecs_nd,
43 const int expected_eig_vals_nd)
44{
45 const int eig_vecs_nd = eig_vecs.get_ndim();
46 const int eig_vals_nd = eig_vals.get_ndim();
47
48 if (eig_vecs_nd != expected_eig_vecs_nd) {
49 throw py::value_error("The output eigenvectors array has ndim=" +
50 std::to_string(eig_vecs_nd) + ", but a " +
51 std::to_string(expected_eig_vecs_nd) +
52 "-dimensional array is expected.");
53 }
54 else if (eig_vals_nd != expected_eig_vals_nd) {
55 throw py::value_error("The output eigenvalues array has ndim=" +
56 std::to_string(eig_vals_nd) + ", but a " +
57 std::to_string(expected_eig_vals_nd) +
58 "-dimensional array is expected.");
59 }
60
61 if (eig_vecs_shape[0] != eig_vecs_shape[1]) {
62 throw py::value_error("Output array with eigenvectors must be square");
63 }
64
65 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vecs);
66 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vals);
67
68 // check compatibility of execution queue and allocation queue
69 if (!dpctl::utils::queues_are_compatible(exec_q, {eig_vecs, eig_vals})) {
70 throw py::value_error(
71 "Execution queue is not compatible with allocation queues");
72 }
73
74 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
75 if (overlap(eig_vecs, eig_vals)) {
76 throw py::value_error("Arrays with eigenvectors and eigenvalues are "
77 "overlapping segments of memory");
78 }
79
80 const bool is_eig_vecs_f_contig = eig_vecs.is_f_contiguous();
81 const bool is_eig_vals_c_contig = eig_vals.is_c_contiguous();
82 if (!is_eig_vecs_f_contig) {
83 throw py::value_error(
84 "An array with input matrix / output eigenvectors "
85 "must be F-contiguous");
86 }
87 else if (!is_eig_vals_c_contig) {
88 throw py::value_error(
89 "An array with output eigenvalues must be C-contiguous");
90 }
91}
92} // namespace dpnp::extensions::lapack::evd