-
Notifications
You must be signed in to change notification settings - Fork 12
Function implementations
Denis Alevi edited this page Aug 19, 2020
·
1 revision
- Currently used e.g. in
synapses_create_generator
templates, which are exclusively run on the host but might need function implementations which are also needed in device code. - Use
__host__ __device__
function declarations to generate a function code for both, host and device usage. - To have different implementations, use preprocessor makros in the function
implementation:
#if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ > 0)) // device code #else // host code #endif
- Use
Function.implementations.add_dynamic_implementation(code_generating_function)
. Thecode_generating_function
is called with the codeobjectowner
, which can be accessed inside the code generating function.
- Brian2
Function
objects have different implementations which are used depending on thedevice
that is set for a simulation (throughset_device(...)
). - The
Function
class defines aFunction.implementations
attribute, which is an instance ofFunctionImplementationContainer
, which inherits fromcollections.Mapping
, which is a write-only container. This container is a one-way mapping, where the__getitem__
method (which is called for when an instance is used with[]
) returns the code for a given target language (e.g.some_function.implementations['cpp']
returns the cpp implementation of that function), but it can't be used to set the implemention (read-only). - To add an implementations, one has to use the
Function.implementations.add_implementation()
orFunction.implementation.add_dynamic_implementation()
method. - The
BinomialFunction
class on the other hand does not need that. It implements a global class attributeBinomialFunction.implementations
, which is a dictionary mapping language (e.g.cpp
orcuda
) to a Python function that returns the code implementation. Once an instance ofBinomialFunction
is created, the inheritedFunction
class is initialized, which then instantiates itsFunction.implementations
attribute. Therefore, there are twoimplementations
attributes. One isBinomialFunction.implementations
, which is just a dictionary. Andbinomial_function_instance.implementations
, which is aFunctionImplementationContainer
instance. - To add an implementation to
BinomialFunction
, just assign it to theBinomialFunction.implementations
dictionary. Whenever the class is instatiated, all implementations in that dictionary will be automatically added to the instancesbinomial_function_instance.implementations
objects (which is a read-onlyFunctionImplementationContainer
instance from above).