From 33ceb305e7a1c6f634b363c99ab7cab92d6df76a Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Mon, 4 Dec 2023 16:48:51 +0100 Subject: [PATCH 1/9] Refactor printing slist/dlist in new function similarly to NEURON --- src/codegen/codegen_neuron_cpp_visitor.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/codegen/codegen_neuron_cpp_visitor.cpp b/src/codegen/codegen_neuron_cpp_visitor.cpp index f4b3747ce0..b6a66b29e1 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.cpp +++ b/src/codegen/codegen_neuron_cpp_visitor.cpp @@ -286,6 +286,13 @@ void CodegenNeuronCppVisitor::print_neuron_includes() { void CodegenNeuronCppVisitor::print_sdlists_init(bool print_initializers) { + printer->add_newline(2); + printer->add_line("static void _initlists() {"); + printer->increase_indent(); + printer->add_multi_line(R"CODE( + static int _first = 1; + if (!_first) return; + )CODE"); for (auto i = 0; i < info.prime_variables_by_order.size(); ++i) { const auto& prime_var = info.prime_variables_by_order[i]; /// TODO: Something similar needs to happen for slist/dlist2 but I don't know their usage at @@ -319,6 +326,9 @@ void CodegenNeuronCppVisitor::print_sdlists_init(bool print_initializers) { position_of_float_var(prime_var_deriv_name)); } } + printer->add_line("_first = 0;"); + printer->decrease_indent(); + printer->add_line("};"); } @@ -389,7 +399,7 @@ void CodegenNeuronCppVisitor::print_mechanism_register() { printer->add_newline(2); printer->add_line("/** register channel with the simulator */"); printer->fmt_push_block("void _{}_reg()", info.mod_file); - print_sdlists_init(true); + printer->add_line("_initlists();"); // type related information printer->add_newline(); printer->fmt_line("int mech_type = nrn_get_mechtype({}[1]);", get_channel_info_var_name()); @@ -659,6 +669,7 @@ void CodegenNeuronCppVisitor::print_codegen_routines() { print_data_structures(true); print_global_variables_for_hoc(); print_compute_functions(); // only nrn_cur and nrn_state + print_sdlists_init(true); print_mechanism_register(); print_namespace_end(); codegen = false; From 4c0c64c8de3abc8b6eb599e5361010436aaafb28 Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Mon, 4 Dec 2023 16:49:46 +0100 Subject: [PATCH 2/9] Fix tests --- test/unit/codegen/codegen_neuron_cpp_visitor.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp index d56141010b..1cceddc020 100644 --- a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp +++ b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp @@ -227,10 +227,7 @@ void _nrn_mechanism_register_data_fields(Args&&... args) { THEN("Placeholder registration function is printed") { std::string expected_placeholder_reg = R"(/** register channel with the simulator */ void __test_reg() { - /* s */ - _slist1[0] = {4, 0}; - /* Ds */ - _dlist1[0] = {7, 0}; + _initlists(); int mech_type = nrn_get_mechtype(mechanism_info[1]); _nrn_mechanism_register_data_fields(mech_type, From 4dc457bedf31228be61de236b695c61ae931f7c3 Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Tue, 5 Dec 2023 16:57:44 +0100 Subject: [PATCH 3/9] Added test for initlists --- test/unit/codegen/codegen_neuron_cpp_visitor.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp index 69863cd156..2c59f098be 100644 --- a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp +++ b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp @@ -227,6 +227,21 @@ void _nrn_mechanism_register_data_fields(Args&&... args) { REQUIRE_THAT(generated, ContainsSubstring(reindent_and_trim_text(expected_placeholder_nrn_state))); } + THEN("Initialization function for slist/dlist is printed correctly") { + std::string expected_initlists = R"( + static void _initlists() { + static int _first = 1; + if (!_first) return; + /* s */ + _slist1[0] = {4, 0}; + /* Ds */ + _dlist1[0] = {7, 0}; + _first = 0; + };)"; + + REQUIRE_THAT(generated, + ContainsSubstring(reindent_and_trim_text(expected_initlists))); + } THEN("Placeholder registration function is printed") { std::string expected_placeholder_reg = R"CODE(/** register channel with the simulator */ extern "C" void __test_reg() { From 387118336c5391a8f731dcbb9366035839e44bdb Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Tue, 5 Dec 2023 17:17:33 +0100 Subject: [PATCH 4/9] Make format happy --- test/unit/codegen/codegen_neuron_cpp_visitor.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp index 2c59f098be..499f289e9e 100644 --- a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp +++ b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp @@ -239,8 +239,7 @@ void _nrn_mechanism_register_data_fields(Args&&... args) { _first = 0; };)"; - REQUIRE_THAT(generated, - ContainsSubstring(reindent_and_trim_text(expected_initlists))); + REQUIRE_THAT(generated, ContainsSubstring(reindent_and_trim_text(expected_initlists))); } THEN("Placeholder registration function is printed") { std::string expected_placeholder_reg = R"CODE(/** register channel with the simulator */ From 05ea2e0c3ca073adb36bd563d285dd20b6890eff Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Wed, 6 Dec 2023 13:15:56 +0100 Subject: [PATCH 5/9] Removed check for second set up of the slist/dlist --- src/codegen/codegen_neuron_cpp_visitor.cpp | 13 ++++--------- test/unit/codegen/codegen_neuron_cpp_visitor.cpp | 3 --- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/codegen/codegen_neuron_cpp_visitor.cpp b/src/codegen/codegen_neuron_cpp_visitor.cpp index 6db3aeb7b5..245a69b793 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.cpp +++ b/src/codegen/codegen_neuron_cpp_visitor.cpp @@ -286,13 +286,10 @@ void CodegenNeuronCppVisitor::print_neuron_includes() { void CodegenNeuronCppVisitor::print_sdlists_init(bool print_initializers) { + /// _initlists() should only be called once by the mechanism registration function + /// (__reg()) printer->add_newline(2); - printer->add_line("static void _initlists() {"); - printer->increase_indent(); - printer->add_multi_line(R"CODE( - static int _first = 1; - if (!_first) return; - )CODE"); + printer->push_block("static void _initlists()"); for (auto i = 0; i < info.prime_variables_by_order.size(); ++i) { const auto& prime_var = info.prime_variables_by_order[i]; /// TODO: Something similar needs to happen for slist/dlist2 but I don't know their usage at @@ -326,9 +323,7 @@ void CodegenNeuronCppVisitor::print_sdlists_init(bool print_initializers) { position_of_float_var(prime_var_deriv_name)); } } - printer->add_line("_first = 0;"); - printer->decrease_indent(); - printer->add_line("};"); + printer->pop_block(";"); } diff --git a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp index 499f289e9e..22db5121f0 100644 --- a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp +++ b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp @@ -230,13 +230,10 @@ void _nrn_mechanism_register_data_fields(Args&&... args) { THEN("Initialization function for slist/dlist is printed correctly") { std::string expected_initlists = R"( static void _initlists() { - static int _first = 1; - if (!_first) return; /* s */ _slist1[0] = {4, 0}; /* Ds */ _dlist1[0] = {7, 0}; - _first = 0; };)"; REQUIRE_THAT(generated, ContainsSubstring(reindent_and_trim_text(expected_initlists))); From a6acc3dfaf5758dfb3c9c35240713774f7a941bb Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Thu, 7 Dec 2023 11:58:16 +0100 Subject: [PATCH 6/9] Addressed Luc's comments --- src/codegen/codegen_neuron_cpp_visitor.cpp | 6 +++--- test/unit/codegen/codegen_neuron_cpp_visitor.cpp | 12 ++++-------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/codegen/codegen_neuron_cpp_visitor.cpp b/src/codegen/codegen_neuron_cpp_visitor.cpp index 245a69b793..1b55d94792 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.cpp +++ b/src/codegen/codegen_neuron_cpp_visitor.cpp @@ -285,7 +285,7 @@ void CodegenNeuronCppVisitor::print_neuron_includes() { } -void CodegenNeuronCppVisitor::print_sdlists_init(bool print_initializers) { +void CodegenNeuronCppVisitor::print_sdlists_init(bool [[maybe_unused]] print_initializers) { /// _initlists() should only be called once by the mechanism registration function /// (__reg()) printer->add_newline(2); @@ -323,7 +323,7 @@ void CodegenNeuronCppVisitor::print_sdlists_init(bool print_initializers) { position_of_float_var(prime_var_deriv_name)); } } - printer->pop_block(";"); + printer->pop_block(); } @@ -454,7 +454,7 @@ void CodegenNeuronCppVisitor::print_mechanism_register() { } -void CodegenNeuronCppVisitor::print_mechanism_range_var_structure(bool print_initializers) { +void CodegenNeuronCppVisitor::print_mechanism_range_var_structure(bool [[maybe_unused]] print_initializers) { printer->add_newline(2); printer->add_line("/* NEURON RANGE variables macro definitions */"); for (auto i = 0; i < codegen_float_variables.size(); ++i) { diff --git a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp index 22db5121f0..b6cfe4a55a 100644 --- a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp +++ b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp @@ -228,15 +228,11 @@ void _nrn_mechanism_register_data_fields(Args&&... args) { ContainsSubstring(reindent_and_trim_text(expected_placeholder_nrn_state))); } THEN("Initialization function for slist/dlist is printed correctly") { - std::string expected_initlists = R"( - static void _initlists() { - /* s */ - _slist1[0] = {4, 0}; - /* Ds */ - _dlist1[0] = {7, 0}; - };)"; + std::string expected_initlists_s_var = "_slist1[0] = {4, 0};"; + std::string expected_initlists_Ds_var = "_dlist1[0] = {7, 0};"; - REQUIRE_THAT(generated, ContainsSubstring(reindent_and_trim_text(expected_initlists))); + REQUIRE_THAT(generated, ContainsSubstring(reindent_and_trim_text(expected_initlists_s_var))); + REQUIRE_THAT(generated, ContainsSubstring(reindent_and_trim_text(expected_initlists_Ds_var))); } THEN("Placeholder registration function is printed") { std::string expected_placeholder_reg = R"CODE(/** register channel with the simulator */ From c726805346a5594db281e6463cfed6024d0ae6e5 Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Thu, 7 Dec 2023 13:09:06 +0100 Subject: [PATCH 7/9] Make format happy --- src/codegen/codegen_neuron_cpp_visitor.cpp | 3 ++- test/unit/codegen/codegen_neuron_cpp_visitor.cpp | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/codegen/codegen_neuron_cpp_visitor.cpp b/src/codegen/codegen_neuron_cpp_visitor.cpp index 1b55d94792..301fc96b0b 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.cpp +++ b/src/codegen/codegen_neuron_cpp_visitor.cpp @@ -454,7 +454,8 @@ void CodegenNeuronCppVisitor::print_mechanism_register() { } -void CodegenNeuronCppVisitor::print_mechanism_range_var_structure(bool [[maybe_unused]] print_initializers) { +void CodegenNeuronCppVisitor::print_mechanism_range_var_structure( + bool [[maybe_unused]] print_initializers) { printer->add_newline(2); printer->add_line("/* NEURON RANGE variables macro definitions */"); for (auto i = 0; i < codegen_float_variables.size(); ++i) { diff --git a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp index b6cfe4a55a..9f759a5cef 100644 --- a/test/unit/codegen/codegen_neuron_cpp_visitor.cpp +++ b/test/unit/codegen/codegen_neuron_cpp_visitor.cpp @@ -231,8 +231,10 @@ void _nrn_mechanism_register_data_fields(Args&&... args) { std::string expected_initlists_s_var = "_slist1[0] = {4, 0};"; std::string expected_initlists_Ds_var = "_dlist1[0] = {7, 0};"; - REQUIRE_THAT(generated, ContainsSubstring(reindent_and_trim_text(expected_initlists_s_var))); - REQUIRE_THAT(generated, ContainsSubstring(reindent_and_trim_text(expected_initlists_Ds_var))); + REQUIRE_THAT(generated, + ContainsSubstring(reindent_and_trim_text(expected_initlists_s_var))); + REQUIRE_THAT(generated, + ContainsSubstring(reindent_and_trim_text(expected_initlists_Ds_var))); } THEN("Placeholder registration function is printed") { std::string expected_placeholder_reg = R"CODE(/** register channel with the simulator */ From eb74411e5dc47d24502ce96813117b1223419454 Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Thu, 7 Dec 2023 13:25:28 +0100 Subject: [PATCH 8/9] Try different syntax for [[maybe_unused]] --- src/codegen/codegen_neuron_cpp_visitor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/codegen_neuron_cpp_visitor.cpp b/src/codegen/codegen_neuron_cpp_visitor.cpp index 301fc96b0b..caecffa03e 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.cpp +++ b/src/codegen/codegen_neuron_cpp_visitor.cpp @@ -285,7 +285,7 @@ void CodegenNeuronCppVisitor::print_neuron_includes() { } -void CodegenNeuronCppVisitor::print_sdlists_init(bool [[maybe_unused]] print_initializers) { +void CodegenNeuronCppVisitor::print_sdlists_init([[maybe_unused]] bool print_initializers) { /// _initlists() should only be called once by the mechanism registration function /// (__reg()) printer->add_newline(2); From c735717cb7c3df3e0cddefaf8322f1e916fad645 Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Thu, 7 Dec 2023 13:27:42 +0100 Subject: [PATCH 9/9] Fix it in both places --- src/codegen/codegen_neuron_cpp_visitor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/codegen_neuron_cpp_visitor.cpp b/src/codegen/codegen_neuron_cpp_visitor.cpp index caecffa03e..d73b89d6e9 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.cpp +++ b/src/codegen/codegen_neuron_cpp_visitor.cpp @@ -455,7 +455,7 @@ void CodegenNeuronCppVisitor::print_mechanism_register() { void CodegenNeuronCppVisitor::print_mechanism_range_var_structure( - bool [[maybe_unused]] print_initializers) { + [[maybe_unused]] bool print_initializers) { printer->add_newline(2); printer->add_line("/* NEURON RANGE variables macro definitions */"); for (auto i = 0; i < codegen_float_variables.size(); ++i) {