Exemple #1
0
from ...codeobject import CodeObject, constant_or_scalar, check_compiler_kwds

from ...templates import Templater
from ...generators.numpy_generator import NumpyCodeGenerator
from ...targets import codegen_targets

__all__ = ['NumpyCodeObject']

# Preferences
prefs.register_preferences(
    'codegen.runtime.numpy',
    'Numpy runtime codegen preferences',
    discard_units = BrianPreference(
        default=False,
        docs='''
        Whether to change the namespace of user-specifed functions to remove
        units.
        '''
        )
    )


class LazyArange(Iterable):
    '''
    A class that can be used as a `~numpy.arange` replacement (with an implied
    step size of 1) but does not actually create an array of values until
    necessary. It is somewhat similar to the ``range()`` function in Python 3,
    but does not use a generator. It is tailored to a special use case, the
    ``_vectorisation_idx`` variable in numpy templates, and not meant for
    general use. The ``_vectorisation_idx`` is used for stateless function
# Preferences
prefs.register_preferences(
    'codegen.generators.cuda',
    'CUDA codegen preferences',
    restrict_keyword = BrianPreference(
        default='__restrict',
        docs='''
        The keyword used for the given compiler to declare pointers as restricted.
        
        This keyword is different on different compilers, the default works for
        gcc and MSVS.
        ''',
        ),
    flush_denormals = BrianPreference(
        default=False,
        docs='''
        Adds code to flush denormals to zero.
        
        The code is gcc and architecture specific, so may not compile on all
        platforms. The code, for reference is::

            #define CSR_FLUSH_TO_ZERO         (1 << 15)
            unsigned csr = __builtin_ia32_stmxcsr();
            csr |= CSR_FLUSH_TO_ZERO;
            __builtin_ia32_ldmxcsr(csr);
            
        Found at `<http://stackoverflow.com/questions/2487653/avoiding-denormal-values-in-c>`_.
        ''',
        ),
    )
Exemple #3
0
__all__ = ['CythonCodeObject']


logger = get_logger(__name__)

# Preferences
prefs.register_preferences(
    'codegen.runtime.cython',
    'Cython runtime codegen preferences',
    extra_compile_args = BrianPreference(
        default=['-w', '-O3'],
        docs='''
        Extra compile arguments to pass to compiler
        '''
        ),
    include_dirs = BrianPreference(
        default=[],
        docs='''
        Include directories to use. Note that ``$prefix/include`` will be
        appended to the end automatically, where ``$prefix`` is Python's
        site-specific directory prefix as returned by `sys.prefix`.
        '''
        )
    )


class CythonCodeObject(NumpyCodeObject):
    '''
    Execute code using Cython.
    '''
    templater = Templater('brian2.codegen.runtime.cython_rt',
Exemple #4
0

logger = get_logger(__name__)

# Preferences
prefs.register_preferences(
    'codegen.runtime.cython',
    'Cython runtime codegen preferences',
    multiprocess_safe = BrianPreference(
        default=True,
        docs='''
        Whether to use a lock file to prevent simultaneous write access
        to cython .pyx and .so files.
        '''
        ),
    cache_dir = BrianPreference(
        default=None,
        validator=lambda x: x is None or isinstance(x, basestring),
        docs='''
        Location of the cache directory for Cython files. By default,
        will be stored in a ``brian_extensions`` subdirectory
        where Cython inline stores its temporary files
        (the result of ``get_cython_cache_dir()``).
        '''
        ),
    )


class CythonCodeObject(NumpyCodeObject):
    '''
    Execute code using Cython.
from brian2.core.preferences import BrianPreference, prefs


def dtype_repr(dtype):
    return dtype.__name__

prefs.register_preferences('core', 'Core Brian preferences',
    default_float_dtype=BrianPreference(
        default=float64,
        docs='''
        Default dtype for all arrays of scalars (state variables, weights, etc.).
        ''',
        representor=dtype_repr,
        ),
    default_integer_dtype=BrianPreference(
        default=int32,
        docs='''
        Default dtype for all arrays of integer scalars.
        ''',
        representor=dtype_repr,
        ),
    outdated_dependency_error=BrianPreference(
        default=True,
        docs='''
        Whether to raise an error for outdated dependencies (``True``) or just
        a warning (``False``).
        '''
        )
    )
