DPNP C++ backend kernel library 0.21.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
evd_batch_common.hpp
1//*****************************************************************************
2// Copyright (c) 2024, 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// - Neither the name of the copyright holder nor the names of its contributors
13// may be used to endorse or promote products derived from this software
14// without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26// THE POSSIBILITY OF SUCH DAMAGE.
27//*****************************************************************************
28
29#pragma once
30
31#include <oneapi/mkl.hpp>
32#include <pybind11/pybind11.h>
33
34#include "dpnp4pybind11.hpp"
35
36// dpnp tensor headers
37#include "utils/type_dispatch.hpp"
38
39#include "common_helpers.hpp"
40#include "evd_common_utils.hpp"
41#include "types_matrix.hpp"
42
43namespace dpnp::extensions::lapack::evd
44{
45typedef sycl::event (*evd_batch_impl_fn_ptr_t)(
46 sycl::queue &,
47 const oneapi::mkl::job,
48 const oneapi::mkl::uplo,
49 const std::int64_t,
50 const std::int64_t,
51 char *,
52 char *,
53 const std::vector<sycl::event> &);
54
55namespace dpnp_td_ns = dpnp::tensor::type_dispatch;
56namespace py = pybind11;
57
58template <typename dispatchT>
59std::pair<sycl::event, sycl::event>
60 evd_batch_func(sycl::queue &exec_q,
61 const std::int8_t jobz,
62 const std::int8_t upper_lower,
63 const dpnp::tensor::usm_ndarray &eig_vecs,
64 const dpnp::tensor::usm_ndarray &eig_vals,
65 const std::vector<sycl::event> &depends,
66 const dispatchT &evd_batch_dispatch_table)
67{
68 const int eig_vecs_nd = eig_vecs.get_ndim();
69
70 const py::ssize_t *eig_vecs_shape = eig_vecs.get_shape_raw();
71 const py::ssize_t *eig_vals_shape = eig_vals.get_shape_raw();
72
73 constexpr int expected_eig_vecs_nd = 3;
74 constexpr int expected_eig_vals_nd = 2;
75
76 common_evd_checks(exec_q, eig_vecs, eig_vals, eig_vecs_shape,
77 expected_eig_vecs_nd, expected_eig_vals_nd);
78
79 if (eig_vecs_shape[2] != eig_vals_shape[0] ||
80 eig_vecs_shape[0] != eig_vals_shape[1]) {
81 throw py::value_error(
82 "The shape of 'eig_vals' must be (batch_size, n), "
83 "where batch_size = " +
84 std::to_string(eig_vecs_shape[0]) +
85 " and n = " + std::to_string(eig_vecs_shape[1]));
86 }
87
88 // Ensure `batch_size` and `n` are non-zero, otherwise return empty events
89 if (helper::check_zeros_shape(eig_vecs_nd, eig_vecs_shape)) {
90 // nothing to do
91 return std::make_pair(sycl::event(), sycl::event());
92 }
93
94 auto array_types = dpnp_td_ns::usm_ndarray_types();
95 const int eig_vecs_type_id =
96 array_types.typenum_to_lookup_id(eig_vecs.get_typenum());
97 const int eig_vals_type_id =
98 array_types.typenum_to_lookup_id(eig_vals.get_typenum());
99
100 evd_batch_impl_fn_ptr_t evd_batch_fn =
101 evd_batch_dispatch_table[eig_vecs_type_id][eig_vals_type_id];
102 if (evd_batch_fn == nullptr) {
103 throw py::value_error(
104 "No evd_batch implementation is available for the specified data "
105 "type of the input and output arrays.");
106 }
107
108 char *eig_vecs_data = eig_vecs.get_data();
109 char *eig_vals_data = eig_vals.get_data();
110
111 const std::int64_t batch_size = eig_vecs_shape[2];
112 const std::int64_t n = eig_vecs_shape[1];
113
114 const oneapi::mkl::job jobz_val = static_cast<oneapi::mkl::job>(jobz);
115 const oneapi::mkl::uplo uplo_val =
116 static_cast<oneapi::mkl::uplo>(upper_lower);
117
118 sycl::event evd_batch_ev =
119 evd_batch_fn(exec_q, jobz_val, uplo_val, batch_size, n, eig_vecs_data,
120 eig_vals_data, depends);
121
122 sycl::event ht_ev = dpnp::utils::keep_args_alive(
123 exec_q, {eig_vecs, eig_vals}, {evd_batch_ev});
124
125 return std::make_pair(ht_ev, evd_batch_ev);
126}
127} // namespace dpnp::extensions::lapack::evd