NEURON
cxprop.cpp
Go to the documentation of this file.
1 #include "arraypool.h" // ArrayPool
2 #include "hocdec.h" // Datum
3 #include "section.h" // Section
4 #include "structpool.h" // Pool
5 #include "../neuron/model_data.hpp"
6 
7 #include <memory>
8 #include <vector>
9 
10 // allocate and free Datum arrays for nrniv this allows for the possibility of
11 // greater cache efficiency
12 
16 
18 // the advantage of this w.r.t `static std::vector<...> datumpools_;` is that the vector destructor
19 // is not called at application shutdown, see e.g. discussion in
20 // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables around global
21 // variables that are not trivially destructible
22 static auto& datumpools() {
23  static auto& x = *new std::vector<std::unique_ptr<DatumArrayPool>>{};
24  return x;
25 }
26 
27 void nrn_mk_prop_pools(int n) {
28  if (n > datumpools().size()) {
29  datumpools().resize(n);
30  }
31 }
32 
33 Datum* nrn_prop_datum_alloc(int type, int count, Prop* p) {
34  if (!datumpools()[type]) {
35  datumpools()[type] = std::make_unique<DatumArrayPool>(1000, count);
36  }
37  assert(datumpools()[type]->d2() == count);
38  p->_alloc_seq = datumpools()[type]->ntget();
39  auto* const ppd = datumpools()[type]->alloc(); // allocates storage for the datums
40  for (int i = 0; i < count; ++i) {
41  // Call the Datum constructor
42  new (ppd + i) Datum();
43  }
44  return ppd;
45 }
46 
48  if (type >= datumpools().size() || datumpools()[type] == nullptr) {
49  return 0;
50  }
51  return datumpools()[type]->d2();
52 }
53 
54 void nrn_prop_datum_free(int type, Datum* ppd) {
55  // if (type > 1) printf("nrn_prop_datum_free %d %s %p\n", type,
56  // memb_func[type].sym->name, ppd);
57  if (ppd) {
58  auto* const datumpool = datumpools()[type].get();
59  assert(datumpool);
60  // Destruct the Datums
61  auto const count = datumpool->d2();
62  for (auto i = 0; i < count; ++i) {
63  ppd[i].~Datum();
64  }
65  // Deallocate them
66  datumpool->hpfree(ppd);
67  }
68 }
69 
71  if (type >= datumpools().size() || datumpools()[type] == nullptr) {
72  return;
73  }
74  if (auto const size = datumpools()[type]->nget(); size != 0) {
76  "nrn_delete_mechanism_prop_datum(%d):"
77  " refusing to delete storage that still hosts"
78  " %ld instances",
79  type,
80  size);
81  }
82  datumpools()[type] = nullptr;
83 }
84 
86  if (!secpool_) {
87  secpool_ = new SectionPool(1000);
88  }
89  auto* const sec = secpool_->alloc();
90  // Call the Section constructor
91  new (sec) Section();
92  return sec;
93 }
94 
96  // Call the Section destructor
97  s->~Section();
98  secpool_->hpfree(s);
99 }
100 
102  if (!secpool_) {
103  return 0;
104  }
105  return secpool_->is_valid_ptr(v);
106 }
107 
108 void nrn_poolshrink(int shrink) {
109  if (shrink) {
110  for (auto& pdatum: datumpools()) {
111  if (pdatum && pdatum->nget() == 0) {
112  pdatum.reset();
113  }
114  }
116  } else {
117  Printf("poolshrink --- type name (dbluse, size) (datumuse, size)\n");
118  for (auto i = 0; i < datumpools().size(); ++i) {
119  auto const& pdatum = datumpools()[i];
120  if (pdatum) {
121  Printf("%d %s (%ld, %d)\n",
122  i,
123  (memb_func[i].sym ? memb_func[i].sym->name : "noname"),
124  (pdatum ? pdatum->nget() : 0),
125  (pdatum ? pdatum->size() : 0));
126  }
127  }
128  }
129 }
130 
131 // for avoiding interthread cache line sharing
132 // each thread needs its own pool instance
133 void* nrn_pool_create(long count, int itemsize) {
134  return new CharArrayPool(count, itemsize);
135 }
136 void nrn_pool_delete(void* pool) {
137  delete static_cast<CharArrayPool*>(pool);
138 }
139 void* nrn_pool_alloc(void* pool) {
140  return static_cast<CharArrayPool*>(pool)->alloc();
141 }
142 void nrn_pool_free(void* pool, void* item) {
143  static_cast<CharArrayPool*>(pool)->hpfree(static_cast<char*>(item));
144 }
145 void nrn_pool_freeall(void* pool) {
146  static_cast<CharArrayPool*>(pool)->free_all();
147 }
T * alloc()
Definition: structpool.h:113
int is_valid_ptr(void *)
Definition: structpool.h:97
void hpfree(T *)
Definition: structpool.h:127
#define v
Definition: md1redef.h:11
#define sec
Definition: md1redef.h:20
#define i
Definition: md1redef.h:19
void nrn_pool_free(void *pool, void *item)
Definition: cxprop.cpp:142
Datum * nrn_prop_datum_alloc(int type, int count, Prop *p)
Definition: cxprop.cpp:33
void nrn_prop_datum_free(int type, Datum *ppd)
Definition: cxprop.cpp:54
void nrn_mk_prop_pools(int n)
Definition: cxprop.cpp:27
static SectionPool * secpool_
Definition: cxprop.cpp:17
void * nrn_pool_alloc(void *pool)
Definition: cxprop.cpp:139
int nrn_mechanism_prop_datum_count(int type)
Definition: cxprop.cpp:47
void * nrn_pool_create(long count, int itemsize)
Definition: cxprop.cpp:133
int nrn_is_valid_section_ptr(void *v)
Definition: cxprop.cpp:101
void nrn_pool_delete(void *pool)
Definition: cxprop.cpp:136
Section * nrn_section_alloc()
Definition: cxprop.cpp:85
static auto & datumpools()
Definition: cxprop.cpp:22
ArrayPool< char > CharArrayPool
Definition: cxprop.cpp:13
void nrn_pool_freeall(void *pool)
Definition: cxprop.cpp:145
void nrn_poolshrink(int shrink)
Definition: cxprop.cpp:108
void nrn_section_free(Section *s)
Definition: cxprop.cpp:95
Pool< Section > SectionPool
Definition: cxprop.cpp:15
void nrn_delete_mechanism_prop_datum(int type)
Definition: cxprop.cpp:70
void hoc_execerr_ext(const char *fmt,...)
printf style specification of hoc_execerror message.
Definition: fileio.cpp:828
#define assert(ex)
Definition: hocassrt.h:24
int Datum
Definition: nrnconf.h:23
Model & model()
Access the global Model instance.
Definition: model_data.hpp:206
struct Section Section
Definition: neuronapi.h:18
int const size_t const size_t n
Definition: nrngsl.h:10
size_t p
s
Definition: multisend.cpp:521
std::vector< Memb_func > memb_func
Definition: init.cpp:145
short type
Definition: cabvars.h:10
Definition: section.h:231
void shrink_to_fit()
Definition: model_data.hpp:120
Non-template stable handle to a generic value.
int Printf(const char *fmt, Args... args)
Definition: logger.hpp:18