-
Notifications
You must be signed in to change notification settings - Fork 16
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
CHOLMOD Wrapper Integration #9
Open
Pranav-Jain
wants to merge
5
commits into
polyfem:main
Choose a base branch
from
Pranav-Jain:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include "CHOLMODSolver.hpp" | ||
#include <Eigen/CholmodSupport> | ||
#include <iostream> | ||
|
||
namespace polysolve | ||
{ | ||
|
||
cholmod_sparse eigen2cholmod(StiffnessMatrixL &mat) | ||
{ | ||
cholmod_sparse res; | ||
res.nzmax = mat.nonZeros(); | ||
res.nrow = mat.rows(); | ||
res.ncol = mat.cols(); | ||
res.p = mat.outerIndexPtr(); | ||
res.i = mat.innerIndexPtr(); | ||
res.x = mat.valuePtr(); | ||
res.z = 0; | ||
res.sorted = 1; | ||
if(mat.isCompressed()) | ||
{ | ||
res.packed = 1; | ||
res.nz = 0; | ||
} | ||
else | ||
{ | ||
res.packed = 0; | ||
res.nz = mat.innerNonZeroPtr(); | ||
} | ||
|
||
res.dtype = CHOLMOD_DOUBLE; | ||
|
||
res.itype = CHOLMOD_LONG; | ||
res.stype = 1; | ||
res.xtype = CHOLMOD_REAL; | ||
|
||
return res; | ||
} | ||
|
||
cholmod_dense eigen2cholmod(Eigen::VectorXd &mat) | ||
{ | ||
cholmod_dense res; | ||
res.nrow = mat.size(); | ||
res.ncol = 1; | ||
res.nzmax = res.nrow * res.ncol; | ||
res.d = mat.derived().size(); | ||
res.x = (void*)(mat.derived().data()); | ||
res.z = 0; | ||
res.xtype = CHOLMOD_REAL; | ||
res.dtype = 0; | ||
|
||
return res; | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
CHOLMODSolver::CHOLMODSolver() | ||
{ | ||
cm = (cholmod_common*)malloc(sizeof(cholmod_common)); | ||
cholmod_l_start (cm); | ||
L = NULL; | ||
|
||
cm->useGPU = 1; | ||
cm->maxGpuMemBytes = 2e9; | ||
cm->print = 4; | ||
cm->supernodal = CHOLMOD_SUPERNODAL; | ||
} | ||
|
||
// void CHOLMODSolver::getInfo(json ¶ms) const | ||
// { | ||
// params["num_iterations"] = num_iterations; | ||
// params["final_res_norm"] = final_res_norm; | ||
// } | ||
|
||
void CHOLMODSolver::analyzePattern(StiffnessMatrixL &Ain) | ||
{ | ||
if(!Ain.isCompressed()) { | ||
Ain.makeCompressed(); | ||
} | ||
A = eigen2cholmod(Ain); | ||
L = cholmod_l_analyze (&A, cm); | ||
} | ||
|
||
void CHOLMODSolver::factorize(StiffnessMatrixL &Ain) | ||
{ | ||
cholmod_l_factorize (&A, L, cm); | ||
} | ||
|
||
void CHOLMODSolver::solve(Eigen::VectorXd &rhs, Eigen::VectorXd &result) | ||
{ | ||
b = eigen2cholmod(rhs); | ||
x = cholmod_l_solve (CHOLMOD_A, L, &b, cm); | ||
|
||
memcpy(result.data(), x->x, result.size() * sizeof(result[0])); | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
CHOLMODSolver::~CHOLMODSolver() | ||
{ | ||
cholmod_l_gpu_stats(cm); | ||
cholmod_l_free_factor (&L, cm); | ||
cholmod_l_free_dense (&x, cm); | ||
} | ||
|
||
} // namespace polysolve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#pragma once | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include <Eigen/Dense> | ||
#include <Eigen/Sparse> | ||
#include "cholmod.h" | ||
#include <memory> | ||
|
||
#include <nlohmann/json.hpp> | ||
using json = nlohmann::json; | ||
|
||
// #define POLYSOLVE_DELETE_MOVE_COPY(Base) \ | ||
// Base(Base &&) = delete; \ | ||
// Base &operator=(Base &&) = delete; \ | ||
// Base(const Base &) = delete; \ | ||
// Base &operator=(const Base &) = delete; | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can delete these comments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
// TODO: | ||
// - [ ] Support both RowMajor + ColumnMajor sparse matrices | ||
// - [ ] Wrapper around MUMPS | ||
// - [ ] Wrapper around other iterative solvers (AMGCL, ViennaCL, etc.) | ||
// - [ ] Document the json parameters for each | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
namespace polysolve | ||
{ | ||
typedef Eigen::SparseMatrix<double, Eigen::ColMajor, long int> StiffnessMatrixL; | ||
/** | ||
@brief Base class for cholmod solver. | ||
*/ | ||
class CHOLMODSolver | ||
{ | ||
|
||
// public: | ||
// Shortcut alias | ||
// typedef Eigen::VectorXd VectorXd; | ||
// template <typename T> | ||
// using Ref = Eigen::Ref<T>; | ||
|
||
// public: | ||
////////////////// | ||
// Constructors // | ||
////////////////// | ||
|
||
// Virtual destructor | ||
|
||
|
||
// Static constructor | ||
// | ||
// @param[in] solver Solver type | ||
// @param[in] precond Preconditioner for iterative solvers | ||
// | ||
// static std::unique_ptr<LinearSolver> create(const std::string &solver, const std::string &precond); | ||
|
||
// List available solvers | ||
// static std::vector<std::string> availableSolvers(); | ||
// static std::string defaultSolver(); | ||
|
||
// List available preconditioners | ||
// static std::vector<std::string> availablePrecond(); | ||
// static std::string defaultPrecond(); | ||
|
||
protected: | ||
|
||
cholmod_common *cm; | ||
cholmod_sparse A; | ||
cholmod_dense *x, b; | ||
cholmod_factor *L; | ||
|
||
public: | ||
CHOLMODSolver(); | ||
~CHOLMODSolver(); | ||
|
||
// Set solver parameters | ||
// virtual void setParameters(const json ¶ms) {} | ||
|
||
// Get info on the last solve step | ||
void getInfo(json ¶ms) const; | ||
|
||
// Analyze sparsity pattern | ||
void analyzePattern(StiffnessMatrixL &Ain); | ||
|
||
// Factorize system matrix | ||
void factorize(StiffnessMatrixL &Ain); | ||
|
||
// | ||
// @brief { Solve the linear system Ax = b } | ||
// | ||
// @param[in] b { Right-hand side. } | ||
// @param[in,out] x { Unknown to compute. When using an iterative | ||
// solver, the input unknown vector is used as an | ||
// initial guess, and must thus be properly allocated | ||
// and initialized. } | ||
// | ||
void solve(Eigen::VectorXd &rhs, Eigen::VectorXd &result); | ||
}; | ||
|
||
} // namespace polysolve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might need to resize result before this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.