Skip to content

Update lapack syevd/heevd implementations #1891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions dpnp/backend/extensions/lapack/evd_common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//*****************************************************************************
// Copyright (c) 2024, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#pragma once

#include <oneapi/mkl.hpp>
#include <pybind11/pybind11.h>

// dpctl tensor headers
#include "utils/memory_overlap.hpp"
#include "utils/output_validation.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"

#include "types_matrix.hpp"

namespace dpnp
{
namespace backend
{
namespace ext
{
namespace lapack
{
namespace evd
{
typedef sycl::event (*evd_impl_fn_ptr_t)(sycl::queue &,
const oneapi::mkl::job,
const oneapi::mkl::uplo,
const std::int64_t,
char *,
char *,
std::vector<sycl::event> &,
const std::vector<sycl::event> &);

namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
namespace py = pybind11;

template <typename dispatchT>
std::pair<sycl::event, sycl::event>
evd_func(sycl::queue &exec_q,
const std::int8_t jobz,
const std::int8_t upper_lower,
dpctl::tensor::usm_ndarray &eig_vecs,
dpctl::tensor::usm_ndarray &eig_vals,
const std::vector<sycl::event> &depends,
const dispatchT &evd_dispatch_table)
{
const int eig_vecs_nd = eig_vecs.get_ndim();
const int eig_vals_nd = eig_vals.get_ndim();

if (eig_vecs_nd != 2) {
throw py::value_error("Unexpected ndim=" + std::to_string(eig_vecs_nd) +
" of an output array with eigenvectors");
}
else if (eig_vals_nd != 1) {
throw py::value_error("Unexpected ndim=" + std::to_string(eig_vals_nd) +
" of an output array with eigenvalues");
}

const py::ssize_t *eig_vecs_shape = eig_vecs.get_shape_raw();
const py::ssize_t *eig_vals_shape = eig_vals.get_shape_raw();

if (eig_vecs_shape[0] != eig_vecs_shape[1]) {
throw py::value_error("Output array with eigenvectors with be square");
}
else if (eig_vecs_shape[0] != eig_vals_shape[0]) {
throw py::value_error(
"Eigenvectors and eigenvalues have different shapes");
}

size_t src_nelems(1);

for (int i = 0; i < eig_vecs_nd; ++i) {
src_nelems *= static_cast<size_t>(eig_vecs_shape[i]);
}

if (src_nelems == 0) {
// nothing to do
return std::make_pair(sycl::event(), sycl::event());
}

dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vecs);
dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vals);

// check compatibility of execution queue and allocation queue
if (!dpctl::utils::queues_are_compatible(exec_q, {eig_vecs, eig_vals})) {
throw py::value_error(
"Execution queue is not compatible with allocation queues");
}

auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
if (overlap(eig_vecs, eig_vals)) {
throw py::value_error("Arrays with eigenvectors and eigenvalues are "
"overlapping segments of memory");
}

bool is_eig_vecs_f_contig = eig_vecs.is_f_contiguous();
bool is_eig_vals_c_contig = eig_vals.is_c_contiguous();
if (!is_eig_vecs_f_contig) {
throw py::value_error(
"An array with input matrix / output eigenvectors "
"must be F-contiguous");
}
else if (!is_eig_vals_c_contig) {
throw py::value_error(
"An array with output eigenvalues must be C-contiguous");
}

auto array_types = dpctl_td_ns::usm_ndarray_types();
int eig_vecs_type_id =
array_types.typenum_to_lookup_id(eig_vecs.get_typenum());
int eig_vals_type_id =
array_types.typenum_to_lookup_id(eig_vals.get_typenum());

evd_impl_fn_ptr_t evd_fn =
evd_dispatch_table[eig_vecs_type_id][eig_vals_type_id];
if (evd_fn == nullptr) {
throw py::value_error(
"Types of input vectors and result array are mismatched.");
}

char *eig_vecs_data = eig_vecs.get_data();
char *eig_vals_data = eig_vals.get_data();

const std::int64_t n = eig_vecs_shape[0];
const oneapi::mkl::job jobz_val = static_cast<oneapi::mkl::job>(jobz);
const oneapi::mkl::uplo uplo_val =
static_cast<oneapi::mkl::uplo>(upper_lower);

std::vector<sycl::event> host_task_events;
sycl::event evd_ev = evd_fn(exec_q, jobz_val, uplo_val, n, eig_vecs_data,
eig_vals_data, host_task_events, depends);

sycl::event args_ev = dpctl::utils::keep_args_alive(
exec_q, {eig_vecs, eig_vals}, host_task_events);

return std::make_pair(args_ev, evd_ev);
}

template <typename dispatchT,
template <typename fnT, typename T, typename RealT>
typename factoryT>
void init_evd_dispatch_table(
dispatchT evd_dispatch_table[][dpctl_td_ns::num_types])
{
dpctl_td_ns::DispatchTableBuilder<dispatchT, factoryT,
dpctl_td_ns::num_types>
contig;
contig.populate_dispatch_table(evd_dispatch_table);
}
} // namespace evd
} // namespace lapack
} // namespace ext
} // namespace backend
} // namespace dpnp
157 changes: 31 additions & 126 deletions dpnp/backend/extensions/lapack/heevd.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//*****************************************************************************
// Copyright (c) 2023-2024, Intel Corporation
// Copyright (c) 2024, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand All @@ -23,16 +23,7 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <pybind11/pybind11.h>

