-
Notifications
You must be signed in to change notification settings - Fork 1
feat(solver): improve solver performance using Eigen #126
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
base: main
Are you sure you want to change the base?
Changes from all commits
bf16f4b
64fbd2e
aa72d75
e754757
e6a74da
2b6a65e
c8697d4
0402e6b
bed1c7e
791716a
7b62baa
26af3cb
140c5fe
0a7b8cb
1198a31
1928e0a
e65b64d
6795bda
0e5f011
8c95cf6
3d6d2ca
62f8ae6
1cf0825
25c6f64
0f433e0
845a68e
4a04462
edd79e0
215d7ca
8d68cca
b3312d8
07bd083
88e856b
60bcbd1
a8b3e3f
d2de2b3
4ed432e
c886d36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| build/ | ||
| build/ | ||
|
|
|
Contributor
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. Double check license restrictions, might want to include the LICENSE somewhere.
Contributor
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. Probably don't need to copy the LICENSE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| #include <Eigen/Sparse> | ||
| #include <Eigen/SparseQR> | ||
| #include <vector> | ||
| #include <iostream> | ||
|
|
||
| using namespace Eigen; | ||
| using namespace std; | ||
|
|
||
| struct EigenQR { | ||
| SparseQR<SparseMatrix<double>, COLAMDOrdering<int>> qr_a; | ||
| SparseQR<SparseMatrix<double>, COLAMDOrdering<int>> qr_at; | ||
|
|
||
| int m; | ||
| int n; | ||
|
|
||
| EigenQR(int num_rows, int num_cols) : m(num_rows), n(num_cols) {} | ||
| }; | ||
|
|
||
| extern "C" { | ||
| void* eigen_create(int m, int n) { | ||
| return new EigenQR(m, n); | ||
| } | ||
|
|
||
| void* eigen_free(void* ptr) { | ||
| if (ptr) { | ||
| delete (EigenQR*) ptr; | ||
| } | ||
| } | ||
|
|
||
| int eigen_compute_qr(void* ptr, int* rows, int* cols, double* vals, int nnz) { | ||
| EigenQR* qr = (EigenQR*) ptr; | ||
|
|
||
| std::vector<Triplet<double>> triplets(nnz); | ||
|
|
||
| for (int i = 0; i < nnz; i++) { | ||
| triplets[i] = Triplet<double>(rows[i], cols[i], vals[i]); | ||
| } | ||
|
|
||
| SparseMatrix<double> A(qr->m, qr->n); | ||
| A.setFromTriplets(triplets.begin(), triplets.end()); | ||
| A.makeCompressed(); | ||
|
|
||
| qr->qr_a.compute(A); | ||
| if (qr->qr_a.info() != Eigen::Success) { | ||
| return 0; | ||
| } | ||
| qr->qr_at.compute(A.transpose()); | ||
| if (qr->qr_at.info() != Eigen::Success) { | ||
| return 0; | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
| int eigen_get_rank(void* ptr) { | ||
| if (ptr) { | ||
| EigenQR* qr = (EigenQR*) ptr; | ||
| return qr->qr_a.rank(); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void eigen_get_q_dense(void* ptr, double* output, int mode) { | ||
| EigenQR* qr = (EigenQR*) ptr; | ||
| auto& qr_mat = (mode == 0) ? qr->qr_a : qr->qr_at; | ||
| int size = (mode == 0) ? qr->m : qr-> n; | ||
|
|
||
| MatrixXd I = MatrixXd::Identity(size, size); | ||
| MatrixXd Q_dense = qr_mat.matrixQ() * I; | ||
|
|
||
| Map<MatrixXd> ret(output, size, size); | ||
| ret = Q_dense; | ||
| } | ||
|
|
||
| void eigen_get_r_dense(void* ptr, double* output, int mode) { | ||
| EigenQR* qr = (EigenQR*) ptr; | ||
| auto& qr_mat = (mode == 0) ? qr->qr_a : qr->qr_at; | ||
| int r_rows = (mode == 0) ? qr->m : qr->n; | ||
| int r_cols = (mode == 0) ? qr->n : qr->m; | ||
|
|
||
| Map<MatrixXd> ret(output, r_rows, r_cols); | ||
| ret.setZero(); | ||
|
|
||
| MatrixXd R_dense = MatrixXd(qr_mat.matrixR()); | ||
| int h = std::min((int) R_dense.rows(), r_rows); | ||
| int w = std::min((int) R_dense.cols(), r_cols); | ||
| ret.block(0, 0, h, w) = R_dense.block(0, 0, h, w); | ||
| } | ||
|
|
||
| void eigen_get_permutation(void* ptr, int* output, int mode) { | ||
| EigenQR* qr = (EigenQR*) ptr; | ||
| auto& qr_mat = (mode == 0) ? qr->qr_a : qr->qr_at; | ||
| int size = (mode == 0) ? qr->n : qr->m; | ||
|
|
||
| auto& p = qr_mat.colsPermutation(); | ||
| for (int i = 0; i < size; i++) { | ||
| output[i] = p.indices()(i); | ||
| } | ||
| } | ||
|
|
||
| //gives the summed absolute value of each null space basis vector along an index | ||
| void eigen_get_free_indices(void* ptr, double* output) { | ||
| EigenQR* qr = (EigenQR*) ptr; | ||
|
|
||
| Map<VectorXd> ret(output, qr->n); | ||
| ret.setZero(); | ||
|
|
||
| if (qr->n - qr->qr_a.rank() <= 0) { | ||
| return; | ||
| } | ||
|
|
||
| VectorXd temp = VectorXd::Zero(qr->n); | ||
|
|
||
| for (int i = qr->qr_a.rank(); i < qr->n; i++) { | ||
| temp.setZero(); | ||
| temp(i) = 1.0; | ||
|
|
||
| VectorXd col = qr->qr_at.matrixQ() * temp; | ||
| ret = ret + col.cwiseAbs(); | ||
| } | ||
| } | ||
|
|
||
| void eigen_apply_q(void* ptr, double* input, double* output, int mode_mat_factor, int mode_transpose) { | ||
| EigenQR* qr = (EigenQR*) ptr; | ||
| auto& qr_mat = (mode_mat_factor == 0) ? qr->qr_a : qr->qr_at; | ||
| int size = (mode_mat_factor == 0) ? qr->m : qr->n; | ||
|
|
||
| Map<VectorXd> in_vec(input, size); | ||
| Map<VectorXd> out_vec(output, size); | ||
|
|
||
| VectorXd temp; | ||
|
|
||
| if (mode_transpose == 0) { | ||
| temp = qr_mat.matrixQ().transpose() * in_vec; | ||
| } else { | ||
| temp = qr_mat.matrixQ() * in_vec; | ||
| } | ||
|
|
||
| out_vec = temp; | ||
| } | ||
| } |
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.
Fix hardcoded paths, make sure it works on other OSes.