Skip to content

Commit

Permalink
Merge e096389 into a0167fc
Browse files Browse the repository at this point in the history
  • Loading branch information
mstimberg committed Apr 29, 2021
2 parents a0167fc + e096389 commit 1ce9158
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,23 @@ cdef void _flush_buffer(buf, dynarr, int buf_len):
cdef size_t _jump
{% endif %}
{% endif %}

{# For a connect call j='k+i for k in range(0, N_post, 2) if k+i < N_post'
"j" is called the "target variable" (and "_post_idx" the "target array", etc.)
"i" is called the "base variable" (and "_pre_idx" the "base array", etc.)
"k" is called the iteration variable #}

# scalar code
_vectorisation_idx = 1
{{scalar_code['setup_iterator']|autoindent}}
{{scalar_code['create_j']|autoindent}}
{{scalar_code['generator_expr']|autoindent}}
{{scalar_code['create_cond']|autoindent}}
{{scalar_code['update_post']|autoindent}}
{{scalar_code['update']|autoindent}}

for _i in range(N_pre):
_raw_pre_idx = _i + _source_offset
for _{{base_var}} in range({{base_var_size}}):
_raw{{base_array}} = _{{base_var}} + {{base_offset}}

{% if not postsynaptic_condition %}
{% if not target_condition %}
{{vector_code['create_cond']|autoindent}}
if not _cond:
continue
Expand Down Expand Up @@ -155,35 +161,35 @@ cdef void _flush_buffer(buf, dynarr, int buf_len):
{% endif %}
{% endif %}

{{vector_code['create_j']|autoindent}}
_raw_post_idx = _j + _target_offset
{{vector_code['generator_expr']|autoindent}}
_raw{{target_array}} = _{{target_var}} + {{target_offset}}

{% if postsynaptic_condition %}
{% if postsynaptic_variable_used %}
{% if target_condition %}
{% if target_variable_used %}
{# The condition could index outside of array range #}
if _j<0 or _j>=N_post:
if _{{target_var}}<0 or _{{target_var}}>={{target_var_size}}:
{% if skip_if_invalid %}
continue
{% else %}
raise IndexError("index j=%d outside allowed range from 0 to %d" % (_j, N_post-1))
raise IndexError("index {{target_var}}=%d outside allowed range from 0 to %d" % (_{{target_var}}, {{target_var_size}}-1))
{% endif %}
{% endif %}
{{vector_code['create_cond']|autoindent}}
{% endif %}
{% if if_expression!='True' and postsynaptic_condition %}
{% if if_expression!='True' and target_condition %}
if not _cond:
continue
{% endif %}
{% if not postsynaptic_variable_used %}
{% if not target_variable_used %}
{# Otherwise, we already checked before #}
if _j<0 or _j>=N_post:
if _{{target_var}}<0 or _{{target_var}}>={{target_var_size}}:
{% if skip_if_invalid %}
continue
{% else %}
raise IndexError("index j=%d outside allowed range from 0 to %d" % (_j, N_post-1))
raise IndexError("index j=%d outside allowed range from 0 to %d" % (_{{target_var}}, {{target_var_size}}-1))
{% endif %}
{% endif %}
{{vector_code['update_post']|autoindent}}
{{vector_code['update']|autoindent}}

for _repetition in range(_n):
_prebuf_ptr[_curbuf] = _pre_idx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,18 @@ _cur_num_synapses = _old_num_synapses
# scalar code
_vectorisation_idx = 1
{{scalar_code['setup_iterator']|autoindent}}
{{scalar_code['create_j']|autoindent}}
{{scalar_code['generator_expr']|autoindent}}
{{scalar_code['create_cond']|autoindent}}
{{scalar_code['update_post']|autoindent}}
{{scalar_code['update']|autoindent}}

for _i in range(N_pre):
_raw_pre_idx = _i + _source_offset
{% if not postsynaptic_condition %}
{# For a connect call j='k+i for k in range(0, N_post, 2) if k+i < N_post'
"j" is called the "target variable" (and "_post_idx" the "target array", etc.)
"i" is called the "base variable" (and "_pre_idx" the "base array", etc.)
"k" is called the iteration variable #}

for _{{base_var}} in range({{base_var_size}}):
_raw{{base_array}} = _{{base_var}} + {{base_offset}}
{% if not target_condition %}
{{vector_code['create_cond']|autoindent}}
if not _cond:
continue
Expand All @@ -119,76 +124,76 @@ for _i in range(N_pre):
{% endif %}

_vectorisation_idx = {{iteration_variable}}
{{vector_code['create_j']|autoindent}}
_vectorisation_idx = _j
_raw_post_idx = _j + _target_offset
{% if postsynaptic_condition %}
{% if postsynaptic_variable_used %}
{{vector_code['generator_expr']|autoindent}}
_vectorisation_idx = _{{target_var}}
_raw{{target_array}} = _{{target_var}} + {{target_offset}}
{% if target_condition %}
{% if target_variable_used %}
{# The condition could index outside of array range #}
{% if skip_if_invalid %}
if _numpy.isscalar(_j):
if _j<0 or _j>=N_post:
if _numpy.isscalar(_{{target_var}}):
if _{{target_var}}<0 or _{{target_var}}>={{target_var_size}}:
continue
else:
_in_range = _numpy.logical_and(_j>=0, _j<N_post)
_j = _j[_in_range]
_in_range = _numpy.logical_and(_{{target_var}}>=0, _{{target_var}}<{{target_var_size}})
_{{target_var}} = _{{target_var}}[_in_range]
{% else %}
if _numpy.isscalar(_j):
_min_j = _max_j = _j
elif _j.shape == (0, ):
if _numpy.isscalar(_{{target_var}}):
_min_val = _max_val = _{{target_var}}
elif _{{target_var}}.shape == (0, ):
continue
else:
_min_j = _numpy.min(_j)
_max_j = _numpy.max(_j)
if _min_j < 0:
raise IndexError("index j=%d outside allowed range from 0 to %d, and the condition refers to a post-synaptic variable" % (_min_j, N_post-1))
elif _max_j >= N_post:
raise IndexError("index j=%d outside allowed range from 0 to %d, and the condition refers to a post-synaptic variable" % (_max_j, N_post-1))
_min_val = _numpy.min(_{{target_var}})
_max_val = _numpy.max(_{{target_var}})
if _min_val < 0:
raise IndexError("index {{target_var}}=%d outside allowed range from 0 to %d, and the condition refers to a post-synaptic variable" % (_min_val, {{target_var_size}}-1))
elif _max_val >= {{target_var_size}}:
raise IndexError("index {{target_var}}=%d outside allowed range from 0 to %d, and the condition refers to a post-synaptic variable" % (_max_val, {{target_var_size}}-1))
{% endif %}
{% endif %}
{{vector_code['create_cond']|autoindent}}
{% endif %}
{% if if_expression!='True' and postsynaptic_condition %}
_j, _cond = _numpy.broadcast_arrays(_j, _cond)
_j = _j[_cond]
{% if if_expression!='True' and target_condition %}
_{{target_var}}, _cond = _numpy.broadcast_arrays(_{{target_var}}, _cond)
_{{target_var}} = _{{target_var}}[_cond]
{% else %}
_j, {{iteration_variable}} = _numpy.broadcast_arrays(_j, {{iteration_variable}})
_{{target_var}}, {{iteration_variable}} = _numpy.broadcast_arrays(_{{target_var}}, {{iteration_variable}})
{% endif %}

{% if skip_if_invalid %}
if _numpy.isscalar(_j):
if _j<0 or _j>=N_post:
if _numpy.isscalar(_{{target_var}}):
if _{{target_var}}<0 or _{{target_var}}>={{target_var_size}}:
continue
else:
_in_range = _numpy.logical_and(_j>=0, _j<N_post)
_j = _j[_in_range]
{% elif not postsynaptic_variable_used %}
_in_range = _numpy.logical_and(_{{target_var}}>=0, _{{target_var}}<{{target_var_size}})
_{{target_var}} = _{{target_var}}[_in_range]
{% elif not target_variable_used %}
{# Otherwise, we already checked before #}
if _numpy.isscalar(_j):
_min_j = _max_j = _j
elif _j.shape == (0, ):
if _numpy.isscalar(_{{target_var}}):
_min_val = _max_val = _{{target_var}}
elif _{{target_var}}.shape == (0, ):
continue
else:
_min_j = _numpy.min(_j)
_max_j = _numpy.max(_j)
if _min_j < 0:
raise IndexError("index j=%d outside allowed range from 0 to %d" % (_min_j, N_post-1))
elif _max_j >= N_post:
raise IndexError("index j=%d outside allowed range from 0 to %d" % (_max_j, N_post-1))
_min_val = _numpy.min(_{{target_var}})
_max_val = _numpy.max(_{{target_var}})
if _min_val < 0:
raise IndexError("index _{{target_var}}=%d outside allowed range from 0 to %d" % (_min_val, {{target_var_size}}-1))
elif _max_val >= {{target_var_size}}:
raise IndexError("index _{{target_var}}=%d outside allowed range from 0 to %d" % (_max_val, {{target_var_size}}-1))
{% endif %}

_vectorisation_idx = _j
_raw_post_idx = _j + _target_offset
{{vector_code['update_post']|autoindent}}
_vectorisation_idx = _{{target_var}}
_raw{{target_array}} = _{{target_var}} + {{target_offset}}
{{vector_code['update']|autoindent}}

if not _numpy.isscalar(_n):
# The "n" expression involved j
_post_idx = _post_idx.repeat(_n[_j])
# The "n" expression involved the target variable
{{target_array}} = {{target_array}}.repeat(_n[_j])
elif _n != 1:
# We have a j-independent number
_post_idx = _post_idx.repeat(_n)
# We have a target-independent number
{{target_array}} = {{target_array}}.repeat(_n)
_numnew = len({{target_array}})

_numnew = len(_post_idx)
_new_num_synapses = _cur_num_synapses + _numnew
{{_dynamic__synaptic_pre}}.resize(_new_num_synapses)
{{_dynamic__synaptic_post}}.resize(_new_num_synapses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@
{{_dynamic_N_incoming}}.resize(_N_post + _target_offset);
{{_dynamic_N_outgoing}}.resize(_N_pre + _source_offset);
size_t _raw_pre_idx, _raw_post_idx;
{# For a connect call j='k+i for k in range(0, N_post, 2) if k+i < N_post'
"j" is called the "target variable" (and "_post_idx" the "target array", etc.)
"i" is called the "base variable" (and "_pre_idx" the "base array", etc.)
"k" is called the iteration variable #}

// scalar code
const size_t _vectorisation_idx = -1;
{{scalar_code['setup_iterator']|autoindent}}
{{scalar_code['create_j']|autoindent}}
{{scalar_code['generator_expr']|autoindent}}
{{scalar_code['create_cond']|autoindent}}
{{scalar_code['update_post']|autoindent}}
for(size_t _i=0; _i<_N_pre; _i++)
{{scalar_code['update']|autoindent}}
for(size_t _{{base_var}}=0; _{{base_var}}<_{{base_var_size}}; _{{base_var}}++)
{
bool __cond, _cond;
_raw_pre_idx = _i + _source_offset;
{% if not postsynaptic_condition %}
_raw{{base_array}} = _{{base_var}} + {{base_offset}};
{% if not target_condition %}
{
{{vector_code['create_cond']|autoindent}}
__cond = _cond;
Expand Down Expand Up @@ -164,26 +169,26 @@
}
{% endif %}
{% endif %}
long __j, _j, _pre_idx, __pre_idx;
long __{{target_var}}, _{{target_var}}, {{base_array}}, _{{base_array}};
{
{{vector_code['create_j']|autoindent}}
__j = _j; // pick up the locally scoped _j and store in __j
__pre_idx = _pre_idx;
{{vector_code['generator_expr']|autoindent}}
__{{target_var}} = _{{target_var}}; // pick up the locally scoped var and store in outer var
_{{base_array}} = {{base_array}};
}
_j = __j; // make the previously locally scoped _j available
_pre_idx = __pre_idx;
_raw_post_idx = _j + _target_offset;
{% if postsynaptic_condition %}
_{{target_var}} = __{{target_var}}; // make the previously locally scoped var available
{{base_array}} = _{{base_array}};
_raw{{target_array}} = _{{target_var}} + {{target_offset}};
{% if target_condition %}
{
{% if postsynaptic_variable_used %}
{% if target_variable_used %}
{# The condition could index outside of array range #}
if(_j<0 || _j>=_N_post)
if(_{{target_var}}<0 || _{{target_var}}>=_{{target_var_size}})
{
{% if skip_if_invalid %}
continue;
{% else %}
cout << "Error: tried to create synapse to neuron j=" << _j << " outside range 0 to " <<
_N_post-1 << endl;
cout << "Error: tried to create synapse to neuron {{target_var}}=" << _{{target_var}} << " outside range 0 to " <<
_{{target_var_size}}-1 << endl;
exit(1);
{% endif %}
}
Expand All @@ -197,20 +202,20 @@
{% if if_expression!='True' %}
if(!_cond) continue;
{% endif %}
{% if not postsynaptic_variable_used %}
{% if not target_variable_used %}
{# Otherwise, we already checked before #}
if(_j<0 || _j>=_N_post)
if(_{{target_var}}<0 || _{{target_var}}>=_{{target_var_size}})
{
{% if skip_if_invalid %}
continue;
{% else %}
cout << "Error: tried to create synapse to neuron j=" << _j << " outside range 0 to " <<
_N_post-1 << endl;
cout << "Error: tried to create synapse to neuron {{target_var}}=" << _{{target_var}} <<
" outside range 0 to " << _{{target_var_size}}-1 << endl;
exit(1);
{% endif %}
}
{% endif %}
{{vector_code['update_post']|autoindent}}
{{vector_code['update']|autoindent}}

for (size_t _repetition=0; _repetition<_n; _repetition++) {
{{_dynamic_N_outgoing}}[_pre_idx] += 1;
Expand Down

0 comments on commit 1ce9158

Please sign in to comment.