// dpctl tensor headers
#include "utils/memory_overlap.hpp"
#include "utils/type_utils.hpp"

#include "heevd.hpp"
#include "types_matrix.hpp"

#include "dpnp_utils.hpp"

namespace dpnp
{
Expand All @@ -43,23 +34,10 @@ namespace ext
namespace lapack
{
namespace mkl_lapack = oneapi::mkl::lapack;
namespace py = pybind11;
namespace type_utils = dpctl::tensor::type_utils;

typedef sycl::event (*heevd_impl_fn_ptr_t)(sycl::queue,
const oneapi::mkl::job,
const oneapi::mkl::uplo,
const std::int64_t,
char *,
char *,
std::vector<sycl::event> &,
const std::vector<sycl::event> &);

static heevd_impl_fn_ptr_t heevd_dispatch_table[dpctl_td_ns::num_types]
[dpctl_td_ns::num_types];

template <typename T, typename RealT>
static sycl::event heevd_impl(sycl::queue exec_q,
static sycl::event heevd_impl(sycl::queue &exec_q,
const oneapi::mkl::job jobz,
const oneapi::mkl::uplo upper_lower,
const std::int64_t n,
Expand Down Expand Up @@ -128,104 +106,8 @@ static sycl::event heevd_impl(sycl::queue exec_q,
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
});
host_task_events.push_back(clean_up_event);
return heevd_event;
}

std::pair<sycl::event, sycl::event>
heevd(sycl::queue exec_q,
const std::int8_t jobz,
const std::int8_t upper_lower,
dpctl::tensor::usm_ndarray eig_vecs,
dpctl::tensor::usm_ndarray eig_vals,
const std::vector<sycl::event> &depends)
{
const int eig_vecs_nd = eig_vecs.get_ndim();
const int eig_vals_nd = eig_vals.get_ndim();

if (eig_vecs_nd != 2) {
throw py::value_error("Unexpected ndim=" + std::to_string(eig_vecs_nd) +
" of an output array with eigenvectors");
}
else if (eig_vals_nd != 1) {
throw py::value_error("Unexpected ndim=" + std::to_string(eig_vals_nd) +
" of an output array with eigenvalues");
}

const py::ssize_t *eig_vecs_shape = eig_vecs.get_shape_raw();
const py::ssize_t *eig_vals_shape = eig_vals.get_shape_raw();

if (eig_vecs_shape[0] != eig_vecs_shape[1]) {
throw py::value_error("Output array with eigenvectors with be square");
}
else if (eig_vecs_shape[0] != eig_vals_shape[0]) {
throw py::value_error(
"Eigenvectors and eigenvalues have different shapes");
}

size_t src_nelems(1);

for (int i = 0; i < eig_vecs_nd; ++i) {
src_nelems *= static_cast<size_t>(eig_vecs_shape[i]);
}

if (src_nelems == 0) {
// nothing to do
return std::make_pair(sycl::event(), sycl::event());
}

// check compatibility of execution queue and allocation queue
if (!dpctl::utils::queues_are_compatible(exec_q, {eig_vecs, eig_vals})) {
throw py::value_error(
"Execution queue is not compatible with allocation queues");
}

auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
if (overlap(eig_vecs, eig_vals)) {
throw py::value_error("Arrays with eigenvectors and eigenvalues are "
"overlapping segments of memory");
}

bool is_eig_vecs_f_contig = eig_vecs.is_f_contiguous();
bool is_eig_vals_c_contig = eig_vals.is_c_contiguous();
if (!is_eig_vecs_f_contig) {
throw py::value_error(
"An array with input matrix / output eigenvectors "
"must be F-contiguous");
}
else if (!is_eig_vals_c_contig) {
throw py::value_error(
"An array with output eigenvalues must be C-contiguous");
}

auto array_types = dpctl_td_ns::usm_ndarray_types();
int eig_vecs_type_id =
array_types.typenum_to_lookup_id(eig_vecs.get_typenum());
int eig_vals_type_id =
array_types.typenum_to_lookup_id(eig_vals.get_typenum());

heevd_impl_fn_ptr_t heevd_fn =
heevd_dispatch_table[eig_vecs_type_id][eig_vals_type_id];
if (heevd_fn == nullptr) {
throw py::value_error("No heevd implementation defined for a pair of "
"type for eigenvectors and eigenvalues");
}

char *eig_vecs_data = eig_vecs.get_data();
char *eig_vals_data = eig_vals.get_data();

const std::int64_t n = eig_vecs_shape[0];
const oneapi::mkl::job jobz_val = static_cast<oneapi::mkl::job>(jobz);
const oneapi::mkl::uplo uplo_val =
static_cast<oneapi::mkl::uplo>(upper_lower);

std::vector<sycl::event> host_task_events;
sycl::event heevd_ev =
heevd_fn(exec_q, jobz_val, uplo_val, n, eig_vecs_data, eig_vals_data,
host_task_events, depends);

sycl::event args_ev = dpctl::utils::keep_args_alive(
exec_q, {eig_vecs, eig_vals}, host_task_events);
return std::make_pair(args_ev, heevd_ev);
return heevd_event;
}

template <typename fnT, typename T, typename RealT>
Expand All @@ -243,12 +125,35 @@ struct HeevdContigFactory
}
};

