Debian Codegraph @

brian2/devices/cpp_standalone/templates/synapses_create_generator.cpp

raw
1 {# USES_VARIABLES { _synaptic_pre, _synaptic_post, rand,
2 N_incoming, N_outgoing, N,
3 N_pre, N_post, _source_offset, _target_offset } #}
4
5 {# WRITES_TO_READ_ONLY_VARIABLES { _synaptic_pre, _synaptic_post,
6 N_incoming, N_outgoing, N}
7 #}
8 {% extends 'common_synapses.cpp' %}
9
10 {% block extra_headers %}
11 {{ super() }}
12 #include <iostream>
13 #include <set>
14 {% endblock %}
15
16 {% block maincode %}
17 {# Get N_post and N_pre in the correct way, regardless of whether they are
18 constants or scalar arrays#}
19 const size_t _N_pre = {{constant_or_scalar('N_pre', variables['N_pre'])}};
20 const size_t _N_post = {{constant_or_scalar('N_post', variables['N_post'])}};
21 {{_dynamic_N_incoming}}.resize(_N_post + _target_offset);
22 {{_dynamic_N_outgoing}}.resize(_N_pre + _source_offset);
23 size_t _raw_pre_idx, _raw_post_idx;
24 {# For a connect call j='k+i for k in range(0, N_post, 2) if k+i < N_post'
25 "j" is called the "result index" (and "_post_idx" the "result index array", etc.)
26 "i" is called the "outer index" (and "_pre_idx" the "outer index array", etc.)
27 "k" is called the inner variable #}
28
29 // scalar code
30 const size_t _vectorisation_idx = -1;
31 {{scalar_code['setup_iterator']|autoindent}}
32 {{scalar_code['generator_expr']|autoindent}}
33 {{scalar_code['create_cond']|autoindent}}
34 {{scalar_code['update']|autoindent}}
35 for(size_t _{{outer_index}}=0; _{{outer_index}}<_{{outer_index_size}}; _{{outer_index}}++)
36 {
37 bool __cond, _cond;
38 _raw{{outer_index_array}} = _{{outer_index}} + {{outer_index_offset}};
39 {% if not result_index_condition %}
40 {
41 {{vector_code['create_cond']|autoindent}}
42 __cond = _cond;
43 }
44 _cond = __cond;
45 if(!_cond) continue;
46 {% endif %}
47 // Some explanation of this hackery. The problem is that we have multiple code blocks.
48 // Each code block is generated independently of the others, and they declare variables
49 // at the beginning if necessary (including declaring them as const if their values don't
50 // change). However, if two code blocks follow each other in the same C++ scope then
51 // that causes a redeclaration error. So we solve it by putting each block inside a
52 // pair of braces to create a new scope specific to each code block. However, that brings
53 // up another problem: we need the values from these code blocks. I don't have a general
54 // solution to this problem, but in the case of this particular template, we know which
55 // values we need from them so we simply create outer scoped variables to copy the value
56 // into. Later on we have a slightly more complicated problem because the original name
57 // _j has to be used, so we create two variables __j, _j at the outer scope, copy
58 // _j to __j in the inner scope (using the inner scope version of _j), and then
59 // __j to _j in the outer scope (to the outer scope version of _j). This outer scope
60 // version of _j will then be used in subsequent blocks.
61 long _uiter_low;
62 long _uiter_high;
63 long _uiter_step;
64 {% if iterator_func=='sample' %}
65 long _uiter_size;
66 double _uiter_p;
67 {% endif %}
68 {
69 {{vector_code['setup_iterator']|autoindent}}
70 _uiter_low = _iter_low;
71 _uiter_high = _iter_high;
72 _uiter_step = _iter_step;
73 {% if iterator_func=='sample' %}
74 {% if iterator_kwds['sample_size'] == 'fixed' %}
75 _uiter_size = _iter_size;
76 {% else %}
77 _uiter_p = _iter_p;
78 {% endif %}
79 {% endif %}
80 }
81 {% if iterator_func=='range' %}
82 for(long {{inner_variable}}=_uiter_low; {{inner_variable}}<_uiter_high; {{inner_variable}}+=_uiter_step)
83 {
84 {% elif iterator_func=='sample' %}
85 const int _iter_sign = _uiter_step > 0 ? 1 : -1;
86 {% if iterator_kwds['sample_size'] == 'fixed' %}
87 std::set<int> _selected_set = std::set<int>();
88 std::set<int>::iterator _selected_it;
89 int _n_selected = 0;
90 int _n_dealt_with = 0;
91 int _n_total;
92 if (_uiter_step > 0)
93 _n_total = (_uiter_high - _uiter_low - 1) / _uiter_step + 1;
94 else
95 _n_total = (_uiter_low - _uiter_high - 1) / -_uiter_step + 1;
96 // Value determined by benchmarking, see github PR #1280
97 const bool _selection_algo = 1.0*_uiter_size / _n_total > 0.06;
98 if (_uiter_size > _n_total)
99 {
100 {% if skip_if_invalid %}
101 _uiter_size = _n_total;
102 {% else %}
103 cout << "Error: Requested sample size " << _uiter_size << " is bigger than the " <<
104 "population size " << _n_total << "." << endl;
105 exit(1);
106 {% endif %}
107 } else if (_uiter_size < 0)
108 {
109 {% if skip_if_invalid %}
110 continue;
111 {% else %}
112 cout << "Error: Requested sample size " << _uiter_size << " is negative." << endl;
113 exit(1);
114 {% endif %}
115 } else if (_uiter_size == 0)
116 continue;
117 long {{inner_variable}};
118
119 if (_selection_algo)
120 {
121 {{inner_variable}} = _uiter_low - _uiter_step;
122 } else
123 {
124 // For the tracking algorithm, we have to first create all values
125 // to make sure they will be iterated in sorted order
126 _selected_set.clear();
127 while (_n_selected < _uiter_size)
128 {
129 int _r = (int)(_rand(_vectorisation_idx) * _n_total);
130 while (! _selected_set.insert(_r).second)
131 _r = (int)(_rand(_vectorisation_idx) * _n_total);
132 _n_selected++;
133 }
134 _n_selected = 0;
135 _selected_it = _selected_set.begin();
136 }
137 while (_n_selected < _uiter_size)
138 {
139 if (_selection_algo)
140 {
141 // Selection sampling technique
142 // See section 3.4.2 of Donald E. Knuth, AOCP, Vol 2, Seminumerical Algorithms
143 {{inner_variable}} += _uiter_step;
144 _n_dealt_with++;
145 const double _U = _rand(_vectorisation_idx);
146 if ((_n_total - _n_dealt_with) * _U >= _uiter_size - _n_selected)
147 continue;
148 } else
149 {
150 {{inner_variable}} = _uiter_low + (*_selected_it)*_uiter_step;
151 _selected_it++;
152 }
153 _n_selected++;
154 {% else %}
155 if(_uiter_p==0) continue;
156 const bool _jump_algo = _uiter_p<0.25;
157 double _log1p;
158 if(_jump_algo)
159 _log1p = log(1-_uiter_p);
160 else
161 _log1p = 1.0; // will be ignored
162 const double _pconst = 1.0/log(1-_uiter_p);
163 for(long {{inner_variable}}=_uiter_low; _iter_sign*{{inner_variable}}<_iter_sign*_uiter_high; {{inner_variable}} += _uiter_step)
164 {
165 if(_jump_algo) {
166 const double _r = _rand(_vectorisation_idx);
167 if(_r==0.0) break;
168 const int _jump = floor(log(_r)*_pconst)*_uiter_step;
169 {{inner_variable}} += _jump;
170 if (_iter_sign*{{inner_variable}} >= _iter_sign * _uiter_high) continue;
171 } else {
172 if (_rand(_vectorisation_idx)>=_uiter_p) continue;
173 }
174 {% endif %}
175 {% endif %}
176 long __{{result_index}}, _{{result_index}}, {{outer_index_array}}, _{{outer_index_array}};
177 {
178 {{vector_code['generator_expr']|autoindent}}
179 __{{result_index}} = _{{result_index}}; // pick up the locally scoped var and store in outer var
180 _{{outer_index_array}} = {{outer_index_array}};
181 }
182 _{{result_index}} = __{{result_index}}; // make the previously locally scoped var available
183 {{outer_index_array}} = _{{outer_index_array}};
184 _raw{{result_index_array}} = _{{result_index}} + {{result_index_offset}};
185 {% if result_index_condition %}
186 {
187 {% if result_index_used %}
188 {# The condition could index outside of array range #}
189 if(_{{result_index}}<0 || _{{result_index}}>=_{{result_index_size}})
190 {
191 {% if skip_if_invalid %}
192 continue;
193 {% else %}
194 cout << "Error: tried to create synapse to neuron {{result_index}}=" << _{{result_index}} << " outside range 0 to " <<
195 _{{result_index_size}}-1 << endl;
196 exit(1);
197 {% endif %}
198 }
199 {% endif %}
200 {{vector_code['create_cond']|autoindent}}
201 __cond = _cond;
202 }
203 _cond = __cond;
204 {% endif %}
205
206 {% if if_expression!='True' %}
207 if(!_cond) continue;
208 {% endif %}
209 {% if not result_index_used %}
210 {# Otherwise, we already checked before #}
211 if(_{{result_index}}<0 || _{{result_index}}>=_{{result_index_size}})
212 {
213 {% if skip_if_invalid %}
214 continue;
215 {% else %}
216 cout << "Error: tried to create synapse to neuron {{result_index}}=" << _{{result_index}} <<
217 " outside range 0 to " << _{{result_index_size}}-1 << endl;
218 exit(1);
219 {% endif %}
220 }
221 {% endif %}
222 {{vector_code['update']|autoindent}}
223
224 for (size_t _repetition=0; _repetition<_n; _repetition++) {
225 {{_dynamic_N_outgoing}}[_pre_idx] += 1;
226 {{_dynamic_N_incoming}}[_post_idx] += 1;
227 {{_dynamic__synaptic_pre}}.push_back(_pre_idx);
228 {{_dynamic__synaptic_post}}.push_back(_post_idx);
229 }
230 }
231 }
232
233 // now we need to resize all registered variables
234 const int32_t newsize = {{_dynamic__synaptic_pre}}.size();
235 {% for varname in owner._registered_variables | variables_to_array_names(access_data=False) | sort%}
236 {{varname}}.resize(newsize);
237 {% endfor %}
238 // Also update the total number of synapses
239 {{N}} = newsize;
240
241 {% if multisynaptic_index %}
242 // Update the "synapse number" (number of synapses for the same
243 // source-target pair)
244 std::map<std::pair<int32_t, int32_t>, int32_t> source_target_count;
245 for (size_t _i=0; _i<newsize; _i++)
246 {
247 // Note that source_target_count will create a new entry initialized
248 // with 0 when the key does not exist yet
249 const std::pair<int32_t, int32_t> source_target = std::pair<int32_t, int32_t>({{_dynamic__synaptic_pre}}[_i], {{_dynamic__synaptic_post}}[_i]);
250 {{get_array_name(variables[multisynaptic_index], access_data=False)}}[_i] = source_target_count[source_target];
251 source_target_count[source_target]++;
252 }
253 {% endif %}
254 {% endblock %}