DPNP C++ backend kernel library 0.20.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// dpctl tensor headers
35#include "utils/type_dispatch.hpp"
36
37#include "common_helpers.hpp"
38#include "evd_common_utils.hpp"
39#include "types_matrix.hpp"
40
41namespace dpnp::extensions::lapack::evd
42{
43typedef sycl::event (*evd_batch_impl_fn_ptr_t)(
44 sycl::queue &,
45 const oneapi::mkl::job,
46 const oneapi::mkl::uplo,
47 const std::int64_t,
48 const std::int64_t,
49 char *,
50 char *,
51 const std::vector<sycl::event> &);
52
53namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
54namespace py = pybind11;
55
56template <typename dispatchT>
57std::pair<sycl::event, sycl::event>
58 evd_batch_func(sycl::queue &exec_q,
59 const std::int8_t jobz,
60 const std::int8_t upper_lower,
61 const dpctl::tensor::usm_ndarray &eig_vecs,
62 const dpctl::tensor::usm_ndarray &eig_vals,
63 const std::vector<sycl::event> &depends,
64 const dispatchT &evd_batch_dispatch_table)
65{
66 const int eig_vecs_nd = eig_vecs.get_ndim();
67
68 const py::ssize_t *eig_vecs_shape = eig_vecs.get_shape_raw();
69 const py::ssize_t *eig_vals_shape = eig_vals.get_shape_raw();
70
71 constexpr int expected_eig_vecs_nd = 3;
72 constexpr int expected_eig_vals_nd = 2;
73
74 common_evd_checks(exec_q, eig_vecs, eig_vals, eig_vecs_shape,
75 expected_eig_vecs_nd, expected_eig_vals_nd);
76
77 if (eig_vecs_shape[2] != eig_vals_shape[0] ||
78 eig_vecs_shape[0] != eig_vals_shape[1])
79 {
80 throw py::value_error(
81 "The shape of 'eig_vals' must be (batch_size, n), "
82 "where batch_size = " +
83 std::to_string(eig_vecs_shape[0]) +
84 " and n = " + std::to_string(eig_vecs_shape[1]));
85 }
86
87 // Ensure `batch_size` and `n` are non-zero, otherwise return empty events
88 if (helper::check_zeros_shape(eig_vecs_nd, eig_vecs_shape)) {
89 // nothing to do
90 return std::make_pair(sycl::event(), sycl::event());
91 }
92
93 auto array_types = dpctl_td_ns::usm_ndarray_types();
94 const int eig_vecs_type_id =
95 array_types.typenum_to_lookup_id(eig_vecs.get_typenum());
96 const int eig_vals_type_id =
97 array_types.typenum_to_lookup_id(eig_vals.get_typenum());
98
99 evd_batch_impl_fn_ptr_t evd_batch_fn =
100 evd_batch_dispatch_table[eig_vecs_type_id][eig_vals_type_id];
101 if (evd_batch_fn == nullptr) {
102 throw py::value_error(
103 "No evd_batch implementation is available for the specified data "
104 "type of the input and output arrays.");
105 }
106
107 char *eig_vecs_data = eig_vecs.get_data();
108 char *eig_vals_data = eig_vals.get_data();
109
110 const std::int64_t batch_size = eig_vecs_shape[2];
111 const std::int64_t n = eig_vecs_shape[1];
112
113 const oneapi::mkl::job jobz_val = static_cast<oneapi::mkl::job>(jobz);
114 const oneapi::mkl::uplo uplo_val =
115 static_cast<oneapi::mkl::uplo>(upper_lower);
116
117 sycl::event evd_batch_ev =
118 evd_batch_fn(exec_q, jobz_val, uplo_val, batch_size, n, eig_vecs_data,
119 eig_vals_data, depends);
120
121 sycl::event ht_ev = dpctl::utils::keep_args_alive(
122 exec_q, {eig_vecs, eig_vals}, {evd_batch_ev});
123
124 return std::make_pair(ht_ev, evd_batch_ev);
125}
126} // namespace dpnp::extensions::lapack::evd