diff --git a/coreneuron/io/nrn_setup.cpp b/coreneuron/io/nrn_setup.cpp index bcf975709..ac7d398ab 100644 --- a/coreneuron/io/nrn_setup.cpp +++ b/coreneuron/io/nrn_setup.cpp @@ -732,15 +732,7 @@ void nrn_cleanup() { NetSendBuffer_t* nsb = ml->_net_send_buffer; if (nsb) { - if (nsb->_size) { - free_memory(nsb->_sendtype); - free_memory(nsb->_vdata_index); - free_memory(nsb->_pnt_index); - free_memory(nsb->_weight_index); - free_memory(nsb->_nsb_t); - free_memory(nsb->_nsb_flag); - } - free_memory(nsb); + delete nsb; } if (tml->dependencies) diff --git a/coreneuron/io/phase2.cpp b/coreneuron/io/phase2.cpp index 31a888304..4e91aa78b 100644 --- a/coreneuron/io/phase2.cpp +++ b/coreneuron/io/phase2.cpp @@ -464,23 +464,9 @@ void Phase2::set_net_send_buffer(Memb_list** ml_list, const std::vector& pn // Does this thread have this type. Memb_list* ml = ml_list[type]; if (ml) { // needs a NetSendBuffer - NetSendBuffer_t* nsb = (NetSendBuffer_t*)ecalloc_align(1, sizeof(NetSendBuffer_t)); - ml->_net_send_buffer = nsb; - // begin with a size equal to twice number of instances - // at present there is no provision for dynamically increasing this. - nsb->_size = ml->nodecount * 2; - nsb->_cnt = 0; - - nsb->_sendtype = (int*)ecalloc_align(nsb->_size, sizeof(int)); - nsb->_vdata_index = (int*)ecalloc_align(nsb->_size, sizeof(int)); - nsb->_pnt_index = (int*)ecalloc_align(nsb->_size, sizeof(int)); - nsb->_weight_index = (int*)ecalloc_align(nsb->_size, sizeof(int)); - // when == 1, NetReceiveBuffer_t is newly allocated (i.e. we need to free previous copy - // and recopy new data - nsb->reallocated = 1; - nsb->_nsb_t = (double*)ecalloc_align(nsb->_size, sizeof(double)); - nsb->_nsb_flag = (double*)ecalloc_align(nsb->_size, sizeof(double)); + NetSendBuffer_t* nsb = new NetSendBuffer_t(ml->nodecount * 2); + ml->_net_send_buffer = nsb; } } } diff --git a/coreneuron/mechanism/mechanism.hpp b/coreneuron/mechanism/mechanism.hpp index 5e5a761eb..4510dea05 100644 --- a/coreneuron/mechanism/mechanism.hpp +++ b/coreneuron/mechanism/mechanism.hpp @@ -27,7 +27,10 @@ THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once +#include + #include "coreneuron/nrnconf.h" +#include "coreneuron/utils/memory.h" namespace coreneuron { #if PG_ACC_BUGS @@ -66,7 +69,7 @@ struct NetReceiveBuffer_t { int _pnt_offset; }; -struct NetSendBuffer_t { +struct NetSendBuffer_t : MemoryManaged { int* _sendtype; // net_send, net_event, net_move int* _vdata_index; int* _pnt_index; @@ -76,6 +79,56 @@ struct NetSendBuffer_t { int _cnt; int _size; /* capacity */ int reallocated; /* if buffer resized/reallocated, needs to be copy to cpu */ + + NetSendBuffer_t(int size) : _size(size) { + _cnt = 0; + + _sendtype = (int*)ecalloc_align(_size, sizeof(int)); + _vdata_index = (int*)ecalloc_align(_size, sizeof(int)); + _pnt_index = (int*)ecalloc_align(_size, sizeof(int)); + _weight_index = (int*)ecalloc_align(_size, sizeof(int)); + // when == 1, NetReceiveBuffer_t is newly allocated (i.e. we need to free previous copy + // and recopy new data + reallocated = 1; + _nsb_t = (double*)ecalloc_align(_size, sizeof(double)); + _nsb_flag = (double*)ecalloc_align(_size, sizeof(double)); + } + + ~NetSendBuffer_t() { + free_memory(_sendtype); + free_memory(_vdata_index); + free_memory(_pnt_index); + free_memory(_weight_index); + free_memory(_nsb_t); + free_memory(_nsb_flag); + } + + void grow() { +#if defined(_OPENACC) + int cannot_reallocate_on_device = 0; + assert(cannot_reallocate_on_device); +#else + int new_size = _size * 2; + grow_buf(&_sendtype, _size, new_size); + grow_buf(&_vdata_index, _size, new_size); + grow_buf(&_pnt_index, _size, new_size); + grow_buf(&_weight_index, _size, new_size); + grow_buf(&_nsb_t, _size, new_size); + grow_buf(&_nsb_flag, _size, new_size); + _size = new_size; +#endif + } + + private: + template + void grow_buf(T** buf, int size, int new_size) { + T* new_buf = nullptr; + new_buf = (T*)ecalloc_align(new_size, sizeof(T)); + memcpy(new_buf, *buf, size * sizeof(T)); + free(*buf); + *buf = new_buf; + } + }; struct Memb_list { diff --git a/external/mod2c b/external/mod2c index 7b0623a2e..5a7f82074 160000 --- a/external/mod2c +++ b/external/mod2c @@ -1 +1 @@ -Subproject commit 7b0623a2e70dc0ab0f143719f725efb2526964b5 +Subproject commit 5a7f820748a0ff8443dc7bdabfb371f2a042d053