Commit 749d4142 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

gui: fix thread status

parent d46db2ef
......@@ -39,7 +39,8 @@ static auto automatic_layout_y_distance = 350.f;
static auto grid_layout_x_distance = 250.f;
static auto grid_layout_y_distance = 250.f;
static ImVec4 operator*(const ImVec4& lhs, const float rhs) noexcept
static ImVec4
operator*(const ImVec4& lhs, const float rhs) noexcept
{
return ImVec4(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs, lhs.w * rhs);
}
......@@ -207,22 +208,21 @@ run_simulation(simulation& sim,
log_w.log(3,
"Simulation initialization failure (%s)\n",
irt::status_string(ret));
st = simulation_status::internal_error;
st = simulation_status::success;
return;
}
do {
if (auto ret = sim.run(current); irt::is_bad(ret)) {
log_w.log(3, "Simulation failure (%s)\n", irt::status_string(ret));
st = simulation_status::internal_error;
st = simulation_status::success;
return;
}
} while (current < end && !stop);
sim.clean();
st = simulation_status::success;
st = simulation_status::running_once_need_join;
}
void
......@@ -659,49 +659,48 @@ struct copier
auto ret = sim.dispatch(
mdl->type,
[ this, &sim, mdl, &
mdl_id_dst ]<typename DynamicsM>(DynamicsM & dynamics_models)
->status {
using Dynamics = typename DynamicsM::value_type;
[this, &sim, mdl, &mdl_id_dst]<typename DynamicsM>(
DynamicsM& dynamics_models) -> status {
using Dynamics = typename DynamicsM::value_type;
irt_return_if_fail(dynamics_models.can_alloc(1),
status::dynamics_not_enough_memory);
irt_return_if_fail(dynamics_models.can_alloc(1),
status::dynamics_not_enough_memory);
auto* dyn_ptr = dynamics_models.try_to_get(mdl->id);
irt_return_if_fail(dyn_ptr, status::dynamics_unknown_id);
auto* dyn_ptr = dynamics_models.try_to_get(mdl->id);
irt_return_if_fail(dyn_ptr, status::dynamics_unknown_id);
auto& new_dyn = dynamics_models.alloc(*dyn_ptr);
auto new_dyn_id = dynamics_models.get_id(new_dyn);
auto& new_dyn = dynamics_models.alloc(*dyn_ptr);
auto new_dyn_id = dynamics_models.get_id(new_dyn);
if constexpr (is_detected_v<has_input_port_t, Dynamics>)
std::fill_n(new_dyn.x,
std::size(new_dyn.x),
static_cast<input_port_id>(0));
if constexpr (is_detected_v<has_input_port_t, Dynamics>)
std::fill_n(new_dyn.x,
std::size(new_dyn.x),
static_cast<input_port_id>(0));
if constexpr (is_detected_v<has_output_port_t, Dynamics>)
std::fill_n(new_dyn.y,
std::size(new_dyn.y),
static_cast<output_port_id>(0));
if constexpr (is_detected_v<has_output_port_t, Dynamics>)
std::fill_n(new_dyn.y,
std::size(new_dyn.y),
static_cast<output_port_id>(0));
irt_return_if_bad(
sim.alloc(new_dyn, new_dyn_id, mdl->name.c_str()));
irt_return_if_bad(
sim.alloc(new_dyn, new_dyn_id, mdl->name.c_str()));
*mdl_id_dst = new_dyn.id;
*mdl_id_dst = new_dyn.id;
if constexpr (is_detected_v<has_input_port_t, Dynamics>)
for (size_t j = 0, ej = std::size(new_dyn.x); j != ej;
++j)
this->c_input_ports.emplace_back(dyn_ptr->x[j],
new_dyn.x[j]);
if constexpr (is_detected_v<has_input_port_t, Dynamics>)
for (size_t j = 0, ej = std::size(new_dyn.x); j != ej;
++j)
this->c_input_ports.emplace_back(dyn_ptr->x[j],
new_dyn.x[j]);
if constexpr (is_detected_v<has_output_port_t, Dynamics>)
for (size_t j = 0, ej = std::size(new_dyn.y); j != ej;
++j)
this->c_output_ports.emplace_back(dyn_ptr->y[j],
new_dyn.y[j]);
if constexpr (is_detected_v<has_output_port_t, Dynamics>)
for (size_t j = 0, ej = std::size(new_dyn.y); j != ej;
++j)
this->c_output_ports.emplace_back(dyn_ptr->y[j],
new_dyn.y[j]);
return status::success;
});
return status::success;
});
irt_return_if_bad(ret);
}
......@@ -1939,10 +1938,12 @@ editor::show_model_dynamics(model& mdl) noexcept
ImGui::PopItemWidth();
if (simulation_show_value && match(st,
simulation_status::success,
simulation_status::running_once,
simulation_status::running_step)) {
if (simulation_show_value &&
match(st,
simulation_status::success,
simulation_status::running_once,
simulation_status::running_once_need_join,
simulation_status::running_step)) {
sim.dispatch(mdl.type, [&](const auto& d_array) {
const auto& dyn = d_array.get(mdl.id);
......@@ -2046,7 +2047,8 @@ static const char* dynamics_type_names[] = {
"qss2_wsum_3", "qss2_wsum_4", "integrator", "quantifier",
"adder_2", "adder_3", "adder_4", "mult_2",
"mult_3", "mult_4", "counter", "generator",
"constant", "cross", "time_func", "accumulator_2", "flow"
"constant", "cross", "time_func", "accumulator_2",
"flow"
};
status
......@@ -2440,26 +2442,27 @@ show_simulation_box(bool* show_simulation)
ImGui::Checkbox("Show values", &ed->simulation_show_value);
if (ImGui::CollapsingHeader("Simulation run one")) {
if (ed->st == simulation_status::running_once_need_join) {
if (ed->simulation_thread.joinable()) {
ed->simulation_thread.join();
ed->st = simulation_status::success;
}
} else {
if (ImGui::Button("run")) {
initialize_observation(ed);
if (ed->simulation_thread.joinable()) {
ed->simulation_thread.join();
ed->st = simulation_status::success;
}
if (ImGui::Button("run")) {
initialize_observation(ed);
ed->st = simulation_status::running_once;
ed->stop = false;
ed->simulation_thread =
std::thread(&run_simulation,
std::ref(ed->sim),
ed->simulation_begin,
ed->simulation_end,
std::ref(ed->simulation_current),
std::ref(ed->st),
std::cref(ed->stop));
ed->st = simulation_status::running_once;
ed->stop = false;
ed->simulation_thread =
std::thread(&run_simulation,
std::ref(ed->sim),
ed->simulation_begin,
ed->simulation_end,
std::ref(ed->simulation_current),
std::ref(ed->st),
std::cref(ed->stop));
}
}
if (ed->st == simulation_status::running_once) {
......@@ -2471,12 +2474,9 @@ show_simulation_box(bool* show_simulation)
if (match(ed->st,
simulation_status::success,
simulation_status::running_step,
simulation_status::uninitialized,
simulation_status::internal_error)) {
simulation_status::running_step)) {
if (ImGui::CollapsingHeader("Simulation step by step")) {
if (ImGui::Button("init.")) {
if (ImGui::Button("init")) {
ed->sim.clean();
initialize_observation(ed);
ed->simulation_current = ed->simulation_begin;
......@@ -2485,10 +2485,10 @@ show_simulation_box(bool* show_simulation)
log_w.log(3,
"Simulation initialization failure (%s)\n",
irt::status_string(ret));
ed->st = simulation_status::internal_error;
return;
ed->st = simulation_status::success;
} else {
ed->st = simulation_status::running_step;
}
ed->st = simulation_status::running_step;
}
if (ed->st == simulation_status::running_step) {
......@@ -2501,8 +2501,7 @@ show_simulation_box(bool* show_simulation)
"Simulation failure (%s)\n",
irt::status_string(ret));
ed->st = simulation_status::internal_error;
return;
ed->st = simulation_status::success;
}
}
}
......@@ -2520,46 +2519,43 @@ show_simulation_box(bool* show_simulation)
"Simulation failure (%s)\n",
irt::status_string(ret));
ed->st = simulation_status::internal_error;
return;
ed->st = simulation_status::success;
}
}
}
}
}
}
}
if (match(ed->st,
simulation_status::success,
simulation_status::running_once,
simulation_status::running_step)) {
ImGui::Text("Current: %g", ed->simulation_current);
const double duration =
ed->simulation_end - ed->simulation_begin;
const double elapsed =
ed->simulation_current - ed->simulation_begin;
const double fraction = elapsed / duration;
ImGui::ProgressBar(static_cast<float>(fraction));
for (const auto& obs : ed->observation_outputs) {
if (obs.observation_type ==
observation_output::type::plot ||
obs.observation_type == observation_output::type::both)
ImGui::PlotLines(obs.name,
obs.data.data(),
static_cast<int>(obs.data.size()),
0,
nullptr,
obs.min,
obs.max,
ImVec2(0.f, 50.f));
if (obs.observation_type ==
observation_output::type::file ||
obs.observation_type == observation_output::type::both)
ImGui::Text("%s: output file", obs.name);
}
if (match(ed->st,
simulation_status::success,
simulation_status::running_once,
simulation_status::running_once_need_join,
simulation_status::running_step)) {
ImGui::Text("Current: %g", ed->simulation_current);
const double duration = ed->simulation_end - ed->simulation_begin;
const double elapsed =
ed->simulation_current - ed->simulation_begin;
const double fraction = elapsed / duration;
ImGui::ProgressBar(static_cast<float>(fraction));
for (const auto& obs : ed->observation_outputs) {
if (obs.observation_type == observation_output::type::plot ||
obs.observation_type == observation_output::type::both)
ImGui::PlotLines(obs.name.c_str(),
obs.data.data(),
static_cast<int>(obs.data.size()),
0,
nullptr,
obs.min,
obs.max,
ImVec2(0.f, 50.f));
if (obs.observation_type == observation_output::type::file ||
obs.observation_type == observation_output::type::both)
ImGui::Text("%s: output file", obs.name);
}
}
}
......
......@@ -119,9 +119,8 @@ enum class simulation_status
{
success,
running_once,
running_once_need_join,
running_step,
uninitialized,
internal_error,
};
static inline constexpr int not_found = -1;
......@@ -259,7 +258,7 @@ struct editor
double simulation_end = 10.0;
double simulation_current = 10.0;
std::thread simulation_thread;
simulation_status st = simulation_status::uninitialized;
simulation_status st = simulation_status::success;
bool simulation_show_value = false;
bool stop = false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment