Skip to content

Commit

Permalink
Fixing spectral APIs (#496)
Browse files Browse the repository at this point in the history
Further cleanup of the spectral APIs.

Authors:
  - Corey J. Nolet (https://github.com/cjnolet)

Approvers:
  - Chuck Hastings (https://github.com/ChuckHastings)

URL: #496
  • Loading branch information
cjnolet authored Feb 9, 2022
1 parent 2ebf89c commit 9b0208b
Show file tree
Hide file tree
Showing 18 changed files with 769 additions and 180 deletions.
12 changes: 5 additions & 7 deletions cpp/include/raft/cluster/detail/kmeans.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#include <raft/device_atomics.cuh>
#include <raft/handle.hpp>
#include <raft/linalg/detail/cublas_wrappers.hpp>
#include <raft/spectral/detail/matrix_wrappers.cuh>
#include <raft/spectral/detail/warn_dbg.hpp>
#include <raft/spectral/matrix_wrappers.hpp>

namespace raft {
namespace cluster {
Expand Down Expand Up @@ -948,8 +948,6 @@ int kmeans(handle_t const& handle,
index_type_t& iters,
unsigned long long seed = 123456)
{
using namespace matrix;

// Check that parameters are valid
RAFT_EXPECTS(n > 0, "invalid parameter (n<1)");
RAFT_EXPECTS(d > 0, "invalid parameter (d<1)");
Expand All @@ -958,10 +956,10 @@ int kmeans(handle_t const& handle,
RAFT_EXPECTS(maxiter >= 0, "invalid parameter (maxiter<0)");

// Allocate memory
vector_t<index_type_t> clusterSizes(handle, k);
vector_t<value_type_t> centroids(handle, d * k);
vector_t<value_type_t> work(handle, n * max(k, d));
vector_t<index_type_t> work_int(handle, 2 * d * n);
raft::spectral::matrix::vector_t<index_type_t> clusterSizes(handle, k);
raft::spectral::matrix::vector_t<value_type_t> centroids(handle, d * k);
raft::spectral::matrix::vector_t<value_type_t> work(handle, n * max(k, d));
raft::spectral::matrix::vector_t<index_type_t> work_int(handle, 2 * d * n);

// Perform k-means
return kmeans<index_type_t, value_type_t>(handle,
Expand Down
144 changes: 67 additions & 77 deletions cpp/include/raft/linalg/detail/lanczos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@
#include <raft/cudart_utils.h>
#include <raft/handle.hpp>
#include <raft/spectral/detail/lapack.hpp>
#include <raft/spectral/detail/matrix_wrappers.cuh>
#include <raft/spectral/detail/warn_dbg.hpp>
#include <raft/spectral/matrix_wrappers.hpp>

namespace raft {

using namespace matrix;
using namespace linalg::detail;

namespace spectral {
namespace linalg {
namespace detail {

// curandGeneratorNormalX
Expand Down Expand Up @@ -87,7 +83,7 @@ inline curandStatus_t curandGenerateNormalX(
*/
template <typename index_type_t, typename value_type_t>
int performLanczosIteration(handle_t const& handle,
sparse_matrix_t<index_type_t, value_type_t> const* A,
spectral::matrix::sparse_matrix_t<index_type_t, value_type_t> const* A,
index_type_t* iter,
index_type_t maxIter,
value_type_t shift,
Expand Down Expand Up @@ -696,11 +692,6 @@ static int lanczosRestart(handle_t const& handle,
return 0;
}

} // namespace detail
} // namespace spectral

namespace detail {

/**
* @brief Compute smallest eigenvectors of symmetric matrix
* Computes eigenvalues and eigenvectors that are least
Expand Down Expand Up @@ -751,26 +742,25 @@ namespace detail {
* @return error flag.
*/
template <typename index_type_t, typename value_type_t>
int computeSmallestEigenvectors(handle_t const& handle,
sparse_matrix_t<index_type_t, value_type_t> const* A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t* effIter,
index_type_t* totalIter,
value_type_t* shift,
value_type_t* __restrict__ alpha_host,
value_type_t* __restrict__ beta_host,
value_type_t* __restrict__ lanczosVecs_dev,
value_type_t* __restrict__ work_dev,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed)
int computeSmallestEigenvectors(
handle_t const& handle,
spectral::matrix::sparse_matrix_t<index_type_t, value_type_t> const* A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t* effIter,
index_type_t* totalIter,
value_type_t* shift,
value_type_t* __restrict__ alpha_host,
value_type_t* __restrict__ beta_host,
value_type_t* __restrict__ lanczosVecs_dev,
value_type_t* __restrict__ work_dev,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed)
{
using namespace raft::spectral::detail;

// Useful constants
constexpr value_type_t one = 1;
constexpr value_type_t zero = 0;
Expand Down Expand Up @@ -993,20 +983,19 @@ int computeSmallestEigenvectors(handle_t const& handle,
}

template <typename index_type_t, typename value_type_t>
int computeSmallestEigenvectors(handle_t const& handle,
sparse_matrix_t<index_type_t, value_type_t> const& A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t& iter,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed = 1234567)
int computeSmallestEigenvectors(
handle_t const& handle,
spectral::matrix::sparse_matrix_t<index_type_t, value_type_t> const& A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t& iter,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed = 1234567)
{
using namespace raft::spectral::detail;

// Matrix dimension
index_type_t n = A.nrows_;

Expand All @@ -1024,8 +1013,8 @@ int computeSmallestEigenvectors(handle_t const& handle,
value_type_t* alpha_host = alpha_host_v.data();
value_type_t* beta_host = beta_host_v.data();

vector_t<value_type_t> lanczosVecs_dev(handle, n * (restartIter + 1));
vector_t<value_type_t> work_dev(handle, (n + restartIter) * restartIter);
spectral::matrix::vector_t<value_type_t> lanczosVecs_dev(handle, n * (restartIter + 1));
spectral::matrix::vector_t<value_type_t> work_dev(handle, (n + restartIter) * restartIter);

// Perform Lanczos method
index_type_t effIter;
Expand Down Expand Up @@ -1097,25 +1086,24 @@ int computeSmallestEigenvectors(handle_t const& handle,
* @return error flag.
*/
template <typename index_type_t, typename value_type_t>
int computeLargestEigenvectors(handle_t const& handle,
sparse_matrix_t<index_type_t, value_type_t> const* A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t* effIter,
index_type_t* totalIter,
value_type_t* __restrict__ alpha_host,
value_type_t* __restrict__ beta_host,
value_type_t* __restrict__ lanczosVecs_dev,
value_type_t* __restrict__ work_dev,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed)
int computeLargestEigenvectors(
handle_t const& handle,
spectral::matrix::sparse_matrix_t<index_type_t, value_type_t> const* A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t* effIter,
index_type_t* totalIter,
value_type_t* __restrict__ alpha_host,
value_type_t* __restrict__ beta_host,
value_type_t* __restrict__ lanczosVecs_dev,
value_type_t* __restrict__ work_dev,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed)
{
using namespace raft::spectral::detail;

// Useful constants
constexpr value_type_t one = 1;
constexpr value_type_t zero = 0;
Expand Down Expand Up @@ -1342,17 +1330,18 @@ int computeLargestEigenvectors(handle_t const& handle,
}

template <typename index_type_t, typename value_type_t>
int computeLargestEigenvectors(handle_t const& handle,
sparse_matrix_t<index_type_t, value_type_t> const& A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t& iter,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed = 123456)
int computeLargestEigenvectors(
handle_t const& handle,
spectral::matrix::sparse_matrix_t<index_type_t, value_type_t> const& A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t& iter,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed = 123456)
{
// Matrix dimension
index_type_t n = A.nrows_;
Expand All @@ -1371,8 +1360,8 @@ int computeLargestEigenvectors(handle_t const& handle,
value_type_t* alpha_host = alpha_host_v.data();
value_type_t* beta_host = beta_host_v.data();

vector_t<value_type_t> lanczosVecs_dev(handle, n * (restartIter + 1));
vector_t<value_type_t> work_dev(handle, (n + restartIter) * restartIter);
spectral::matrix::vector_t<value_type_t> lanczosVecs_dev(handle, n * (restartIter + 1));
spectral::matrix::vector_t<value_type_t> work_dev(handle, (n + restartIter) * restartIter);

// Perform Lanczos method
index_type_t effIter;
Expand All @@ -1398,4 +1387,5 @@ int computeLargestEigenvectors(handle_t const& handle,
}

} // namespace detail
} // namespace linalg
} // namespace raft
49 changes: 27 additions & 22 deletions cpp/include/raft/linalg/lanczos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#pragma once

#include "detail/lanczos.hpp"
#include <raft/spectral/matrix_wrappers.hpp>

namespace raft {
namespace linalg {

// =========================================================
// Eigensolver
Expand Down Expand Up @@ -62,17 +64,18 @@ namespace raft {
* @return error flag.
*/
template <typename index_type_t, typename value_type_t>
int computeSmallestEigenvectors(handle_t const& handle,
sparse_matrix_t<index_type_t, value_type_t> const& A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t& iter,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed = 1234567)
int computeSmallestEigenvectors(
handle_t const& handle,
spectral::matrix::sparse_matrix_t<index_type_t, value_type_t> const& A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t& iter,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed = 1234567)
{
return detail::computeSmallestEigenvectors(handle,
A,
Expand Down Expand Up @@ -125,17 +128,18 @@ int computeSmallestEigenvectors(handle_t const& handle,
* @return error flag.
*/
template <typename index_type_t, typename value_type_t>
int computeLargestEigenvectors(handle_t const& handle,
sparse_matrix_t<index_type_t, value_type_t> const& A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t& iter,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed = 123456)
int computeLargestEigenvectors(
handle_t const& handle,
spectral::matrix::sparse_matrix_t<index_type_t, value_type_t> const& A,
index_type_t nEigVecs,
index_type_t maxIter,
index_type_t restartIter,
value_type_t tol,
bool reorthogonalize,
index_type_t& iter,
value_type_t* __restrict__ eigVals_dev,
value_type_t* __restrict__ eigVecs_dev,
unsigned long long seed = 123456)
{
return detail::computeLargestEigenvectors(handle,
A,
Expand All @@ -150,4 +154,5 @@ int computeLargestEigenvectors(handle_t const& handle,
seed);
}

} // namespace linalg
} // namespace raft
Loading

0 comments on commit 9b0208b

Please sign in to comment.