Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor printing setup code in entrypoints. #1502

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions src/codegen/codegen_neuron_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,41 @@ void CodegenNeuronCppVisitor::print_initial_block(const InitialBlock* node) {
}
}

void CodegenNeuronCppVisitor::print_entrypoint_setup_code_from_memb_list() {
printer->add_line(
"_nrn_mechanism_cache_range _lmc{_sorted_token, *nt, *_ml_arg, _ml_arg->type()};");
printer->fmt_line("auto inst = make_instance_{}(_lmc);", info.mod_suffix);
if (!info.artificial_cell) {
printer->fmt_line("auto node_data = make_node_data_{}(*nt, *_ml_arg);", info.mod_suffix);
}
printer->add_line("auto* _thread = _ml_arg->_thread;");
if (!codegen_thread_variables.empty()) {
printer->fmt_line("auto _thread_vars = {}(_thread[{}].get<double*>());",
thread_variables_struct(),
info.thread_var_thread_id);
}
}


void CodegenNeuronCppVisitor::print_entrypoint_setup_code_from_prop() {
printer->add_line("Datum* _ppvar = _nrn_mechanism_access_dparam(prop);");
printer->add_line("_nrn_mechanism_cache_instance _lmc{prop};");
printer->add_line("const size_t id = 0;");

printer->fmt_line("auto inst = make_instance_{}(_lmc);", info.mod_suffix);
if (!info.artificial_cell) {
printer->fmt_line("auto node_data = make_node_data_{}(prop);", info.mod_suffix);
}

if (!codegen_thread_variables.empty()) {
printer->fmt_line("auto _thread_vars = {}({}_global.thread_data);",
thread_variables_struct(),
info.mod_suffix);
}

printer->add_newline();
}


void CodegenNeuronCppVisitor::print_global_function_common_code(BlockType type,
const std::string& function_name) {
Expand All @@ -1598,18 +1633,8 @@ void CodegenNeuronCppVisitor::print_global_function_common_code(BlockType type,
{"", "Memb_list*", "", "_ml_arg"},
{"", "int", "", "_type"}};
printer->fmt_push_block("void {}({})", method, get_parameter_str(args));

printer->add_line("_nrn_mechanism_cache_range _lmc{_sorted_token, *nt, *_ml_arg, _type};");
printer->fmt_line("auto inst = make_instance_{}(_lmc);", info.mod_suffix);
printer->fmt_line("auto node_data = make_node_data_{}(*nt, *_ml_arg);", info.mod_suffix);

print_entrypoint_setup_code_from_memb_list();
printer->add_line("auto nodecount = _ml_arg->nodecount;");
printer->add_line("auto* _thread = _ml_arg->_thread;");
if (!codegen_thread_variables.empty()) {
printer->fmt_line("auto _thread_vars = {}(_thread[{}].get<double*>());",
thread_variables_struct(),
info.thread_var_thread_id);
}
}


Expand Down Expand Up @@ -1659,11 +1684,7 @@ void CodegenNeuronCppVisitor::print_nrn_jacob() {
get_parameter_str(args)); // begin function


printer->add_multi_line(
"_nrn_mechanism_cache_range _lmc{_sorted_token, *nt, *_ml_arg, _type};");

printer->fmt_line("auto inst = make_instance_{}(_lmc);", info.mod_suffix);
printer->fmt_line("auto node_data = make_node_data_{}(*nt, *_ml_arg);", info.mod_suffix);
print_entrypoint_setup_code_from_memb_list();
printer->fmt_line("auto nodecount = _ml_arg->nodecount;");
printer->push_block("for (int id = 0; id < nodecount; id++)"); // begin for

Expand All @@ -1680,25 +1701,6 @@ void CodegenNeuronCppVisitor::print_nrn_jacob() {
}


void CodegenNeuronCppVisitor::print_callable_preamble_from_prop() {
printer->add_line("Datum* _ppvar = _nrn_mechanism_access_dparam(prop);");
printer->add_line("_nrn_mechanism_cache_instance _lmc{prop};");
printer->add_line("const size_t id = 0;");

printer->fmt_line("auto inst = make_instance_{}(_lmc);", info.mod_suffix);
if (!info.artificial_cell) {
printer->fmt_line("auto node_data = make_node_data_{}(prop);", info.mod_suffix);
}

if (!codegen_thread_variables.empty()) {
printer->fmt_line("auto _thread_vars = {}({}_global.thread_data);",
thread_variables_struct(),
info.mod_suffix);
}

printer->add_newline();
}

void CodegenNeuronCppVisitor::print_nrn_constructor_declaration() {
if (info.constructor_node) {
printer->fmt_line("void {}(Prop* prop);", method_name(naming::NRN_CONSTRUCTOR_METHOD));
Expand All @@ -1709,7 +1711,7 @@ void CodegenNeuronCppVisitor::print_nrn_constructor() {
if (info.constructor_node) {
printer->fmt_push_block("void {}(Prop* prop)", method_name(naming::NRN_CONSTRUCTOR_METHOD));

print_callable_preamble_from_prop();
print_entrypoint_setup_code_from_prop();

auto block = info.constructor_node->get_statement_block();
print_statement_block(*block, false, false);
Expand All @@ -1725,7 +1727,7 @@ void CodegenNeuronCppVisitor::print_nrn_destructor_declaration() {

void CodegenNeuronCppVisitor::print_nrn_destructor() {
printer->fmt_push_block("void {}(Prop* prop)", method_name(naming::NRN_DESTRUCTOR_METHOD));
print_callable_preamble_from_prop();
print_entrypoint_setup_code_from_prop();

for (const auto& rv: info.random_variables) {
printer->fmt_line("nrnran123_deletestream((nrnran123_State*) {});",
Expand Down
25 changes: 20 additions & 5 deletions src/codegen/codegen_neuron_cpp_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,26 @@ class CodegenNeuronCppVisitor: public CodegenCppVisitor {
void print_global_function_common_code(BlockType type,
const std::string& function_name = "") override;

/**
* Prints setup code for entrypoints from NEURON.
*
* The entrypoints typically receive a `sorted_token` and a bunch of other things, which then
* need to be converted into the default arguments for functions called (recursively) from the
* entrypoint.
*
* This variation prints the fast entrypoint, where NEURON is fully initialized and setup.
*/
void print_entrypoint_setup_code_from_memb_list();


/**
* Prints setup code for entrypoints NEURON.
*
* See `print_entrypoint_setup_code_from_memb_list`. This variation should be used when one only
* has access to a `Prop`, but not the full `Memb_list`.
*/
void print_entrypoint_setup_code_from_prop();


/**
* Print the \c nrn\_init function definition
Expand All @@ -509,11 +529,6 @@ class CodegenNeuronCppVisitor: public CodegenCppVisitor {
void print_nrn_constructor() override;
void print_nrn_constructor_declaration();

/**
* Print the set of common variables from a `Prop` only.
*/
void print_callable_preamble_from_prop();

/**
* Print nrn_destructor function definition
*
Expand Down
Loading