From ebb11f7a273a87c159d0ec37dd165ba03d7e2e48 Mon Sep 17 00:00:00 2001 From: Theo Heimel Date: Mon, 23 Nov 2026 13:34:47 +0100 Subject: [PATCH 1/2] Ctrl-C menu: apply event and thread count changes promptly Changing the requested events from the generation menu only took effect after the current round of batches, which for large targets came much later or never. The batches not dispatched yet are now dropped, so the round ends once the jobs in flight commit and the new target applies. A thread count change instead pauses the dispatch until no job is in flight, applies the new count and resumes the round from its queue, so the events stay identical to a run without the change. The "requested events" and "CPU thread count" messages are only logged in log mode; in pretty mode they broke the progress boxes and the menu. Co-Authored-By: Claude Opus 5.5 --- madgraph/mg7/madgraph_process.py | 14 ++- .../madspace/driver/event_generator.hpp | 17 ++-- madspace/src/driver/event_generator.cpp | 87 ++++++++++++++----- 3 files changed, 87 insertions(+), 31 deletions(-) diff --git a/madgraph/mg7/madgraph_process.py b/madgraph/mg7/madgraph_process.py index e2987bdca5..de2b376de0 100644 --- a/madgraph/mg7/madgraph_process.py +++ b/madgraph/mg7/madgraph_process.py @@ -76,8 +76,10 @@ class GenerationControl: MENU_TIMEOUT = 60 - def __init__(self, event_generator, run_path): + def __init__(self, event_generator, run_path, pretty=False): self.event_generator = event_generator + # log lines would break the progress boxes and the menu + self.pretty = pretty self.control_path = os.path.join(run_path, "control") self.armed = False self.installed = False @@ -154,12 +156,15 @@ def stop(self): def set_events(self, count): self.event_generator.set_target_count(count) self.pending_events = count - logger.info("requested events set to %d (from the end of the running batches)", count) + if not self.pretty: + logger.info("requested events set to %d (from the end of the running batches)", + count) def set_threads(self, count): self.event_generator.set_thread_count(count) self.pending_threads = count - logger.info("CPU threads set to %d (from the end of the running batches)", count) + if not self.pretty: + logger.info("CPU threads set to %d (from the end of the running batches)", count) def run_control_file(self): with open(self.control_path) as f: @@ -2515,7 +2520,8 @@ def generate_events(self, output_format=None) -> None: if self.gridpack_run_mode != "regular": self.finish_without_events() return - with GenerationControl(self.event_generator, self.run_path) as control: + pretty = self.event_generator_config.verbosity == ms.Verbosity.pretty + with GenerationControl(self.event_generator, self.run_path, pretty) as control: control.armed = True self.event_generator.generate() control.armed = False diff --git a/madspace/include/madspace/driver/event_generator.hpp b/madspace/include/madspace/driver/event_generator.hpp index 7c4771d4e6..55238f8605 100644 --- a/madspace/include/madspace/driver/event_generator.hpp +++ b/madspace/include/madspace/driver/event_generator.hpp @@ -56,14 +56,15 @@ class EventGenerator { static void request_stop(bool stop = true) { _stop_requested = stop; } /// Whether @ref request_stop asked the generation to stop. static bool stop_requested() { return _stop_requested; } - /// Change the number of events @ref generate aims for. It takes effect once - /// the batches in flight are committed, as do the shares of the channels. + /// Change the number of events @ref generate aims for. The batches not + /// dispatched yet are dropped; it takes effect once the batches in flight + /// are committed, as do the shares of the channels. void set_target_count(std::size_t target_count) { _pending_target_count = target_count; } - /// Change the thread count of the CPU contexts. It takes effect once the - /// batches in flight are committed, as the per-thread resources cannot grow - /// while jobs use them. + /// Change the thread count of the CPU contexts. The dispatch of new batches + /// pauses until the ones in flight are committed, as the per-thread resources + /// cannot grow while jobs use them; the events do not change. void set_thread_count(std::size_t thread_count) { _pending_thread_count = thread_count; } @@ -192,7 +193,8 @@ class EventGenerator { }; inline static std::function _abort_check_function = [] {}; inline static std::atomic _stop_requested = false; - // set_target_count() and set_thread_count(), applied between rounds + // set_target_count(), applied between rounds, and set_thread_count(), applied + // once no job is in flight std::optional _pending_target_count; std::optional _pending_thread_count; @@ -276,6 +278,8 @@ class EventGenerator { std::size_t start_jobs(); std::size_t wait_for_result(std::size_t& in_flight); void reset_jobs(); + // Drop the batches not dispatched yet, so that the round ends early. + void drop_ready_jobs(); void update_integral(); void update_integral_status(); // With `final_step`, the integrals after the weighting steps are used. They @@ -291,6 +295,7 @@ class EventGenerator { void finish_stopped_generation(); // Apply set_target_count() and set_thread_count(), with no job in flight. void apply_pending_changes(); + void apply_thread_count(); void update_counts(); void reset_start_time(); void add_timing_data(const std::string& key); diff --git a/madspace/src/driver/event_generator.cpp b/madspace/src/driver/event_generator.cpp index bcdc56edc4..aa9ca8a67b 100644 --- a/madspace/src/driver/event_generator.cpp +++ b/madspace/src/driver/event_generator.cpp @@ -191,25 +191,43 @@ void EventGenerator::run_generate(GenerateMode mode) { } } - std::size_t job_id_before = _job_id; - std::size_t unweight_dispatched = start_jobs(); - std::size_t round_in_flight = (_job_id - job_id_before) + unweight_dispatched; + // A thread count change pauses the dispatch until the jobs in flight are + // done, then the round resumes from its queue: the events do not change. + auto dispatch = [&](bool pipeline = true) -> std::size_t { + if (_pending_thread_count) { + return 0; + } + std::size_t job_id_before = _job_id; + std::size_t unweight_dispatched = start_jobs(); + return (_job_id - job_id_before) + unweight_dispatched + + (pipeline ? start_pipeline_jobs() : 0); + }; + apply_thread_count(); + std::size_t round_in_flight = dispatch(false); + auto resume = [&] { + if (!_pending_thread_count) { + return false; + } + apply_thread_count(); + round_in_flight = dispatch(); + return round_in_flight > 0; + }; // Wait for the round to commit before updating the channel fractions. - while (round_in_flight > 0) { + while (round_in_flight > 0 || resume()) { std::size_t job_id = wait_for_result(round_in_flight); if (stopping()) { // no new batches; the ones in flight still commit and unweight _ready_jobs.clear(); + } else if (mode == GenerateMode::events && _pending_target_count) { + // end the round early, the new target applies after it + drop_ready_jobs(); } if (WeightingPipeline::owns_job(job_id)) { _pipeline->commit_job(job_id); update_counts(); print_gen_update(false); - std::size_t job_id_refill = _job_id; - std::size_t refill_unweight_dispatched = start_jobs(); - round_in_flight += (_job_id - job_id_refill) + - refill_unweight_dispatched + start_pipeline_jobs(); + round_in_flight += dispatch(); continue; } auto& job = _running_jobs.at(job_id); @@ -237,10 +255,7 @@ void EventGenerator::run_generate(GenerateMode mode) { _running_jobs.erase(commit_id); } } - std::size_t job_id_refill = _job_id; - std::size_t refill_unweight_dispatched = start_jobs(); - round_in_flight += (_job_id - job_id_refill) + refill_unweight_dispatched + - start_pipeline_jobs(); + round_in_flight += dispatch(); } drain_pipeline(); @@ -493,6 +508,25 @@ std::size_t EventGenerator::wait_for_result(std::size_t& in_flight) { std::rethrow_exception(aborted ? aborted : exception); } +void EventGenerator::drop_ready_jobs() { + for (auto& ready_job : _ready_jobs) { + std::size_t channel_index = ready_job.channel_index; + if (ready_job.is_vegas_batch) { + // dispatched atomically, so none of it is running + _channel_optimizing.at(channel_index) = false; + } else { + // finish_channel_job() clears the pending flag once the running part + // commits + _channel_batch_dispatch_done.at(channel_index) = true; + if (_channel_job_counts.at(channel_index) == 0) { + _channel_batch_pending.at(channel_index) = false; + } + } + } + _ready_jobs.clear(); + _ready_job_rr_cursor = 0; +} + void EventGenerator::reset_jobs() { _running_jobs.clear(); if (_pipeline) { @@ -664,24 +698,35 @@ void EventGenerator::update_integral_status() { sum_channel_status(_channels, _status); } -void EventGenerator::apply_pending_changes() { +void EventGenerator::apply_thread_count() { if (_pending_thread_count) { for (auto& context : _contexts) { if (context->device()->device_type() == DeviceType::cpu) { context->thread_pool().set_thread_count(*_pending_thread_count); } } - Logger::info(std::format("CPU thread count set to {}", *_pending_thread_count)); + // a log line would break the pretty boxes + if (_config.verbosity == Verbosity::log) { + Logger::info( + std::format("CPU thread count set to {}", *_pending_thread_count) + ); + } _pending_thread_count.reset(); } +} + +void EventGenerator::apply_pending_changes() { + apply_thread_count(); if (_pending_target_count && _generate_mode == GenerateMode::events) { - Logger::info( - std::format( - "requested events changed from {} to {}", - _config.target_count, - *_pending_target_count - ) - ); + if (_config.verbosity == Verbosity::log) { + Logger::info( + std::format( + "requested events changed from {} to {}", + _config.target_count, + *_pending_target_count + ) + ); + } _config.target_count = *_pending_target_count; _status.count_target = *_pending_target_count; _pending_target_count.reset(); From d5c3dcc9fa8643fbe3af94258209717496357a59 Mon Sep 17 00:00:00 2001 From: Theo Heimel Date: Mon, 23 Nov 2026 13:39:57 +0100 Subject: [PATCH 2/2] Ctrl-C menu: confirm control-file changes, keep the round for an unchanged target Review follow-up. In pretty mode, nevents/nb_core from /control were applied silently, as only the menu shows the changes: they are logged again outside the menu, and the progress boxes redrawn below. Re-entering the current number of events no longer drops the batches not dispatched yet. Co-Authored-By: Claude Opus 5.5 --- madgraph/mg7/madgraph_process.py | 8 +++++--- madspace/src/driver/event_generator.cpp | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/madgraph/mg7/madgraph_process.py b/madgraph/mg7/madgraph_process.py index de2b376de0..4638e9aec0 100644 --- a/madgraph/mg7/madgraph_process.py +++ b/madgraph/mg7/madgraph_process.py @@ -78,7 +78,7 @@ class GenerationControl: def __init__(self, event_generator, run_path, pretty=False): self.event_generator = event_generator - # log lines would break the progress boxes and the menu + # the menu shows the changes, a log line would break it self.pretty = pretty self.control_path = os.path.join(run_path, "control") self.armed = False @@ -130,6 +130,8 @@ def handle(self, signum, frame): try: if os.path.exists(self.control_path): self.run_control_file() + # below its log lines + self.event_generator.redraw_progress() return tty = self.open_tty() if tty is None: @@ -156,14 +158,14 @@ def stop(self): def set_events(self, count): self.event_generator.set_target_count(count) self.pending_events = count - if not self.pretty: + if not (self.pretty and self.in_menu): logger.info("requested events set to %d (from the end of the running batches)", count) def set_threads(self, count): self.event_generator.set_thread_count(count) self.pending_threads = count - if not self.pretty: + if not (self.pretty and self.in_menu): logger.info("CPU threads set to %d (from the end of the running batches)", count) def run_control_file(self): diff --git a/madspace/src/driver/event_generator.cpp b/madspace/src/driver/event_generator.cpp index aa9ca8a67b..926070ba14 100644 --- a/madspace/src/driver/event_generator.cpp +++ b/madspace/src/driver/event_generator.cpp @@ -219,7 +219,8 @@ void EventGenerator::run_generate(GenerateMode mode) { if (stopping()) { // no new batches; the ones in flight still commit and unweight _ready_jobs.clear(); - } else if (mode == GenerateMode::events && _pending_target_count) { + } else if (mode == GenerateMode::events && _pending_target_count && + *_pending_target_count != _config.target_count) { // end the round early, the new target applies after it drop_ready_jobs(); }