From 8b8ce981cb7fd97a1c6a864fe92813d1b8e3be99 Mon Sep 17 00:00:00 2001 From: Marcel Stimberg Date: Fri, 8 Sep 2023 11:04:01 +0200 Subject: [PATCH] Fix GSL for Cython and standalone --- brian2/codegen/generators/GSL_generator.py | 22 +++++++++++-------- .../devices/cpp_standalone/GSLcodeobject.py | 3 +-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/brian2/codegen/generators/GSL_generator.py b/brian2/codegen/generators/GSL_generator.py index 29e3ddb4a..138b75921 100644 --- a/brian2/codegen/generators/GSL_generator.py +++ b/brian2/codegen/generators/GSL_generator.py @@ -325,7 +325,9 @@ def get_dimension_code(self, diff_num): set_dimension_code : str The code describing the target language function in a single string """ - code = ["\n{start_declare}int set_dimension(size_t * dimension){open_function}"] + code = [ + "\n{start_func_declare}int set_dimension(size_t * dimension){open_function}" + ] code += ["\tdimension[0] = %d{end_statement}" % diff_num] code += ["\treturn GSL_SUCCESS{end_statement}{end_function}"] return ("\n").join(code).format(**self.syntax) @@ -352,11 +354,11 @@ def yvector_code(self, diff_vars): ``_empty_y_vector``) as single string. """ fill_y = [ - "\n{start_declare}int _fill_y_vector(_dataholder *" + "\n{start_func_declare}int _fill_y_vector(_dataholder *" "_GSL_dataholder, double * _GSL_y, int _idx){open_function}" ] empty_y = [ - "\n{start_declare}int _empty_y_vector(_dataholder * " + "\n{start_func_declare}int _empty_y_vector(_dataholder * " "_GSL_dataholder, double * _GSL_y, int _idx){" "open_function}" ] @@ -397,11 +399,11 @@ def make_function_code(self, lines): code describing ``_GSL_func`` that is sent to GSL integrator. """ code = [ - "\n{start_declare}int _GSL_func(double t, const double " + "\n{start_func_declare}int _GSL_func(double t, const double " "_GSL_y[], double f[], void * params){open_function}" - "\n\t{start_declare}_dataholder * _GSL_dataholder = {open_cast}" + "\n\t{start_var_declare}_dataholder * _GSL_dataholder = {open_cast}" "_dataholder *{close_cast} params{end_statement}" - "\n\t{start_declare}int _idx = _GSL_dataholder{access_pointer}_idx" + "\n\t{start_var_declare}int _idx = _GSL_dataholder{access_pointer}_idx" "{end_statement}" ] code += [lines] @@ -449,7 +451,7 @@ def write_dataholder(self, variables_in_vector): code : str code for _dataholder struct """ - code = ["\n{start_declare}struct _dataholder{open_struct}"] + code = ["\n{start_var_declare}struct _dataholder{open_struct}"] code += ["\tint _idx{end_statement}"] for var, var_obj in list(variables_in_vector.items()): if ( @@ -1053,7 +1055,8 @@ class GSLCythonCodeGenerator(GSLCodeGenerator): syntax = { "end_statement": "", "access_pointer": ".", - "start_declare": "cdef extern ", + "start_func_declare": "cdef extern ", + "start_var_declare": "cdef ", "open_function": ":", "open_struct": ":", "end_function": "", @@ -1113,7 +1116,8 @@ def __getattr__(self, item): syntax = { "end_statement": ";", "access_pointer": "->", - "start_declare": 'extern "C" ', + "start_func_declare": 'extern "C" ', + "start_var_declare": "", "open_function": "\n{", "open_struct": "\n{", "end_function": "\n}", diff --git a/brian2/devices/cpp_standalone/GSLcodeobject.py b/brian2/devices/cpp_standalone/GSLcodeobject.py index 6ef49966f..867698668 100644 --- a/brian2/devices/cpp_standalone/GSLcodeobject.py +++ b/brian2/devices/cpp_standalone/GSLcodeobject.py @@ -3,13 +3,12 @@ GNU Scientific Library """ -from brian2.codegen.codeobject import CodeObject from brian2.codegen.generators.cpp_generator import CPPCodeGenerator from brian2.codegen.generators.GSL_generator import GSLCPPCodeGenerator from brian2.devices.cpp_standalone import CPPStandaloneCodeObject -class GSLCPPStandaloneCodeObject(CodeObject): +class GSLCPPStandaloneCodeObject(CPPStandaloneCodeObject): templater = CPPStandaloneCodeObject.templater.derive( "brian2.devices.cpp_standalone", templates_dir="templates_GSL" )