From b549a0cc2eeecba539623c5b29d4050d17d17741 Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Wed, 13 Nov 2024 09:09:30 +0100 Subject: [PATCH] Add tests. --- test/usecases/CMakeLists.txt | 1 + test/usecases/protect/shared_counter.mod | 17 +++++++++++ test/usecases/protect/test_shared_counter.py | 30 ++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 test/usecases/protect/shared_counter.mod create mode 100644 test/usecases/protect/test_shared_counter.py 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()