diff --git a/brian2/codegen/runtime/numpy_rt/templates/group_variable_set.py_ b/brian2/codegen/runtime/numpy_rt/templates/group_variable_set.py_ new file mode 100644 index 000000000..25f4b4edb --- /dev/null +++ b/brian2/codegen/runtime/numpy_rt/templates/group_variable_set.py_ @@ -0,0 +1,6 @@ +# USES_VARIABLES { _group_idx } +_idx = _group_idx +_vectorisation_idx = _idx +{% for line in code_lines %} +{{line}} +{% endfor %} diff --git a/brian2/codegen/runtime/numpy_rt/templates/ratemonitor.py_ b/brian2/codegen/runtime/numpy_rt/templates/ratemonitor.py_ index ddd5606d4..cd0bb7440 100644 --- a/brian2/codegen/runtime/numpy_rt/templates/ratemonitor.py_ +++ b/brian2/codegen/runtime/numpy_rt/templates/ratemonitor.py_ @@ -1,5 +1,5 @@ -# { USES_VARIABLES _rate, _t, _spikes, _num_source_neurons, t, dt } - +# { USES_VARIABLES _rate, _t, _spikespace, _num_source_neurons, t, dt } +_spikes = _spikespace[:_spikespace[-1]] _new_len = len(_t) + 1 _t.resize(_new_len) _rate.resize(_new_len) diff --git a/brian2/codegen/runtime/numpy_rt/templates/reset.py_ b/brian2/codegen/runtime/numpy_rt/templates/reset.py_ index 6436dafc7..cb801c215 100644 --- a/brian2/codegen/runtime/numpy_rt/templates/reset.py_ +++ b/brian2/codegen/runtime/numpy_rt/templates/reset.py_ @@ -1,5 +1,5 @@ -# USES_VARIABLES { _spikes } -_idx = _spikes +# USES_VARIABLES { _spikespace } +_idx = _spikespace[:_spikespace[-1]] _vectorisation_idx = _idx {% for line in code_lines %} {{line}} diff --git a/brian2/codegen/runtime/numpy_rt/templates/spikemonitor.py_ b/brian2/codegen/runtime/numpy_rt/templates/spikemonitor.py_ index 4ba82b17c..b9144cae0 100644 --- a/brian2/codegen/runtime/numpy_rt/templates/spikemonitor.py_ +++ b/brian2/codegen/runtime/numpy_rt/templates/spikemonitor.py_ @@ -1,7 +1,7 @@ -# { USES_VARIABLES _i, _t, _spikes, _count, t, _source_start, _source_end} +# { USES_VARIABLES _i, _t, _spikespace, _count, t, _source_start, _source_end} import numpy as np +_spikes = _spikespace[:_spikespace[-1]] # Take subgroups into account -_spikes = np.asarray(_spikes) _spikes = _spikes[(_spikes >= _source_start) & (_spikes < _source_end)] _spikes -= _source_start _n_spikes = len(_spikes) diff --git a/brian2/codegen/runtime/weave_rt/templates/group_variable_set.cpp b/brian2/codegen/runtime/weave_rt/templates/group_variable_set.cpp new file mode 100644 index 000000000..c0c425c28 --- /dev/null +++ b/brian2/codegen/runtime/weave_rt/templates/group_variable_set.cpp @@ -0,0 +1,40 @@ +//////////////////////////////////////////////////////////////////////////// +//// MAIN CODE ///////////////////////////////////////////////////////////// + +{% macro main() %} + // USES_VARIABLES { _group_idx } + + ////// HANDLE DENORMALS /// + {% for line in denormals_code_lines %} + {{line}} + {% endfor %} + + ////// HASH DEFINES /////// + {% for line in hashdefine_lines %} + {{line}} + {% endfor %} + + ///// POINTERS //////////// + {% for line in pointers_lines %} + {{line}} + {% endfor %} + + //// MAIN CODE //////////// + for(int _idx_group_idx=0; _idx_group_idx<_num_group_idx; _idx_group_idx++) + { + const int _idx = _group_idx[_idx_group_idx]; + const int _vectorisation_idx = _idx; + {% for line in code_lines %} + {{line}} + {% endfor %} + } +{% endmacro %} + +//////////////////////////////////////////////////////////////////////////// +//// SUPPORT CODE ////////////////////////////////////////////////////////// + +{% macro support_code() %} + {% for line in support_code_lines %} + {{line}} + {% endfor %} +{% endmacro %} diff --git a/brian2/codegen/runtime/weave_rt/templates/ratemonitor.cpp b/brian2/codegen/runtime/weave_rt/templates/ratemonitor.cpp index 9d43f00c1..ac9749117 100644 --- a/brian2/codegen/runtime/weave_rt/templates/ratemonitor.cpp +++ b/brian2/codegen/runtime/weave_rt/templates/ratemonitor.cpp @@ -1,6 +1,7 @@ {% macro main() %} - // USES_VARIABLES { _t, _rate, t, dt, _spikes } + // USES_VARIABLES { _t, _rate, t, dt, _spikespace } + const int _num_spikes = _spikespace[_num_spikespace-1]; // Calculate the new length for the arrays const npy_int _new_len = (npy_int)(_t.attr("shape")[0]) + 1; diff --git a/brian2/codegen/runtime/weave_rt/templates/reset.cpp b/brian2/codegen/runtime/weave_rt/templates/reset.cpp index d413b53bb..934b8f6ed 100644 --- a/brian2/codegen/runtime/weave_rt/templates/reset.cpp +++ b/brian2/codegen/runtime/weave_rt/templates/reset.cpp @@ -2,7 +2,7 @@ //// MAIN CODE ///////////////////////////////////////////////////////////// {% macro main() %} - // USES_VARIABLES { _spikes } + // USES_VARIABLES { _spikespace } ////// HANDLE DENORMALS /// {% for line in denormals_code_lines %} @@ -20,9 +20,10 @@ {% endfor %} //// MAIN CODE //////////// + const int _num_spikes = _spikespace[_num_spikespace-1]; for(int _index_spikes=0; _index_spikes<_num_spikes; _index_spikes++) { - const int _idx = _spikes[_index_spikes]; + const int _idx = _spikespace[_index_spikes]; const int _vectorisation_idx = _idx; {% for line in code_lines %} {{line}} diff --git a/brian2/codegen/runtime/weave_rt/templates/spikemonitor.cpp b/brian2/codegen/runtime/weave_rt/templates/spikemonitor.cpp index 9d8fac46d..752d7855d 100644 --- a/brian2/codegen/runtime/weave_rt/templates/spikemonitor.cpp +++ b/brian2/codegen/runtime/weave_rt/templates/spikemonitor.cpp @@ -1,8 +1,8 @@ {% macro main() %} - // USES_VARIABLES { _t, _i, t, _spikes, _count, + // USES_VARIABLES { _t, _i, t, _spikespace, _count, // _source_start, _source_end} - + int _num_spikes = _spikespace[_num_spikespace-1]; if (_num_spikes > 0) { // For subgroups, we do not want to record all spikes @@ -12,7 +12,7 @@ int _end_idx = - 1; for(int _i=0; _i<_num_spikes; _i++) { - const int _idx = _spikes[_i]; + const int _idx = _spikespace[_i]; if (_idx >= _source_start) { _start_idx = _i; break; @@ -20,7 +20,7 @@ } for(int _i=_start_idx; _i<_num_spikes; _i++) { - const int _idx = _spikes[_i]; + const int _idx = _spikespace[_i]; if (_idx >= _source_end) { _end_idx = _i; break; @@ -45,7 +45,7 @@ // Copy the values across for(int _i=_start_idx; _i<_end_idx; _i++) { - const int _idx = _spikes[_i]; + const int _idx = _spikespace[_i]; _t_data[_curlen + _i - _start_idx] = t; _i_data[_curlen + _i - _start_idx] = _idx - _source_start; _count[_idx - _source_start]++; diff --git a/brian2/groups/group.py b/brian2/groups/group.py index 747ba49cc..371998b64 100644 --- a/brian2/groups/group.py +++ b/brian2/groups/group.py @@ -203,15 +203,15 @@ def _set_with_code(self, variable, group_indices, code, # TODO: Find a name that makes sense for reset and variable setting # with code additional_variables = self.item_mapping.variables - additional_variables['_spikes'] = ArrayVariable('_spikes', - Unit(1), - value=group_indices.astype(np.int32), - group_name=self.name) + additional_variables['_group_idx'] = ArrayVariable('_group_idx', + Unit(1), + value=group_indices.astype(np.int32), + group_name=self.name) # TODO: Have an additional argument to avoid going through the index # array for situations where iterate_all could be used codeobj = create_runner_codeobj(self, abstract_code, - 'reset', + 'group_variable_set', additional_variables=additional_variables, additional_namespace=additional_namespace, check_units=check_units) diff --git a/brian2/groups/neurongroup.py b/brian2/groups/neurongroup.py index 9005fdd40..843750c85 100644 --- a/brian2/groups/neurongroup.py +++ b/brian2/groups/neurongroup.py @@ -390,8 +390,6 @@ def _create_variables(self): s.update({'_spikespace': ArrayVariable('_spikespace', Unit(1), self._spikespace, group_name=self.name)}) - s.update({'_spikes': AttributeVariable(Unit(1), self, - 'spikes', constant=False)}) for eq in self.equations.itervalues(): if eq.type in (DIFFERENTIAL_EQUATION, PARAMETER): diff --git a/brian2/monitors/ratemonitor.py b/brian2/monitors/ratemonitor.py index c4ae45ec6..aba850527 100644 --- a/brian2/monitors/ratemonitor.py +++ b/brian2/monitors/ratemonitor.py @@ -53,8 +53,7 @@ def __init__(self, source, when=None, name='ratemonitor*', self.variables = {'t': AttributeVariable(second, self.clock, 't'), 'dt': AttributeVariable(second, self.clock, 'dt', constant=True), - '_spikes': AttributeVariable(Unit(1), - self.source, 'spikes'), + '_spikespace': self.source.variables['_spikespace'], # The template needs to have access to the # DynamicArray here, having access to the underlying # array is not enough since we want to do the resize diff --git a/brian2/monitors/spikemonitor.py b/brian2/monitors/spikemonitor.py index 076b6848a..acad6a692 100644 --- a/brian2/monitors/spikemonitor.py +++ b/brian2/monitors/spikemonitor.py @@ -58,8 +58,7 @@ def __init__(self, source, record=True, when=None, name='spikemonitor*', end = getattr(self.source, 'end', len(self.source)) self.variables = {'t': AttributeVariable(second, self.clock, 't'), - '_spikes': AttributeVariable(Unit(1), self.source, - 'spikes'), + '_spikespace': self.source.variables['_spikespace'], # The template needs to have access to the # DynamicArray here, having access to the underlying # array is not enough since we want to do the resize