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

treeset: use eigen instead of sparse13 #2643

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5c5c730
Sparse13
alkino Dec 13, 2023
42fa353
Bump Eigen to stable version 3.4.0
alkino Dec 14, 2023
52816fc
Merge remote-tracking branch 'origin/cornu/bump_eigen'
alkino Dec 14, 2023
7b93bc5
Fix indices
alkino Dec 14, 2023
e85f753
Use not sparse matrix because of pointers
alkino Dec 14, 2023
29c58af
Merge remote-tracking branch 'origin/master'
alkino Dec 14, 2023
6ce1dcb
_sp13_rhs 0-indexed
alkino Dec 14, 2023
20e595b
Let avoid include headers
alkino Dec 14, 2023
2abf500
Format
alkino Dec 14, 2023
6e03f48
Format
alkino Dec 14, 2023
6e2eb67
Merge remote-tracking branch 'origin/master'
alkino Jun 4, 2024
135fc52
more -1+1
alkino Jun 4, 2024
8794fda
Merge remote-tracking branch 'origin/master'
alkino Jun 4, 2024
d52b91e
Fix formatting
github-actions[bot] Jun 4, 2024
c4ae1a0
Simplify MatrixMap
alkino Jun 4, 2024
dfbc7ac
Merge branch 'foo'
alkino Jun 4, 2024
ad75001
Merge remote-tracking branch 'origin/master'
Jun 5, 2024
94276eb
Revert change
Jun 5, 2024
92b0857
Simplify & fix weird things
Jun 5, 2024
a2eae5e
Merge remote-tracking branch 'origin/master'
Jun 5, 2024
3e64192
Place holder & renaming
Jun 5, 2024
e08cef1
Let's go for green!
Jun 5, 2024
f17b925
Fix formatting
github-actions[bot] Jun 5, 2024
cbd6995
Avoid including eigen there
Jun 5, 2024
09546eb
Merge remote-tracking branch 'origin/cornu/sparse13_treeset'
Jun 5, 2024
3706ba0
format
Jun 5, 2024
61aca57
Prepare the matrix for solving with SparseLU
alkino Jun 6, 2024
846364d
Compress as soon as possible
alkino Jun 6, 2024
f4bc02d
Use triplets to set the matrix
alkino Jun 6, 2024
40d6844
Be expressive
alkino Jun 6, 2024
bd7248d
Any fixed typo is good
alkino Jun 6, 2024
299d2ed
Fix plen
alkino Jun 6, 2024
e1df5f2
Fix formatting
github-actions[bot] Jun 6, 2024
9541fd9
Merge branch 'master' into cornu/sparse13_treeset
alkino Oct 22, 2024
6b411b6
Remove problem during merge
alkino Oct 22, 2024
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
8 changes: 4 additions & 4 deletions src/nrncvode/occvode.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <Eigen/Eigen>

#include <../../nrnconf.h>
#include <errno.h>
#include "nrn_ansi.h"
Expand All @@ -14,6 +16,7 @@

#include <utility>


extern void setup_topology(), v_setup_vectors();
extern void recalc_diam();
extern int nrn_errno_check(int);
Expand All @@ -23,9 +26,6 @@ extern int nrn_errno_check(int);

extern Symlist* hoc_built_in_symlist;

#include "spmatrix.h"
extern double* sp13mat;

