Commit f46b49c7 authored by Paul Schütze's avatar Paul Schütze
Browse files

Merge branch 'abort_event' into 'master'

Introduce Possibility to Abort Event

See merge request allpix-squared/allpix-squared!706
parents 435ed6dd aa8c1f49
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -37,4 +37,4 @@ propagate_holes = true
[ROOTObjectWriter]
log_level = DEBUG

#PASS (STATUS) [F:ROOTObjectWriter] Wrote 45317 objects to 6 branches in file
#PASS (STATUS) [F:ROOTObjectWriter] Wrote 45317 objects to 8 branches in file
+6 −0
Original line number Diff line number Diff line
@@ -74,6 +74,12 @@ namespace allpix {
         */
        uint64_t getRandomNumber() { return getRandomEngine()(); }

        /**
         * @brief Returns the current seed for the ranom number generator
         * @return The random seed of the current event
         */
        uint64_t getSeed() const { return seed_; }

    private:
        /**
         * @brief Sets the random engine and seed it to be used by this event
+21 −5
Original line number Diff line number Diff line
@@ -714,6 +714,7 @@ void ModuleManager::run(RandomNumberGenerator& seeder) {

    // Push all events to the thread pool
    std::atomic<uint64_t> finished_events{0};
    std::atomic<uint64_t> aborted_events{0};
    global_config.setDefault<uint64_t>("number_of_events", 1u);
    auto number_of_events = global_config.get<uint64_t>("number_of_events");

@@ -741,7 +742,8 @@ void ModuleManager::run(RandomNumberGenerator& seeder) {

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-overflow"
        auto event_function_with_module = [this, plot, number_of_events, event_num = i, event_seed = seed, &finished_events](
        auto event_function_with_module =
            [this, plot, number_of_events, event_num = i, event_seed = seed, &finished_events, &aborted_events](
                std::shared_ptr<Event> event,
                ModuleList::iterator module_iter,
                long double event_time,
@@ -783,6 +785,7 @@ void ModuleManager::run(RandomNumberGenerator& seeder) {

                // Run module
                bool stop = false;
                bool abort = false;
                try {
                    if(module->require_sequence() && event_num != thread_pool_->minimumUncompleted()) {
                        stop = true;
@@ -791,6 +794,9 @@ void ModuleManager::run(RandomNumberGenerator& seeder) {
                    }
                } catch(const MissingDependenciesException& e) {
                    stop = true;
                } catch(const AbortEventException& e) {
                    LOG(WARNING) << "Event aborted:" << std::endl << e.what();
                    abort = true;
                } catch(const EndOfRunException& e) {
                    // Terminate if the module threw the EndOfRun request exception:
                    LOG(WARNING) << "Request to terminate:" << std::endl << e.what();
@@ -812,6 +818,12 @@ void ModuleManager::run(RandomNumberGenerator& seeder) {
                    this->module_event_time_[module.get()]->Fill(static_cast<double>(duration));
                }

                if(abort) {
                    // Break module execution loop:
                    aborted_events++;
                    break;
                }

                if(stop) {
                    LOG(DEBUG) << "Event " << event->number
                               << " was interrupted because of missing dependencies, rescheduling...";
@@ -865,6 +877,10 @@ void ModuleManager::run(RandomNumberGenerator& seeder) {
    LOG_PROGRESS(STATUS, "EVENT_LOOP") << "Finished run of " << finished_events << " events";
    global_config.set<uint64_t>("number_of_events", finished_events);

    if(aborted_events > 0) {
        LOG(WARNING) << "Aborted " << aborted_events << " events in this run";
    }

    auto end_time = std::chrono::steady_clock::now();
    total_time_ += static_cast<std::chrono::duration<long double>>(end_time - start_time).count();

+18 −0
Original line number Diff line number Diff line
@@ -139,6 +139,24 @@ namespace allpix {
        explicit EndOfRunException(std::string reason) { error_message_ = std::move(reason); }
    };

    /**
     * @ingroup Exceptions
     * @brief Exception for modules to request the abortion of the current event
     * @note Non-fatal error used to interrupt the processing of the current event.
     *
     * This error can be raised by modules if they would like to request an interruption of the current event processing
     * because an issue was detected.
     */
    class AbortEventException : public EndOfRunException {
    public:
        /**
         * @brief Constructs request to abort the current event processing with a description
         * @param reason Text explaining the reason of the requested abortion of the event
         */
        // TODO [doc] the module itself is missing
        explicit AbortEventException(std::string reason) : EndOfRunException(reason) { error_message_ = std::move(reason); }
    };

    /**
     * @ingroup Exceptions
     * @brief Exception for modules to request an interrupt because dependencies are missing
+30 −20
Original line number Diff line number Diff line
@@ -310,6 +310,7 @@ void DepositionGeant4Module::run(Event* event) {
    auto seed2 = event->getRandomNumber();
    LOG(DEBUG) << "Seeding Geant4 event with seeds " << seed1 << " " << seed2;

    try {
        if(multithreadingEnabled()) {
            auto* run_manager_mt = static_cast<MTRunManager*>(run_manager_g4_);
            run_manager_mt->Run(static_cast<int>(number_of_particles_), seed1, seed2);
@@ -337,6 +338,15 @@ void DepositionGeant4Module::run(Event* event) {
                energy_per_event_[sensor->getName()]->Fill(deposited_energy);
            }
        }
    } catch(AbortEventException& e) {
        // Clear charge deposits of all sensors
        for(auto& sensor : sensors_) {
            sensor->clearEventInfo();
        }
        run_manager_g4_->AbortRun();
        track_info_manager_->resetTrackInfoManager();
        throw;
    }

    track_info_manager_->resetTrackInfoManager();
}
Loading