CoreNEURON
tqueue.hpp
Go to the documentation of this file.
1 /*
2 # =============================================================================
3 # Copyright (c) 2016 - 2022 Blue Brain Project/EPFL
4 #
5 # See top-level LICENSE file for details.
6 # =============================================================================.
7 */
8 
9 #pragma once
10 
11 /*
12 ** SPTREE: The following type declarations provide the binary tree
13 ** representation of event-sets or priority queues needed by splay trees
14 **
15 ** assumes that data and datb will be provided by the application
16 ** to hold all application specific information
17 **
18 ** assumes that key will be provided by the application, comparable
19 ** with the compare function applied to the addresses of two keys.
20 */
21 // bin queue for the fixed step method for NetCons and PreSyns. Splay tree
22 // for others.
23 // fifo for the NetCons and PreSyns with same delay. Splay tree for
24 // others (especially SelfEvents).
25 // note that most methods below assume a TQItem is in the splay tree
26 // For the bin part, only insert_fifo, and remove make sense,
27 // The bin part assumes a fixed step method.
28 
29 #include <cstdio>
30 #include <cassert>
31 #include <queue>
32 #include <vector>
33 #include <map>
34 #include <utility>
35 
36 namespace coreneuron {
37 #define STRCMP(a, b) (a - b)
38 
39 class TQItem;
40 #define SPBLK TQItem
41 #define leftlink left_
42 #define rightlink right_
43 #define uplink parent_
44 #define cnt cnt_
45 #define key t_
46 
47 struct SPTREE {
48  SPBLK* root; /* root node */
49 
50  /* Statistics, not strictly necessary, but handy for tuning */
51  int enqcmps; /* compares in spenq */
52 };
53 
54 #define spinit sptq_spinit
55 #define spenq sptq_spenq
56 #define spdeq sptq_spdeq
57 #define splay sptq_splay
58 #define sphead sptq_sphead
59 #define spdelete sptq_spdelete
60 
61 extern void spinit(SPTREE*); /* init tree */
62 extern SPBLK* spenq(SPBLK*, SPTREE*); /* insert item into the tree */
63 extern SPBLK* spdeq(SPBLK**); /* return and remove lowest item in subtree */
64 extern void splay(SPBLK*, SPTREE*); /* reorganize tree */
65 extern SPBLK* sphead(SPTREE*); /* return first node in tree */
66 extern void spdelete(SPBLK*, SPTREE*); /* delete node from tree */
67 
68 struct DiscreteEvent;
69 class TQItem {
70  public:
71  DiscreteEvent* data_ = nullptr;
72  double t_ = 0;
73  TQItem* left_ = nullptr;
74  TQItem* right_ = nullptr;
75  TQItem* parent_ = nullptr;
76  int cnt_ = 0; // reused: -1 means it is in the splay tree, >=0 gives bin
77 };
78 
79 using TQPair = std::pair<double, TQItem*>;
80 
81 struct less_time {
82  bool operator()(const TQPair& x, const TQPair& y) const {
83  return x.first > y.first;
84  }
85 };
86 
87 // helper class for the TQueue (SplayTBinQueue).
88 class BinQ {
89  public:
90  BinQ();
91  ~BinQ();
92  void enqueue(double tt, TQItem*);
93  void shift(double tt) {
94  assert(!bins_[qpt_]);
95  tt_ = tt;
96  if (++qpt_ >= nbin_) {
97  qpt_ = 0;
98  }
99  }
100  TQItem* top() {
101  return bins_[qpt_];
102  }
103  TQItem* dequeue();
104  double tbin() {
105  return tt_;
106  }
107  // for iteration
108  TQItem* first();
109  TQItem* next(TQItem*);
110  void remove(TQItem*);
111  void resize(int);
112 
113  private:
114  double tt_; // time at beginning of qpt_ interval
115  int nbin_, qpt_;
117  std::vector<std::vector<TQItem*>> vec_bins;
118 };
119 
121 
122 template <container C = spltree>
123 class TQueue {
124  public:
125  TQueue();
126  ~TQueue();
127 
128  inline TQItem* least() {
129  return least_;
130  }
131  inline TQItem* insert(double t, DiscreteEvent* data);
132  inline TQItem* enqueue_bin(double t, DiscreteEvent* data);
133  inline TQItem* dequeue_bin() {
134  return binq_->dequeue();
135  }
136  inline void shift_bin(double _t_) {
137  ++nshift_;
138  binq_->shift(_t_);
139  }
140  inline TQItem* top() {
141  return binq_->top();
142  }
143 
144  inline TQItem* atomic_dq(double til);
145  inline void remove(TQItem*);
146  inline void move(TQItem*, double tnew);
147  int nshift_;
148 
149  /// Priority queue of vectors for queuing the events. enqueuing for move() and
150  /// move_least_nolock() is not implemented
151  std::priority_queue<TQPair, std::vector<TQPair>, less_time> pq_que_;
152  /// Types of queuing statistics
153  enum qtype { enq = 0, spike, ite, deq };
154 
155  private:
156  double least_t_nolock() {
157  if (least_) {
158  return least_->t_;
159  } else {
160  return 1e15;
161  }
162  }
163  void move_least_nolock(double tnew);
165 
166  public:
168 
169  private:
172  return TQPair(p->t_, p);
173  }
174 };
175 } // namespace coreneuron
coreneuron::TQueue::atomic_dq
TQItem * atomic_dq(double til)
coreneuron::TQueue::binq_
BinQ * binq_
Definition: tqueue.hpp:167
coreneuron::TQueue::enqueue_bin
TQItem * enqueue_bin(double t, DiscreteEvent *data)
Definition: tqueue.ipp:71
coreneuron::TQueue::enq
@ enq
Definition: tqueue.hpp:153
coreneuron::TQueue::deq
@ deq
Definition: tqueue.hpp:153
coreneuron::BinQ::enqueue
void enqueue(double tt, TQItem *)
Definition: tqueue.cpp:68
coreneuron::BinQ::tbin
double tbin()
Definition: tqueue.hpp:104
coreneuron::TQueue::spike
@ spike
Definition: tqueue.hpp:153
coreneuron::BinQ::next
TQItem * next(TQItem *)
Definition: tqueue.cpp:101
coreneuron::TQItem
Definition: tqueue.hpp:69
data
Definition: alignment.cpp:18
coreneuron::TQueue::dequeue_bin
TQItem * dequeue_bin()
Definition: tqueue.hpp:133
coreneuron::TQueue::least_
TQItem * least_
Definition: tqueue.hpp:170
coreneuron::TQItem::right_
TQItem * right_
Definition: tqueue.hpp:74
tqueue.ipp
coreneuron::BinQ::BinQ
BinQ()
Definition: tqueue.cpp:29
coreneuron::TQPair
std::pair< double, TQItem * > TQPair
Definition: tqueue.hpp:79
coreneuron::TQItem::t_
double t_
Definition: tqueue.hpp:72
coreneuron::BinQ::nbin_
int nbin_
Definition: tqueue.hpp:115
coreneuron::TQueue::least
TQItem * least()
Definition: tqueue.hpp:128
coreneuron::SPTREE::enqcmps
int enqcmps
Definition: tqueue.hpp:51
coreneuron::TQueue::sptree_
SPTREE * sptree_
Definition: tqueue.hpp:164
coreneuron::TQueue::nshift_
int nshift_
Definition: tqueue.hpp:147
coreneuron
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
Definition: corenrn_parameters.cpp:12
coreneuron::t
double t
Definition: register_mech.cpp:22
coreneuron::TQueue::remove
void remove(TQItem *)
coreneuron::TQueue::top
TQItem * top()
Definition: tqueue.hpp:140
coreneuron::TQueue::insert
TQItem * insert(double t, DiscreteEvent *data)
coreneuron::BinQ::top
TQItem * top()
Definition: tqueue.hpp:100
coreneuron::BinQ::vec_bins
std::vector< std::vector< TQItem * > > vec_bins
Definition: tqueue.hpp:117
coreneuron::SPTREE
Definition: tqueue.hpp:47
coreneuron::pq_que
@ pq_que
Definition: tqueue.hpp:120
coreneuron::spenq
SPBLK * spenq(SPBLK *n, SPTREE *q)
Definition: tqueue.cpp:197
coreneuron::sphead
SPBLK * sphead(SPTREE *q)
Definition: tqueue.cpp:503
coreneuron::DiscreteEvent
Definition: netcon.hpp:33
coreneuron::TQueue::shift_bin
void shift_bin(double _t_)
Definition: tqueue.hpp:136
coreneuron::spdelete
void spdelete(SPBLK *n, SPTREE *q)
Definition: tqueue.cpp:532
coreneuron::spltree
@ spltree
Definition: tqueue.hpp:120
coreneuron::spdeq
SPBLK * spdeq(SPBLK **np)
Definition: tqueue.cpp:325
coreneuron::BinQ::shift
void shift(double tt)
Definition: tqueue.hpp:93
coreneuron::TQItem::cnt_
int cnt_
Definition: tqueue.hpp:76
coreneuron::TQueue::make_TQPair
TQPair make_TQPair(TQItem *p)
Definition: tqueue.hpp:171
coreneuron::BinQ::bins_
TQItem ** bins_
Definition: tqueue.hpp:116
coreneuron::BinQ
Definition: tqueue.hpp:88
coreneuron::less_time::operator()
bool operator()(const TQPair &x, const TQPair &y) const
Definition: tqueue.hpp:82
coreneuron::TQueue::~TQueue
~TQueue()
Definition: tqueue.ipp:42
coreneuron::BinQ::remove
void remove(TQItem *)
Definition: tqueue.cpp:113
coreneuron::spinit
void spinit(SPTREE *q)
Definition: tqueue.cpp:180
coreneuron::TQueue::move
void move(TQItem *, double tnew)
SPBLK
#define SPBLK
Definition: tqueue.hpp:40
coreneuron::TQueue::least_t_nolock
double least_t_nolock()
Definition: tqueue.hpp:156
coreneuron::splay
void splay(SPBLK *n, SPTREE *q)
Definition: tqueue.cpp:402
coreneuron::TQueue::move_least_nolock
void move_least_nolock(double tnew)
coreneuron::TQueue::pq_que_
std::priority_queue< TQPair, std::vector< TQPair >, less_time > pq_que_
Priority queue of vectors for queuing the events.
Definition: tqueue.hpp:151
coreneuron::TQItem::left_
TQItem * left_
Definition: tqueue.hpp:73
coreneuron::BinQ::qpt_
int qpt_
Definition: tqueue.hpp:115
coreneuron::container
container
Definition: tqueue.hpp:120
coreneuron::TQueue< QTYPE >::qtype
qtype
Types of queuing statistics.
Definition: tqueue.hpp:153
coreneuron::TQItem::data_
DiscreteEvent * data_
Definition: tqueue.hpp:71
coreneuron::SPTREE::root
SPBLK * root
Definition: tqueue.hpp:48
coreneuron::TQueue
Definition: tqueue.hpp:123
coreneuron::BinQ::dequeue
TQItem * dequeue()
Definition: tqueue.cpp:85
coreneuron::BinQ::first
TQItem * first()
Definition: tqueue.cpp:93
coreneuron::BinQ::resize
void resize(int)
Definition: tqueue.cpp:47
coreneuron::less_time
Definition: tqueue.hpp:81
coreneuron::TQueue::ite
@ ite
Definition: tqueue.hpp:153
coreneuron::BinQ::~BinQ
~BinQ()
Definition: tqueue.cpp:39
coreneuron::BinQ::tt_
double tt_
Definition: tqueue.hpp:114
coreneuron::TQueue::TQueue
TQueue()
Definition: tqueue.ipp:33
coreneuron::TQItem::parent_
TQItem * parent_
Definition: tqueue.hpp:75