BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
gui2::JobManager Class Reference

Handles all thread activity for running job simulation in the background. More...

Inheritance diagram for gui2::JobManager:
[legend]
Collaboration diagram for gui2::JobManager:
[legend]

Public Slots

void onInterruptRequest ()
 Processes interrupt request by setting corresponding flag. More...
 
void requestSimulation (const multislice_t &multislice, const std::vector< double > &qvalues, double intensity)
 Performs simulation request. More...
 

Signals

void progressChanged (int value)
 
void simulationCompleted ()
 

Public Member Functions

 JobManager (QObject *parent=nullptr)
 
 ~JobManager () override
 
SimulationResult simulationResult ()
 Returns vector representing results of a simulation. More...
 

Private Member Functions

void wait_and_run ()
 Performs concequent simulations for given simulation parameter. More...
 

Private Attributes

bool m_interruptRequest {false}
 
std::atomic< bool > m_isRunning
 
ModelView::threadsafe_stack< SimulationInputm_requestedInputValues
 
std::thread m_simThread
 
ModelView::threadsafe_stack< SimulationResultm_simulationResult
 

Detailed Description

Handles all thread activity for running job simulation in the background.

Definition at line 27 of file jobmanager.h.

Constructor & Destructor Documentation

◆ JobManager()

gui2::JobManager::JobManager ( QObject *  parent = nullptr)

Definition at line 20 of file jobmanager.cpp.

20  : QObject(parent), m_isRunning(true)
21 {
22  // starting thread to run consequent simulations
23  m_simThread = std::thread{&JobManager::wait_and_run, this};
24 }
void wait_and_run()
Performs concequent simulations for given simulation parameter.
Definition: jobmanager.cpp:68
std::atomic< bool > m_isRunning
Definition: jobmanager.h:51
std::thread m_simThread
Definition: jobmanager.h:48

References m_simThread, and wait_and_run().

Here is the call graph for this function:

◆ ~JobManager()

gui2::JobManager::~JobManager ( )
override

Definition at line 26 of file jobmanager.cpp.

27 {
28  m_isRunning = false;
29  // making stack throw to stops waiting in JobManager::wait_and_run
31  m_simThread.join();
32 }
ModelView::threadsafe_stack< SimulationInput > m_requestedInputValues
Definition: jobmanager.h:49

References m_isRunning, m_requestedInputValues, and m_simThread.

Member Function Documentation

◆ onInterruptRequest

void gui2::JobManager::onInterruptRequest ( )
slot

Processes interrupt request by setting corresponding flag.

Definition at line 59 of file jobmanager.cpp.

60 {
61  m_interruptRequest = true;
62 }
bool m_interruptRequest
Definition: jobmanager.h:52

References m_interruptRequest.

Referenced by gui2::QuickSimController::onInterruptRequest().

◆ progressChanged

void gui2::JobManager::progressChanged ( int  value)
signal

◆ requestSimulation

void gui2::JobManager::requestSimulation ( const multislice_t multislice,
const std::vector< double > &  qvalues,
double  intensity 
)
slot

Performs simulation request.

Given multislice will be stored in a stack of values to trigger a waiting thread.

Definition at line 45 of file jobmanager.cpp.

47 {
48  // At this point, non-empty stack means that currently simulation thread is busy.
49  // Replacing top value in a stack, meaning that we are droping previous request.
50  SimulationInput input_data;
51  input_data.slice_data = multislice;
52  input_data.qvalues = qvalues;
53  input_data.intensity = intensity;
54  m_requestedInputValues.update_top(input_data);
55 }

References gui2::SimulationInput::intensity, m_requestedInputValues, gui2::SimulationInput::qvalues, and gui2::SimulationInput::slice_data.

Referenced by gui2::QuickSimController::submit_specular_simulation().

◆ simulationCompleted

void gui2::JobManager::simulationCompleted ( )
signal

◆ simulationResult()

SimulationResult gui2::JobManager::simulationResult ( )

Returns vector representing results of a simulation.

Definition at line 36 of file jobmanager.cpp.

37 {
38  auto result = m_simulationResult.try_pop();
39  return result ? *result.get() : SimulationResult();
40 }
Wrapper around OutputData<double> that also provides unit conversions.
ModelView::threadsafe_stack< SimulationResult > m_simulationResult
Definition: jobmanager.h:50

References m_simulationResult.

Referenced by gui2::QuickSimController::onSimulationCompleted().

◆ wait_and_run()

void gui2::JobManager::wait_and_run ( )
private

Performs concequent simulations for given simulation parameter.

Waits for simulation input parameter to appear in a stack, starts new simulation as soon as input data is ready. Method is intended for execution in a thread.

Definition at line 68 of file jobmanager.cpp.

69 {
70  while (m_isRunning) {
71  try {
72  // Waiting here for the value which we will use as simulation input parameter.
73  auto value = m_requestedInputValues.wait_and_pop();
74 
75  // preparing simulation
76  SpecularToySimulation simulation(*value.get());
77  auto on_progress = [this](int value) {
78  progressChanged(value);
79  return m_interruptRequest;
80  };
81  simulation.setProgressCallback(on_progress);
82 
83  // running simulation
84  simulation.runSimulation();
85 
86  // Saving simulation result, overwrite previous if exists. If at this point stack
87  // with results is not empty it means that plotting is disabled or running too slow.
88  m_simulationResult.update_top(simulation.simulationResult());
90 
91  } catch (std::exception& ex) {
92  // Exception is thrown
93  // a) If waiting on stack was stopped my calling threadsafe_stack::stop.
94  // b) If simulation was interrupted via interrupt_request
95  m_interruptRequest = false;
96  progressChanged(0);
97  }
98  }
99 }
void simulationCompleted()
void progressChanged(int value)

References m_interruptRequest, m_isRunning, m_requestedInputValues, m_simulationResult, progressChanged(), gui2::SpecularToySimulation::runSimulation(), gui2::SpecularToySimulation::setProgressCallback(), simulationCompleted(), and gui2::SpecularToySimulation::simulationResult().

Referenced by JobManager().

Here is the call graph for this function:

Member Data Documentation

◆ m_interruptRequest

bool gui2::JobManager::m_interruptRequest {false}
private

Definition at line 52 of file jobmanager.h.

Referenced by onInterruptRequest(), and wait_and_run().

◆ m_isRunning

std::atomic<bool> gui2::JobManager::m_isRunning
private

Definition at line 51 of file jobmanager.h.

Referenced by ~JobManager(), and wait_and_run().

◆ m_requestedInputValues

ModelView::threadsafe_stack<SimulationInput> gui2::JobManager::m_requestedInputValues
private

Definition at line 49 of file jobmanager.h.

Referenced by ~JobManager(), requestSimulation(), and wait_and_run().

◆ m_simThread

std::thread gui2::JobManager::m_simThread
private

Definition at line 48 of file jobmanager.h.

Referenced by JobManager(), and ~JobManager().

◆ m_simulationResult

ModelView::threadsafe_stack<SimulationResult> gui2::JobManager::m_simulationResult
private

Definition at line 50 of file jobmanager.h.

Referenced by simulationResult(), and wait_and_run().


The documentation for this class was generated from the following files: