Skip to content

Commit

Permalink
Merge branch 'master' into cornu/remove_regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino authored Feb 1, 2023
2 parents 981efdf + c829a5f commit cf8c258
Show file tree
Hide file tree
Showing 28 changed files with 530 additions and 186 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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 22.1.0
rev: 22.12.0
hooks:
- id: black
language_version: python3
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
57 changes: 55 additions & 2 deletions src/coreneuron/mpi/lib/nrnmpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <mpi.h>
namespace coreneuron {


MPI_Comm nrnmpi_world_comm;
MPI_Comm nrnmpi_comm;
int nrnmpi_numprocs_;
Expand All @@ -34,6 +35,8 @@ static void nrn_fatal_error(const char* msg) {
nrnmpi_abort_impl(-1);
}

void corenrn_subworld();

nrnmpi_init_ret_t nrnmpi_init_impl(int* pargc, char*** pargv, bool is_quiet) {
// Execute at most once per launch. Avoid memory leak.
static bool executed = false;
Expand All @@ -54,10 +57,13 @@ nrnmpi_init_ret_t nrnmpi_init_impl(int* pargc, char*** pargv, bool is_quiet) {
nrn_assert(MPI_Init(pargc, pargv) == MPI_SUCCESS);
#endif
}

nrn_assert(MPI_Comm_dup(MPI_COMM_WORLD, &nrnmpi_world_comm) == MPI_SUCCESS);
nrn_assert(MPI_Comm_dup(nrnmpi_world_comm, &nrnmpi_comm) == MPI_SUCCESS);
nrn_assert(MPI_Comm_rank(nrnmpi_world_comm, &nrnmpi_myid_) == MPI_SUCCESS);
nrn_assert(MPI_Comm_size(nrnmpi_world_comm, &nrnmpi_numprocs_) == MPI_SUCCESS);
corenrn_subworld(); // split nrnmpi_comm if ParallelContext.subworlds has been used
nrn_assert(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid_) == MPI_SUCCESS);
nrn_assert(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs_) == MPI_SUCCESS);

nrnmpi_spike_initialize();

if (nrnmpi_myid_ == 0 && !is_quiet) {
Expand All @@ -82,6 +88,53 @@ void nrnmpi_finalize_impl(void) {
}
}

extern "C" {
extern void (*nrn2core_subworld_info_)(int&, int&, int&, int&, int&);
}

void corenrn_subworld() {
// If ParallelContext.subworlds has been invoked, split the world
// communicator according to the subworld partitioning.
static int change_cnt{0};
int nrn_subworld_change_cnt, nrn_subworld_index, nrn_subworld_rank, nrn_mpi_numprocs_subworld,
nrn_mpi_numprocs_world;
if (!nrn2core_subworld_info_) {
return;
}
(*nrn2core_subworld_info_)(nrn_subworld_change_cnt,
nrn_subworld_index,
nrn_subworld_rank,
nrn_mpi_numprocs_subworld,
nrn_mpi_numprocs_world);
if (nrn_subworld_change_cnt == change_cnt) {
return;
}
change_cnt = nrn_subworld_change_cnt;

// clean up / free old nrn_mpi_comm
nrn_assert(MPI_Comm_free(&nrnmpi_comm) == MPI_SUCCESS);

// ensure world size is the same as NEURON
int world_size{-1};
nrn_assert(MPI_Comm_size(nrnmpi_world_comm, &world_size) == MPI_SUCCESS);
nrn_assert(world_size == nrn_mpi_numprocs_world);

// create a new nrnmpi_comm based on subworld partitioning
nrn_assert(
MPI_Comm_split(nrnmpi_world_comm, nrn_subworld_index, nrn_subworld_rank, &nrnmpi_comm) ==
MPI_SUCCESS);

// assert that rank order and size is consistent between NEURON and CoreNEURON
int subworld_rank{-1};
nrn_assert(MPI_Comm_rank(nrnmpi_comm, &subworld_rank) == MPI_SUCCESS);
nrn_assert(nrn_subworld_rank == subworld_rank);

int subworld_size{-1};
nrn_assert(MPI_Comm_size(nrnmpi_comm, &subworld_size) == MPI_SUCCESS);
nrn_assert(subworld_size == nrn_mpi_numprocs_subworld);
}


// check if appropriate threading level supported (i.e. MPI_THREAD_FUNNELED)
void nrnmpi_check_threading_support_impl() {
int th = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/coreneuron/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ double nrn_wtime() {
return (time1.tv_sec + time1.tv_usec / 1.e6);
}
}

extern "C" {
void (*nrn2core_subworld_info_)(int&, int&, int&);
}


} // namespace coreneuron
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 @@ -2865,6 +2864,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 cf8c258

Please sign in to comment.