-
Notifications
You must be signed in to change notification settings - Fork 13
Function implementations
Denis Alevi edited this page Aug 19, 2020
·
1 revision
- Currently used e.g. in
synapses_create_generatortemplates, 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_functionis called with the codeobjectowner, which can be accessed inside the code generating function.
- Brian2
Functionobjects have different implementations which are used depending on thedevicethat is set for a simulation (throughset_device(...)). - The
Functionclass defines aFunction.implementationsattribute, 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
BinomialFunctionclass on the other hand does not need that. It implements a global class attributeBinomialFunction.implementations, which is a dictionary mapping language (e.g.cpporcuda) to a Python function that returns the code implementation. Once an instance ofBinomialFunctionis created, the inheritedFunctionclass is initialized, which then instantiates itsFunction.implementationsattribute. Therefore, there are twoimplementationsattributes. One isBinomialFunction.implementations, which is just a dictionary. Andbinomial_function_instance.implementations, which is aFunctionImplementationContainerinstance. - To add an implementation to
BinomialFunction, just assign it to theBinomialFunction.implementationsdictionary. Whenever the class is instatiated, all implementations in that dictionary will be automatically added to the instancesbinomial_function_instance.implementationsobjects (which is a read-onlyFunctionImplementationContainerinstance from above).