def checkBrian(test=False): """Check for the Brian2 simulator""" try: import brian2 as br found = True if verbose > 1: print('Brian module found!') if test: print('Running suite of tests...') br.prefs.codegen.target = 'cython' #'numpy #'auto' #'weave' - Python 2 only out = br.codegen.cpp_prefs.get_compiler_and_args() print(out) br.test() except ImportError: found = False if verbose > 1: print('Brian module not found!') return found
""" Run all the Cython tests (including tests that take a long time) using nose. Exits with error code 1 if a test failed. """ import sys import brian2 if not brian2.test("cython", long_tests=True): # If the test fails, exit with a non-zero error code sys.exit(1)
import brian2 brian2.test()
import sys import brian2genn import brian2 import numpy as np if __name__ == '__main__': success = brian2.test([], test_codegen_independent=False, test_standalone='genn', fail_for_not_implemented=False, float_dtype=np.float32) if not success: sys.exit(1)
targets = None independent = True if operating_system == 'windows' or report_coverage: in_parallel = [] else: in_parallel = ['codegen_independent', 'numpy', 'cython', 'cpp_standalone'] if operating_system in ['linux', 'windows']: openmp = True else: openmp = False reset_preferences = not cross_compiled if standalone: result = brian2.test([], test_codegen_independent=False, test_standalone='cpp_standalone', test_openmp=openmp, test_in_parallel=in_parallel, reset_preferences=reset_preferences) else: result = brian2.test(targets, test_codegen_independent=independent, test_standalone=None, test_in_parallel=in_parallel, reset_preferences=reset_preferences) if not result: sys.exit(1)
import sys import numpy as np import brian2genn import brian2 if __name__ == '__main__': success = brian2.test([], test_codegen_independent=False, test_standalone='genn', build_options={'use_GPU': False}, fail_for_not_implemented=False, float_dtype=np.float32, reset_preferences=False) if not success: sys.exit(1)
import sys import brian2genn import brian2 if __name__ == '__main__': success = brian2.test([], test_codegen_independent=False, test_standalone='genn', build_options={'use_GPU': False}, fail_for_not_implemented=False, reset_preferences=False) if not success: sys.exit(1)
import sys import brian2 split_run = os.environ.get('SPLIT_RUN', None) standalone = os.environ.get('STANDALONE', 'FALSE').lower() == 'true' if split_run == '1': targets = ['numpy', 'weave'] independent = True elif split_run == '2': targets = ['cython'] independent = False else: targets = None independent = True if standalone: result = brian2.test([], test_codegen_independent=False, test_standalone='cpp_standalone', test_in_parallel=[]) else: result = brian2.test(targets, test_codegen_independent=independent, test_standalone=None, test_in_parallel=[]) if not result: sys.exit(1)
''' Run all the numpy tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if not brian2.test('numpy', test_codegen_independent=False): # If the test fails, exit with a non-zero error code sys.exit(1)
""" Run all the non-standalone tests using nose. Exits with error code 1 if a test failed. """ import sys import brian2 if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "no-parallel": if not brian2.test(test_in_parallel=[]): # If the test fails, exit with a non-zero error code sys.exit(1) elif len(sys.argv) > 1 and sys.argv[1] == "cross-compiled": if not brian2.test(reset_preferences=False): # If the test fails, exit with a non-zero error code sys.exit(1) else: if not brian2.test(): # If the test fails, exit with a non-zero error code sys.exit(1)
""" Run brian2 test suite on standalone architectures """ from brian2 import test import argparse parser = argparse.ArgumentParser(description='Run the brian2 testsuite on GPU.') parser.add_argument('--targets', nargs='*', default='cuda_standalone', type=str, choices=['cuda_standalone', 'genn', 'cpp_standalone'], help=("Which codegeneration targets to use, can be multiple. " "Only standalone targets. (default='cuda_standalone')")) parser.add_argument('--no_long_tests', action='store_false', help="Set to not run long tests. By default they are run.") args = parser.parse_args() if 'cuda_standalone' in args.targets: import brian2cuda if 'genn' in args.targets: import brian2genn for target in args.targets: test(codegen_targets=[], long_tests=args.no_long_tests, test_codegen_independent=False, test_standalone=target )
''' Run all the numpy tests (including tests that take a long time) using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if not brian2.test('numpy', long_tests=True): # If the test fails, exit with a non-zero error code sys.exit(1)
def run(test_standalone=['cuda_standalone'], long_tests=False, reset_preferences=True, fail_for_not_implemented=False, test_in_parallel=False, build_options=None, extra_test_dirs=None, float_dtype=None, quiet=True, debug=False, additional_args=[], threads=None): """ Run the brian2cuda test suite. This includes all standalone-compatible tests from the brian2 test suite. Needs an installation of the pytest testing tool. For testing, the preferences will be reset to the default preferences. After testing, the user preferences will be restored. Parameters ---------- test_standalone : list of str, optional Specify a list of standlone devices to run the tests for. Should be the names of standalone modes (e.g. ``'cuda_standalone'``) and expects that a device of that name and an accordingly named "simple" device (e.g. ``'cuda_standalone_simple'`` exists that can be used for testing (see `CUDAStandaloneSimpleDevice` for details. Defaults to ``['cuda_standalone']``. long_tests : bool, optional Whether to run tests that take a long time. Defaults to ``False``. reset_preferences : bool, optional Whether to reset all preferences to the default preferences before running the test suite. Defaults to ``True`` to get test results independent of the user's preference settings but can be switched off when the preferences are actually necessary to pass the tests (e.g. for device-specific settings). fail_for_not_implemented : bool, optional Whether to fail for tests raising a `NotImplementedError`. Defaults to ``False``, because some of the Brian2 tests can't pass in Brian2CUDA due to the way the tests are implemented. test_in_parallel : bool, optional Whether to run multiple tests in parallel (requires ``xdist`` installed). If ``True``, use all available CPU threads to run one test per thread in parallel. Note: This can lead to increased RAM usage! If this this is enabled, consider reducing the number of threads during parallel compilation of each test (via the ``threads`` parameter). excessive CPU usage during compilation. build_options : dict, optional Non-default build options that will be passed as arguments to the `set_device` call for the device specified in ``test_standalone``. extra_test_dirs : list of str or str, optional Additional directories as a list of strings (or a single directory as a string) that will be searched for additional tests. float_dtype : np.dtype, optional Set the dtype to use for floating point variables to a value different from the default `core.default_float_dtype` setting. quiet : bool, optional Disable verbose output during test runs. debug : bool, optional Disable pytest output capture and enable brian2 output. additional_args : list of str, optional Optional command line arguments to pass to ``pytest`` disable_compiler_optim : bool, optional Disable all compiler optimizations for host and device compiler. This speeds up compilation but reduces binary runtimes. threads : int, optional Number of CPU threads to use for parallel compilation. Default (``None``) uses all available threads. Using more threads requires more CPU RAM. """ b2c_dir = os.path.abspath(os.path.dirname(brian2cuda.__file__)) print( f"Running Brian2CUDA version {brian2cuda.__version__} from {b2c_dir}") brian2cuda_tests_dir = os.path.abspath(os.path.dirname(__file__)) if extra_test_dirs is None: extra_test_dirs = [brian2cuda_tests_dir] elif isinstance(extra_test_dirs, str): extra_test_dirs = [brian2cuda_tests_dir, extra_test_dirs] else: extra_test_dirs = [brian2cuda_tests_dir, *extra_test_dirs] pytest_rootdir = os.path.dirname(b2c_dir) pytest_args = [ # Set the pytest rootdir such that we know the node paths for --deselect f'--rootdir={pytest_rootdir}', # Set confcutdir, such that all `conftest.py` files inside the brian2 and # brian2cuda directories are loaded (this overwrites confcutdir set in brian2's # `make_argv`, which stops searching for `conftest.py` files outside the # `brian2` directory) f'--confcutdir={os.path.commonpath([brian2.__file__, brian2cuda.__file__])}', ] if quiet: # set verbosity to max(?) additional_args += ['-vvv'] if not quiet: # set verbosity to max(?) pytest_args += ['-vvv'] extra_build_options = {} if debug: # disable output capture pytest_args += ['-s'] # enable brian2 output extra_build_options = {'with_output': True} if build_options: extra_build_options.update(**build_options) # Deselect some tests from the brian2 test suite ignore_brian2_tests = [ # These tests assert on the number of warning and fail because of warnings # from brian2cuda (fixed versions of these tests are added to the brian2cuda # test suite) 'tests/test_neurongroup.py::test_semantics_floor_division' ] for test in ignore_brian2_tests: test_abspath = os.path.abspath( os.path.join(os.path.dirname(brian2.__file__), test)) test_nodeid = os.path.relpath(test_abspath, pytest_rootdir) additional_args += ['--deselect', test_nodeid] pytest_args += additional_args if reset_preferences: print('Resetting to default preferences') stored_user_prefs = prefs.as_file prefs.read_preference_file(StringIO(prefs.defaults_as_file)) # Surpress warnings from nvcc compiler nvcc_compiler_args = [ '-Xcudafe "--diag_suppress=declared_but_not_referenced"' ] # Set number of threads for parallel compilation if threads is not None: print(f'Compiling in parallel with {threads} threads') k = 'devices.cpp_standalone.extra_make_args_unix' assert '-j' in prefs[k], f"``-j`` is not in default prefs[{k}] anymore" new_j = f'-j{threads}' prefs[k].remove('-j') prefs[k].append(new_j) prefs[ 'devices.cuda_standalone.cuda_backend.extra_compile_args_nvcc'].extend( nvcc_compiler_args) try: successes = [] stored_prefs = prefs.as_file for target in test_standalone: # Reset preference between test runs (since brian2.test modifies them) prefs.read_preference_file(StringIO(stored_prefs)) success = brian2.test( codegen_targets=[], long_tests=long_tests, test_codegen_independent=False, test_standalone=target, reset_preferences=False, fail_for_not_implemented=fail_for_not_implemented, test_in_parallel=[target] if test_in_parallel else [], extra_test_dirs=extra_test_dirs, float_dtype=float_dtype, additional_args=pytest_args, build_options=extra_build_options) successes.append(success) all_success = all(successes) if not all_success: num_fails = len(successes) - sum(successes) print( f"ERROR: Test suite(s) for {num_fails}/{len(successes)} standalone" f" targets did not complete successfully (see above).") else: print( f"OK: Test suite(s) for {len(successes)}/{len(successes)} standalone " f"targets did complete successfully") return all_success finally: if reset_preferences: # Restore the user preferences prefs.read_preference_file(StringIO(stored_user_prefs)) prefs._backup()
import brian2 as b2 # import matplotlib.pyplot as plt # import numpy as np from neurodynex.leaky_integrate_and_fire import LIF # from neurodynex.tools import input_factory, plot_tools b2.test() LIF.getting_started() LIF.print_default_parameters()
''' Run all the non-standalone tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if __name__ == '__main__': if len(sys.argv) > 1 and sys.argv[1] == 'no-parallel': if not brian2.test( test_in_parallel=[] ): # If the test fails, exit with a non-zero error code sys.exit(1) elif len(sys.argv) > 1 and sys.argv[1] == 'cross-compiled': if not brian2.test( reset_preferences=False ): # If the test fails, exit with a non-zero error code sys.exit(1) else: if not brian2.test( ): # If the test fails, exit with a non-zero error code sys.exit(1)
''' Run all the standalone tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if __name__ == '__main__': if len(sys.argv) > 1 and sys.argv[1] == 'no-parallel': if not brian2.test( [], test_codegen_independent=False, test_standalone='cpp_standalone', test_in_parallel=[], test_openmp=True ): # If the test fails, exit with a non-zero error code sys.exit(1) else: if not brian2.test( [], test_codegen_independent=False, test_standalone='cpp_standalone', test_openmp=True ): # If the test fails, exit with a non-zero error code sys.exit(1)
import sys import brian2 import numpy as np # Run tests for float32 and float64 success = [ brian2.test(long_tests=True, test_standalone='cpp_standalone', float_dtype=np.float32), brian2.test(long_tests=True, test_standalone='cpp_standalone', float_dtype=np.float64) ] result = [ 'Tests for {} dtype: {}'.format(dtype, 'passed' if status else 'FAILED') for status, dtype in zip(success, ['float32', 'float64']) ] print('\t--\t'.join(result)) if all(success): print('OK: All tests passed successfully') else: print('FAILED: Not all tests passed successfully (see above)') sys.exit(1)
if operating_system in ['linux', 'windows']: openmp = True else: openmp = False reset_preferences = not cross_compiled if dtype_32_bit: float_dtype = np.float32 else: float_dtype = None if standalone: result = brian2.test([], test_codegen_independent=False, test_standalone='cpp_standalone', test_openmp=openmp, test_in_parallel=in_parallel, reset_preferences=reset_preferences, float_dtype=float_dtype) else: result = brian2.test(targets, test_codegen_independent=independent, test_standalone=None, test_in_parallel=in_parallel, reset_preferences=reset_preferences, float_dtype=float_dtype) if not result: sys.exit(1)
import sys import brian2 if not brian2.test(long_tests=True, test_standalone='cpp_standalone'): sys.exit(1)
''' Run all the Cython tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if not brian2.test('cython'): # If the test fails, exit with a non-zero error code sys.exit(1)
import sys import brian2 import numpy as np # Run tests for float32 and float64 success = [brian2.test(long_tests=True, test_standalone='cpp_standalone', float_dtype=np.float32), brian2.test(long_tests=True, test_standalone='cpp_standalone', float_dtype=np.float64)] result = ['Tests for {} dtype: {}'.format(dtype, 'passed' if status else 'FAILED') for status, dtype in zip(success, ['float32', 'float64'])] print('\t--\t'.join(result)) if all(success): print('OK: All tests passed successfully') else: print('FAILED: Not all tests passed successfully (see above)') sys.exit(1)
''' Run all the Cython tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if not brian2.test( 'cython'): # If the test fails, exit with a non-zero error code sys.exit(1)
''' Run all the standalone tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if not brian2.test([], test_codegen_independent=False, test_standalone='cpp_standalone'): # If the test fails, exit with a non-zero error code sys.exit(1)
import brian2genn import brian2 from brian2.tests.features import (Configuration, DefaultConfiguration, run_feature_tests, run_single_feature_test) from brian2genn.correctness_testing import GeNNConfiguration from brian2.tests.features.synapses import * from brian2.tests.features.neurongroup import * from brian2.tests.features.monitors import * from brian2.tests.features.speed import * from brian2.tests.features.input import * from brian2.tests.features import CPPStandaloneConfiguration from brian2 import prefs if __name__ == '__main__': brian2.test([], test_codegen_independent=False, test_standalone='genn')
''' Run all the numpy tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if not brian2.test('numpy'): # If the test fails, exit with a non-zero error code sys.exit(1)
import brian2genn import brian2 from brian2.tests.features import (Configuration, DefaultConfiguration, run_feature_tests, run_single_feature_test) from brian2genn.correctness_testing import GeNNConfiguration from brian2.tests.features.synapses import * from brian2.tests.features.neurongroup import * from brian2.tests.features.monitors import * from brian2.tests.features.speed import * from brian2.tests.features.input import * from brian2.tests.features import CPPStandaloneConfiguration from brian2 import prefs if __name__=='__main__': brian2.test([], test_codegen_independent=False, test_standalone='genn')
''' Run all the Weave tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if not brian2.test( 'weave'): # If the test fails, exit with a non-zero error code sys.exit(1)
import sys import numpy as np import brian2genn import brian2 if __name__ == '__main__': success = brian2.test([], test_codegen_independent=False, test_standalone='genn', build_options={'use_GPU': False}, fail_for_not_implemented=False, float_dtype=np.float32) if not success: sys.exit(1)
''' Run all the numpy tests (including tests that take a long time) using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if not brian2.test( 'numpy', long_tests=True): # If the test fails, exit with a non-zero error code sys.exit(1)
if prefs_dict is not None: buffer.add( f"{n + 1}. RUN: test suite on CUDA_STANDALONE with prefs: ") # print and set preferences print_lines = utils.print_single_prefs(prefs_dict, set_prefs=prefs, return_lines=True) buffer.add(print_lines) buffer.print_all() success = test(codegen_targets=[], long_tests=args.no_long_tests, test_codegen_independent=False, test_standalone=target, reset_preferences=False, fail_for_not_implemented=args.fail_not_implemented, test_in_parallel=test_in_parallel, extra_test_dirs=extra_test_dirs, float_dtype=None, additional_args=additional_args, build_options=build_options) successes.append(success) buffer.add(f"\nTARGET: {target.upper()}") all_success, print_lines = utils.check_success(successes, all_prefs_combinations, return_lines=True) all_successes.append(all_success) buffer.add(print_lines) buffer.print_all()
''' Run all the standalone tests using nose. Exits with error code 1 if a test failed. ''' import sys import brian2 if __name__ == '__main__': if len(sys.argv) > 1 and sys.argv[1] == 'no-parallel': if not brian2.test([], test_codegen_independent=False, test_standalone='cpp_standalone', test_in_parallel=[]): # If the test fails, exit with a non-zero error code sys.exit(1) else: if not brian2.test([], test_codegen_independent=False, test_standalone='cpp_standalone'): # If the test fails, exit with a non-zero error code sys.exit(1)
if prefs_dict is not None: buffer.add("{}. RUN: test suite on CUDA_STANDALONE with prefs: " "".format(n + 1)) # print and set preferences print_lines = utils.print_single_prefs(prefs_dict, set_prefs=prefs, return_lines=True) buffer.add(print_lines) buffer.print_all() success = test(codegen_targets=[], long_tests=args.no_long_tests, test_codegen_independent=False, test_standalone=target, reset_preferences=False, fail_for_not_implemented=not args.skip_not_implemented, test_in_parallel=test_in_parallel, extra_test_dirs=extra_test_dirs, float_dtype=None) successes.append(success) buffer.add("\nTARGET: {}".format(target.upper())) all_success, print_lines = utils.check_success(successes, all_prefs_combinations, return_lines=True) all_successes.append(all_success) buffer.add(print_lines) buffer.print_all()
# Surpress some warnings from nvcc compiler prefs['codegen.cuda.extra_compile_args_nvcc'].extend( ['-Xcudafe "--diag_suppress=declared_but_not_referenced"']) prefs['devices.cpp_standalone.extra_make_args_unix'] = [ '-j' + str(args.jobs[0]) ] extra_test_dirs = os.path.abspath(os.path.dirname(brian2cuda.__file__)) if 'genn' in args.targets: import brian2genn if args.test_parallel is None: args.test_parallel = args.targets for target in args.targets: test_in_parallel = [] if target in args.test_parallel: test_in_parallel = [target] test(codegen_targets=[], long_tests=args.no_long_tests, test_codegen_independent=False, test_standalone=target, reset_preferences=False, fail_for_not_implemented=args.fail_not_implemented, test_in_parallel=test_in_parallel, extra_test_dirs=extra_test_dirs)
''' Run all the tests using nose. ''' import brian2 brian2.test()