| 1 |
{# USES_VARIABLES { _synaptic_pre, _synaptic_post, rand, N, |
| 2 |
N_pre, N_post, _source_offset, _target_offset } #} |
| 3 |
{# WRITES_TO_READ_ONLY_VARIABLES { _synaptic_pre, _synaptic_post, N} #} |
| 4 |
{# ITERATE_ALL { _idx } #} |
| 5 |
{% extends 'common_group.pyx' %} |
| 6 |
|
| 7 |
######################## TEMPLATE SUPPORT CODE ############################## |
| 8 |
|
| 9 |
{% block template_support_code %} |
| 10 |
from libc.string cimport memcpy |
| 11 |
|
| 12 |
cdef int _buffer_size = 1024 |
| 13 |
cdef int[:] _prebuf = _numpy.zeros(_buffer_size, dtype=_numpy.int32) |
| 14 |
cdef int[:] _postbuf = _numpy.zeros(_buffer_size, dtype=_numpy.int32) |
| 15 |
cdef int _curbuf = 0 |
| 16 |
cdef int _raw_pre_idx |
| 17 |
cdef int _raw_post_idx |
| 18 |
|
| 19 |
# We now update this function to be a use direct dynamic array pointers |
| 20 |
cdef void _flush_buffer(int[:] buf,DynamicArray1DCpp[int32_t]* dynarr, int buf_len): |
| 21 |
cdef size_t _curlen = dynarr.size() |
| 22 |
cdef size_t _newlen = _curlen+buf_len |
| 23 |
# Resize the array |
| 24 |
dynarr.resize(_newlen) |
| 25 |
# Get raw data pointer from C++ array |
| 26 |
cdef int32_t* data_ptr = dynarr.get_data_ptr() |
| 27 |
|
| 28 |
# Use memcpy for fast bulk copy |
| 29 |
memcpy(&data_ptr[_curlen], &buf[0], buf_len * sizeof(int32_t)) |
| 30 |
|
| 31 |
{% endblock %} |
| 32 |
|
| 33 |
######################## MAIN CODE ############################## |
| 34 |
|
| 35 |
{% block maincode %} |
| 36 |
|
| 37 |
cdef int* _prebuf_ptr = &(_prebuf[0]) |
| 38 |
cdef int* _postbuf_ptr = &(_postbuf[0]) |
| 39 |
|
| 40 |
global _curbuf |
| 41 |
|
| 42 |
cdef size_t oldsize = {{_dynamic__synaptic_pre_ptr}}.size() |
| 43 |
cdef size_t newsize |
| 44 |
|
| 45 |
# The following variables are only used for probabilistic connections |
| 46 |
{% if iterator_func=='sample' %} |
| 47 |
cdef int _iter_sign |
| 48 |
{% if iterator_kwds['sample_size'] == 'fixed' %} |
| 49 |
cdef bool _selection_algo |
| 50 |
cdef set[int] _selected_set = set[int]() |
| 51 |
cdef set[int].iterator _selected_it |
| 52 |
cdef int _n_selected |
| 53 |
cdef int _n_dealt_with |
| 54 |
cdef int _n_total |
| 55 |
cdef double _U |
| 56 |
{% else %} |
| 57 |
cdef bool _jump_algo |
| 58 |
cdef double _log1p, _pconst |
| 59 |
cdef size_t _jump |
| 60 |
{% endif %} |
| 61 |
{% endif %} |
| 62 |
|
| 63 |
{# For a connect call j='k+i for k in range(0, N_post, 2) if k+i < N_post' |
| 64 |
"j" is called the "result index" (and "_post_idx" the "result index array", etc.) |
| 65 |
"i" is called the "outer index" (and "_pre_idx" the "outer index array", etc.) |
| 66 |
"k" is called the inner variable #} |
| 67 |
|
| 68 |
# scalar code |
| 69 |
_vectorisation_idx = 1 |
| 70 |
{{scalar_code['setup_iterator']|autoindent}} |
| 71 |
{{scalar_code['generator_expr']|autoindent}} |
| 72 |
{{scalar_code['create_cond']|autoindent}} |
| 73 |
{{scalar_code['update']|autoindent}} |
| 74 |
|
| 75 |
for _{{outer_index}} in range({{outer_index_size}}): |
| 76 |
_raw{{outer_index_array}} = _{{outer_index}} + {{outer_index_offset}} |
| 77 |
|
| 78 |
{% if not result_index_condition %} |
| 79 |
{{vector_code['create_cond']|autoindent}} |
| 80 |
if not _cond: |
| 81 |
continue |
| 82 |
{% endif %} |
| 83 |
{{vector_code['setup_iterator']|autoindent}} |
| 84 |
{% if iterator_func=='range' %} |
| 85 |
for {{inner_variable}} in range(_iter_low, _iter_high, _iter_step): |
| 86 |
{% elif iterator_func=='sample' %} |
| 87 |
{% if iterator_kwds['sample_size'] == 'fixed' %} |
| 88 |
# Note that the following code is written in a slightly convoluted way, |
| 89 |
# but we have to plug it together with the following code that checks |
| 90 |
# for the fulfillment of the condition. |
| 91 |
_n_selected = 0 |
| 92 |
_n_dealt_with = 0 |
| 93 |
with _cython.cdivision(True): |
| 94 |
if _iter_step > 0: |
| 95 |
_n_total = (_iter_high - _iter_low - 1) // _iter_step + 1 |
| 96 |
else: |
| 97 |
_n_total = (_iter_low - _iter_high - 1) // -_iter_step + 1 |
| 98 |
# Value determined by benchmarking, see github PR #1280 |
| 99 |
_selection_algo = 1.0*_iter_size / _n_total > 0.06 |
| 100 |
if _iter_size > _n_total: |
| 101 |
{% if skip_if_invalid %} |
| 102 |
_iter_size = _n_total |
| 103 |
{% else %} |
| 104 |
raise IndexError(f"Requested sample size {_iter_size} is bigger than the " |
| 105 |
f"population size {_n_total}.") |
| 106 |
{% endif %} |
| 107 |
elif _iter_size < 0: |
| 108 |
{% if skip_if_invalid %} |
| 109 |
continue |
| 110 |
{% else %} |
| 111 |
raise IndexError(f"Requested sample size {_iter_size} is negative.") |
| 112 |
{% endif %} |
| 113 |
if _selection_algo: |
| 114 |
{{inner_variable}} = _iter_low - _iter_step |
| 115 |
else: |
| 116 |
# For the tracking algorithm, we have to first create all values |
| 117 |
# to make sure they will be iterated in sorted order |
| 118 |
_selected_set.clear() |
| 119 |
while _n_selected < _iter_size: |
| 120 |
_r = <int> (_rand(_vectorisation_idx) * _n_total) |
| 121 |
while not _selected_set.insert(_r).second: # .second will be False if duplicate |
| 122 |
_r = <int> (_rand(_vectorisation_idx) * _n_total) |
| 123 |
_n_selected += 1 |
| 124 |
_n_selected = 0 |
| 125 |
_selected_it = _selected_set.begin() |
| 126 |
|
| 127 |
while _n_selected < _iter_size: |
| 128 |
if _selection_algo: |
| 129 |
{{inner_variable}} += _iter_step |
| 130 |
# Selection sampling technique |
| 131 |
# See section 3.4.2 of Donald E. Knuth, AOCP, Vol 2, |
| 132 |
# Seminumerical Algorithms |
| 133 |
_n_dealt_with += 1 |
| 134 |
_U = _rand(_vectorisation_idx) |
| 135 |
if (_n_total - _n_dealt_with) * _U >= _iter_size - _n_selected: |
| 136 |
continue |
| 137 |
else: |
| 138 |
{{inner_variable}} = _iter_low + _deref(_selected_it)*_iter_step |
| 139 |
_preinc(_selected_it) |
| 140 |
_n_selected += 1 |
| 141 |
|
| 142 |
{% else %} |
| 143 |
if _iter_p==0: |
| 144 |
continue |
| 145 |
if _iter_step < 0: |
| 146 |
_iter_sign = -1 |
| 147 |
else: |
| 148 |
_iter_sign = 1 |
| 149 |
_jump_algo = _iter_p<0.25 |
| 150 |
if _jump_algo: |
| 151 |
_log1p = log(1-_iter_p) |
| 152 |
else: |
| 153 |
_log1p = 1.0 # will be ignored |
| 154 |
_pconst = 1.0/_log1p |
| 155 |
{{inner_variable}} = _iter_low-_iter_step |
| 156 |
while _iter_sign*({{inner_variable}} + _iter_step) < _iter_sign*_iter_high: |
| 157 |
{{inner_variable}} += _iter_step |
| 158 |
if _jump_algo: |
| 159 |
_jump = <int>(log(_rand(_vectorisation_idx))*_pconst)*_iter_step |
| 160 |
{{inner_variable}} += _jump |
| 161 |
if _iter_sign*{{inner_variable}} >= _iter_sign*_iter_high: |
| 162 |
break |
| 163 |
else: |
| 164 |
if _rand(_vectorisation_idx)>=_iter_p: |
| 165 |
continue |
| 166 |
{% endif %} |
| 167 |
{% endif %} |
| 168 |
|
| 169 |
{{vector_code['generator_expr']|autoindent}} |
| 170 |
_raw{{result_index_array}} = _{{result_index}} + {{result_index_offset}} |
| 171 |
|
| 172 |
{% if result_index_condition %} |
| 173 |
{% if result_index_used %} |
| 174 |
{# The condition could index outside of array range #} |
| 175 |
if _{{result_index}}<0 or _{{result_index}}>={{result_index_size}}: |
| 176 |
{% if skip_if_invalid %} |
| 177 |
continue |
| 178 |
{% else %} |
| 179 |
# Note that with Jinja using a lot of curly braces, it is a better |
| 180 |
# solution to use the outdated % syntax instead of f-strings here. |
| 181 |
raise IndexError("index {{result_index}}=%d outside allowed range from 0 to %d" % (_{{result_index}}, {{result_index_size}}-1)) |
| 182 |
{% endif %} |
| 183 |
{% endif %} |
| 184 |
{{vector_code['create_cond']|autoindent}} |
| 185 |
{% endif %} |
| 186 |
{% if if_expression!='True' and result_index_condition %} |
| 187 |
if not _cond: |
| 188 |
continue |
| 189 |
{% endif %} |
| 190 |
{% if not result_index_used %} |
| 191 |
{# Otherwise, we already checked before #} |
| 192 |
if _{{result_index}}<0 or _{{result_index}}>={{result_index_size}}: |
| 193 |
{% if skip_if_invalid %} |
| 194 |
continue |
| 195 |
{% else %} |
| 196 |
raise IndexError("index j=%d outside allowed range from 0 to %d" % (_{{result_index}}, {{result_index_size}}-1)) |
| 197 |
{% endif %} |
| 198 |
{% endif %} |
| 199 |
{{vector_code['update']|autoindent}} |
| 200 |
|
| 201 |
for _repetition in range(_n): |
| 202 |
_prebuf_ptr[_curbuf] = _pre_idx |
| 203 |
_postbuf_ptr[_curbuf] = _post_idx |
| 204 |
_curbuf += 1 |
| 205 |
# Flush buffer |
| 206 |
if _curbuf==_buffer_size: |
| 207 |
_flush_buffer(_prebuf, {{_dynamic__synaptic_pre_ptr}}, _curbuf) |
| 208 |
_flush_buffer(_postbuf, {{_dynamic__synaptic_post_ptr}}, _curbuf) |
| 209 |
_curbuf = 0 |
| 210 |
|
| 211 |
# Final buffer flush |
| 212 |
_flush_buffer(_prebuf, {{_dynamic__synaptic_pre_ptr}}, _curbuf) |
| 213 |
_flush_buffer(_postbuf, {{_dynamic__synaptic_post_ptr}}, _curbuf) |
| 214 |
_curbuf = 0 # reset the buffer for the next run |
| 215 |
|
| 216 |
newsize = {{_dynamic__synaptic_pre_ptr}}.size() |
| 217 |
# now we need to resize all registered variables and set the total number |
| 218 |
# of synapse (via Python) |
| 219 |
_owner._resize(newsize) |
| 220 |
|
| 221 |
# And update N_incoming, N_outgoing and synapse_number |
| 222 |
_owner._update_synapse_numbers(oldsize) |
| 223 |
|
| 224 |
{% endblock %} |