Skip to content

Commit

Permalink
Introduce free_event_queues() to free any existing event queues (#2180)
Browse files Browse the repository at this point in the history
* Add .vscode to .gitignore

* Turn TQItemPool and SelfEventPool from macro to tempalte MutexPool

* Trying to clear TQItemPool

* Fix assert error

* Clearing TQItemPool and SelfEventPool

* Make clang-format happy?

* drop declarePool/implementPool from .clang-format.changes

* Create new API to clear event queue

* Initialize MutexPool members with brace initialization

* Small cleanup

* Introduce freeing of the queue pools

* Trying to clear TQItem pool as well

* Improved test related to poolshrink() and free_event_queues()

* Added a bit of documentation related to freeing the SelfEventQueue and TQItemQueue

* Make formatting happy

* Fix test

* Add documention for poolshrink() and free_event_queues()

---------

Co-authored-by: Ioannis Magkanaris <ioannis.magkanaris@epfl.ch>
Co-authored-by: Alexandru Săvulescu <alexandru.savulescu@epfl.ch>
  • Loading branch information
3 people authored Feb 1, 2023
1 parent d2a76ab commit c829a5f
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 170 deletions.
4 changes: 2 additions & 2 deletions .clang-format.changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ SortIncludes: false
Standard: c++17
StatementMacros: [MKDLL, MKDLLdec, MKDLLif, MKDLLvp, MKDLLvpf, PyObject_HEAD,
declareActionCallback, declareAdjustStepper, declareArrowGlyph, declareFieldEditorCallback,
declareFieldSEditorCallback, declareFileChooserCallback, declareIOCallback, declareList, declarePool,
declareFieldSEditorCallback, declareFileChooserCallback, declareIOCallback, declareList,
declarePtrList, declareRubberCallback, declareSelectionCallback, declareTable, declareTable2,
implementActionCallback, implementAdjustStepper, implementArrowGlyph, implementFieldEditorCallback,
implementFieldSEditorCallback, implementFileChooserCallback, implementIOCallback, implementList,
implementPool, implementPtrList, implementRubberCallback, implementSelectionCallback, implementTable,
implementPtrList, implementRubberCallback, implementSelectionCallback, implementTable,
implementTable2, nrn_pragma_acc, nrn_pragma_omp, TBUF]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ virtualenv
*.lo
docs/_build
docs/_generated
.vscode
41 changes: 41 additions & 0 deletions docs/hoc/simctrl/cvode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,47 @@ CVode



.. hoc:method:: CVode.free_event_queues
Syntax:
``cvode.free_event_queues()``


Description:
This function takes cares of clearing and free all the event queues allocated in NEURON.
More specifically, it frees the `TQItemPool`, `SelfEventPool` and `SelfQueue` members of
the `NetCvodeThreadData`.
This method should be called only after the end of the NEURON simulation since calling it
will clear all the Event Queues and it should only be used for freeing up memory.

----



.. hoc:method:: CVode.poolshrink
Syntax:
``cvode.poolshrink()``

``cvode.poolshrink(1)``


Description:
This function is used to either print or free the `DoubleArrayPool` s and `DatumArrayPool` s
used by the mechanisms' data.
If the function is called with argument `1` it deletes the pools if the number of items used
is 0.
If the function is called without arguments or with argument `0` it prints current number of
items used and number of items allocated for double arrays and Datum arrays.
This method should be called only after the end of the NEURON simulation for freeing up
memory.

----



.. hoc:method:: CVode.rtol
Expand Down
41 changes: 41 additions & 0 deletions docs/python/simctrl/cvode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,47 @@ CVode



.. method:: CVode.free_event_queues


Syntax:
``cvode.free_event_queues()``


Description:
This function takes cares of clearing and free all the event queues allocated in NEURON.
More specifically, it frees the `TQItemPool`, `SelfEventPool` and `SelfQueue` members of
the `NetCvodeThreadData`.
This method should be called only after the end of the NEURON simulation since calling it
will clear all the Event Queues and it should only be used for freeing up memory.

----



.. method:: CVode.poolshrink


Syntax:
``cvode.poolshrink()``

``cvode.poolshrink(1)``


Description:
This function is used to either print or free the `DoubleArrayPool` s and `DatumArrayPool` s
used by the mechanisms' data.
If the function is called with argument `1` it deletes the pools if the number of items used
is 0.
If the function is called without arguments or with argument `0` it prints current number of
items used and number of items allocated for double arrays and Datum arrays.
This method should be called only after the end of the NEURON simulation for freeing up
memory.

----



.. method:: CVode.rtol


Expand Down
7 changes: 7 additions & 0 deletions src/nrncvode/cvodeobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern int hoc_return_type_code;
#include "netcvode.h"
#include "membfunc.h"
#include "nrn_ansi.h"
#include "nrncvode.h"
#include "nrndaspk.h"
#include "nrniv_mf.h"
#include "tqueue.h"
Expand Down Expand Up @@ -587,6 +588,11 @@ static double poolshrink(void*) {
return double(i);
}

static double free_event_queues(void*) {
free_event_queues();
return 0;
}

static Member_func members[] = {{"solve", solve},
{"atol", nrn_atol},
{"rtol", rtol},
Expand Down Expand Up @@ -636,6 +642,7 @@ static Member_func members[] = {{"solve", solve},
{"extra_scatter_gather_remove", extra_scatter_gather_remove},
{"use_fast_imem", use_fast_imem},
{"poolshrink", poolshrink},
{"free_event_queues", free_event_queues},
{nullptr, nullptr}};

static Member_ret_obj_func omembers[] = {{"netconlist", netconlist}, {nullptr, nullptr}};
Expand Down
6 changes: 6 additions & 0 deletions src/nrncvode/cvodestb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ void clear_event_queue() {
}
}

void free_event_queues() {
if (net_cvode_instance) {
net_cvode_instance->free_event_pools();
}
}

void init_net_events() {
if (net_cvode_instance) {
net_cvode_instance->init_events();
Expand Down
3 changes: 1 addition & 2 deletions src/nrncvode/hocevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include <nrnoc2iv.h>
#include <mymath.h>

declarePool(HocEventPool, HocEvent)
implementPool(HocEventPool, HocEvent)
using HocEventPool = MutexPool<HocEvent>;
HocEventPool* HocEvent::hepool_;

HocEvent::HocEvent() {
Expand Down
4 changes: 3 additions & 1 deletion src/nrncvode/netcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "htlist.h"
#include "nrnmpi.h"
#include "nrnneosm.h"
#include "pool.h"

#include <InterViews/observe.h>

Expand All @@ -29,7 +30,8 @@ class TQueue;
class TQItem;
struct NrnThread;
class NetCvode;
class HocEventPool;
class HocEvent;
using HocEventPool = MutexPool<HocEvent>;
class HocCommand;
struct STETransition;
class IvocVect;
Expand Down
24 changes: 22 additions & 2 deletions src/nrncvode/netcvode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ struct InterThreadEvent {
};

typedef std::vector<WatchCondition*> WatchList;
declarePool(SelfEventPool, SelfEvent)
implementPool(SelfEventPool, SelfEvent)
using SelfEventPool = MutexPool<SelfEvent>;
typedef std::vector<TQItem*> TQList;

// allows marshalling of all items in the event queue that need to be
Expand Down Expand Up @@ -2918,6 +2917,27 @@ void NetCvode::clear_events() {
}
}

// Frees the allocated memory for the SelfEvent pool and TQItemPool after cleaning them
void NetCvode::free_event_pools() {
clear_events();
for (int i = 0; i < nrn_nthread; ++i) {
NetCvodeThreadData& d = p[i];
if (d.sepool_) {
delete d.sepool_;
}
if (d.selfqueue_) {
delete d.selfqueue_;
}
if (d.tqe_) {
delete d.tqe_;
}
if (d.tpool_) {
d.tpool_->free_all();
delete d.tpool_;
}
}
}

void NetCvode::init_events() {
hoc_Item* q;
int i, j;
Expand Down
5 changes: 3 additions & 2 deletions src/nrncvode/netcvode.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "mymath.h"

#include "cvodeobj.h"
#include "tqueue.h"

#include <cmath>
Expand All @@ -17,9 +18,8 @@ class HocDataPaths;
using PreSynTable = std::unordered_map<double*, PreSyn*>;
class NetCon;
class DiscreteEvent;
class TQItemPool;
class SelfEventPool;
class SelfEvent;
using SelfEventPool = MutexPool<SelfEvent>;
struct hoc_Item;
class PlayRecord;
class PlayRecList;
Expand Down Expand Up @@ -113,6 +113,7 @@ class NetCvode {
void deliver_events(double til, NrnThread*); // for initialization events
void solver_prepare();
void clear_events();
void free_event_pools();
void init_events();
void print_event_queue();
void event_queue_info();
Expand Down
Loading

0 comments on commit c829a5f

Please sign in to comment.