NEURON
netcon.h
Go to the documentation of this file.
1 #ifndef netcon_h
2 #define netcon_h
3 
4 #undef check
5 #if MAC
6 #define NetCon nrniv_Dinfo
7 #endif
8 
9 #include <InterViews/observe.h>
10 #include <OS/list.h>
11 #include "htlist.h"
12 #include "nrnneosm.h"
13 #include "nrnmpi.h"
14 #include <unordered_map>
15 #include <memory>
16 #include <vector>
17 
18 #if 0
19 #define STATISTICS(arg) ++arg
20 #else
21 #define STATISTICS(arg) /**/
22 #endif
23 
24 class PreSyn;
25 class PlayRecord;
26 class Cvode;
27 class TQueue;
28 class TQItem;
29 struct NrnThread;
30 class NetCvode;
31 class HocEventPool;
32 class HocCommand;
33 class NetConSaveWeightTable;
34 class NetConSaveIndexTable;
35 class PreSynSaveIndexTable;
36 class STETransition;
37 class IvocVect;
38 class BGP_DMASend;
39 class BGP_DMASend_Phase2;
40 class Point_process;
41 using SelfEventPPTable = std::unordered_map< long, Point_process*> ;
42 
43 #define DiscreteEventType 0
44 #define TstopEventType 1
45 #define NetConType 2
46 #define SelfEventType 3
47 #define PreSynType 4
48 #define HocEventType 5
49 #define PlayRecordEventType 6
50 // the above will in turn steer to proper PlayRecord type
51 #define NetParEventType 7
52 
53 #if DISCRETE_EVENT_OBSERVER
54 class DiscreteEvent : public Observer {
55 #else
57 #endif
58 public:
59  DiscreteEvent();
60  virtual ~DiscreteEvent();
61  virtual void send(double deliverytime, NetCvode*, NrnThread*);
62  virtual void deliver(double t, NetCvode*, NrnThread*);
63  virtual void pr(const char*, double t, NetCvode*);
64  virtual void disconnect(Observable*) {};
65  virtual int pgvts_op(int& i) { i = 0; return 2; }
66  virtual void pgvts_deliver(double t, NetCvode*);
67  virtual NrnThread* thread();
68 
69  virtual int type() { return DiscreteEventType; }
70  virtual DiscreteEvent* savestate_save();
71  virtual void savestate_restore(double deliverytime, NetCvode*);
72  virtual void savestate_write(FILE*);
73  static DiscreteEvent* savestate_read(FILE*);
74 
75  // actions performed over each item in the event queue.
76  virtual void frecord_init(TQItem*) {};
77 
78  static unsigned long discretevent_send_;
79  static unsigned long discretevent_deliver_;
80 };
81 
82 class NetCon : public DiscreteEvent {
83 public:
84  NetCon();
85  NetCon(PreSyn* src, Object* target);
86  virtual ~NetCon();
87  virtual void send(double sendtime, NetCvode*, NrnThread*);
88  virtual void deliver(double, NetCvode*, NrnThread*);
89  virtual void pr(const char*, double t, NetCvode*);
90  virtual int pgvts_op(int& i) { i = 1; return 2; }
91  virtual void pgvts_deliver(double t, NetCvode*);
92  virtual NrnThread* thread();
93 
94  virtual int type() { return NetConType; }
95  virtual DiscreteEvent* savestate_save();
96  static DiscreteEvent* savestate_read(FILE*);
97 
98  void chksrc();
99  void chktar();
100  void rmsrc();
101  void replace_src(PreSyn*);
102  virtual void disconnect(Observable*);
103 
104  double delay_;
107  double* weight_;
109  int cnt_;
110  bool active_;
111 
112  static unsigned long netcon_send_active_;
113  static unsigned long netcon_send_inactive_;
114  static unsigned long netcon_deliver_;
115 };
116 class NetConSave : public DiscreteEvent {
117 public:
118  NetConSave(NetCon*);
119  virtual ~NetConSave();
120  virtual void savestate_restore(double deliverytime, NetCvode*);
121  virtual void savestate_write(FILE*);
123 
124  static void invalid();
125  static NetCon* weight2netcon(double*);
126  static NetCon* index2netcon(long);
127 private:
128  static NetConSaveWeightTable* wtable_;
129  static NetConSaveIndexTable* idxtable_;
130 };
131 
132 class SelfEvent : public DiscreteEvent {
133 public:
134  SelfEvent();
135  virtual ~SelfEvent();
136  virtual void deliver(double, NetCvode*, NrnThread*);
137  virtual void pr(const char*, double t, NetCvode*);
138  void clear(){} // called by sepool_->free_all
139  virtual int pgvts_op(int& i) { i = 1; return 2; }
140  virtual void pgvts_deliver(double t, NetCvode*);
141 
142  virtual int type() { return SelfEventType; }
143  virtual DiscreteEvent* savestate_save();
144  virtual void savestate_restore(double deliverytime, NetCvode*);
145  virtual void savestate_write(FILE*);
146  static DiscreteEvent* savestate_read(FILE*);
147  virtual NrnThread* thread();
148 
149  double flag_;
151  double* weight_;
152  void** movable_; // actually a TQItem**
153 
154  static unsigned long selfevent_send_;
155  static unsigned long selfevent_move_;
156  static unsigned long selfevent_deliver_;
157  static void savestate_free();
158 private:
159  void call_net_receive(NetCvode*);
160  static Point_process* index2pp(int type, int oindex);
161  static std::unique_ptr<SelfEventPPTable> sepp_;
162 };
163 
164 using NetConPList = std::vector<NetCon*>;
165 
167 public:
168  // condition detection factored out of PreSyn for re-use
169  ConditionEvent();
170  virtual ~ConditionEvent();
171  virtual void check(NrnThread*, double sendtime, double teps = 0.0);
172  virtual double value() { return -1.; }
173  void condition(Cvode*);
174  void abandon_statistics(Cvode*);
175  virtual void asf_err() = 0;
176 
177  double valold_, told_;
178  double valthresh_; // go below this to reset threshold detector.
180  bool flag_; // true when below, false when above.
181 
182  static unsigned long init_above_;
183  static unsigned long send_qthresh_;
184  static unsigned long abandon_;
185  static unsigned long eq_abandon_;
186  static unsigned long abandon_init_above_;
187  static unsigned long abandon_init_below_;
188  static unsigned long abandon_above_;
189  static unsigned long abandon_below_;
190  static unsigned long deliver_qthresh_;
191 };
192 
193 class WatchCondition : public ConditionEvent, public HTList {
194 public:
196  virtual ~WatchCondition();
197  virtual double value() { return (*c_)(pnt_); }
198  virtual void send(double, NetCvode*, NrnThread*);
199  virtual void deliver(double, NetCvode*, NrnThread*);
200  virtual void pr(const char*, double t, NetCvode*);
201  void activate(double flag);
202  virtual void asf_err();
203  virtual int pgvts_op(int& i) { i = 1; return 2; }
204  virtual void pgvts_deliver(double t, NetCvode*);
205  virtual NrnThread* thread();
206 
207  double nrflag_;
209  double(*c_)(Point_process*);
210  // For WatchCondition transfer to CoreNEURON.
211  // Could be figured out from watch semantics and
212  // the index where this == _watch_array[index]
213  // At least this avoids a search over the _watch_array.
215 
216  static unsigned long watch_send_;
217  static unsigned long watch_deliver_;
218 };
219 
220 class STECondition : public WatchCondition {
221 public:
222  STECondition(Point_process*, double(*)(Point_process*) = NULL);
223  virtual ~STECondition();
224  virtual void deliver(double, NetCvode*, NrnThread*);
225  virtual void pgvts_deliver(double t, NetCvode*);
226  virtual double value();
227  virtual NrnThread* thread();
228 
230 };
231 
232 class PreSyn : public ConditionEvent {
233 public:
234  PreSyn(double* src, Object* osrc, Section* ssrc = nil);
235  virtual ~PreSyn();
236  virtual void send(double sendtime, NetCvode*, NrnThread*);
237  virtual void deliver(double, NetCvode*, NrnThread*);
238  virtual void pr(const char*, double t, NetCvode*);
239  virtual void asf_err();
240  virtual int pgvts_op(int& i) { i = 0; return 0; }
241  virtual void pgvts_deliver(double t, NetCvode*);
242  virtual NrnThread* thread();
243 
244  virtual int type() { return PreSynType; }
245  virtual DiscreteEvent* savestate_save();
246  static DiscreteEvent* savestate_read(FILE*);
247 
248  virtual double value() { return *thvar_ - threshold_; }
249 
250  void update(Observable*);
251  void disconnect(Observable*);
252  void update_ptr(double*);
253  void record_stmt(const char*);
254  void record_stmt(Object*);
255  void record(IvocVect*, IvocVect* idvec = nil, int rec_id = 0);
256  void record(double t);
257  void init();
258  double mindelay();
259  void fanout(double, NetCvode*, NrnThread*); // used by bbsavestate
260 
262  double threshold_;
263  double delay_;
264  double* thvar_;
271  hoc_Item* hi_; // in the netcvode psl_
272  hoc_Item* hi_th_; // in the netcvode psl_th_
273  long hi_index_; // for SaveState read and write
275  int rec_id_;
277  int gid_;
278 #if NRNMPI
279  unsigned char localgid_; // compressed gid for spike transfer
280 #endif
281 #if NRN_MUSIC
282  void* music_port_;
283 #endif
284 #if BGPDMA
285  union { // A PreSyn cannot be both a source spike generator
286  // and a receiver of off-host spikes.
287  BGP_DMASend* dma_send_;
288  BGP_DMASend_Phase2* dma_send_phase2_;
289  int srchost_;
290  } bgp;
291 #endif
292 
293  static unsigned long presyn_send_mindelay_;
294  static unsigned long presyn_send_direct_;
295  static unsigned long presyn_deliver_netcon_;
296  static unsigned long presyn_deliver_direct_;
297  static unsigned long presyn_deliver_ncsend_;
298 };
299 class PreSynSave : public DiscreteEvent {
300 public:
301  PreSynSave(PreSyn*);
302  virtual ~PreSynSave();
303  virtual void savestate_restore(double deliverytime, NetCvode*);
304  virtual void savestate_write(FILE*);
307 
308  static void invalid();
309  static PreSyn* hindx2presyn(long);
310 private:
311  static PreSynSaveIndexTable* idxtable_;
312 };
313 
314 
315 class HocEvent : public DiscreteEvent {
316 public:
317  HocEvent();
318  virtual ~HocEvent();
319  virtual void pr(const char*, double t, NetCvode*);
320  static HocEvent* alloc(const char* stmt, Object*, int, Object* pyact=nil);
321  void hefree();
322  void clear(); // called by hepool_->free_all
323  virtual void deliver(double, NetCvode*, NrnThread*);
324  virtual void allthread_handle();
325  static void reclaim();
326  virtual int pgvts_op(int& i) { i = 0; return 2; }
327  virtual void pgvts_deliver(double t, NetCvode*);
328  HocCommand* stmt() { return stmt_; }
329 
330  virtual int type() { return HocEventType; }
331  virtual DiscreteEvent* savestate_save();
332  virtual void savestate_restore(double deliverytime, NetCvode*);
333  virtual void savestate_write(FILE*);
334  static DiscreteEvent* savestate_read(FILE*);
335 
336  static unsigned long hocevent_send_;
337  static unsigned long hocevent_deliver_;
338 private:
341  int reinit_;
343  static HocEventPool* hepool_;
344 };
345 
346 class TstopEvent : public DiscreteEvent {
347 public:
348  TstopEvent();
349  virtual ~TstopEvent();
350  virtual void deliver(double t, NetCvode*, NrnThread*);
351  virtual void pr(const char*, double t, NetCvode*);
352  virtual int pgvts_op(int& i) { i = 0; return 2; }
353  virtual void pgvts_deliver(double t, NetCvode*);
354 
355  virtual int type() { return TstopEventType; }
356  virtual DiscreteEvent* savestate_save();
357  virtual void savestate_restore(double deliverytime, NetCvode*);
358  virtual void savestate_write(FILE*);
359  static DiscreteEvent* savestate_read(FILE*);
360 };
361 
362 class NetParEvent : public DiscreteEvent {
363 public:
364  NetParEvent();
365  virtual ~NetParEvent();
366  virtual void send(double, NetCvode*, NrnThread*);
367  virtual void deliver(double, NetCvode*, NrnThread*);
368  virtual void pr(const char*, double t, NetCvode*);
369  virtual int pgvts_op(int& i) { i = 0; return 4; }
370  virtual void pgvts_deliver(double t, NetCvode*);
371 
372  virtual int type() { return NetParEventType; }
373  virtual DiscreteEvent* savestate_save();
374  virtual void savestate_restore(double deliverytime, NetCvode*);
375  virtual void savestate_write(FILE*);
376  static DiscreteEvent* savestate_read(FILE*);
377 public:
378  double wx_, ws_; // exchange time and "spikes to Presyn" time
379  int ithread_; // for pr()
380 };
381 
382 extern PreSyn* nrn_gid2outputpresyn(int gid);
383 
384 #endif
static unsigned long abandon_init_below_
Definition: netcon.h:187
IvocVect * tvec_
Definition: netcon.h:267
static unsigned long send_qthresh_
Definition: netcon.h:183
int cnt_
Definition: netcon.h:109
HocCommand * stmt_
Definition: netcon.h:339
bool flag_
Definition: netcon.h:180
virtual double value()
Definition: netcon.h:197
std::unordered_map< long, Point_process * > SelfEventPPTable
Definition: netcon.h:41
static unsigned long presyn_send_direct_
Definition: netcon.h:294
static unsigned long hocevent_deliver_
Definition: netcon.h:337
Definition: netcon.h:232
HocCommand * stmt()
Definition: netcon.h:328
static unsigned long abandon_below_
Definition: netcon.h:189
bool active_
Definition: netcon.h:110
virtual int pgvts_op(int &i)
Definition: netcon.h:90
virtual int pgvts_op(int &i)
Definition: netcon.h:352
virtual void disconnect(Observable *)
Definition: netcon.h:64
NrnThread * nt_
Definition: netcon.h:270
bool have_qthresh_
Definition: netcon.h:306
static unsigned long abandon_init_above_
Definition: netcon.h:186
Represent main neuron object computed by single thread.
Definition: multicore.h:58
virtual int pgvts_op(int &i)
Definition: netcon.h:139
HocCommand * stmt_
Definition: netcon.h:269
STETransition * stet_
Definition: netcon.h:229
int output_index_
Definition: netcon.h:276
static DiscreteEvent * savestate_read(FILE *)
Definition: netcvode.cpp:4658
Object * obj_
Definition: netcon.h:108
Object * ppobj_
Definition: netcon.h:340
static NetConSaveIndexTable * idxtable_
Definition: netcon.h:129
static void update(NrnThread *)
Definition: fadvance.cpp:570
Object * osrc_
Definition: netcon.h:265
Point_process * target_
Definition: netcon.h:106
IvocVect * idvec_
Definition: netcon.h:268
Section * ssrc_
Definition: netcon.h:266
PreSyn * nrn_gid2outputpresyn(int gid)
Definition: netpar.cpp:1487
#define PreSynType
Definition: netcon.h:47
int ithread_
Definition: netcon.h:379
static unsigned long netcon_deliver_
Definition: netcon.h:114
std::vector< NetCon * > NetConPList
Definition: netcon.h:164
virtual int pgvts_op(int &i)
Definition: netcon.h:369
Definition: bbtqueue.h:6
double delay_
Definition: netcon.h:104
virtual int type()
Definition: netcon.h:355
Point_process * pnt_
Definition: netcon.h:208
static unsigned long selfevent_send_
Definition: netcon.h:154
int watch_index_
Definition: netcon.h:214
PreSyn * src_
Definition: netcon.h:105
static NetConSaveWeightTable * wtable_
Definition: netcon.h:128
static unsigned long selfevent_move_
Definition: netcon.h:155
void init()
Definition: init.cpp:169
virtual int pgvts_op(int &i)
Definition: netcon.h:203
int use_min_delay_
Definition: netcon.h:274
virtual void frecord_init(TQItem *)
Definition: netcon.h:76
static double check(double t, Daspk *ida)
Definition: nrndaspk.cpp:214
double threshold_
Definition: netcon.h:262
virtual NrnThread * thread()
Definition: netcvode.cpp:3078
virtual int type()
Definition: netcon.h:330
static unsigned long deliver_qthresh_
Definition: netcon.h:190
virtual int pgvts_op(int &i)
Definition: netcon.h:326
int rec_id_
Definition: netcon.h:275
static unsigned long discretevent_deliver_
Definition: netcon.h:79
double nrflag_
Definition: netcon.h:207
double valold_
Definition: netcon.h:177
static HocEventPool * hepool_
Definition: netcon.h:343
virtual int type()
Definition: netcon.h:69
static unsigned long presyn_deliver_direct_
Definition: netcon.h:296
static unsigned long presyn_send_mindelay_
Definition: netcon.h:293
static unsigned long netcon_send_inactive_
Definition: netcon.h:113
#define DiscreteEventType
Definition: netcon.h:43
static unsigned long watch_deliver_
Definition: netcon.h:217
static unsigned long presyn_deliver_ncsend_
Definition: netcon.h:297
static unsigned long eq_abandon_
Definition: netcon.h:185
void ** movable_
Definition: netcon.h:152
double * thvar_
Definition: netcon.h:264
#define NetConType
Definition: netcon.h:45
virtual int pgvts_op(int &i)
Definition: netcon.h:65
long hi_index_
Definition: netcon.h:273
virtual void deliver(double t, NetCvode *, NrnThread *)
Definition: netcvode.cpp:3074
static unsigned long init_above_
Definition: netcon.h:182
virtual void send(double deliverytime, NetCvode *, NrnThread *)
Definition: netcvode.cpp:3069
static HocEvent * next_del_
Definition: netcon.h:342
double * weight_
Definition: netcon.h:151
virtual int type()
Definition: netcon.h:244
#define nil
Definition: enter-scope.h:36
virtual void savestate_write(FILE *)
Definition: netcvode.cpp:4662
Definition: netcon.h:82
double * weight_
Definition: netcon.h:107
int gid_
Definition: netcon.h:277
virtual int type()
Definition: netcon.h:372
static unsigned long discretevent_send_
Definition: netcon.h:76
static uint32_t value
Definition: scoprand.cpp:26
double flag_
Definition: netcon.h:149
static unsigned long netcon_send_active_
Definition: netcon.h:112
virtual void pgvts_deliver(double t, NetCvode *)
Definition: netcvode.cpp:3080
virtual double value()
Definition: netcon.h:248
Definition: htlist.h:35
static unsigned long selfevent_deliver_
Definition: netcon.h:156
hoc_Item * hi_
Definition: netcon.h:271
TQItem * qthresh_
Definition: netcon.h:179
Definition: hocdec.h:226
static unsigned long watch_send_
Definition: netcon.h:216
static unsigned long hocevent_send_
Definition: netcon.h:336
Definition: cvodeobj.h:75
virtual void savestate_restore(double deliverytime, NetCvode *)
Definition: netcvode.cpp:4652
NetConPList dil_
Definition: netcon.h:261
#define i
Definition: md1redef.h:12
#define SelfEventType
Definition: netcon.h:46
virtual int type()
Definition: netcon.h:94
static void activate()
static std::unique_ptr< SelfEventPPTable > sepp_
Definition: netcon.h:161
hoc_Item * hi_th_
Definition: netcon.h:272
static unsigned long presyn_deliver_netcon_
Definition: netcon.h:295
int reinit_
Definition: netcon.h:341
static unsigned long abandon_
Definition: netcon.h:184
static PreSynSaveIndexTable * idxtable_
Definition: netcon.h:311
virtual int type()
Definition: netcon.h:142
void clear()
Definition: netcon.h:138
double t
Definition: init.cpp:123
double valthresh_
Definition: netcon.h:178
#define NetParEventType
Definition: netcon.h:51
#define HocEventType
Definition: netcon.h:48
virtual ~DiscreteEvent()
Definition: netcvode.cpp:4641
NetCon * netcon_
Definition: netcon.h:122
PreSyn * presyn_
Definition: netcon.h:305
static unsigned long abandon_above_
Definition: netcon.h:188
return NULL
Definition: cabcode.cpp:461
virtual double value()
Definition: netcon.h:172
virtual int pgvts_op(int &i)
Definition: netcon.h:240
double wx_
Definition: netcon.h:378
static realtype c_
Point_process * target_
Definition: netcon.h:150
double delay_
Definition: netcon.h:263
virtual void pr(const char *, double t, NetCvode *)
Definition: netcvode.cpp:3084
#define TstopEventType
Definition: netcon.h:44
virtual DiscreteEvent * savestate_save()
Definition: netcvode.cpp:4643