void init_heevd_dispatch_table(void)
using evd::evd_impl_fn_ptr_t;

void init_heevd(py::module_ m)
{
dpctl_td_ns::DispatchTableBuilder<heevd_impl_fn_ptr_t, HeevdContigFactory,
dpctl_td_ns::num_types>
contig;
contig.populate_dispatch_table(heevd_dispatch_table);
using arrayT = dpctl::tensor::usm_ndarray;
using event_vecT = std::vector<sycl::event>;

static evd_impl_fn_ptr_t heevd_dispatch_table[dpctl_td_ns::num_types]
[dpctl_td_ns::num_types];

{
evd::init_evd_dispatch_table<evd_impl_fn_ptr_t, HeevdContigFactory>(
heevd_dispatch_table);

auto heevd_pyapi = [&](sycl::queue &exec_q, const std::int8_t jobz,
const std::int8_t upper_lower, arrayT &eig_vecs,
arrayT &eig_vals,
const event_vecT &depends = {}) {
return evd::evd_func(exec_q, jobz, upper_lower, eig_vecs, eig_vals,
depends, heevd_dispatch_table);
};

m.def("_heevd", heevd_pyapi,
"Call `heevd` from OneMKL LAPACK library to return "
"the eigenvalues and eigenvectors of a complex Hermitian matrix",
py::arg("sycl_queue"), py::arg("jobz"), py::arg("upper_lower"),
py::arg("eig_vecs"), py::arg("eig_vals"),
py::arg("depends") = py::list());
}
}
} // namespace lapack
} // namespace ext
Expand Down
Loading
Loading