Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions brian2/codegen/codeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import collections
import copy
import platform
from abc import ABC, abstractmethod

from brian2.core.base import weakproxy_with_fallback
from brian2.core.functions import DEFAULT_FUNCTIONS, Function
Expand Down Expand Up @@ -42,7 +43,7 @@ def constant_or_scalar(varname, variable):
return f"{varname}"


class CodeObject(Nameable):
class CodeObject(Nameable, ABC):
Copy link
Contributor

Choose a reason for hiding this comment

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

yay ... now we use the ABC class :)

Copy link
Member Author

Choose a reason for hiding this comment

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

😆

"""
Executable code object.

Expand Down Expand Up @@ -105,9 +106,7 @@ def is_available(cls):
Whether this target for code generation is available. Should use a
minimal example to check whether code generation works in general.
"""
raise NotImplementedError(
f"CodeObject class {cls.__name__} is missing an 'is_available' method."
)
return True

def update_namespace(self):
"""
Expand All @@ -117,8 +116,8 @@ def update_namespace(self):
"""
pass

def compile_block(self, block):
raise NotImplementedError("Implement compile_block method")
@abstractmethod
def compile_block(self, block): ...

def compile(self):
for block in ["before_run", "run", "after_run"]:
Expand All @@ -130,8 +129,8 @@ def __call__(self, **kwds):

return self.run()

def run_block(self, block):
raise NotImplementedError("Implement run_block method")
@abstractmethod
def run_block(self, block): ...

def before_run(self):
"""
Expand Down
13 changes: 12 additions & 1 deletion brian2/codegen/generators/GSL_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ def __getattr__(self, item):
syntax = {
"end_statement": ";",
"access_pointer": "->",
"start_declare": 'extern "C" ',
"start_declare": "",
"open_function": "\n{",
"open_struct": "\n{",
"end_function": "\n}",
Expand Down Expand Up @@ -1159,3 +1159,14 @@ def unpack_namespace_single(self, var_obj, in_vector, in_scalar):
return f"_GSL_dataholder.{var_obj.name} = {var_obj.name};"
else:
return ""

def make_function_code(self, lines):
# To pass constants that might be used in the function, we need to
# add a %CONSTANTS% placeholder that will be replaced by the standalone
# device
return """
// CONSTANTS
%CONSTANTS%
""" + super().make_function_code(
lines
)
5 changes: 0 additions & 5 deletions brian2/codegen/runtime/numpy_rt/numpy_rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ def __init__(
)
self.variables_to_namespace()

@classmethod
def is_available(cls):
# no test necessary for numpy
return True

def variables_to_namespace(self):
# Variables can refer to values that are either constant (e.g. dt)
# or change every timestep (e.g. t). We add the values of the
Expand Down
5 changes: 4 additions & 1 deletion brian2/codegen/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ def __init__(
self.templates = LazyTemplateLoader(self.env, extension)

def __getattr__(self, item):
return self.templates.get_template(item)
try:
return self.templates.get_template(item)
except KeyError as ex:
raise AttributeError from ex # raise the correct error type for __getattr__

def derive(
self, package_name, extension=None, env_globals=None, templates_dir="templates"
Expand Down
3 changes: 1 addition & 2 deletions brian2/devices/cpp_standalone/GSLcodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
4 changes: 4 additions & 0 deletions brian2/stateupdaters/GSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
provided in the GNU Scientific Library (GSL)
"""

import os
import sys

from brian2.utils.logger import get_logger
Expand Down Expand Up @@ -105,6 +106,9 @@ def get_codeobj_class(self):
device.define_macros += [("WIN32", "1"), ("GSL_DLL", "1")]
if prefs.GSL.directory is not None:
device.include_dirs += [prefs.GSL.directory]
device.library_dirs += [
os.path.abspath(os.path.join(prefs.GSL.directory, "..", "lib"))
]
return GSLCPPStandaloneCodeObject

elif isinstance(device, RuntimeDevice):
Expand Down
Loading