Conversation
shakedregev
left a comment
There was a problem hiding this comment.
Still need to test this. Looks good except for naming inconsistencies.
| IdxT rows_size_; | ||
| IdxT columns_size_; | ||
| bool sorted_; | ||
| std::vector<IdxT> map2csr_; |
There was a problem hiding this comment.
Change this to follow naming conventions globally.
Use _ and to instead of 2
| const IdxT nnz_dup = static_cast<IdxT>(row_indices_.size()); | ||
|
|
||
| // Sort the entries while preserving the mapping | ||
| std::vector<IdxT> map2sorted(nnz_dup); |
|
|
||
| // Deduplicate entries by summing values | ||
| std::vector<IdxT> row_ptrs(rows_size_ + 1, 0); | ||
| std::vector<IdxT> map2dedup(nnz_dup); |
| for (size_t i = 0; i < ordervec.size(); i++) | ||
| { | ||
| // permutation swap | ||
| while (ordervec[i] != ordervec[ordervec[i]]) |
GridKit/Solver/Dynamic/Ida.cpp
Outdated
| int Ida<ScalarT, IdxT>::CsrJac(RealT t, RealT cj, N_Vector yy, N_Vector yp, N_Vector, SUNMatrix J, void* user_data, N_Vector, N_Vector, N_Vector) | ||
| { | ||
| GridKit::Model::Evaluator<ScalarT, IdxT>* model = static_cast<GridKit::Model::Evaluator<ScalarT, IdxT>*>(user_data); | ||
|
|
||
| model->updateTime(t, cj); | ||
| copyVec(yy, model->y()); | ||
| copyVec(yp, model->yp()); | ||
|
|
||
| model->evaluateJacobian(); | ||
|
|
||
| using CsrMatrix = GridKit::LinearAlgebra::CsrMatrix<RealT, IdxT>; | ||
| const CsrMatrix* Jac = model->getCsrJacobian(); | ||
|
|
||
| SUNMatZero(J); | ||
|
|
||
| sunindextype* sun_rptr = SUNSparseMatrix_IndexPointers(J); | ||
| sunindextype* sun_cind = SUNSparseMatrix_IndexValues(J); | ||
| RealT* sun_vals = SUNSparseMatrix_Data(J); | ||
|
|
||
| IdxT n = Jac->getRows(); | ||
| IdxT nnz = Jac->getNnz(); | ||
|
|
||
| // Get reference to the jacobian entries | ||
| const IdxT* rptr = Jac->getRowPtr(); | ||
| const IdxT* cind = Jac->getColInd(); | ||
| const RealT* vals = Jac->getValues(); | ||
|
|
||
| // Copy data from model jac to sundials | ||
| std::copy(rptr, rptr + n + 1, sun_rptr); | ||
| std::copy(cind, cind + nnz, sun_cind); | ||
| std::copy(vals, vals + nnz, sun_vals); |
There was a problem hiding this comment.
I assume these are copied over, but this file violates the naming conventions in almost every variable and function.
There was a problem hiding this comment.
Some of these are SUNDIALS functions, so they don't follow our style guidelines.
shakedregev
left a comment
There was a problem hiding this comment.
Approved pending fixes to variable names
|
It's impressive that there are such substantial speed gains on even a small system. This means that for real systems it's probably astronomical. I wonder if we can test that. |
| // Basic Verification test | ||
| std::vector<size_t> csr_rtest = {0, 2, 4, 6}; | ||
| std::vector<size_t> csr_ctest = {0, 1, 1, 2, 0, 2}; | ||
| std::vector<double> csr_valtest = {2.6, 1.1, 2.9, 1.3, 1.4, 3.3}; | ||
| std::vector<size_t> maptest = {2, 5, 0, 0, 4, 2, 1, 5, 3}; | ||
|
|
||
| std::vector<size_t>& map_to_csr = C.getMapToCsr(); | ||
|
|
||
| assert(csr_rtest.size() == csr_r.size()); | ||
| assert(csr_ctest.size() == csr_c.size()); | ||
| assert(csr_valtest.size() == csr_v.size()); | ||
| assert(maptest.size() == map_to_csr.size()); | ||
|
|
||
| for (size_t i = 0; i < csr_rtest.size(); i++) | ||
| { | ||
| if (csr_r[i] != csr_rtest[i]) |
| std::vector<IdxT>& getMapToCsr(); | ||
|
|
||
| void printMatrix(std::string name = ""); | ||
|
|
||
| void printMatrixMarket(const std::string& filename, const std::string& comment); | ||
|
|
||
| static void sortSparseCOO(std::vector<IdxT>& rows, std::vector<IdxT>& columns, std::vector<RealT>& values); | ||
| static void sortSparseCOO(std::vector<IdxT>& rows, std::vector<IdxT>& columns, std::vector<RealT>& values, std::vector<IdxT>& map); |
There was a problem hiding this comment.
We should consistently capitalize entire acronyms or just the first letter. I tend to favor the Csr vs COO style better. We are essentially using the acronym as a word. In comments, we can have it all capitalized.
There was a problem hiding this comment.
PS: Looks like most of the code actually capitalizes the entire acronym for COO, but not for CSR. @pelesh - thoughts?
There was a problem hiding this comment.
I agree on this
There was a problem hiding this comment.
Please follow style guidelines. Some of the non-compliant code was grandfathered in. That is why we still have some inconsistencies.
| std::vector<size_t> order_vec(rows.size()); | ||
| std::size_t n(0); | ||
| std::generate(std::begin(order_vec), std::end(order_vec), [&] |
There was a problem hiding this comment.
I missed this (perhaps multiple times), but why are we using std::vector here? Is this to keep consistency with the rest of the code?
Edit: is this just for comparison?
There was a problem hiding this comment.
Yes, to keep consistency with the rest of the code for now.
There was a problem hiding this comment.
Please remove std::vector here.
There was a problem hiding this comment.
- CSR matrix implementation should remain in the
*.cppfile. - I would avoid using STL containers here because we want to prototype parallel implementations.
- Do not use existing COO matrix implementation in GridKit. If needed, port COO matrix from Re::Solve.
I made some other (mostly nitpicking) comments about the code.
| template <typename RealT, typename IdxT> | ||
| std::tuple<std::vector<IdxT>, std::vector<IdxT>, std::vector<RealT>> COO_Matrix<RealT, IdxT>::getCsrData() |
| std::vector<IdxT>& getMapToCsr(); | ||
|
|
||
| void printMatrix(std::string name = ""); | ||
|
|
||
| void printMatrixMarket(const std::string& filename, const std::string& comment); | ||
|
|
||
| static void sortSparseCOO(std::vector<IdxT>& rows, std::vector<IdxT>& columns, std::vector<RealT>& values); | ||
| static void sortSparseCOO(std::vector<IdxT>& rows, std::vector<IdxT>& columns, std::vector<RealT>& values, std::vector<IdxT>& map); |
There was a problem hiding this comment.
Please follow style guidelines. Some of the non-compliant code was grandfathered in. That is why we still have some inconsistencies.
| sortSparseCOO(row_indices_, column_indices_, values_, map_to_sorted); | ||
|
|
There was a problem hiding this comment.
Per guidelines:
| sortSparseCOO(row_indices_, column_indices_, values_, map_to_sorted); | |
| sortSparseCoo(row_indices_, column_indices_, values_, map_to_sorted); | |
| * @param[in] n number of columns | ||
| */ | ||
| template <typename RealT, typename IdxT> | ||
| void COO_Matrix<RealT, IdxT>::resetEntries(std::vector<IdxT> r, std::vector<IdxT> c, std::vector<RealT> v, IdxT m, IdxT n) |
There was a problem hiding this comment.
I would avoid using COO_Matrix altogether. We are going to remove this class.
| std::vector<size_t> order_vec(rows.size()); | ||
| std::size_t n(0); | ||
| std::generate(std::begin(order_vec), std::end(order_vec), [&] |
There was a problem hiding this comment.
Please remove std::vector here.




Description
Jacobian assembly currently uses a COO matrix, and we sort and rebuild CSR row pointers each step for IDA. The overhead from this process can become a bottleneck as nnz grows. This change keeps the system Jacobian in CSR and preserves a COO→CSR mapping for direct CSR updates.
Closes #301
cc @nkoukpaizan
Proposed changes
PowerElectronicsModel.allocate()to populate CSR entries and preserve the mapping.CsrJacfor the solver configuration (PhasorDynamicsstill uses theJacfunction).Currently
PowerElectronicsonly.Checklist
-Wall -Wpedantic -Wconversion -Wextra.