Skip to content
Open
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
16 changes: 12 additions & 4 deletions madgraph/mg7/madgraph_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 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
self.installed = False
Expand Down Expand Up @@ -128,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:
Expand All @@ -154,12 +158,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 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
logger.info("CPU threads set to %d (from the end of the running batches)", count)
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):
with open(self.control_path) as f:
Expand Down Expand Up @@ -2515,7 +2522,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
Expand Down
17 changes: 11 additions & 6 deletions madspace/include/madspace/driver/event_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -192,7 +193,8 @@ class EventGenerator {
};
inline static std::function<void(void)> _abort_check_function = [] {};
inline static std::atomic<bool> _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<std::size_t> _pending_target_count;
std::optional<std::size_t> _pending_thread_count;

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
88 changes: 67 additions & 21 deletions madspace/src/driver/event_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,44 @@ 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 &&
*_pending_target_count != _config.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);
Expand Down Expand Up @@ -237,10 +256,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();

Expand Down Expand Up @@ -493,6 +509,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) {
Expand Down Expand Up @@ -664,24 +699,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();
Expand Down
Loading