#if 1 || NRNMPI
extern void (*nrnthread_v_transfer_)(NrnThread*);
extern void (*nrnmpi_v_transfer_)();
Expand Down Expand Up @@ -356,7 +356,7 @@ void Cvode::daspk_init_eqn() {
if (use_sparse13 == 0 || diam_changed != 0) {
recalc_diam();
}
zneq = spGetSize(_nt->_sp13mat, 0);
zneq = _nt->_sparseMat->cols();
z.neq_v_ = z.nonvint_offset_ = zneq;
// now add the membrane mechanism ode's to the count
for (cml = z.cv_memb_list_; cml; cml = cml->next) {
Expand Down
9 changes: 7 additions & 2 deletions src/nrniv/matrixmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "matrixmap.h"
#include <vector>

#include "spmatrix.h"
#include <Eigen/Eigen>

MatrixMap::MatrixMap(Matrix& mat)
: m_(mat) {}
Expand All @@ -24,6 +24,7 @@ void MatrixMap::add(double fac) {
}

void MatrixMap::alloc(int start, int nnode, Node** nodes, int* layer) {
static double place_holder = 0.;
NrnThread* _nt = nrn_threads;
mmfree();

Expand Down Expand Up @@ -54,7 +55,11 @@ void MatrixMap::alloc(int start, int nnode, Node** nodes, int* layer) {
} else {
jt = start + j - nnode;
}
ptree_[plen_] = spGetElement(_nt->_sp13mat, it, jt);
if (it == 0 || jt == 0) {
ptree_[plen_] = &place_holder;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a joke of sparse13 I can explain that (this is funny)

} else {
ptree_[plen_] = &_nt->_sparseMat->coeffRef(it - 1, jt - 1);
}
++plen_;
}
}
6 changes: 3 additions & 3 deletions src/nrniv/nrndae.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void NrnDAE::dkmap(std::vector<neuron::container::data_handle<double>>& pv,
y_.data() + i};
pvdot[bmap_[i] - 1] =
neuron::container::data_handle<double>{neuron::container::do_not_search,
_nt->_sp13_rhs + bmap_[i]};
_nt->_sparse_rhs + bmap_[i]};
}
}

