46 std::vector<std::string> default_op_names = {
47 "update staticness",
"bound space",
"behavior",
48 "mechanical forces",
"discretization",
"propagate staticness agentop",
51 std::vector<std::string> pre_scheduled_ops_names = {
"set up iteration",
52 "propagate staticness"};
63 std::vector<std::string> post_scheduled_ops_names = {
64 "load balancing",
"tear down iteration",
"update environment",
65 "visualize",
"update time series"};
69 "distribute run displacment info",
72 "tear down iteration"};
74 auto disabled_op_names =
76 if (!param->detect_static_agents) {
77 disabled_op_names.push_back(
"propagate staticness");
78 disabled_op_names.push_back(
"propagate staticness agentop");
81 std::vector<std::vector<std::string>*> all_op_names;
82 all_op_names.push_back(&pre_scheduled_ops_names);
83 all_op_names.push_back(&default_op_names);
84 all_op_names.push_back(&post_scheduled_ops_names);
88 for (
auto* op_list : all_op_names) {
89 for (
auto op_name_iter = op_list->begin();
90 op_name_iter != op_list->end();) {
91 if (std::find(disabled_op_names.begin(), disabled_op_names.end(),
92 *op_name_iter) != disabled_op_names.end() &&
95 op_name_iter = op_list->erase(op_name_iter);
103 for (
auto& def_op : default_op_names) {
107 for (
auto& def_op : pre_scheduled_ops_names) {
111 for (
auto& def_op : post_scheduled_ops_names) {
115 if (!
GetOps(
"visualize").empty()) {
136 for (
unsigned step = 0; step < steps; step++) {
146 while (!exit_condition()) {
155 sim->GetExecutionContext()->SetupIterationAll(sim->GetAllExecCtxts());
179 "You tried to unschedule the protected operation ", op->
name_,
180 "! This request was ignored.");
185 bool not_in_scheduled_ops =
true;
187 if (op == scheduled_op) {
188 not_in_scheduled_ops =
false;
192 if (not_in_scheduled_ops) {
194 "You tried to unschedule the non-scheduled operation ",
195 op->
name_,
"! This request was ignored.");
203 std::vector<std::string> list;
205 list.push_back(op->name_);
211 std::vector<std::string> list;
213 list.push_back(op->name_);
219 std::vector<Operation*> ret;
224 Log::Warning(
"Scheduler::GetOps",
"The operation '", name,
225 "' is a protected operation. Request ignored.");
230 if ((*it)->name_ == name) {
266 Timing::Time(op->name_, [&]() { op->SetUp(); });
271 void Scheduler::TearDownOps() {
272 ForEachScheduledOperation([&](
Operation* op) {
274 Timing::Time(op->name_, [&]() { op->TearDown(); });
279 void Scheduler::RunPreScheduledOps()
const {
280 for (
auto* pre_op : pre_scheduled_ops_) {
281 if (pre_op->frequency_ != 0 && total_steps_ % pre_op->frequency_ == 0) {
282 Timing::Time(pre_op->name_, [&]() { (*pre_op)(); });
289 auto* sim = Simulation::GetActive();
290 auto* rm = sim->GetResourceManager();
291 auto* param = sim->GetParam();
292 auto batch_size = param->scheduling_batch_size;
294 std::vector<Operation*> agent_ops;
295 for (
auto* op : scheduled_agent_ops_) {
296 if (op->frequency_ != 0 && total_steps_ % op->frequency_ == 0 &&
297 !op->IsExcluded(filter)) {
298 agent_ops.push_back(op);
302 const auto& all_exec_ctxts = sim->GetAllExecCtxts();
303 all_exec_ctxts[0]->SetupAgentOpsAll(all_exec_ctxts);
305 if (param->execution_order == Param::ExecutionOrder::kForEachAgentForEachOp) {
307 Timing::Time(
"agent ops", [&]() {
308 rm->ForEachAgentParallel(batch_size, functor, filter);
311 for (
auto* op : agent_ops) {
312 decltype(agent_ops) ops = {op};
314 Timing::Time(op->name_, [&]() {
315 rm->ForEachAgentParallel(batch_size, functor, filter);
320 all_exec_ctxts[0]->TearDownAgentOpsAll(all_exec_ctxts);
324 void Scheduler::RunScheduledOps() {
328 if (agent_filters_.size() == 0) {
329 RunAgentOps(
nullptr);
331 for (
auto* filter : agent_filters_) {
337 for (
auto* op : scheduled_standalone_ops_) {
338 if (op->frequency_ != 0 && total_steps_ % op->frequency_ == 0) {
339 Timing::Time(op->name_, [&]() { (*op)(); });
346 void Scheduler::RunPostScheduledOps()
const {
347 for (
auto* post_op : post_scheduled_ops_) {
348 if (post_op->frequency_ != 0 && total_steps_ % post_op->frequency_ == 0) {
349 Timing::Time(post_op->name_, [&]() { (*post_op)(); });
354 void Scheduler::Execute() {
355 auto* param = Simulation::GetActive()->GetParam();
356 if (param->use_progress_bar) {
357 assert(progress_bar_ !=
nullptr);
358 progress_bar_->Step();
359 progress_bar_->PrintProgressBar();
360 }
else if (param->show_simulation_step != 0 &&
361 total_steps_ % param->show_simulation_step == 0) {
362 std::cout <<
"Time step: " << total_steps_ << std::endl;
366 RunPreScheduledOps();
368 RunPostScheduledOps();
371 void Scheduler::PrintInfo(std::ostream& out)
const {
372 out <<
"\n" << std::string(80,
'-') <<
"\n\n";
373 out <<
"Scheduler information:\n";
374 out << std::setw(80) <<
"frequency"
376 out <<
"Pre-scheduled operations:\n";
378 for (
auto* pre_op : pre_scheduled_ops_) {
379 out << std::setw(60) << pre_op->name_ << std::setw(20) << pre_op->frequency_
383 out <<
"\nAgent operations:\n";
384 for (
auto* op : scheduled_agent_ops_) {
385 out << std::setw(60) << op->name_ << std::setw(20) << op->frequency_
388 out <<
"\nStandalone operations:\n";
389 for (
auto* op : scheduled_standalone_ops_) {
390 out << std::setw(60) << op->name_ << std::setw(20) << op->frequency_
394 out <<
"\nPost-scheduled operations:\n";
395 for (
auto* post_op : post_scheduled_ops_) {
396 out << std::setw(60) << post_op->name_ << std::setw(20)
397 << post_op->frequency_ <<
"\n";
400 if (!unschedule_ops_.empty()) {
401 out <<
"\nUnschedule operations:\n";
402 for (
auto* unschedule_op : unschedule_ops_) {
403 out << std::setw(60) << unschedule_op->name_ << std::setw(20)
404 << unschedule_op->frequency_ <<
"\n";
408 if (!schedule_ops_.empty()) {
409 out <<
"\nSchedule operations:\n";
410 out <<
"(0) kSchedule, (1) kPreSchedule, (2) kPostSchedule\n";
411 for (
auto schedule_op : schedule_ops_) {
412 out <<
"(" << schedule_op.first <<
")" << std::setw(57)
413 << schedule_op.second->name_ << std::setw(20)
414 << schedule_op.second->frequency_ <<
"\n";
417 out <<
"\n" << std::string(80,
'-') <<
"\n";
420 void Scheduler::Backup() {
421 using std::chrono::duration_cast;
422 using std::chrono::seconds;
423 auto* param = Simulation::GetActive()->GetParam();
424 if (backup_->BackupEnabled() &&
425 duration_cast<seconds>(Clock::now() - last_backup_).count() >=
426 param->backup_interval) {
427 last_backup_ = Clock::now();
428 backup_->Backup(total_steps_);
435 bool Scheduler::Restore(uint64_t* steps) {
436 if (backup_->RestoreEnabled() && restore_point_ > total_steps_ + *steps) {
437 total_steps_ += *steps;
441 }
else if (backup_->RestoreEnabled() && restore_point_ > total_steps_ &&
442 restore_point_ < total_steps_ + *steps) {
445 *steps = total_steps_ + *steps - restore_point_;
446 total_steps_ = restore_point_;
451 void Scheduler::UpdateSimulatedTime() {
452 simulated_time_ += Simulation::GetActive()->GetParam()->simulation_time_step;
458 void Scheduler::Initialize(uint64_t steps) {
459 auto* sim = Simulation::GetActive();
460 auto* env = sim->GetEnvironment();
461 auto* rm = sim->GetResourceManager();
462 auto* param = sim->GetParam();
465 const auto& all_exec_ctxts = sim->GetAllExecCtxts();
466 all_exec_ctxts[0]->SetupIterationAll(all_exec_ctxts);
468 if (param->bound_space != Param::BoundSpaceMode::kOpen) {
470 rm->ForEachAgentParallel(*bound_space);
475 if (param->use_progress_bar) {
476 delete progress_bar_;
477 progress_bar_ =
nullptr;
479 progress_bar_->SetTimeUnit(param->progress_bar_time_unit);
483 sim->GetAgentUidGenerator()->Update();
495 if (dgrid !=
nullptr) {
505 void Scheduler::ScheduleOps() {
506 auto* param = Simulation::GetActive()->GetParam();
508 for (
auto it = schedule_ops_.begin(); it != schedule_ops_.end();) {
509 auto op_type = it->first;
510 auto* op = it->second;
514 if (param->compute_target ==
"cuda" &&
515 op->IsComputeTargetSupported(
kCuda)) {
516 op->SelectComputeTarget(
kCuda);
517 }
else if (param->compute_target ==
"opencl" &&
518 op->IsComputeTargetSupported(
kOpenCl)) {
519 op->SelectComputeTarget(
kOpenCl);
521 op->SelectComputeTarget(
kCpu);
527 pre_scheduled_ops_.push_back(op);
530 post_scheduled_ops_.push_back(op);
533 if (op->IsStandalone()) {
534 scheduled_standalone_ops_.push_back(op);
536 scheduled_agent_ops_.push_back(op);
541 it = schedule_ops_.erase(it);
545 for (
auto it = unschedule_ops_.begin(); it != unschedule_ops_.end();) {
549 std::vector<std::vector<Operation*>*> op_lists = {
550 &scheduled_agent_ops_, &scheduled_standalone_ops_, &pre_scheduled_ops_,
551 &post_scheduled_ops_};
553 for (
auto* op_list : op_lists) {
554 for (
auto it2 = op_list->begin(); it2 != op_list->end(); ++it2) {
556 it2 = op_list->erase(it2);
562 it = unschedule_ops_.erase(it);