Skip to content
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

add optional argument for specifying factorization strategy #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 4 additions & 5 deletions src/cholesky_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ void csc_sum_duplicates(int n_rows, int &m_nnz, int **col_ptr, int **rows, doubl
}

template <typename Float>
CholeskySolver<Float>::CholeskySolver(int n_rows, int nnz, int *ii, int *jj, double *x, MatrixType type, bool cpu) : m_n(n_rows), m_nnz(nnz), m_cpu(cpu) {

CholeskySolver<Float>::CholeskySolver(int n_rows, int nnz, int *ii, int *jj, double *x, MatrixType type, bool cpu, int strategy) : m_n(n_rows), m_nnz(nnz), m_cpu(cpu) {

// Placeholders for the CSC matrix data
int *col_ptr, *rows;
Expand Down Expand Up @@ -188,7 +187,7 @@ CholeskySolver<Float>::CholeskySolver(int n_rows, int nnz, int *ii, int *jj, dou
}

// Run the Cholesky factorization through CHOLMOD and run the analysis
factorize(col_ptr, rows, data);
factorize(col_ptr, rows, data, strategy);

if (type != MatrixType::CSC) {
free(col_ptr);
Expand All @@ -198,12 +197,12 @@ CholeskySolver<Float>::CholeskySolver(int n_rows, int nnz, int *ii, int *jj, dou
}

template <typename Float>
void CholeskySolver<Float>::factorize(int *col_ptr, int *rows, double *data) {
void CholeskySolver<Float>::factorize(int *col_ptr, int *rows, double *data, int strategy) {
cholmod_sparse *A;

cholmod_start(&m_common);

m_common.supernodal = CHOLMOD_SIMPLICIAL;
m_common.supernodal = strategy;
m_common.final_ll = 1; // compute LL' factorization instead of LDL´ (default for simplicial)
m_common.nmethods = 1;
m_common.method[0].ordering = CHOLMOD_NESDIS;
Expand Down
5 changes: 3 additions & 2 deletions src/cholesky_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class CholeskySolver {
* @param x Array of nonzero entries
* @param type The type of the matrix representation. Can be COO, CSC or CSR
* @param cpu Whether or not to run the CPU version of the solver.
* @param strategy 0: SIMPLICIAL, 1: AUTO, 2: SUPERNODAL
*/
CholeskySolver(int n_rows, int nnz, int *ii, int *jj, double *x, MatrixType type, bool cpu);
CholeskySolver(int n_rows, int nnz, int *ii, int *jj, double *x, MatrixType type, bool cpu, int strategy);

~CholeskySolver();

Expand All @@ -49,7 +50,7 @@ class CholeskySolver {
private:

// Factorize the CSC matrix using CHOLMOD
void factorize(int *col_ptr, int *rows, double *data);
void factorize(int *col_ptr, int *rows, double *data, int strategy);

// Run the analysis of a triangular matrix obtained through Cholesky
void analyze_cuda(int n_rows, int n_entries, void *csr_rows, void *csr_cols, Float *csr_data, bool lower);
Expand Down
2 changes: 2 additions & 0 deletions src/docstr.h
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Parameters
- x - The array of nonzero entries.
- type - The matrix representation type, of type MatrixType. Available types
are MatrixType.COO, MatrixType.CSC and MatrixType.CSR.
- strategy - the factorization strategy to be used by CHOLMOD. Available options are
0 (Simplicial), 1 (automatic selectiong, default), 2 (Supernodal).
)";

const char *doc_matrix_type =
Expand Down
8 changes: 5 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ void declare_cholesky(nb::module_ &m, const std::string &typestr, const char *do
nb::ndarray<int32_t, nb::shape<nb::any>, nb::c_contig> ii,
nb::ndarray<int32_t, nb::shape<nb::any>, nb::c_contig> jj,
nb::ndarray<double, nb::shape<nb::any>, nb::c_contig> x,
MatrixType type) {
MatrixType type,
int strategy) {

if (type == MatrixType::COO){
if (ii.shape(0) != jj.shape(0))
Expand Down Expand Up @@ -60,14 +61,14 @@ void declare_cholesky(nb::module_ &m, const std::string &typestr, const char *do
cuda_check(cuMemcpyAsync((CUdeviceptr) indices_b, (CUdeviceptr) jj.data(), jj.shape(0)*sizeof(int), 0));
cuda_check(cuMemcpyAsync((CUdeviceptr) data, (CUdeviceptr) x.data(), x.shape(0)*sizeof(double), 0));

new (self) Class(n_rows, x.shape(0), indices_a, indices_b, data, type, false);
new (self) Class(n_rows, x.shape(0), indices_a, indices_b, data, type, false, strategy);

free(indices_a);
free(indices_b);
free(data);
} else if (ii.device_type() == nb::device::cpu::value) {
// CPU init
new (self) Class(n_rows, x.shape(0), (int *) ii.data(), (int *) jj.data(), (double *) x.data(), type, true);
new (self) Class(n_rows, x.shape(0), (int *) ii.data(), (int *) jj.data(), (double *) x.data(), type, true, strategy);
} else
throw std::invalid_argument("Unsupported input device! Only CPU and CUDA arrays are supported.");
},
Expand All @@ -76,6 +77,7 @@ void declare_cholesky(nb::module_ &m, const std::string &typestr, const char *do
nb::arg("jj"),
nb::arg("x"),
nb::arg("type"),
nb::arg("strategy") = 1,
doc_constructor)
.def("solve", [](Class &self,
nb::ndarray<Float, nb::c_contig> b,
Expand Down
Loading