Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for speedup/real-time simulation in non-GUI mode #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 8 additions & 4 deletions libstage/stage.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

// C++ libs
#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#include <list>
Expand Down Expand Up @@ -852,13 +853,19 @@ protected:
std::list<float *> ray_list; ///< List of rays traced for debug visualization
usec_t sim_time; ///< the current sim time in this world in microseconds
std::map<point_int_t, SuperRegion *> superregions;
std::chrono::time_point<std::chrono::steady_clock> next_run_time;
usec_t real_time_between_runs;
/** Stage attempts to run this many times faster than real
time. If -1, Stage runs as fast as possible. */
double speedup;

uint64_t updates; ///< the number of simulated time steps executed so far
Worldfile *wf; ///< If set, points to the worldfile used to create this world

void CallUpdateCallbacks(); ///< Call all calbacks in cb_list, removing any that return true;

public:
std::chrono::time_point<std::chrono::steady_clock> NextRunTime() { return next_run_time; }
uint64_t UpdateCount() { return updates; }
bool paused; ///< if true, the simulation is stopped

Expand Down Expand Up @@ -1032,6 +1039,7 @@ updates */
public:
/** returns true when time to quit, false otherwise */
static bool UpdateAll();
static bool UpdateAll(std::chrono::time_point<std::chrono::steady_clock> &next_run);

/** run all worlds.
* If only non-gui worlds were created, UpdateAll() is
Expand Down Expand Up @@ -1430,10 +1438,6 @@ private:
FileManager *fileMan; ///< Used to load and save worldfiles
std::vector<usec_t> interval_log;

/** Stage attempts to run this many times faster than real
time. If -1, Stage runs as fast as possible. */
double speedup;

bool confirm_on_quit; ///< if true, show save dialog on (GUI) exit (default)

Fl_Menu_Bar *mbar;
Expand Down
38 changes: 35 additions & 3 deletions libstage/world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ using std::abs;

#include <cstdlib>

#include <chrono>
#include <thread>
#include <stdlib.h>
#include <assert.h>
#include <libgen.h> // for dirname(3)
Expand Down Expand Up @@ -138,7 +140,9 @@ World::World(const std::string &,

// protected
cb_list(), extent(), graphics(false), option_table(), powerpack_list(), quit_time(0),
ray_list(), sim_time(0), superregions(), updates(0), wf(NULL), paused(false),
ray_list(), sim_time(0), superregions(), next_run_time(std::chrono::time_point<std::chrono::steady_clock>::min()),
real_time_between_runs(0), speedup(-1.0), // default as fast as possible
updates(0), wf(NULL), paused(false),
event_queues(1), // use 1 thread by default
pending_update_callbacks(), active_energy(), active_velocity(),
sim_interval(1e5), // 100 msec has proved a good default
Expand Down Expand Up @@ -208,8 +212,10 @@ void World::Run()
Fl::wait();
}
} else {
while (!UpdateAll())
;
std::chrono::time_point<std::chrono::steady_clock> next_run;
while (!UpdateAll(next_run)) {
std::this_thread::sleep_until(next_run);
}
}
}

Expand All @@ -225,6 +231,21 @@ bool World::UpdateAll()
return quit;
}

bool World::UpdateAll(std::chrono::time_point<std::chrono::steady_clock> &next_run)
{
bool quit(true);

next_run = std::chrono::time_point<std::chrono::steady_clock>::max();
FOR_EACH (world_it, World::world_set) {
if ((*world_it)->Update() == false) {
quit = false;
next_run = std::min(next_run, (*world_it)->NextRunTime());
}
}

return quit;
}

void *World::update_thread_entry(std::pair<World *, int> *thread_info)
{
World *world(thread_info->first);
Expand Down Expand Up @@ -412,6 +433,9 @@ void World::LoadWorldPostHook()
// read msec instead of usec: easier for user
this->sim_interval = 1e3 * wf->ReadFloat(0, "interval_sim", this->sim_interval / 1e3);

this->speedup = wf->ReadFloat(0, "speedup", this->speedup);
this->real_time_between_runs = this->speedup > 0 ? this->sim_interval / this->speedup : 0;

this->worker_threads = wf->ReadInt(0, "threads", this->worker_threads);
if (this->worker_threads < 1) {
PRINT_WARN("threads set to <1. Forcing to 1");
Expand Down Expand Up @@ -612,6 +636,14 @@ bool World::Update()
if (PastQuitTime() || World::quit_all || this->quit)
return true;

if (real_time_between_runs > 0) {
if (std::chrono::steady_clock::now() < next_run_time)
return false;
next_run_time += std::chrono::microseconds(real_time_between_runs);
if (next_run_time < std::chrono::steady_clock::now())
next_run_time = std::chrono::steady_clock::now() + std::chrono::microseconds(real_time_between_runs);
}

if (show_clock && ((this->updates % show_clock_interval) == 0)) {
printf("\r[Stage: %s]", ClockString().c_str());
fflush(stdout);
Expand Down
5 changes: 3 additions & 2 deletions libstage/worldgui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ static const char *MoreHelpText =

WorldGui::WorldGui(int width, int height, const char *caption)
: Fl_Window(width, height, NULL), canvas(new Canvas(this, 0, 30, width, height - 30)),
drawOptions(), fileMan(new FileManager()), interval_log(), speedup(1.0), // real time
drawOptions(), fileMan(new FileManager()), interval_log(),
confirm_on_quit(true), mbar(new Fl_Menu_Bar(0, 0, width, 30)), oDlg(NULL), pause_time(false),
real_time_interval(sim_interval), real_time_now(RealTimeNow()),
real_time_recorded(real_time_now), timing_interval(20)
{
speedup = 1.0; // default in GUI mode is realtime

Fl::lock(); // start FLTK's thread safe behaviour

Fl::scheme("");
Expand Down Expand Up @@ -301,7 +303,6 @@ void WorldGui::LoadWorldGuiPostHook(usec_t load_start_time)
{
// worldgui exclusive properties live in the top-level section
const int world_section = 0;
speedup = wf->ReadFloat(world_section, "speedup", speedup);
paused = wf->ReadInt(world_section, "paused", paused);
confirm_on_quit = wf->ReadInt(world_section, "confirm_on_quit", confirm_on_quit);

Expand Down