From cbf2d2b7591d8b8a13ff477bd08e4fb7b2c793d3 Mon Sep 17 00:00:00 2001 From: Steven Burns Date: Fri, 20 Nov 2026 11:46:39 -0800 Subject: [PATCH] A per-context fill-reuse switch, ctx->fill_reuse OPENNEC_FILL_REUSE is read at every fill and the environment is process-wide, so an embedding that runs several solves at once (a server, a binding with the GIL released) cannot let one solve choose differently from another. ctx->fill_reuse is that choice: 0 (calloc's default) leaves it to OPENNEC_FILL_REUSE and then the build default exactly as before, 1 turns reuse on and -1 off for this context alone. Builds without OpenMP have no reuse path, so 1 is ignored there. onec never sets it, so its behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SKce1zVChRtJMQadEiWtJY --- src/internals.h | 6 ++++++ src/matrix.c | 4 +++- .../check_fill_reuse_sampling.py | 8 ++++++++ tests/benchmark_tests/fill_reuse_state.c | 18 +++++++++++++++--- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/internals.h b/src/internals.h index 0fc8626..c1cfc4c 100644 --- a/src/internals.h +++ b/src/internals.h @@ -741,6 +741,12 @@ struct context_t size_t fill_reuse_class_count; size_t fill_reuse_snapshot_count; bool fill_reuse_skip_cached; + /* The caller's fill-reuse choice for this context: 0 leaves it to + * OPENNEC_FILL_REUSE and then the build default, 1 turns reuse on, -1 off. + * A per-context switch lets several solves in one process choose + * differently; the environment is process-wide. Builds without OpenMP + * have no reuse path, so 1 is ignored there. */ + int fill_reuse; }; /* Internal initialization and cleanup (called by create_context/destroy_context) */ diff --git a/src/matrix.c b/src/matrix.c index 339c089..df21940 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -750,7 +750,9 @@ static int fill_wire_observers(context_t *ctx, int nrow, complex double *cm, int #else int reuse_enabled = 0; #endif - if (reuse_setting) + if (ctx->fill_reuse) + reuse_enabled = ctx->fill_reuse > 0; + else if (reuse_setting) reuse_enabled = reuse_setting[0] == '1' && reuse_setting[1] == '\0'; if ((size_t)n > ctx->fill_sources_count) { wire_source_t *grown = realloc(ctx->fill_sources, (size_t)n * sizeof(*grown)); diff --git a/tests/benchmark_tests/check_fill_reuse_sampling.py b/tests/benchmark_tests/check_fill_reuse_sampling.py index 28e265b..85d4ea3 100644 --- a/tests/benchmark_tests/check_fill_reuse_sampling.py +++ b/tests/benchmark_tests/check_fill_reuse_sampling.py @@ -93,6 +93,14 @@ def main(): if state_probe: subprocess.run([state_probe, str(deck), "reject" if reject else "reuse"], env=env, capture_output=True, text=True, check=True) + if state_probe and name == "small_repeated": + # The context's own choice wins over the environment, both ways. + for mode, env_value in (("ctx-off", "1"), ("ctx-on", "0")): + env = os.environ.copy() + env.update(OMP_NUM_THREADS="8", VECLIB_MAXIMUM_THREADS="8", + OPENNEC_FILL_REUSE=env_value) + subprocess.run([state_probe, str(deck), mode], env=env, + capture_output=True, text=True, check=True) choice = f"{'reject' if reject else 'reuse'}, " if state_probe else "" print(f"PASS {name}: {choice}1/8 threads, 3 frequencies") print(f"All {len(cases)} sampling cases passed.") diff --git a/tests/benchmark_tests/fill_reuse_state.c b/tests/benchmark_tests/fill_reuse_state.c index e5b2f28..6044c8c 100644 --- a/tests/benchmark_tests/fill_reuse_state.c +++ b/tests/benchmark_tests/fill_reuse_state.c @@ -1,15 +1,23 @@ /* Test-only driver: inspect the retained cache after a complete simulation. * Link against the same OpenMP libonec.a and backend libraries as onec. - * Usage: fill_reuse_state deck.nec reuse|reject + * Usage: fill_reuse_state deck.nec reuse|reject|ctx-on|ctx-off + * ctx-on and ctx-off set the context's own fill_reuse (1 or -1), which must + * win over OPENNEC_FILL_REUSE: run them with the environment saying the + * opposite. */ #include "internals.h" #include "reporting.h" int main(int argc, char **argv) { - if (argc != 3 || (strcmp(argv[2], "reuse") && strcmp(argv[2], "reject"))) + if (argc != 3 || (strcmp(argv[2], "reuse") && strcmp(argv[2], "reject") && + strcmp(argv[2], "ctx-on") && strcmp(argv[2], "ctx-off"))) return 2; context_t *ctx = create_context(); + int ctx_on = strcmp(argv[2], "ctx-on") == 0; + int ctx_off = strcmp(argv[2], "ctx-off") == 0; + if (ctx && (ctx_on || ctx_off)) + ctx->fill_reuse = ctx_on ? 1 : -1; FILE *input = fopen(argv[1], "r"); FILE *output = tmpfile(); if (!ctx || !input || !output) { @@ -33,7 +41,11 @@ int main(int argc, char **argv) int reject = strcmp(argv[2], "reject") == 0; int ok = rc == 0 && ctx->fill_reuse_snapshot != NULL && ctx->fill_reuse_skip_cached == reject; - if (reject) + if (ctx_off) + ok = rc == 0 && ctx->fill_reuse_snapshot == NULL && ctx->fill_reuse_class_ids == NULL; + else if (ctx_on) + ok = rc == 0 && ctx->fill_reuse_snapshot != NULL; + else if (reject) ok = ok && ctx->fill_reuse_class_ids == NULL && ctx->fill_reuse_class_count == 0; else ok = ok && ctx->fill_reuse_class_ids != NULL &&