From 96299cb93a25f9ae38f7392ecb1eb3da8273f515 Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Thu, 14 Nov 2024 15:17:56 +0100 Subject: [PATCH 1/3] Support PROTECT via _NMODLMUTEX{,UN}LOCK. (#1557) * Fix thread_variable conditions. * Support PROTECT via `_NMODLMUTEX{,UN}LOCK`. * Add tests. --- src/codegen/codegen_helper_visitor.cpp | 2 +- src/codegen/codegen_neuron_cpp_visitor.cpp | 9 ++++++ src/codegen/codegen_neuron_cpp_visitor.hpp | 1 + test/usecases/CMakeLists.txt | 1 + test/usecases/protect/shared_counter.mod | 17 +++++++++++ test/usecases/protect/test_shared_counter.py | 30 ++++++++++++++++++++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/usecases/protect/shared_counter.mod create mode 100644 test/usecases/protect/test_shared_counter.py diff --git a/src/codegen/codegen_helper_visitor.cpp b/src/codegen/codegen_helper_visitor.cpp index f2b5eb7ff..4067de9b7 100644 --- a/src/codegen/codegen_helper_visitor.cpp +++ b/src/codegen/codegen_helper_visitor.cpp @@ -290,7 +290,7 @@ void CodegenHelperVisitor::find_non_range_variables() { // if model is thread safe and if parameter is being written then // those variables should be promoted to thread safe variable - if (info.vectorize && info.thread_safe && var->get_write_count() > 0) { + if (info.vectorize && info.declared_thread_safe && var->get_write_count() > 0) { var->mark_thread_safe(); info.thread_variables.push_back(var); info.thread_var_data_size += var->get_length(); diff --git a/src/codegen/codegen_neuron_cpp_visitor.cpp b/src/codegen/codegen_neuron_cpp_visitor.cpp index 7e8420cce..ebcf222c5 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.cpp +++ b/src/codegen/codegen_neuron_cpp_visitor.cpp @@ -1103,6 +1103,7 @@ void CodegenNeuronCppVisitor::print_neuron_includes() { printer->add_multi_line(R"CODE( #include "mech_api.h" #include "neuron/cache/mechanism_range.hpp" + #include "nmodlmutex.h" #include "nrniv_mf.h" #include "section_fwd.hpp" )CODE"); @@ -3063,6 +3064,14 @@ void CodegenNeuronCppVisitor::visit_for_netcon(const ast::ForNetcon& node) { printer->pop_block(); } +void CodegenNeuronCppVisitor::visit_protect_statement(const ast::ProtectStatement& node) { + printer->add_line("_NMODLMUTEXLOCK"); + printer->add_indent(); + node.get_expression()->accept(*this); + printer->add_text(";"); + printer->add_line("_NMODLMUTEXUNLOCK"); +} + } // namespace codegen } // namespace nmodl diff --git a/src/codegen/codegen_neuron_cpp_visitor.hpp b/src/codegen/codegen_neuron_cpp_visitor.hpp index 9b36b1c22..4e0ebb106 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.hpp +++ b/src/codegen/codegen_neuron_cpp_visitor.hpp @@ -839,6 +839,7 @@ class CodegenNeuronCppVisitor: public CodegenCppVisitor { void visit_for_netcon(const ast::ForNetcon& node) override; void visit_longitudinal_diffusion_block(const ast::LongitudinalDiffusionBlock& node) override; void visit_lon_diffuse(const ast::LonDiffuse& node) override; + void visit_protect_statement(const ast::ProtectStatement& node) override; public: /****************************************************************************************/ diff --git a/test/usecases/CMakeLists.txt b/test/usecases/CMakeLists.txt index f776c578d..73aa0ced3 100644 --- a/test/usecases/CMakeLists.txt +++ b/test/usecases/CMakeLists.txt @@ -25,6 +25,7 @@ set(NMODL_USECASE_DIRS point_process pointer procedure + protect random solve state diff --git a/test/usecases/protect/shared_counter.mod b/test/usecases/protect/shared_counter.mod new file mode 100644 index 000000000..c431c7ce3 --- /dev/null +++ b/test/usecases/protect/shared_counter.mod @@ -0,0 +1,17 @@ +NEURON { + SUFFIX shared_counter + GLOBAL g_cnt +} + +ASSIGNED { + g_cnt +} + + +INITIAL { + PROTECT g_cnt = 0 +} + +BREAKPOINT { + PROTECT g_cnt = g_cnt + 1 +} diff --git a/test/usecases/protect/test_shared_counter.py b/test/usecases/protect/test_shared_counter.py new file mode 100644 index 000000000..edc52ae6d --- /dev/null +++ b/test/usecases/protect/test_shared_counter.py @@ -0,0 +1,30 @@ +from neuron import h, gui + + +def test_shared_counter(): + nthreads = 32 + nseg = 10 + nsteps = 100 + + sections = [h.Section() for _ in range(nthreads)] + for s in sections: + s.insert("shared_counter") + s.nseg = nseg + + pc = h.ParallelContext() + + pc.nthread(nthreads) + for k, s in enumerate(sections): + pc.partition(k, h.SectionList([s])) + + h.finitialize() + for _ in range(nsteps): + h.step() + + expected = nthreads * nseg * nsteps + g_cnt = h.g_cnt_shared_counter + assert h.g_cnt_shared_counter == expected, f"{g_cnt}" + + +if __name__ == "__main__": + test_shared_counter() From a851a1b145416805a601b9a7b8ff87d0c0c76d3c Mon Sep 17 00:00:00 2001 From: JCGoran Date: Tue, 19 Nov 2024 12:07:11 +0100 Subject: [PATCH 2/3] Bump min Python version from 3.8 to 3.9 (#1559) --- .github/workflows/nmodl-ci.yml | 2 +- .github/workflows/nmodl-doc.yml | 2 +- CMakeLists.txt | 2 +- INSTALL.rst | 2 +- azure-pipelines.yml | 16 +++++++--------- pyproject.toml | 4 ++-- 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.github/workflows/nmodl-ci.yml b/.github/workflows/nmodl-ci.yml index a794c00fb..4e98cf6ad 100644 --- a/.github/workflows/nmodl-ci.yml +++ b/.github/workflows/nmodl-ci.yml @@ -15,7 +15,7 @@ on: - release/** env: - PYTHON_VERSION: 3.8 + PYTHON_VERSION: 3.9 DESIRED_CMAKE_VERSION: 3.15.0 jobs: diff --git a/.github/workflows/nmodl-doc.yml b/.github/workflows/nmodl-doc.yml index 1c190c963..1d7100a12 100644 --- a/.github/workflows/nmodl-doc.yml +++ b/.github/workflows/nmodl-doc.yml @@ -16,7 +16,7 @@ on: env: BUILD_TYPE: Release - PYTHON_VERSION: 3.8 + PYTHON_VERSION: 3.9 DESIRED_CMAKE_VERSION: 3.15.0 jobs: diff --git a/CMakeLists.txt b/CMakeLists.txt index f8f631bb7..1a98986d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,7 +177,7 @@ endif() # Find required python packages # ============================================================================= message(STATUS "CHECKING FOR PYTHON") -find_package(Python 3.8 REQUIRED COMPONENTS Interpreter) +find_package(Python 3.9 REQUIRED COMPONENTS Interpreter) cpp_cc_strip_python_shims(EXECUTABLE "${PYTHON_EXECUTABLE}" OUTPUT PYTHON_EXECUTABLE) # ============================================================================= diff --git a/INSTALL.rst b/INSTALL.rst index 08262235e..a249ebaaf 100644 --- a/INSTALL.rst +++ b/INSTALL.rst @@ -30,7 +30,7 @@ support is necessary. Make sure you have following packages available: - flex (>=2.6) - bison (>=3.0) - CMake (>=3.15) -- Python (>=3.8) +- Python (>=3.9) - Python packages : jinja2 (>=2.10), pyyaml (>=3.13), pytest (>=4.0.0), sympy (>=1.3), textwrap diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 5ae118e36..be6737ee0 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -84,10 +84,10 @@ stages: sudo apt-add-repository -y ppa:deadsnakes/ppa sudo apt-get update sudo apt-get install -y g++-9 flex bison libfl-dev cython libx11-dev libxcomposite-dev libncurses-dev mpich - sudo apt-get install -y python3.8 python3.8-dev python3.8-venv ninja-build + sudo apt-get install -y python3.9 python3.9-dev python3.9-venv ninja-build sudo apt-get remove -y python3-importlib-metadata - python3.8 -m pip install --upgrade pip setuptools - python3.8 -m pip install --user -r requirements.txt + python3.9 -m pip install --upgrade pip setuptools + python3.9 -m pip install --user -r requirements.txt # we manually get version 3.15.0 to make sure that changes in the cmake # files do not require unsupported versions of cmake in our package. wget --quiet --output-document=- "https://github.com/Kitware/CMake/releases/download/$CMAKE_VER/$CMAKE_PKG.tar.gz" | tar xzpf - @@ -101,7 +101,7 @@ stages: mkdir -p $(Build.Repository.LocalPath)/build cd $(Build.Repository.LocalPath)/build cmake --version - cmake .. -DPYTHON_EXECUTABLE=$(which python3.8) -DCMAKE_INSTALL_PREFIX=$HOME/nmodl -DCMAKE_BUILD_TYPE=Release + cmake .. -DPYTHON_EXECUTABLE=$(which python3.9) -DCMAKE_INSTALL_PREFIX=$HOME/nmodl -DCMAKE_BUILD_TYPE=Release make -j 2 if [ $? -ne 0 ] then @@ -121,7 +121,7 @@ stages: mkdir nrn/build cd nrn/build cmake --version - cmake .. -DNRN_ENABLE_CORENEURON=ON -DNRN_ENABLE_INTERVIEWS=OFF -DNRN_ENABLE_RX3D=OFF -DNRN_ENABLE_MPI=ON -DNRN_ENABLE_TESTS=ON -DCORENRN_ENABLE_NMODL=ON -DCORENRN_NMODL_DIR=$HOME/nmodl -DPYTHON_EXECUTABLE=$(which python3.8) -DCORENRN_NMODL_FLAGS="sympy --analytic" + cmake .. -DNRN_ENABLE_CORENEURON=ON -DNRN_ENABLE_INTERVIEWS=OFF -DNRN_ENABLE_RX3D=OFF -DNRN_ENABLE_MPI=ON -DNRN_ENABLE_TESTS=ON -DCORENRN_ENABLE_NMODL=ON -DCORENRN_NMODL_DIR=$HOME/nmodl -DPYTHON_EXECUTABLE=$(which python3.9) -DCORENRN_NMODL_FLAGS="sympy --analytic" make -j 2 if [ $? -ne 0 ] then @@ -215,7 +215,7 @@ stages: displayName: "Set wheel tag" - script: | - echo "##vso[task.setvariable variable=CIBW_BUILD;]cp38* cp312*" + echo "##vso[task.setvariable variable=CIBW_BUILD;]cp39* cp312*" echo "Build identifiers: $CIBW_BUILD" condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest')) displayName: "Set build identifiers" @@ -260,7 +260,7 @@ stages: displayName: "Set wheel tag" - script: | - echo "##vso[task.setvariable variable=CIBW_BUILD;]cp38* cp312*" + echo "##vso[task.setvariable variable=CIBW_BUILD;]cp39* cp312*" echo "Build identifiers: $CIBW_BUILD" condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest')) displayName: "Set build identifiers" @@ -291,8 +291,6 @@ stages: strategy: matrix: ${{ if eq(variables.buildWheel, True) }}: - Python38: - python.version: '3.8' Python39: python.version: '3.9' Python310: diff --git a/pyproject.toml b/pyproject.toml index 28082e014..36332b0aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ dependencies = [ "importlib-resources;python_version<'3.9'", ] scripts = {nmodl = "nmodl._binwrapper:main"} -requires-python = ">=3.8" +requires-python = ">=3.9" optional-dependencies.test = ["pytest>=3.3.0", "pytest-cov", "numpy", "scipy"] optional-dependencies.docs = [ @@ -76,4 +76,4 @@ environment = { PATH = "/nmodlwheel/flex/bin:/nmodlwheel/bison/bin:$PATH" } test-command = "true" [tool.cibuildwheel.windows] -environment = { SKBUILD_CMAKE_ARGS = "-DNMODL_BUILD_WHEEL=ON;-DFLEX_INCLUDE_PATH=C:/ProgramData/chocolatey/lib/winflexbison3/tools" } \ No newline at end of file +environment = { SKBUILD_CMAKE_ARGS = "-DNMODL_BUILD_WHEEL=ON;-DFLEX_INCLUDE_PATH=C:/ProgramData/chocolatey/lib/winflexbison3/tools" } From 977ab5fa1e82e3299010315316166a8cc0c2e59c Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Wed, 13 Nov 2024 14:12:37 +0100 Subject: [PATCH 3/3] Update `clang-format == 19.1.3`. Fixes a bug in clang-format that would reorder multi-line string literals according to the rules for `#include` directives. --- .bbp-project.yaml | 2 +- src/lexer/modtoken.hpp | 2 +- src/visitors/sympy_solver_visitor.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.bbp-project.yaml b/.bbp-project.yaml index d233e4e16..807a0a763 100644 --- a/.bbp-project.yaml +++ b/.bbp-project.yaml @@ -1,7 +1,7 @@ tools: ClangFormat: enable: True - version: ~=13.0 + version: == 19.1.3 exclude: match: - ext/.* diff --git a/src/lexer/modtoken.hpp b/src/lexer/modtoken.hpp index 4bb60d229..d65673436 100644 --- a/src/lexer/modtoken.hpp +++ b/src/lexer/modtoken.hpp @@ -68,7 +68,7 @@ class ModToken { /// \{ ModToken() - : pos(nullptr, 0){}; + : pos(nullptr, 0) {}; explicit ModToken(bool ext) : pos(nullptr, 0) diff --git a/src/visitors/sympy_solver_visitor.hpp b/src/visitors/sympy_solver_visitor.hpp index a77373e2d..5408f137e 100644 --- a/src/visitors/sympy_solver_visitor.hpp +++ b/src/visitors/sympy_solver_visitor.hpp @@ -170,7 +170,7 @@ class SympySolverVisitor: public AstVisitor { int SMALL_LINEAR_SYSTEM_MAX_STATES = 3) : use_pade_approx(use_pade_approx) , elimination(elimination) - , SMALL_LINEAR_SYSTEM_MAX_STATES(SMALL_LINEAR_SYSTEM_MAX_STATES){}; + , SMALL_LINEAR_SYSTEM_MAX_STATES(SMALL_LINEAR_SYSTEM_MAX_STATES) {}; void visit_var_name(ast::VarName& node) override; void visit_diff_eq_expression(ast::DiffEqExpression& node) override;