NEURON
sptbinq.h
Go to the documentation of this file.
1 //#ifndef tqueue_h
2 //#define tqueue_h
3 
4 // bin queue for the fixed step method for NetCons and PreSyns. Splay tree
5 // for others.
6 // fifo for the NetCons and PreSyns with same delay. Splay tree for
7 // others (especially SelfEvents).
8 // note that most methods below assume a TQItem is in the splay tree
9 // For the bin part, only insert_fifo, and remove make sense,
10 // and forall_callback does the splay tree first and then the bin (so
11 // not in time order)
12 // The bin part assumes a fixed step method.
13 
14 #include <assert.h>
15 
16 #define COLLECT_TQueue_STATISTICS 1
17 template <typename T>
18 struct SPTREE;
19 class TQItemPool;
20 
21 class TQItem {
22  public:
23  TQItem();
24  virtual ~TQItem();
25  bool check();
26  void clear(){};
27 
28  public:
29  void* data_;
30  double t_;
31  TQItem* left_;
32  TQItem* right_;
33  TQItem* parent_;
34  int cnt_; // reused: -1 means it is in the splay tree, >=0 gives bin
35 };
36 
37 // helper class for the TQueue (SplayTBinQueue).
38 class BinQ {
39  public:
40  BinQ();
41  virtual ~BinQ();
42  void enqueue(double tt, TQItem*);
43  void shift(double tt) {
44  assert(!bins_[qpt_]);
45  tt_ = tt;
46  if (++qpt_ >= nbin_) {
47  qpt_ = 0;
48  }
49  }
50  TQItem* top() {
51  return bins_[qpt_];
52  }
53  TQItem* dequeue();
54  double tbin() {
55  return tt_;
56  }
57  // for iteration
58  TQItem* first();
59  TQItem* next(TQItem*);
60  void remove(TQItem*);
61  void resize(int);
62 #if COLLECT_TQueue_STATISTICS
63  public:
64  int nfenq, nfdeq, nfrem;
65 #endif
66  private:
67  double tt_; // time at beginning of qpt_ interval
68  int nbin_, qpt_;
70 };
71 
72 class TQueue {
73  public:
74  TQueue(TQItemPool*, int mkmut = 0);
75  virtual ~TQueue();
76 
77 #if FAST_LEAST
78  TQItem* least() {
79  return least_;
80  }
81  TQItem* second_least(double t);
82 #if USE_PTHREAD
83  double least_t() {
84  double tt;
85  MUTLOCK;
86  if (least_) {
87  tt = least_->t_;
88  } else {
89  tt = 1e15;
90  }
91  MUTUNLOCK;
92  return tt;
93  }
94 #else
95  double least_t() {
96  if (least_) {
97  return least_->t_;
98  } else {
99  return 1e15;
100  }
101  }
102 #endif
103 #else
104  TQItem* least(); // does not remove from TQueue
105  double least_t();
106 #endif
107  TQItem* atomic_dq(double til);
108  TQItem* insert(double t, void* data);
109  TQItem* enqueue_bin(double t, void* data);
111  return binq_->dequeue();
112  }
113  void shift_bin(double t) {
114  ++nshift_;
115  binq_->shift(t);
116  }
117  double tbin() {
118  return binq_->tbin();
119  }
120  TQItem* top() {
121  return binq_->top();
122  }
123  void release(TQItem*);
124  TQItem* find(double t);
125  void remove(TQItem*);
126  void move(TQItem*, double tnew);
127  void move_least(double tnew);
128  void print();
129  void check(const char* errmess);
130  void statistics();
131  void spike_stat(double*);
132  void forall_callback(void (*)(const TQItem*, int));
133  int nshift_;
134  void deleteitem(TQItem*);
135 
136  public:
137  BinQ* binq() {
138  return binq_;
139  }
140 
141  private:
142  double least_t_nolock() {
143  if (least_) {
144  return least_->t_;
145  } else {
146  return 1e15;
147  }
148  }
149  void move_least_nolock(double tnew);
152  TQItem* least_;
153  TQItemPool* tpool_;
154  MUTDEC
155 #if COLLECT_TQueue_STATISTICS
156  unsigned long ninsert, nrem, nleast, nbal, ncmplxrem;
157  unsigned long ncompare, nleastsrch, nfind, nfindsrch, nmove, nfastmove;
158 #endif
159 };
160 
161 //#endif
Definition: sptbinq.h:38
TQItem * top()
Definition: sptbinq.h:50
virtual ~BinQ()
Definition: sptbinq.cpp:294
BinQ()
Definition: sptbinq.cpp:281
void resize(int)
Definition: sptbinq.cpp:301
double tbin()
Definition: sptbinq.h:54
int nfenq
Definition: sptbinq.h:64
TQItem * next(TQItem *)
Definition: sptbinq.cpp:374
void shift(double tt)
Definition: sptbinq.h:43
double tt_
Definition: sptbinq.h:67
TQItem * first()
Iterate in ascending bin order starting at current bin.
Definition: sptbinq.cpp:363
int qpt_
Definition: sptbinq.h:68
int nbin_
Definition: sptbinq.h:68
int nfrem
Definition: sptbinq.h:64
void enqueue(double tt, TQItem *)
Definition: sptbinq.cpp:324
void remove(TQItem *)
Definition: sptbinq.cpp:388
TQItem * dequeue()
Definition: sptbinq.cpp:351
TQItem ** bins_
Definition: sptbinq.h:69
int nfdeq
Definition: sptbinq.h:64
Definition: bbtqueue.h:6
virtual ~TQItem()
void clear()
Definition: sptbinq.h:26
double t_
Definition: bbtqueue.h:23
bool check()
TQItem * right_
Definition: bbtqueue.h:25
int cnt_
Definition: spt2queue.h:24
TQItem * parent_
Definition: bbtqueue.h:26
TQItem * left_
Definition: bbtqueue.h:24
void * data_
Definition: bbtqueue.h:22
SPTREE< TQItem > * sptree_
Definition: sptbinq.h:150
void statistics()
unsigned long nleastsrch
Definition: bbtqueue.h:59
void remove(TQItem *)
TQItem * enqueue_bin(double t, void *data)
Definition: sptbinq.cpp:215
TQItem * dequeue_bin()
Definition: sptbinq.h:110
double least_t()
Definition: bbtqueue.cpp:131
void deleteitem(TQItem *)
Definition: sptbinq.cpp:87
TQItem * top()
Definition: sptbinq.h:120
double least_t_nolock()
Definition: sptbinq.h:142
TQItem * least()
Definition: bbtqueue.cpp:140
void check(const char *errmess)
BinQ * binq()
Definition: sptbinq.h:137
virtual ~TQueue()
TQItemPool * tpool_
Definition: sptbinq.h:153
TQItem * least()
void move(TQItem *, double tnew)
void shift_bin(double t)
Definition: sptbinq.h:113
unsigned long nmove
Definition: bbtqueue.h:59
void release(TQItem *)
Definition: sptbinq.cpp:226
TQItem * least_
Definition: bbtqueue.h:54
TQueue()
Definition: bbtqueue.cpp:90
int nshift_
Definition: sptbinq.h:133
unsigned long ninsert
Definition: bbtqueue.h:58
void print()
unsigned long nrem
Definition: bbtqueue.h:58
unsigned long nfastmove
Definition: bbtqueue.h:59
TQItem * atomic_dq(double til)
Definition: sptbinq.cpp:251
TQItem * find(double t)
unsigned long nfindsrch
Definition: bbtqueue.h:59
unsigned long ncompare
Definition: bbtqueue.h:59
TQItem * insert(double t, void *data)
MUTDEC unsigned long nbal
Definition: sptbinq.h:156
unsigned long nleast
Definition: bbtqueue.h:58
BinQ * binq_
Definition: sptbinq.h:151
unsigned long nfind
Definition: bbtqueue.h:59
void move_least_nolock(double tnew)
Definition: sptbinq.cpp:140
unsigned long ncmplxrem
Definition: bbtqueue.h:58
void forall_callback(void(*)(const TQItem *, int))
double tbin()
Definition: sptbinq.h:117
void move_least(double tnew)
void spike_stat(double *)
double t
Definition: cvodeobj.cpp:59
#define assert(ex)
Definition: hocassrt.h:32
#define MUTDEC
Definition: nrnmutdec.h:68
#define MUTLOCK
Definition: nrnmutdec.h:72
#define MUTUNLOCK
Definition: nrnmutdec.h:73
#define data
Definition: rbtqueue.cpp:49
Definition: sptree.h:40