From 57d6654e38034d37e20426a9ef342a46e380cecc Mon Sep 17 00:00:00 2001 From: Nicolai Haug Date: Thu, 25 May 2023 12:33:35 +0200 Subject: [PATCH 1/2] Port issue-1640.sli to Pytest --- .../sli2py_regressions/test_issue_1640.py | 68 ++++++++++++++ testsuite/regressiontests/issue-1640.sli | 93 ------------------- 2 files changed, 68 insertions(+), 93 deletions(-) create mode 100644 testsuite/pytests/sli2py_regressions/test_issue_1640.py delete mode 100644 testsuite/regressiontests/issue-1640.sli diff --git a/testsuite/pytests/sli2py_regressions/test_issue_1640.py b/testsuite/pytests/sli2py_regressions/test_issue_1640.py new file mode 100644 index 0000000000..d7ee2a274b --- /dev/null +++ b/testsuite/pytests/sli2py_regressions/test_issue_1640.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +# +# test_issue_1640.py +# +# This file is part of NEST. +# +# Copyright (C) 2004 The NEST Initiative +# +# NEST is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# NEST is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with NEST. If not, see . + +""" +Regression test for Issue #1640 (GitHub). +""" + +import numpy as np +import pytest + +import nest + + +@pytest.mark.skipif_missing_threads +@pytest.mark.parametrize("num_threads", [1, 2, 3, 4]) +@pytest.mark.parametrize("num_neurons", range(1, 11, 1)) +def test_getconnections_with_pre_post_device_multithreaded(num_threads, num_neurons): + """ + Multithreaded test of `GetConnections` with devices as source and target. + + The test ensures that `GetConnections` does not return erroneous connections + when devices are connected as both source and target while using + multiple threads. + """ + + nest.ResetKernel() + nest.local_num_threads = num_threads + + neurons = nest.Create("iaf_psc_alpha", num_neurons) + pgen = nest.Create("poisson_generator") + srec = nest.Create("spike_recorder") + + # A poisson_generator is connected as source + nest.Connect(pgen, neurons) + # An additional device is connected as target + nest.Connect(neurons, srec) + + # We only want the connections where the poisson_generator is source + conns = nest.GetConnections(source=pgen) + + # Check that the poisson_generator is the source for all connections + expected_src_id = pgen.global_id + actual_src_ids = conns.get("source") + actual_src_ids = np.atleast_1d(actual_src_ids) # Ensure source ids are iterable + assert np.all(actual_src_ids == expected_src_id) + + # Check that number of connections is correct + expected_num_conns = num_neurons + actual_num_conns = actual_src_ids.size + assert actual_num_conns == expected_num_conns diff --git a/testsuite/regressiontests/issue-1640.sli b/testsuite/regressiontests/issue-1640.sli deleted file mode 100644 index a2347d9f0e..0000000000 --- a/testsuite/regressiontests/issue-1640.sli +++ /dev/null @@ -1,93 +0,0 @@ -/* - * issue-1640.sli - * - * This file is part of NEST. - * - * Copyright (C) 2004 The NEST Initiative - * - * NEST is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * NEST is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NEST. If not, see . - * - */ - - - /** @BeginDocumentation -Name: testsuite::issue-1640 - -Synopsis: (issue-1640) run -> NEST exits if test fails - -Description: -Ensure that GetConnections does not return erroneous connections -when devices are connected as both source and target while using multiple threads. - -Author: Håkon Mørk -FirstVersion: August 2020 -*/ - -skip_if_not_threaded - -(unittest) run -/unittest using - -M_ERROR setverbosity - -{ - /all_conns_correct true def - [2 4 1] Range - { - /num_threads Set - [1 10 1] Range - { - /num_neurons Set - /expected_num_conns num_neurons def - - ResetKernel - << /local_num_threads num_threads >> SetKernelStatus - - /neurons /iaf_psc_alpha num_neurons Create def - - % A poisson_generator is connected as source - /pg /poisson_generator Create def - pg neurons Connect - - % An additional device is connected as target - /sr /spike_recorder Create def - neurons sr Connect - - % We only want the connections where the poisson_generator is source - /conns << /source pg >> GetConnections def - - % Check that the poisson_generator is the source for all connections - /pg_id pg GetStatus 0 get /global_id get def - /conn_source_correct true conns {GetStatus /source get pg_id eq and} Fold def - - % Check that number of connections is correct - /conn_length_correct conns length expected_num_conns eq def - - /conns_correct conn_source_correct conn_length_correct and def - conns_correct all_conns_correct and /all_conns_correct Set - - % Helpful information in case of failure - conns_correct not - { - num_threads cvs ( threads: ) join conns length cvs join - ( conns, expected ) join expected_num_conns cvs join = - } if - } forall - } forall - - all_conns_correct -} -assert_or_die - -endusing From e94704eb020609911a789d4c0b5254a34583d3f1 Mon Sep 17 00:00:00 2001 From: Nicolai Haug <39106781+nicolossus@users.noreply.github.com> Date: Wed, 30 Aug 2023 09:32:04 +0200 Subject: [PATCH 2/2] Use same ranges as in SLI test --- testsuite/pytests/sli2py_regressions/test_issue_1640.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testsuite/pytests/sli2py_regressions/test_issue_1640.py b/testsuite/pytests/sli2py_regressions/test_issue_1640.py index d7ee2a274b..486c746329 100644 --- a/testsuite/pytests/sli2py_regressions/test_issue_1640.py +++ b/testsuite/pytests/sli2py_regressions/test_issue_1640.py @@ -30,8 +30,8 @@ @pytest.mark.skipif_missing_threads -@pytest.mark.parametrize("num_threads", [1, 2, 3, 4]) -@pytest.mark.parametrize("num_neurons", range(1, 11, 1)) +@pytest.mark.parametrize("num_threads", [2, 3, 4]) +@pytest.mark.parametrize("num_neurons", range(1, 11)) def test_getconnections_with_pre_post_device_multithreaded(num_threads, num_neurons): """ Multithreaded test of `GetConnections` with devices as source and target.