Expand All @@ -231,7 +231,7 @@ void NrnDAE::update() {
// note that the following is correct also for states that refer
// to the internal potential of a segment. i.e rhs is v + vext[0]
for (int i = 0; i < size_; ++i) {
y_[i] += _nt->_sp13_rhs[bmap_[i]];
y_[i] += _nt->_sparse_rhs[bmap_[i]];
}
// for (int i=0; i < size_; ++i) printf(" i=%d bmap_[i]=%d y_[i]=%g\n", i, bmap_[i],
// y_->elem(i));
Expand Down Expand Up @@ -311,7 +311,7 @@ void NrnDAE::rhs() {
v2y();
f_(y_, yptmp_, size_);
for (int i = 0; i < size_; ++i) {
_nt->_sp13_rhs[bmap_[i]] += yptmp_[i];
_nt->_sparse_rhs[bmap_[i]] += yptmp_[i];
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/nrnoc/fadvance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "utils/profile/profiler_interface.h"
#include "nonvintblock.h"
#include "nrncvode.h"
#include "spmatrix.h"
#include <Eigen/Eigen>

#include <vector>

Expand Down Expand Up @@ -672,11 +672,11 @@ void nrn_print_matrix(NrnThread* _nt) {
Node* nd;
if (use_sparse13) {
if (ifarg(1) && chkarg(1, 0., 1.) == 0.) {
spPrint(_nt->_sp13mat, 1, 0, 1);
std::cout << &_nt->_sparseMat << std::endl;
} else {
int i, n = spGetSize(_nt->_sp13mat, 0);
spPrint(_nt->_sp13mat, 1, 1, 1);
for (i = 1; i <= n; ++i) {
std::cout << &_nt->_sparseMat << std::endl;
int n = _nt->_sparseMat->cols();
for (int i = 1; i <= n; ++i) {
Comment on lines -675 to +679
Copy link
Member Author

Choose a reason for hiding this comment

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

What should we do about that? I'm for removing...

Printf("%d %g\n", i, _nt->actual_rhs(i));
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/nrnoc/multicore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ the handling of v_structure_change as long as possible.
#include <vector>
#include <iostream>

#include <Eigen/Eigen>

#define CACHELINE_ALLOC(name, type, size) \
name = (type*) nrn_cacheline_alloc((void**) &name, size * sizeof(type))
#define CACHELINE_CALLOC(name, type, size) \
Expand Down Expand Up @@ -337,14 +339,14 @@ void nrn_threads_create(int n, bool parallel) {
for (j = 0; j < BEFORE_AFTER_SIZE; ++j) {
nt->tbl[j] = (NrnThreadBAList*) 0;
}
nt->_sp13_rhs = nullptr;
nt->_sparse_rhs = nullptr;
nt->_v_parent_index = 0;
nt->_v_node = 0;
nt->_v_parent = 0;
nt->_ecell_memb_list = 0;
nt->_ecell_child_cnt = 0;
nt->_ecell_children = NULL;
nt->_sp13mat = 0;
nt->_sparseMat = nullptr;
nt->_ctime = 0.0;
nt->_vcv = 0;
nt->_node_data_offset = 0;
Expand Down Expand Up @@ -438,9 +440,9 @@ void nrn_threads_free() {
free(nt->_ecell_children);
nt->_ecell_children = NULL;
}
if (nt->_sp13mat) {
spDestroy(nt->_sp13mat);
nt->_sp13mat = 0;
if (nt->_sparseMat) {
delete nt->_sparseMat;
nt->_sparseMat = nullptr;
}
nt->end = 0;
nt->ncell = 0;
Expand Down
16 changes: 10 additions & 6 deletions src/nrnoc/multicore.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ actual_v, etc.
*/

#include "membfunc.h"
namespace Eigen {
template <typename _Scalar, int _Options, typename _StorageIndex>
class SparseMatrix;
}
Comment on lines +30 to +33
Copy link
Member Author

Choose a reason for hiding this comment

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

This file is installed and use, so if we include <Eigen> we need to install Eigen header too...


#include <cstddef>

Expand Down Expand Up @@ -89,12 +93,12 @@ struct NrnThread {
int* _v_parent_index;
Node** _v_node;
Node** _v_parent;
double* _sp13_rhs; /* rhs matrix for sparse13 solver. updates to and from this vector
need to be transfered to actual_rhs */
char* _sp13mat; /* handle to general sparse matrix */
Memb_list* _ecell_memb_list; /* normally nullptr */
Node** _ecell_children; /* nodes with no extcell but parent has it */
void* _vcv; /* replaces old cvode_instance and nrn_cvode_ */
double* _sparse_rhs; /* rhs matrix for sparse13 solver. updates to and from this vector
need to be transfered to actual_rhs */
Eigen::SparseMatrix<double, 1, int>* _sparseMat{}; /* handle to general sparse matrix */
Copy link
Member Author

@alkino alkino Jun 6, 2024

Choose a reason for hiding this comment

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

We give number otherwise we have to declare the enum behind the option and I have no idea how to do that without conflict at resolution...

Memb_list* _ecell_memb_list; /* normally nullptr */
Node** _ecell_children; /* nodes with no extcell but parent has it */
void* _vcv; /* replaces old cvode_instance and nrn_cvode_ */

#if 1
double _ctime; /* computation time in seconds (using nrnmpi_wtime) */
Expand Down
20 changes: 6 additions & 14 deletions src/nrnoc/solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ node.v + extnode.v[0]
#include "nrnmpiuse.h"
#include "ocnotify.h"
#include "section.h"
#include "spmatrix.h"
#include <Eigen/Eigen>
#include <Eigen/LU>
#include "treeset.h"

#include <stdio.h>
Expand Down Expand Up @@ -364,22 +365,13 @@ void nrn_solve(NrnThread* _nt) {
}
#else
if (use_sparse13) {
int e;
nrn_thread_error("solve use_sparse13");
update_sp13_mat_based_on_actual_d(_nt);
e = spFactor(_nt->_sp13mat);
if (e != spOKAY) {
switch (e) {
case spZERO_DIAG:
hoc_execerror("spFactor error:", "Zero Diagonal");
case spNO_MEMORY:
hoc_execerror("spFactor error:", "No Memory");
case spSINGULAR:
hoc_execerror("spFactor error:", "Singular");
}
}
update_sp13_rhs_based_on_actual_rhs(_nt);
spSolve(_nt->_sp13mat, _nt->_sp13_rhs, _nt->_sp13_rhs);
_nt->_sparseMat->makeCompressed();
Eigen::SparseLU<Eigen::SparseMatrix<double, Eigen::RowMajor>> lu(*_nt->_sparseMat);
Eigen::Map<Eigen::VectorXd> rhs(_nt->_sparse_rhs + 1, _nt->_sparseMat->cols());
Copy link
Member Author

Choose a reason for hiding this comment

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

other joke of sparse13 the matrix is 1-indexed so we use the vector off by one.

rhs = lu.solve(rhs);
update_actual_d_based_on_sp13_mat(_nt);
update_actual_rhs_based_on_sp13_rhs(_nt);
} else {
Expand Down
Loading
Loading