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