Debian Codegraph @

brian2/synapses/spikequeue.h

raw
1 #ifndef C_SPIKEQUEUE_H
2 #define C_SPIKEQUEUE_H
3 #include<iostream>
4 #include<vector>
5 #include<map>
6 #include<iterator>
7 #include<algorithm>
8 #include"stdint_compat.h"
9 #include<assert.h>
10 using namespace std;
11
12 //TODO: The data type for indices is currently fixed (int), all floating point
13 // variables (delays, dt) are assumed to use the same data type
14 class CSpikeQueue
15 {
16 public:
17 vector< vector<int32_t> > queue; // queue[(offset+i)%queue.size()] is delay i relative to current time
18 double dt;
19 int offset;
20 bool scalar_delay;
21 int *delays;
22 int32_t source_start;
23 int32_t source_end;
24 int openmp_padding;
25 vector< vector<int> > synapses;
26 // data structures for the store/restore mechanism
27
28 CSpikeQueue(int _source_start, int _source_end)
29 : source_start(_source_start), source_end(_source_end)
30 {
31 queue.resize(1);
32 offset = 0;
33 dt = 0.0;
34 delays = NULL;
35 openmp_padding = 0;
36 scalar_delay = 0;
37 };
38
39 ~CSpikeQueue()
40 {
41 if (delays)
42 {
43 delete[] delays;
44 delays = NULL;
45 }
46 }
47
48 template <typename scalar> void prepare(scalar *real_delays, int n_delays,
49 int32_t *sources, int n_synapses,
50 double _dt)
51 {
52
53 assert(n_delays == 1 || n_delays == n_synapses);
54
55 if (delays)
56 delete [] delays;
57
58 if (dt != 0.0 && dt != _dt)
59 {
60 // dt changed, we have to get the old spikes out of the queue and
61 // reinsert them at the correct positions
62 vector< vector<int32_t> > queue_copy = queue; // does a real copy
63 const double conversion_factor = dt / _dt;
64 const size_t oldsize = queue.size();
65 const size_t newsize = (int)(oldsize * conversion_factor) + 1;
66 queue.clear();
67 queue.resize(newsize);
68 for (size_t i=0; i<oldsize; i++)
69 {
70 vector<int32_t> spikes = queue_copy[(i + offset) % oldsize];
71 queue[(int)(i * conversion_factor + 0.5)] = spikes;
72 }
73 offset = 0;
74 }
75
76 delays = new int[n_delays];
77 synapses.clear();
78 synapses.resize(source_end - source_start);
79
80 // Note that n_synapses and n_delays do not have to be identical
81 // (homogeneous delays are stored as a single scalar), we therefore
82 // use two independent loops to initialize the delays and the synapses
83 // array
84 scalar first_delay = n_delays > 0 ? real_delays[0] : 0.0;
85 int min_delay = (int)(first_delay / _dt + 0.5);
86 int max_delay = min_delay;
87 for (int i=0; i<n_delays; i++)
88 {
89 //round to nearest int
90 delays[i] = (int)(real_delays[i] / _dt + 0.5);
91 if (delays[i] > max_delay)
92 max_delay = delays[i];
93 else if (delays[i] < min_delay)
94 min_delay = delays[i];
95 }
96 for (int i=0; i<n_synapses; i++)
97 synapses[sources[i] - source_start].push_back(i + openmp_padding);
98
99 dt = _dt;
100
101 // Ensure that our spike queue is sufficiently big
102 ensure_delay(max_delay);
103
104 scalar_delay = (min_delay == max_delay);
105 }
106
107 pair <int, vector< vector<int32_t> > > _full_state() const
108 {
109 pair <int, vector< vector<int32_t> > > state(offset, queue);
110 return state;
111 }
112
113 void _clear()
114 {
115 }
116
117 void _restore_from_full_state(const pair <int, vector< vector<int32_t> > > state)
118 {
119 int stored_offset = state.first;
120 vector< vector<int32_t> > stored_queue = state.second;
121 size_t size = stored_queue.size();
122 queue.clear();
123 if (size == 0) // the queue did not exist at the time of the store call
124 size = 1;
125 queue.resize(size);
126 for (size_t i=0; i<stored_queue.size(); i++)
127 queue[i] = stored_queue[i];
128 offset = stored_offset;
129 }
130
131 void expand(size_t newsize)
132 {
133 const size_t n = queue.size();
134 if (newsize <= n)
135 return;
136 // rotate offset back to start (leaves the circular structure unchanged)
137 rotate(queue.begin(), queue.begin()+offset, queue.end());
138 offset = 0;
139 // add new elements
140 queue.resize(newsize);
141 };
142
143 inline void ensure_delay(int delay)
144 {
145 if(delay >= (int)queue.size())
146 {
147 expand(delay+1);
148 }
149 };
150
151 void push(int32_t *spikes, int nspikes)
152 {
153 if(nspikes==0) return;
154 const int start = static_cast<int>(distance(spikes, lower_bound(spikes, spikes+nspikes, source_start)));
155 const int stop = static_cast<int>(distance(spikes, upper_bound(spikes, spikes+nspikes, source_end-1)));
156 const int32_t * __restrict rspikes = spikes;
157 if(scalar_delay)
158 {
159 vector<int32_t> &homog_queue = queue[(offset+delays[0])%queue.size()];
160 for(int idx_spike=start; idx_spike<stop; idx_spike++)
161 {
162 const int idx_neuron = rspikes[idx_spike] - source_start;
163 const int num_indices = synapses[idx_neuron].size();
164 if(num_indices==0) continue;
165 const int* __restrict cur_indices = &(synapses[idx_neuron][0]);
166 const int cur_homog_queue_size = homog_queue.size();
167 homog_queue.resize(cur_homog_queue_size+num_indices);
168 int32_t * __restrict hq = &(homog_queue[cur_homog_queue_size]);
169 for(int idx_indices=0; idx_indices<num_indices; idx_indices++)
170 {
171 hq[idx_indices] = cur_indices[idx_indices];
172 }
173 }
174 } else // (!scalar_delay)
175 {
176 int * __restrict rdelays = delays-openmp_padding;
177 for(int idx_spike=start; idx_spike<stop; idx_spike++)
178 {
179 const int idx_neuron = rspikes[idx_spike] - source_start;
180 const int num_indices = synapses[idx_neuron].size();
181 if(num_indices==0) continue;
182 const int* __restrict cur_indices = &(synapses[idx_neuron][0]);
183 for(int idx_indices=0; idx_indices<num_indices; idx_indices++)
184 {
185 const int synaptic_index = cur_indices[idx_indices];
186 int delay = rdelays[synaptic_index];
187 queue[(offset+delay)%queue.size()].push_back(synaptic_index);
188 }
189 }
190 }
191 };
192
193 inline vector<int32_t>* peek()
194 {
195 return &queue[offset];
196 };
197
198 void advance()
199 {
200 // empty the current queue, note that for most compilers this shouldn't deallocate the memory,
201 // although VC<=7.1 will, so it will be less efficient with that compiler
202 queue[offset].clear();
203 // and advance to the next offset
204 offset = (offset+1)%queue.size();
205 };
206 };
207
208 extern ostream& operator<<(ostream& os, const CSpikeQueue& queue);
209 extern istream& operator>>(istream& is, CSpikeQueue& queue);
210 #endif