Exemple #6
0
from brian2.units import ms
from brian2.utils.logger import get_logger
from brian2.utils.stringtools import code_representation, indent

__all__ = ['Device', 'RuntimeDevice',
           'get_device', 'set_device',
           'all_devices', 'reinit_devices',
           'reset_device', 'device', 'seed'
           ]

logger = get_logger(__name__)

all_devices = {}


prefs.register_preferences('devices', 'Device preferences')


#: caches the automatically determined code generation target
_auto_target = None

def auto_target():
    '''
    Automatically chose a code generation target (invoked when the
    `codegen.target` preference is set to `'auto'`. Caches its result so it
    only does the check once. Prefers weave > cython > numpy.

    Returns
    -------
    target : class derived from `CodeObject`
        The target to use
Exemple #7
0
prefs.register_preferences(
    'codegen',
    'Code generation preferences',
    target=BrianPreference(
        default='auto',
        docs='''
        Default target for code generation.
        
        Can be a string, in which case it should be one of:
        
        * ``'auto'`` the default, automatically chose the best code generation
          target available.
        * ``'weave'`` uses ``scipy.weave`` to generate and compile C++ code,
          should work anywhere where ``gcc`` is installed and available at the
          command line.
        * ``'cython'``, uses the Cython package to generate C++ code. Needs a
          working installation of Cython and a C++ compiler.
        * ``'numpy'`` works on all platforms and doesn't need a C compiler but
          is often less efficient.
        
        Or it can be a ``CodeObject`` class.
        ''',
        validator=lambda target: isinstance(target, basestring) or issubclass(target, CodeObject),
        ),
    string_expression_target=BrianPreference(
        default='numpy',
        docs='''
        Default target for the evaluation of string expressions (e.g. when
        indexing state variables). Should normally not be changed from the
        default numpy target, because the overhead of compiling code is not
        worth the speed gain for simple expressions.

        Accepts the same arguments as `codegen.target`, except for ``'auto'``
        ''',
        validator=lambda target: isinstance(target, basestring) or issubclass(target, CodeObject),
        ),
    loop_invariant_optimisations=BrianPreference(
        default=True,
        docs='''
        Whether to pull out scalar expressions out of the statements, so that
        they are only evaluated once instead of once for every neuron/synapse/...
        Can be switched off, e.g. because it complicates the code (and the same
        optimisation is already performed by the compiler) or because the
        code generation target does not deal well with it. Defaults to ``True``.
        '''
    ),
    max_cache_dir_size=BrianPreference(
      default=1000,
      docs='''
      The size of a directory (in MB) with cached code for weave or Cython that triggers a warning.
      Set to 0 to never get a warning.
      '''
    )
)
Exemple #8
0
prefs.register_preferences(
    'codegen.cpp',
    'C++ compilation preferences',
    compiler = BrianPreference(
        default='',
        docs='''
        Compiler to use (uses default if empty)
        
        Should be gcc or msvc.
        '''
        ),
    extra_compile_args=BrianPreference(
        default=None,
        validator=lambda v: True,
        docs='''
        Extra arguments to pass to compiler (if None, use either
        ``extra_compile_args_gcc`` or ``extra_compile_args_msvc``).
        '''
        ),
    extra_compile_args_gcc=BrianPreference(
        default=['-w', '-O3', '-ffast-math', '-fno-finite-math-only', '-march=native'],
        docs='''
        Extra compile arguments to pass to GCC compiler
        '''
        ),
    extra_compile_args_msvc=BrianPreference(
        default=['/Ox', '/w', msvc_arch_flag, '/MP'],
        docs='''
        Extra compile arguments to pass to MSVC compiler (the default
        ``/arch:`` flag is determined based on the processor architecture)
        '''
        ),
    extra_link_args=BrianPreference(
        default=[],
        docs='''
        Any extra platform- and compiler-specific information to use when
        linking object files together.
        '''
    ),
    include_dirs=BrianPreference(
        default=[],
        docs='''
        Include directories to use. Note that ``$prefix/include`` will be
        appended to the end automatically, where ``$prefix`` is Python's
        site-specific directory prefix as returned by `sys.prefix`.
        '''
        ),
    library_dirs=BrianPreference(
        default=[],
        docs='''
        List of directories to search for C/C++ libraries at link time.
        Note that ``$prefix/lib`` will be appended to the end automatically,
        where ``$prefix`` is Python's site-specific directory prefix as returned
        by `sys.prefix`.
        '''
    ),
    runtime_library_dirs=BrianPreference(
        default=[],
        docs='''
        List of directories to search for C/C++ libraries at run time.
        '''
    ),
    libraries=BrianPreference(
        default=[],
        docs='''
        List of library names (not filenames or paths) to link against.
        '''
    ),
    headers=BrianPreference(
        default=[],
        docs='''
        A list of strings specifying header files to use when compiling the
        code. The list might look like ["<vector>","'my_header'"]. Note that
        the header strings need to be in a form than can be pasted at the end
        of a #include statement in the C++ code.
        '''
    ),
    define_macros=BrianPreference(
        default=[],
        docs='''
        List of macros to define; each macro is defined using a 2-tuple,
        where 'value' is either the string to define it to or None to
        define it without a particular value (equivalent of "#define
        FOO" in source or -DFOO on Unix C compiler command line).
        '''
    ),
    msvc_vars_location=BrianPreference(
        default='',
        docs='''
        Location of the MSVC command line tool (or search for best by default).
        '''),
    msvc_architecture=BrianPreference(
        default='',
        docs='''
        MSVC architecture name (or use system architectue by default).
        
        Could take values such as x86, amd64, etc.
        '''),
    )
Exemple #9
0
        raise PreferenceError(('Illegal value for GSL directory: %s, '
                                'has to be existing directory' % (val)))
    if any(not os.path.isfile(os.path.join(val, 'gsl', filename))
           for filename in ['gsl_odeiv2.h', 'gsl_errno.h', 'gsl_matrix.h']):
        raise PreferenceError(('Illegal value for GSL directory: %s, '
                               'has to contain gsl_odeiv2.h, gsl_errno.h '
                               'and gsl_matrix.h' % (val)))
    return True


prefs.register_preferences(
    'GSL',
    'Directory containing GSL code',
    directory=BrianPreference(
        validator=valid_gsl_dir,
        docs=("Set path to directory containing GSL header files (gsl_odeiv2.h etc.)"
              "\nIf this directory is already in Python's include (e.g. because of "
              "conda installation), this path can be set to None."),
        default=None
    )
)


class GSLCodeGenerator(object):
    '''
    GSL code generator.

    Notes
    -----
    Approach is to first let the already existing code generator for a target
    language do the bulk of the translating from abstract_code to actual code.
Exemple #10
0
# Register the base category before importing the indivial generators with
# their subcategories
from brian2.core.preferences import prefs

prefs.register_preferences(
    "codegen.generators", ("Codegen generator preferences (see subcategories " "for individual languages)")
)

from .base import *
from .cpp_generator import *
from .numpy_generator import *

try:
    from .cython_generator import *
except ImportError:
    pass  # todo: raise a warning?
Exemple #11
0
prefs.register_preferences(
    'codegen.runtime.cython',
    'Cython runtime codegen preferences',
    multiprocess_safe = BrianPreference(
        default=True,
        docs='''
        Whether to use a lock file to prevent simultaneous write access
        to cython .pyx and .so files.
        '''
        ),
    cache_dir = BrianPreference(
        default=None,
        validator=lambda x: x is None or isinstance(x, basestring),
        docs='''
        Location of the cache directory for Cython files. By default,
        will be stored in a ``brian_extensions`` subdirectory
        where Cython inline stores its temporary files
        (the result of ``get_cython_cache_dir()``).
        '''
        ),
    delete_source_files = BrianPreference(
        default=True,
        docs='''
        Whether to delete source files after compiling. The Cython
        source files can take a significant amount of disk space, and
        are not used anymore when the compiled library file exists.
        They are therefore deleted by default, but keeping them around
        can be useful for debugging.
        '''
        )
    )
Exemple #12
0
prefs.register_preferences(
    "logging",
    "Logging system preferences",
    delete_log_on_exit=BrianPreference(
        default=True,
        docs="""
        Whether to delete the log and script file on exit.
        
        If set to ``True`` (the default), log files (and the copy of the main
        script) will be deleted after the brian process has exited, unless an
        uncaught exception occured. If set to ``False``, all log files will be kept.
        """,
    ),
    file_log_level=BrianPreference(
        default="DIAGNOSTIC",
        docs="""
        What log level to use for the log written to the log file.
        
        In case file logging is activated (see `logging.file_log`), which log
        level should be used for logging. Has to be one of CRITICAL, ERROR,
        WARNING, INFO, DEBUG or DIAGNOSTIC.
        """,
        validator=log_level_validator,
    ),
    console_log_level=BrianPreference(
        default="INFO",
        docs="""
        What log level to use for the log written to the console.
        
        Has to be one of CRITICAL, ERROR, WARNING, INFO, DEBUG or DIAGNOSTIC.
        """,
        validator=log_level_validator,
    ),
    file_log=BrianPreference(
        default=True,
        docs="""
        Whether to log to a file or not.
        
        If set to ``True`` (the default), logging information will be written
        to a file. The log level can be set via the `logging.file_log_level`
        preference.
        """,
    ),
    save_script=BrianPreference(
        default=True,
        docs="""
        Whether to save a copy of the script that is run.
        
        If set to ``True`` (the default), a copy of the currently run script
        is saved to a temporary location. It is deleted after a successful
        run (unless `logging.delete_log_on_exit` is ``False``) but is kept after
        an uncaught exception occured. This can be helpful for debugging,
        in particular when several simulations are running in parallel.
        """,
    ),
    std_redirection=BrianPreference(
        default=True,
        docs="""
        Whether or not to redirect stdout/stderr to null at certain places.
        
        This silences a lot of annoying compiler output, but will also hide
        error messages making it harder to debug problems. You can always
        temporarily switch it off when debugging. If
        `logging.std_redirection_to_file` is set to ``True`` as well, then the
        output is saved to a file and if an error occurs the name of this file
        will be printed.
        """,
    ),
    std_redirection_to_file=BrianPreference(
        default=True,
        docs="""
        Whether to redirect stdout/stderr to a file.

        If both ``logging.std_redirection`` and this preference are set to
        ``True``, all standard output/error (most importantly output from
        the compiler) will be stored in files and if an error occurs the name
        of this file will be printed. If `logging.std_redirection` is ``True``
        and this preference is ``False``, then all standard output/error will
        be completely suppressed, i.e. neither be displayed nor stored in a
        file.

        The value of this preference is ignore if `logging.std_redirection` is
        set to ``False``.
        """,
    ),
)
Exemple #13
0
# Register the base category before importing the indivial generators with
# their subcategories
from brian2.core.preferences import prefs
prefs.register_preferences('codegen.generators',
                           ('Codegen generator preferences (see subcategories '
                            'for individual languages)'))

from .base import *
from .cpp_generator import *
from .numpy_generator import *
try:
    from .cython_generator import *
except ImportError:
    pass # todo: raise a warning?
try:
    from .GSL_generator import *
except ImportError:
    pass

Exemple #14
0
from brian2.core.preferences import prefs, BrianPreference
from brian2.core.namespace import get_local_namespace
from .base import device_override

__all__ = ["Network", "profiling_summary"]


logger = get_logger(__name__)


prefs.register_preferences(
    "core.network",
    "Network preferences",
    default_schedule=BrianPreference(
        default=["start", "groups", "thresholds", "synapses", "resets", "end"],
        docs="""
                               Default schedule used for networks that
                               don't specify a schedule.
                               """,
    ),
)


def _format_time(time_in_s):
    """
    Helper function to format time in seconds, minutes, hours, days, depending
    on the magnitude.

    Examples
    --------
    >>> from brian2.core.network import _format_time
Exemple #15
0
from .codeobject import CPPStandaloneCodeObject, openmp_pragma


__all__ = []

logger = get_logger(__name__)


# Preferences
prefs.register_preferences(
    'devices.cpp_standalone',
    'C++ standalone preferences ',
    openmp_threads = BrianPreference(
        default=0,
        docs='''
        The number of threads to use if OpenMP is turned on. By default, this value is set to 0 and the C++ code
        is generated without any reference to OpenMP. If greater than 0, then the corresponding number of threads
        are used to launch the simulation.
        ''',
        ),
    )


class CPPWriter(object):
    def __init__(self, project_dir):
        self.project_dir = project_dir
        self.source_files = []
        self.header_files = []
        
    def write(self, filename, contents):
        logger.debug('Writing file %s:\n%s' % (filename, contents))
Exemple #16
0
from .codeobject import CUDAStandaloneCodeObject
from brian2.devices.cpp_standalone.device import CPPWriter, CPPStandaloneDevice
from brian2.monitors.statemonitor import StateMonitor
from brian2.groups.neurongroup import Thresholder


__all__ = []

logger = get_logger(__name__)

prefs.register_preferences(
    'devices.cuda_standalone',
    'CUDA standalone preferences ',
    SM_multiplier = BrianPreference(
        default=1,
        docs='''
        The number of blocks per SM. By default, this value is set to 1.
        ''',
        ),
    )

class CUDAWriter(CPPWriter):
    def __init__(self, project_dir):
        self.project_dir = project_dir
        self.source_files = []
        self.header_files = []
        
    def write(self, filename, contents):
        logger.debug('Writing file %s:\n%s' % (filename, contents))
        if filename.lower().endswith('.cu'):
            self.source_files.append(filename)
Exemple #17
0
from ...targets import codegen_targets
from ...cpp_prefs import get_compiler_and_args
from .extension_manager import cython_extension_manager

__all__ = ['CythonCodeObject']


logger = get_logger(__name__)

# Preferences
prefs.register_preferences(
    'codegen.runtime.cython',
    'Cython runtime codegen preferences',
    multiprocess_safe = BrianPreference(
        default=True,
        docs='''
        Whether to use a lock file to prevent simultaneous write access
        to cython .pyx and .so files.
        '''
        )
    )


class CythonCodeObject(NumpyCodeObject):
    '''
    Execute code using Cython.
    '''
    templater = Templater('brian2.codegen.runtime.cython_rt',
                          env_globals={'cpp_dtype': get_cpp_dtype,
                                       'numpy_dtype': get_numpy_dtype,
                                       'dtype': numpy.dtype})
Exemple #18
0
prefs.register_preferences(
    'codegen.cpp',
    'C++ compilation preferences',
    compiler = BrianPreference(
        default='',
        docs='''
        Compiler to use (uses default if empty)
        
        Should be gcc or msvc.
        '''
        ),
    extra_compile_args = BrianPreference(
        default=None,
        validator=lambda v: True,
        docs='''
        Extra arguments to pass to compiler (if None, use either
        ``extra_compile_args_gcc`` or ``extra_compile_args_msvs``).
        '''
        ),
    extra_compile_args_gcc = BrianPreference(
        default=['-w', '-O3'],
        docs='''
        Extra compile arguments to pass to GCC compiler
        '''
        ),
    extra_compile_args_msvc = BrianPreference(
        default=['/Ox', '/EHsc', '/w'],
        docs='''
        Extra compile arguments to pass to MSVC compiler
        '''
        ),
    include_dirs = BrianPreference(
        default=[],
        docs='''
        Include directories to use. Note that ``$prefix/include`` will be
        appended to the end automatically, where ``$prefix`` is Python's
        site-specific directory prefix as returned by `sys.prefix`.
        '''
        ),
    msvc_vars_location = BrianPreference(
        default='',
        docs='''
        Location of the MSVC command line tool (or search for best by default).
        '''),
    msvc_architecture = BrianPreference(
        default='',
        docs='''
        MSVC architecture name (or use system architectue by default).
        
        Could take values such as x86, amd64, etc.
        '''),
    )
Exemple #19
0
def default_float_dtype_validator(dtype):
    return dtype in [float32, float64]


prefs.register_preferences('core', 'Core Brian preferences',
    default_float_dtype=BrianPreference(
        default=float64,
        docs='''
        Default dtype for all arrays of scalars (state variables, weights, etc.).
        ''',
        representor=dtype_repr,
        validator=default_float_dtype_validator,
        ),
    default_integer_dtype=BrianPreference(
        default=int32,
        docs='''
        Default dtype for all arrays of integer scalars.
        ''',
        representor=dtype_repr,
        ),
    outdated_dependency_error=BrianPreference(
        default=True,
        docs='''
        Whether to raise an error for outdated dependencies (``True``) or just
        a warning (``False``).
        '''
        )
    )

prefs.register_preferences('legacy', 'Preferences to enable legacy behaviour',
    refractory_timing=BrianPreference(
        default=False,
Exemple #20
0
prefs.register_preferences('logging', 'Logging system preferences',
    delete_log_on_exit=BrianPreference(
        default=True,
        docs=    '''
        Whether to delete the log and script file on exit.
        
        If set to ``True`` (the default), log files (and the copy of the main
        script) will be deleted after the brian process has exited, unless an
        uncaught exception occured. If set to ``False``, all log files will be kept.
        ''',
        ),
    file_log_level=BrianPreference(
        default='DIAGNOSTIC',
        docs='''
        What log level to use for the log written to the log file.
        
        In case file logging is activated (see `logging.file_log`), which log
        level should be used for logging. Has to be one of CRITICAL, ERROR,
        WARNING, INFO, DEBUG or DIAGNOSTIC.
        ''',
        validator=log_level_validator),
    console_log_level=BrianPreference(
        default='INFO',
        docs='''
        What log level to use for the log written to the console.
        
        Has to be one of CRITICAL, ERROR, WARNING, INFO, DEBUG or DIAGNOSTIC.
        ''',
        validator=log_level_validator),
    file_log=BrianPreference(
        default=True,
        docs= '''
        Whether to log to a file or not.
        
        If set to ``True`` (the default), logging information will be written
        to a file. The log level can be set via the `logging.file_log_level`
        preference.
        '''),
    save_script=BrianPreference(
        default=True,
        docs= '''
        Whether to save a copy of the script that is run.
        
        If set to ``True`` (the default), a copy of the currently run script
        is saved to a temporary location. It is deleted after a successful
        run (unless `logging.delete_log_on_exit` is ``False``) but is kept after
        an uncaught exception occured. This can be helpful for debugging,
        in particular when several simulations are running in parallel.
        '''),
    std_redirection = BrianPreference(
        default=True,
        docs='''
        Whether or not to redirect stdout/stderr to null at certain places.
        
        This silences a lot of annoying compiler output, but will also hide
        error messages making it harder to debug problems. You can always
        temporarily switch it off when debugging. In any case, the output
        is saved to a file and if an error occurs the name of this file
        will be printed.
        '''
        ),                           
    )
Exemple #21
0
'''
Runtime targets for code generation.
'''
# Register the base category before importing the indivial codegen targets with
# their subcategories
from brian2.core.preferences import prefs
prefs.register_preferences('codegen.runtime',
                           ('Runtime codegen preferences (see subcategories '
                            'for individual targets)'))

from .numpy_rt import *
from .weave_rt import *
try:
    from .cython_rt import *
except ImportError:
    pass # todo: raise a warning?
Exemple #22
0
from .base import device_override

__all__ = ['Network', 'profiling_summary']


logger = get_logger(__name__)


prefs.register_preferences('core.network', 'Network preferences',
                           default_schedule=BrianPreference(
                               default=['start',
                                        'groups',
                                        'thresholds',
                                        'synapses',
                                        'resets',
                                        'end',
                                        ],
                               docs='''
                               Default schedule used for networks that
                               don't specify a schedule.
                               '''
                           )
                           )

def _format_time(time_in_s):
    '''
    Helper function to format time in seconds, minutes, hours, days, depending
    on the magnitude.

    Examples
    --------
Exemple #23
0
def default_float_dtype_validator(dtype):
    return dtype is float64


prefs.register_preferences('core',
                           'Core Brian preferences',
                           default_float_dtype=BrianPreference(
                               default=float64,
                               docs='''
        Default dtype for all arrays of scalars (state variables, weights, etc.).

        Currently, this is not supported (only float64 can be used).
        ''',
                               representor=dtype_repr,
                               validator=default_float_dtype_validator,
                           ),
                           default_integer_dtype=BrianPreference(
                               default=int32,
                               docs='''
        Default dtype for all arrays of integer scalars.
        ''',
                               representor=dtype_repr,
                           ),
                           outdated_dependency_error=BrianPreference(
                               default=True,
                               docs='''
        Whether to raise an error for outdated dependencies (``True``) or just
        a warning (``False``).
        '''))