Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upper case state variable name naming clash #766

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pynestml/codegeneration/printers/gsl_reference_converter.py
Expand Up @@ -204,7 +204,7 @@ def array_index(self, symbol: VariableSymbol) -> str:
:param symbol: a single variable symbol
:return: the corresponding string format
"""
return 'State_::' + self.convert_to_cpp_name(symbol.get_symbol_name())
return 'State_::STATE_VEC_VAR_' + self.convert_to_cpp_name(symbol.get_symbol_name())

def name(self, symbol: VariableSymbol) -> str:
"""
Expand All @@ -213,6 +213,6 @@ def name(self, symbol: VariableSymbol) -> str:
:return: the corresponding string format
"""
if symbol.is_state() and not symbol.is_inline_expression:
return 'ode_state[State_::' + self.convert_to_cpp_name(symbol.get_symbol_name()) + ']'
return 'ode_state[State_::STATE_VEC_VAR_' + self.convert_to_cpp_name(symbol.get_symbol_name()) + ']'

return super().name(symbol)
Expand Up @@ -87,7 +87,7 @@ template <> void RecordablesMap<{{neuronName}}>::create()
{%- if has_state_vectors %}
{%- for sym in recordable_state_variables %}
{%- if not sym.has_vector_parameter() %}
insert("{{sym.get_symbol_name()}}", host.get_data_access_functor( {{neuronName}}::State_::{{names.name(sym).upper()}} ));
insert("{{sym.get_symbol_name()}}", host.get_data_access_functor( {{neuronName}}::State_::STATE_VEC_VAR_{{names.name(sym).upper()}} ));
{%- endif %}
{%- endfor %}
{%- else %}
Expand Down Expand Up @@ -127,7 +127,7 @@ template <> void RecordablesMap<{{neuronName}}>::create()
{%- for variable in neuron.get_vector_state_symbols() %}
for (size_t i = 0; i < {{printer.print_vector_size_parameter(variable)}}; i++)
{
size_t elem = {{neuronName}}::State_::{{names.name(variable).upper()}} + i;
size_t elem = {{neuronName}}::State_::STATE_VEC_VAR_{{names.name(variable).upper()}} + i;
recordablesMap_.insert(get_var_name(i, "{{names.name(variable).upper()}}_"), this->get_data_access_functor(elem));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. The code will compile, but we still have an unchecked runtime problem. The function insert_recordables at line 131 inserts in the map as key the array's name in an upper case form and adds a _{i} as the entry name in the recordables. So if we have an array x with n elements, we will insert X_{i} for i in [1, n]. With this in mind, the user might then want to create two variables in his nestml file, one array variable named x and another double variable named X_1. What will happen when inserting the recordables is that the double variables X_1 will be inserted first, and then the array entries, but the map already contains X_1, and therefore the first entry of the array will never be recorded.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that, and I think the best way is to override the find function in the generic RecordableMap as the create function. Instead of inserting each entry of the array, we should only register a constant pointer to the array and only store its name as x, and then we have to implement a logic that can resolve the global name to a local name (mapping x to x_i).
Assume we have the following map : map : { "x" : [x1, x2,...., xn]}, and let's say we want to deliver x_i to the DataLoggingRequest. Calling map.find("x_i"), should return a pair containing x as the name and a callable that can access the array at position i.

With this, we can also save us runtime overhead. Assume that we have an array with size n >M (big number), and we are only interested in the result of the last entry to be recorded, but at the same time that entry depends on the previous entries. Firstly, the constructor will be very expensive as it must iterate over the entries and register them in the map, but from the python side, the user will only want to record the last entry. With the new suggested changes, the entries will be registered all together at once by having the whole array representing them, and then when setting the record_from in pynest to x_i, the find function should parse the input and split it to (x, i) and then returns an access function to the array at position i.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could keep it simple and add a check that if a vector variable foo exists, there exists no declaration in the model of a variable named FOO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but u must prevent not just FOO but also every FOO_i for i < len(foo)

}
{%- endfor %}
Expand Down Expand Up @@ -679,7 +679,7 @@ void
double ode_state_bak[State_::STATE_VEC_SIZE];

{%- for variable_name in numeric_state_variables %}
ode_state_bak[State_::{{variable_name}}] = S_.ode_state[State_::{{variable_name}}];
ode_state_bak[State_::STATE_VEC_VAR_{{variable_name}}] = S_.ode_state[State_::STATE_VEC_VAR_{{variable_name}}];
{%- endfor %}

{%- if uses_numeric_solver %}
Expand All @@ -688,12 +688,12 @@ ode_state_bak[State_::{{variable_name}}] = S_.ode_state[State_::{{variable_name}

// restore non-synapse->neuron-moved variables
{%- for variable_name in numeric_state_variables %}
S_.ode_state[State_::{{variable_name}}] = ode_state_bak[State_::{{variable_name}}];
S_.ode_state[State_::STATE_VEC_VAR_{{variable_name}}] = ode_state_bak[State_::STATE_VEC_VAR_{{variable_name}}];
{%- endfor %}

// restore variables solved analytically
{%- for variable_name in numeric_state_variables %}
S_.ode_state[State_::{{variable_name}}] = ode_state_bak[State_::{{variable_name}}];
S_.ode_state[State_::STATE_VEC_VAR_{{variable_name}}] = ode_state_bak[State_::STATE_VEC_VAR_{{variable_name}}];
{%- endfor %}
{%- endif %}

Expand Down Expand Up @@ -829,7 +829,7 @@ for (int i = 0; i < STATE_VEC_SIZE; ++i) {

{%- for variable_name in numeric_state_variables_moved|sort %}
{%- if not variable_name in analytic_state_variables_moved %}
S_.ode_state[State_::{{variable_name}}] = ode_state_tmp[State_::{{variable_name}}];
S_.ode_state[State_::STATE_VEC_VAR_{{variable_name}}] = ode_state_tmp[State_::STATE_VEC_VAR_{{variable_name}}];
{%- endif %}
{%- endfor %}
{%- endif %}
Expand Down Expand Up @@ -911,7 +911,7 @@ for (int i = 0; i < STATE_VEC_SIZE; ++i) {

{%- for variable_name in numeric_state_variables_moved|sort %}
{%- if not variable_name in analytic_state_variables_moved %}
S_.ode_state[State_::{{variable_name}}] = ode_state_tmp[State_::{{variable_name}}];
S_.ode_state[State_::STATE_VEC_VAR_{{variable_name}}] = ode_state_tmp[State_::STATE_VEC_VAR_{{variable_name}}];
{%- endif %}
{%- endfor %}
{%- endif %}
Expand Down
Expand Up @@ -496,25 +496,25 @@ private:
{#- N.B. numeric solver contains all state variables, including those that will be solved by analytic solver #}
{%- if uses_numeric_solver %}
{%- for variable_name in numeric_state_variables %}
{{variable_name}},
STATE_VEC_VAR_{{variable_name}},
{%- endfor %}
// moved state variables from synapse
{%- for variable_name in purely_numeric_state_variables_moved|sort %}
{{variable_name}},
STATE_VEC_VAR_{{variable_name}},
{%- endfor %}
{%- for variable_name in analytic_state_variables_moved|sort %}
{{variable_name}},
STATE_VEC_VAR_{{variable_name}},
{%- endfor %}
{%- for variable_name in non_equations_state_variables: %}
{{variable_name}},
STATE_VEC_VAR_{{variable_name}},
{%- endfor %}
{%- else %}
{#- analytic solver only #}
{%- for variable_name in analytic_state_variables: %}
{{variable_name}},
STATE_VEC_VAR_{{variable_name}},
{%- endfor %}
{%- for variable_name in non_equations_state_variables: %}
{{variable_name}},
STATE_VEC_VAR_{{variable_name}},
{%- endfor %}
{%- endif %}
STATE_VEC_SIZE
Expand Down
Expand Up @@ -21,20 +21,20 @@ inline double get_state_element(size_t elem)
{%- if size|int == 0 %}
{%- set size = printer.print_vector_size_parameter(variable) %}
{%- endif -%}
(elem >= State_::{{names.name(variable).upper()}} && elem < State_::{{names.name(variable).upper()}} + {{size}})
(elem >= State_::STATE_VEC_VAR_{{names.name(variable).upper()}} && elem < State_::STATE_VEC_VAR_{{names.name(variable).upper()}} + {{size}})
{
return S_.{{names.name(variable)}}[ elem - State_::{{names.name(variable).upper()}}];
return S_.{{names.name(variable)}}[ elem - State_::STATE_VEC_VAR_{{names.name(variable).upper()}}];
}
{%- else %}
(elem == State_::{{names.name(variable).upper()}})
(elem == State_::STATE_VEC_VAR_{{names.name(variable).upper()}})
{
return S_.{{names.name(variable)}};
}
{%- endif %}
{%- else %}
{%- if variable.has_vector_parameter() %}
{
return S_.{{names.name(variable)}}[ elem - State_::{{names.name(variable).upper()}}];
return S_.{{names.name(variable)}}[ elem - State_::STATE_VEC_VAR_{{names.name(variable).upper()}}];
}
{%- else %}
{
Expand Down
Expand Up @@ -8,7 +8,7 @@ enum StateVecVars {
{%- for variable in neuron.get_state_symbols() %}
{% set varDomain = declarations.get_domain_from_type(variable.get_type_symbol()) -%}
{% if varDomain == "double" and variable.is_recordable -%}
{{names.name(variable).upper()}} = {{ns.count}},
STATE_VEC_VAR_{{names.name(variable).upper()}} = {{ns.count}},
{%- if variable.has_vector_parameter() -%}
{%- set size = utils.get_numeric_vector_size(variable) -%}
{%- set ns.count = ns.count + size -%}
Expand Down