Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 8ac2d6f

Browse files
authored
Fixes for multi-execution in-memory transfer with NEURON (#349)
- global ion maps were deleted at the end of every run - in case of in-memory execution mechanisms are registered only once and hence ions map is created only one - this is why ions shouldnt not be deleted if we are doing embedded run with neuron
1 parent 8cd98e7 commit 8ac2d6f

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

coreneuron/io/nrn_setup.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -985,11 +985,34 @@ inline void mech_layout(FileHandler& F, T* data, int cnt, int sz, int layout) {
985985
}
986986
}
987987

988+
/**
989+
* Cleanup global ion map created during mechanism registration
990+
*
991+
* In case of coreneuron standalone execution nrn_ion_global_map
992+
* can be deleted at the end of execution. But in case embedded
993+
* run via neuron, mechanisms are registered only once i.e. during
994+
* first call to coreneuron. This is why we call cleanup only in
995+
* case of standalone coreneuron execution via nrniv-core or
996+
* special-core.
997+
*
998+
* @todo coreneuron should have finalise callback which can be
999+
* called from NEURON for final memory cleanup including global
1000+
* state like registered mechanisms and ions map.
1001+
*/
1002+
void nrn_cleanup_ion_map() {
1003+
for (int i = 0; i < nrn_ion_global_map_size; i++) {
1004+
free_memory(nrn_ion_global_map[i]);
1005+
}
1006+
free_memory(nrn_ion_global_map);
1007+
nrn_ion_global_map = nullptr;
1008+
nrn_ion_global_map_size = 0;
1009+
}
1010+
9881011
/* nrn_threads_free() presumes all NrnThread and NrnThreadMembList data is
9891012
* allocated with malloc(). This is not the case here, so let's try and fix
9901013
* things up first. */
9911014

992-
void nrn_cleanup(bool clean_ion_global_map) {
1015+
void nrn_cleanup() {
9931016
clear_event_queue(); // delete left-over TQItem
9941017
gid2in.clear();
9951018
gid2out.clear();
@@ -1000,15 +1023,6 @@ void nrn_cleanup(bool clean_ion_global_map) {
10001023
nrnthread_chkpnt = nullptr;
10011024
}
10021025

1003-
// clean ions global maps
1004-
if (clean_ion_global_map) {
1005-
for (int i = 0; i < nrn_ion_global_map_size; i++)
1006-
free_memory(nrn_ion_global_map[i]);
1007-
free_memory(nrn_ion_global_map);
1008-
nrn_ion_global_map = nullptr;
1009-
nrn_ion_global_map_size = 0;
1010-
}
1011-
10121026
// clean NrnThreads
10131027
for (int it = 0; it < nrn_nthread; ++it) {
10141028
NrnThread* nt = nrn_threads + it;

coreneuron/mechanism/mech/enginemech.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ void modl_reg() {
2828
extern bool nrn_have_gaps;
2929
extern bool nrn_use_fast_imem;
3030

31+
/// function defined in coreneuron library
32+
extern void nrn_cleanup_ion_map();
3133
} // namespace coreneuron
3234

3335
/** Initialize mechanisms and run simulation using CoreNEURON
@@ -37,7 +39,9 @@ extern bool nrn_use_fast_imem;
3739
int solve_core(int argc, char** argv) {
3840
mk_mech_init(argc, argv);
3941
coreneuron::modl_reg();
40-
return run_solve_core(argc, argv);
42+
int ret = run_solve_core(argc, argv);
43+
coreneuron::nrn_cleanup_ion_map();
44+
return ret;
4145
}
4246

4347
extern "C" {

coreneuron/nrniv/nrniv_decl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ extern double* stdindex2ptr(int mtype, int index, NrnThread&);
6767
extern void delete_trajectory_requests(NrnThread&);
6868
extern int nrn_setup_multiple;
6969
extern int nrn_setup_extracon;
70-
extern void nrn_cleanup(bool clean_ion_global_map = true);
70+
extern void nrn_cleanup();
71+
extern void nrn_cleanup_ion_map();
7172
extern void BBS_netpar_solve(double);
7273
extern void nrn_mkPatternStim(const char* filename);
7374
extern int nrn_extra_thread0_vdata;

tests/jenkins/neuron_direct.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
tvstd = tv.cl()
2828
i_memstd = i_mem.cl()
2929

30-
#h.CoreNeuronRun[0].run()
3130
pc = h.ParallelContext()
3231
h.stdinit()
3332
pc.nrncore_run("-e %g"%h.tstop, 0)
33+
# running second time for testing multiple executions
34+
h.stdinit()
35+
pc.nrncore_run("-e %g"%h.tstop, 0)
3436

3537
if not bool(tv.eq(tvstd)):
3638
print("Voltage times are different")

0 commit comments

Comments
 (0)