Skip to content

Commit 9b79ffc

Browse files
authored
Merge branch 'master' into python-package-name
2 parents d731d8a + 5ac449d commit 9b79ffc

5 files changed

Lines changed: 79 additions & 33 deletions

File tree

.github/workflows/formatting.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ on:
1818
jobs:
1919
all:
2020
name: C/C++, CMake and Python
21-
runs-on: ubuntu-22.04
21+
runs-on: ubuntu-24.04
2222
timeout-minutes: 5
2323
steps:
2424
- uses: actions/checkout@v4

CMakeLists.txt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -949,21 +949,43 @@ add_custom_target(
949949
COMMAND ${PROJECT_SOURCE_DIR}/external/coding-conventions/bin/format
950950
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
951951

952-
# Prepare a shell script to format only files modified with respect to master branch
952+
# Prepare a shell script to format only files modified with respect to master/main branch that still
953+
# exist on disk (filters out deleted files).
953954
file(
954955
WRITE ${CMAKE_CURRENT_BINARY_DIR}/format-pr.sh
955-
"\
956-
#!bash\n\
957-
set -e\n\
958-
cmd='cd ${PROJECT_SOURCE_DIR} && external/coding-conventions/bin/format `git diff --name-only master`'\n\
959-
echo $cmd\n\
960-
cd ${PROJECT_SOURCE_DIR} && external/coding-conventions/bin/format `git diff --name-only master`\n\
956+
"#!/bin/bash
957+
set -euo pipefail
958+
959+
# Try 'main' first, then fall back to 'master'
960+
for base in main master; do
961+
if git rev-parse --verify \"\$base\" >/dev/null 2>&1; then
962+
BASE_BRANCH=\"\$base\"
963+
break
964+
fi
965+
done
966+
967+
if [ -z \"\${BASE_BRANCH:-}\" ]; then
968+
echo \"Error: Neither 'main' nor 'master' branch found.\" >&2
969+
exit 1
970+
fi
971+
972+
echo \"Formatting changes vs '\$BASE_BRANCH' (existing files only)...\" >&2
973+
974+
# Get changed files (null-delimited so names with spaces work), only pass
975+
# files that still exist.
976+
git diff -z --name-only \"\$BASE_BRANCH\" | \\
977+
while IFS= read -r -d '' file; do
978+
if [ -e \"\$file\" ]; then
979+
printf '%s\\0' \"\$file\"
980+
fi
981+
done | \\
982+
xargs -0 --no-run-if-empty external/coding-conventions/bin/format \"\$@\"
961983
")
962984

963985
add_custom_target(
964986
format-pr
965987
COMMAND bash ${CMAKE_CURRENT_BINARY_DIR}/format-pr.sh
966-
COMMENT "Format only files modified with respect to master branch."
988+
COMMENT "Format only files modified with respect to main/master (existing files only)"
967989
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
968990

969991
# =============================================================================

external/coding-conventions

src/nrnpython/grids.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,10 +1094,18 @@ int ECS_Grid_node::add_multicompartment_reaction(int nstates, int* indices, int
10941094
void ECS_Grid_node::clear_multicompartment_reaction() {
10951095
free(all_reaction_states);
10961096
free(react_offsets);
1097-
if (multicompartment_inititalized)
1097+
if (multicompartment_inititalized) {
10981098
free(all_reaction_indices);
1099-
else
1099+
#if NRNMPI
1100+
if (nrnmpi_use)
1101+
free(reaction_indices);
1102+
#endif
1103+
} else {
11001104
free(reaction_indices);
1105+
}
1106+
free(induced_currents);
1107+
induced_currents = NULL;
1108+
local_induced_currents = NULL;
11011109
all_reaction_indices = NULL;
11021110
all_reaction_states = NULL;
11031111
reaction_indices = NULL;
@@ -1126,12 +1134,11 @@ void ECS_Grid_node::initialize_multicompartment_reaction() {
11261134
break;
11271135

11281136
if (i != nrnmpi_numprocs) {
1137+
total_reaction_states = 0;
11291138
// number of offsets (Reaction) stored in each process
1130-
proc_num_reactions = (int*) calloc(nrnmpi_numprocs, sizeof(int));
11311139
proc_num_reactions[nrnmpi_myid] = react_offset_count;
11321140

11331141
// number of states/indices stored in each process
1134-
proc_num_reaction_states = (int*) calloc(nrnmpi_numprocs, sizeof(int));
11351142
proc_num_reaction_states[nrnmpi_myid] = react_offsets[react_offset_count - 1];
11361143
nrnmpi_int_allgather_inplace(proc_num_reactions, 1);
11371144
nrnmpi_int_allgather_inplace(proc_num_reaction_states, 1);
@@ -1142,35 +1149,31 @@ void ECS_Grid_node::initialize_multicompartment_reaction() {
11421149
proc_num_reactions[i] = total_reaction_states;
11431150
total_reaction_states += proc_num_reaction_states[i];
11441151
}
1145-
1146-
// Move the offsets for each reaction so they reference the
1147-
// corresponding indices in the all_reaction_indices array
1148-
for (j = 0; j < react_offset_count; j++)
1149-
react_offsets[j] += start_state;
1152+
free(all_reaction_indices);
1153+
free(all_reaction_states);
11501154

11511155
all_reaction_indices = (int*) malloc(total_reaction_states * sizeof(int));
11521156
all_reaction_states = (double*) calloc(total_reaction_states, sizeof(double));
1153-
11541157
memcpy(&all_reaction_indices[start_state],
11551158
reaction_indices,
11561159
proc_num_reaction_states[nrnmpi_myid] * sizeof(int));
11571160
nrnmpi_int_allgatherv_inplace(all_reaction_indices,
11581161
proc_num_reaction_states,
11591162
proc_num_reactions);
1160-
free(reaction_indices);
1161-
reaction_indices = NULL;
1163+
11621164
multicompartment_inititalized = TRUE;
11631165

11641166
// Handle currents induced by multicompartment reactions.
1167+
int local_induced_current_count = induced_current_count;
11651168
proc_induced_current_count[nrnmpi_myid] = induced_current_count;
11661169
nrnmpi_int_allgather_inplace(proc_induced_current_count, 1);
1170+
11671171
proc_induced_current_offset[0] = 0;
11681172
for (i = 1; i < nrnmpi_numprocs; i++)
11691173
proc_induced_current_offset[i] = proc_induced_current_offset[i - 1] +
11701174
proc_induced_current_count[i - 1];
11711175
induced_current_count = proc_induced_current_offset[nrnmpi_numprocs - 1] +
11721176
proc_induced_current_count[nrnmpi_numprocs - 1];
1173-
11741177
all_scales = (double*) malloc(induced_current_count * sizeof(double));
11751178
all_indices = (int*) malloc(induced_current_count * sizeof(int));
11761179
memcpy(&all_scales[proc_induced_current_offset[nrnmpi_myid]],
@@ -1193,16 +1196,18 @@ void ECS_Grid_node::initialize_multicompartment_reaction() {
11931196
free(induced_currents);
11941197
induced_currents_scale = all_scales;
11951198
induced_currents_index = all_indices;
1196-
induced_currents = (double*) malloc(induced_current_count * sizeof(double));
1199+
induced_currents = (double*) calloc(induced_current_count, sizeof(double));
11971200
local_induced_currents = &induced_currents[proc_induced_current_offset[nrnmpi_myid]];
1201+
// set to local count to avoid accumulation with repeated calls
1202+
induced_current_count = local_induced_current_count;
11981203
}
11991204
} else {
12001205
if (!multicompartment_inititalized) {
12011206
total_reaction_states = react_offsets[react_offset_count - 1];
12021207
all_reaction_indices = reaction_indices;
12031208
all_reaction_states = (double*) calloc(total_reaction_states, sizeof(double));
12041209
multicompartment_inititalized = TRUE;
1205-
induced_currents = (double*) malloc(induced_current_count * sizeof(double));
1210+
induced_currents = (double*) calloc(induced_current_count, sizeof(double));
12061211
local_induced_currents = induced_currents;
12071212
}
12081213
}
@@ -1212,7 +1217,7 @@ void ECS_Grid_node::initialize_multicompartment_reaction() {
12121217
all_reaction_indices = reaction_indices;
12131218
all_reaction_states = (double*) calloc(total_reaction_states, sizeof(double));
12141219
multicompartment_inititalized = TRUE;
1215-
induced_currents = (double*) malloc(induced_current_count * sizeof(double));
1220+
induced_currents = (double*) calloc(induced_current_count, sizeof(double));
12161221
local_induced_currents = induced_currents;
12171222
}
12181223
#endif
@@ -1237,7 +1242,7 @@ void ECS_Grid_node::do_multicompartment_reactions(double* result) {
12371242
for (i = 0; i < total_reaction_states; i++)
12381243
result[all_reaction_indices[i]] += all_reaction_states[i];
12391244
}
1240-
memset(all_reaction_states, 0, total_reaction_states * sizeof(int));
1245+
memset(all_reaction_states, 0, total_reaction_states * sizeof(double));
12411246
}
12421247

12431248
// TODO: Implement this
@@ -1261,12 +1266,18 @@ ECS_Grid_node::~ECS_Grid_node() {
12611266
free(proc_num_fluxes);
12621267
free(proc_num_reaction_states);
12631268
free(proc_num_reactions);
1269+
free(reaction_indices);
12641270
}
12651271
#endif
1272+
free(all_reaction_indices);
1273+
reaction_indices = nullptr;
1274+
free(react_offsets);
12661275
free(all_currents);
12671276
free(ecs_adi_dir_x);
12681277
free(ecs_adi_dir_y);
12691278
free(ecs_adi_dir_z);
1279+
if (get_alpha == get_alpha_scalar)
1280+
free(alpha);
12701281
if (node_flux_count > 0) {
12711282
free(node_flux_idx);
12721283
free(node_flux_scale);

src/nrnpython/rxd.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,10 @@ static void free_currents() {
614614
for (i = 0; i < _memb_count; i++) {
615615
for (j = 0; j < _memb_species_count[i]; j++) {
616616
free(_memb_cur_mapped[i][j]);
617+
free(_memb_cur_mapped_ecs[i][j]);
617618
}
618619
free(_memb_cur_mapped[i]);
620+
free(_memb_cur_mapped_ecs[i]);
619621
}
620622
_memb_cur_ptrs.clear();
621623
free(_memb_cur_mapped);
@@ -973,6 +975,8 @@ extern "C" NRN_EXPORT void register_rate(int nspecies,
973975
react->flux = (double**) malloc(react->icsN * sizeof(double*));
974976
for (i = 0; i < react->icsN; i++)
975977
react->flux[i] = (double*) malloc(react->num_regions * sizeof(double));
978+
} else {
979+
react->flux = nullptr;
976980
}
977981
react->states_for_reaction = (double**) malloc(react->num_species * sizeof(double*));
978982
react->states_for_reaction_dx = (double**) malloc(react->num_species * sizeof(double*));
@@ -991,8 +995,6 @@ extern "C" NRN_EXPORT void register_rate(int nspecies,
991995
react->params_for_reaction = (double**) malloc(react->num_params * sizeof(double*));
992996
for (i = 0; i < react->num_params; i++)
993997
react->params_for_reaction[i] = (double*) malloc(react->num_regions * sizeof(double));
994-
if (react->num_mult > 0)
995-
react->mc_mult = (double*) malloc(react->num_mult * sizeof(double));
996998

997999
if (_reactions == NULL) {
9981000
_reactions = react;
@@ -1017,28 +1019,34 @@ extern "C" NRN_EXPORT void clear_rates() {
10171019
if (react->vptrs != NULL)
10181020
free(react->vptrs);
10191021
for (i = 0; i < react->num_segments; i++) {
1020-
for (j = 0; j < react->num_species; j++) {
1022+
for (j = 0; j < react->num_species + react->num_params; j++) {
10211023
free(react->state_idx[i][j]);
10221024
}
10231025
free(react->state_idx[i]);
10241026

10251027
if (react->num_ecs_species + react->num_ecs_params > 0) {
10261028
free(react->ecs_state[i]);
1029+
free(react->ecs_index[i]);
10271030
}
10281031
}
1032+
free(react->state_idx);
1033+
if (react->num_ecs_species + react->num_ecs_params > 0) {
1034+
free(react->ecs_index);
1035+
free(react->ecs_state);
1036+
free(react->ecs_offset_index);
1037+
free(react->ecs_grid);
1038+
}
10291039
if (react->num_mult > 0) {
10301040
for (i = 0; i < react->num_mult; i++)
10311041
free(react->mc_multiplier[i]);
10321042
free(react->mc_multiplier);
10331043
}
10341044

1035-
free(react->state_idx);
1036-
free(react->ecs_state);
10371045
prev = react;
10381046

10391047
if (react->num_mult > 0)
10401048
free(react->mc_mult);
1041-
if (_membrane_flux) {
1049+
if (react->flux != NULL) {
10421050
for (i = 0; i < react->icsN; i++)
10431051
free(react->flux[i]);
10441052
free(react->flux);
@@ -1052,10 +1060,15 @@ extern "C" NRN_EXPORT void clear_rates() {
10521060
}
10531061
for (i = 0; i < react->num_species; i++) {
10541062
free(react->states_for_reaction[i]);
1063+
free(react->states_for_reaction_dx[i]);
10551064
free(react->result_array[i]);
1065+
free(react->result_array_dx[i]);
10561066
}
10571067
free(react->states_for_reaction);
1068+
free(react->states_for_reaction_dx);
1069+
10581070
free(react->result_array);
1071+
free(react->result_array_dx);
10591072
for (i = 0; i < react->num_params; i++) {
10601073
free(react->params_for_reaction[i]);
10611074
}

0 commit comments

Comments
 (0)