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

Replace FOR_THREADS macro #3282

Merged
merged 4 commits into from
Dec 12, 2024
Merged
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
17 changes: 6 additions & 11 deletions src/nrncvode/cvodeobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,29 +956,25 @@ void Cvode::maxstate(bool b, NrnThread* nt) {
}

void Cvode::maxstate(double* pd) {
int i;
NrnThread* nt;
if (maxstate_) {
FOR_THREADS(nt) {
for (NrnThread* nt: for_threads(nrn_threads, nrn_nthread)) {
double* m = n_vector_data(maxstate_, nt->id);
int n = ctd_[nt->id].nvsize_;
int o = ctd_[nt->id].nvoffset_;
for (i = 0; i < n; ++i) {
for (int i = 0; i < n; ++i) {
pd[i + o] = m[i];
}
}
}
}

void Cvode::maxacor(double* pd) {
int i;
NrnThread* nt;
if (maxacor_) {
FOR_THREADS(nt) {
for (const NrnThread* nt: for_threads(nrn_threads, nrn_nthread)) {
double* m = n_vector_data(maxacor_, nt->id);
int n = ctd_[nt->id].nvsize_;
int o = ctd_[nt->id].nvoffset_;
for (i = 0; i < n; ++i) {
for (int i = 0; i < n; ++i) {
pd[i + o] = m[i];
}
}
Expand Down Expand Up @@ -1221,13 +1217,12 @@ int Cvode::init(double tout) {
}

int Cvode::interpolate(double tout) {
NrnThread* _nt;
if (neq_ == 0) {
t_ = tout;
if (nth_) {
nth_->_t = t_;
} else {
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
_nt->_t = t_;
}
}
Expand All @@ -1241,7 +1236,7 @@ int Cvode::interpolate(double tout) {
if (nth_) { // lvardt
nth_->_t = tout;
} else {
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
_nt->_t = tout; // but leave t_ at the initialization point.
}
}
Expand Down
49 changes: 25 additions & 24 deletions src/nrncvode/netcvode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,6 @@ bool NetCvode::init_global() {
structure_change_cnt_ = structure_change_cnt;
matrix_change_cnt_ = -1;
playrec_change_cnt_ = 0;
NrnThread* _nt;
// We copy Memb_list* into cml->ml below. At the moment this CVode code
// generates its own complicated set of Memb_list* that operate in
// list-of-handles mode instead of referring to contiguous sets of values.
Expand All @@ -1486,7 +1485,7 @@ bool NetCvode::init_global() {
del_cv_memb_list();
Cvode& cv = *gcv_;
distribute_dinfo(nullptr, 0);
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
CvodeThreadData& z = cv.ctd_[_nt->id];
z.rootnodecount_ = _nt->ncell;
z.v_node_count_ = _nt->end;
Expand Down Expand Up @@ -3963,7 +3962,6 @@ void NetCvode::re_init(double t) {
}

void NetCvode::fornetcon_prepare() {
NrnThread* nt;
NrnThreadMembList* tml;
if (fornetcon_change_cnt_ == structure_change_cnt) {
return;
Expand Down Expand Up @@ -3994,17 +3992,19 @@ void NetCvode::fornetcon_prepare() {
fnc->size = 0;
}
} else {
FOR_THREADS(nt) for (tml = nt->tml; tml; tml = tml->next) if (tml->index == type) {
Memb_list* m = tml->ml;
for (j = 0; j < m->nodecount; ++j) {
void** v = &(m->pdata[j][index].literal_value<void*>());
_nrn_free_fornetcon(v);
ForNetConsInfo* fnc = new ForNetConsInfo;
*v = fnc;
fnc->argslist = 0;
fnc->size = 0;
}
}
for (NrnThread* nt: for_threads(nrn_threads, nrn_nthread))
for (tml = nt->tml; tml; tml = tml->next)
if (tml->index == type) {
Memb_list* m = tml->ml;
for (j = 0; j < m->nodecount; ++j) {
void** v = &(m->pdata[j][index].literal_value<void*>());
_nrn_free_fornetcon(v);
ForNetConsInfo* fnc = new ForNetConsInfo;
*v = fnc;
fnc->argslist = 0;
fnc->size = 0;
}
}
}
}
// two loops over all netcons. one to count, one to fill in argslist
Expand Down Expand Up @@ -4037,18 +4037,19 @@ void NetCvode::fornetcon_prepare() {
}
}
} else {
FOR_THREADS(nt)
for (tml = nt->tml; tml; tml = tml->next)
if (tml->index == nrn_fornetcon_type_[i]) {
Memb_list* m = tml->ml;
for (j = 0; j < m->nodecount; ++j) {
auto* fnc = static_cast<ForNetConsInfo*>(m->pdata[j][index].get<void*>());
if (fnc->size > 0) {
fnc->argslist = new double*[fnc->size];
fnc->size = 0;
for (NrnThread* nt: for_threads(nrn_threads, nrn_nthread))
for (tml = nt->tml; tml; tml = tml->next)
if (tml->index == nrn_fornetcon_type_[i]) {
Memb_list* m = tml->ml;
for (j = 0; j < m->nodecount; ++j) {
auto* fnc = static_cast<ForNetConsInfo*>(
m->pdata[j][index].get<void*>());
if (fnc->size > 0) {
fnc->argslist = new double*[fnc->size];
fnc->size = 0;
}
}
}
}
}
}
// fill in argslist and count again
Expand Down
7 changes: 3 additions & 4 deletions src/nrncvode/occvode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ bool Cvode::init_global() {
void Cvode::init_eqn() {
double vtol;

NrnThread* _nt;
CvMembList* cml;
int i, j, zneq, zneq_v, zneq_cap_v;
// printf("Cvode::init_eqn\n");
Expand All @@ -125,7 +124,7 @@ void Cvode::init_eqn() {
daspk_init_eqn();
return;
}
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
// for lvardt, this body done only once and for ctd_[0]
CvodeThreadData& z = ctd_[_nt->id];
// how many ode's are there? First ones are non-zero capacitance
Expand Down Expand Up @@ -236,7 +235,7 @@ printf("%d Cvode::init_eqn id=%d neq_v_=%d #nonvint=%d #nonvint_extra=%d nvsize=
z.no_cap_child_count_ = j;

// use the sentinal values in NODERHS to construct a new no cap membrane list
new_no_cap_memb(z, _nt);
new_no_cap_memb(z, nullptr);

// map the membrane mechanism ode state and dstate pointers
int ieq = zneq_v;
Expand Down Expand Up @@ -270,7 +269,7 @@ printf("%d Cvode::init_eqn id=%d neq_v_=%d #nonvint=%d #nonvint_extra=%d nvsize=
structure_change_ = false;
}

void Cvode::new_no_cap_memb(CvodeThreadData& z, NrnThread* _nt) {
void Cvode::new_no_cap_memb(CvodeThreadData& z, NrnThread* /* thread */) {
z.delete_memb_list(z.no_cap_memb_);
z.no_cap_memb_ = nullptr;
CvMembList* ncm{};
Expand Down
3 changes: 1 addition & 2 deletions src/nrniv/nrncore_write/data/cell_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,8 @@ void CellGroup::mk_tml_with_art(neuron::model_sorted_token const& cache_token, C
size_t CellGroup::get_mla_rankbytes(CellGroup* cellgroups_) {
size_t mla_rankbytes = 0;
size_t nbytes;
NrnThread* nt;
NrnThreadMembList* tml;
FOR_THREADS(nt) {
for (const NrnThread* nt: for_threads(nrn_threads, nrn_nthread)) {
size_t threadbytes = 0;
size_t npnt = 0;
size_t nart = 0;
Expand Down
12 changes: 4 additions & 8 deletions src/nrniv/savstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,10 @@ void SaveState::ssfree() {
}

void SaveState::save() {
NrnThread* nt;
if (!check(false)) {
alloc();
}
FOR_THREADS(nt) {
for (const NrnThread* nt: for_threads(nrn_threads, nrn_nthread)) {
assert(t == nt->_t);
}
t_ = t;
Expand Down Expand Up @@ -673,12 +672,11 @@ void SaveState::saveacell(ACellState& ac, int type) {
}

void SaveState::restore(int type) {
NrnThread* nt;
if (!check(true)) {
hoc_execerror("SaveState:", "Stored state inconsistent with current neuron structure");
}
t = t_;
FOR_THREADS(nt) {
for (NrnThread* nt: for_threads(nrn_threads, nrn_nthread)) {
nt->_t = t_;
}
for (int isec = 0; isec < nsec_; ++isec) {
Expand Down Expand Up @@ -948,8 +946,7 @@ void SaveState::savenet() {
}
alloc_tq();
tqcnt_ = 0;
NrnThread* nt;
FOR_THREADS(nt) {
for (NrnThread* nt: for_threads(nrn_threads, nrn_nthread)) {
TQueue* tq = net_cvode_instance_event_queue(nt);
this_savestate = this;
callback_mode = 1;
Expand Down Expand Up @@ -1195,8 +1192,7 @@ void SaveState::free_tq() {
void SaveState::alloc_tq() {
free_tq();
tqcnt_ = 0;
NrnThread* nt;
FOR_THREADS(nt) {
for (NrnThread* nt: for_threads(nrn_threads, nrn_nthread)) {
TQueue* tq = net_cvode_instance_event_queue(nt);
this_savestate = this;
callback_mode = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/nrnoc/extcelln.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ static void printnode(const char* s) {
Extnode* nde;
double *pd;
NrnThread* _nt;
FOR_THREADS(_nt) for (in=0; in < _nt->end; ++in) {
for (NrnThread* _nt : for_threads(nrn_threads, nrn_nthread)) for (in=0; in < _nt->end; ++in) {
nd = _nt->_v_node[in];
if (nd->extnode) {
sec = nd->sec;
Expand Down
7 changes: 3 additions & 4 deletions src/nrnoc/fadvance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,6 @@ void verify_structure(void) {

void nrn_finitialize(int setv, double v) {
int iord, i;
NrnThread* _nt;
extern int _ninits;
extern short* nrn_is_artificial_;
++_ninits;
Expand Down Expand Up @@ -834,14 +833,14 @@ void nrn_finitialize(int setv, double v) {
nrn_deliver_events(nrn_threads + i); /* The play events at t=0 */
}
if (setv) {
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
auto const vec_v = _nt->node_voltage_storage();
std::fill_n(vec_v, _nt->end, v);
}
}
#if 1 || NRNMPI
if (nrnthread_vi_compute_)
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
(*nrnthread_vi_compute_)(_nt);
}
{
Expand All @@ -850,7 +849,7 @@ void nrn_finitialize(int setv, double v) {
(nrnmpi_v_transfer_)();
}
if (nrnthread_v_transfer_)
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
(*nrnthread_v_transfer_)(_nt);
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/nrnoc/multicore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,6 @@ void nrn_thread_memblist_setup() {
/* in passing, also set start and end indices. */

void reorder_secorder() {
NrnThread* _nt;
Section *sec, *ch;
Node* nd;
hoc_Item* qsec;
Expand All @@ -667,7 +666,7 @@ void reorder_secorder() {
sec->order = -1;
}
order = 0;
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
/* roots of this thread */
sl = _nt->roots;
inode = 0;
Expand Down Expand Up @@ -711,7 +710,7 @@ void reorder_secorder() {
sec->order = -1;
}
order = 0;
FOR_THREADS(_nt) {
for (NrnThread* _nt: for_threads(nrn_threads, nrn_nthread)) {
/* roots of this thread */
sl = _nt->roots;
inode = 0;
Expand Down Expand Up @@ -767,9 +766,10 @@ void reorder_secorder() {
in either case, we can then point to v, d, rhs in proper
node order
*/
FOR_THREADS(_nt) for (inode = 0; inode < _nt->end; ++inode) {
_nt->_v_node[inode]->_classical_parent = _nt->_v_parent[inode];
}
for (const NrnThread* _nt: for_threads(nrn_threads, nrn_nthread))
for (inode = 0; inode < _nt->end; ++inode) {
_nt->_v_node[inode]->_classical_parent = _nt->_v_parent[inode];
}
if (nrn_multisplit_setup_) {
/* classical order abandoned */
(*nrn_multisplit_setup_)();
Expand Down Expand Up @@ -948,7 +948,6 @@ int nrn_user_partition() {
hoc_List* sl;
char buf[256];
Section* sec;
NrnThread* nt;
/* all one or all the other*/
b = (nrn_threads[0].userpart != nullptr);
for (it = 1; it < nrn_nthread; ++it) {
Expand All @@ -962,7 +961,7 @@ int nrn_user_partition() {

/* discard partition if any section mentioned has been deleted. The
model has changed */
FOR_THREADS(nt) {
for (NrnThread* nt: for_threads(nrn_threads, nrn_nthread)) {
sl = nt->roots;
ITERATE(qsec, sl) {
sec = hocSEC(qsec);
Expand All @@ -983,7 +982,7 @@ int nrn_user_partition() {
/* fill in ncell and verify consistency */
n = 0;
for (it = 0; it < nrn_nthread; ++it) {
nt = nrn_threads + it;
NrnThread* nt = nrn_threads + it;
sl = nt->roots;
nt->ncell = 0;
ITERATE(qsec, sl) {
Expand Down
38 changes: 37 additions & 1 deletion src/nrnoc/multicore.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,43 @@ void reorder_secorder();
void nrn_thread_memblist_setup();
std::size_t nof_worker_threads();

#define FOR_THREADS(nt) for (nt = nrn_threads; nt < nrn_threads + nrn_nthread; ++nt)

// helper function for iterating over ``NrnThread``s
inline auto for_threads(NrnThread* threads, int num_threads) {
struct iterator {
NrnThread* current;

NrnThread* operator*() const {
return current;
}
iterator& operator++() {
++current;
return *this;
}
bool operator!=(const iterator& other) const {
return current != other.current;
}
};

struct iterable_wrapper {
NrnThread* base_;
int count_;

iterable_wrapper(NrnThread* base, int count)
: base_(base)
, count_(count) {}

iterator begin() const {
return iterator{base_};
}
iterator end() const {
return iterator{base_ + count_};
}
};

return iterable_wrapper(threads, num_threads);
}


// olupton 2022-01-31: could add a _NrnThread typedef here for .mod file
// backwards compatibility if needed.
Loading
Loading