5#ifndef LINE_SOLVERS_LDES_LDES_STATION_H
6#define LINE_SOLVERS_LDES_LDES_STATION_H
89 double deadline = std::numeric_limits<double>::infinity();
100 std::uint64_t
id = 0;
278 const double ma = mean_of(a), mb = mean_of(b);
279 if (ma != mb)
return ma > mb;
283 const double ma = mean_of(a), mb = mean_of(b);
284 if (ma != mb)
return ma < mb;
321 if (ka != kb)
return ka > kb;
330 double mean_of(
const Job& j)
const {
351template <
typename T,
typename Cmp>
352inline std::size_t
jpq_sift_down(std::vector<T>& es, std::size_t k, T x, std::size_t n,
const Cmp& cmp) {
353 const std::size_t half = n >> 1;
355 std::size_t child = 2 * k + 1;
356 const std::size_t right = child + 1;
357 if (right < n && cmp(es[child], es[right])) child = right;
358 if (!cmp(x, es[child]))
break;
366template <
typename T,
typename Cmp>
367inline void jpq_sift_up(std::vector<T>& es, std::size_t k, T x,
const Cmp& cmp) {
369 const std::size_t parent = (k - 1) >> 1;
370 if (!cmp(es[parent], x))
break;
378template <
typename T,
typename Cmp>
379inline void jpq_offer(std::vector<T>& es,
const T& x,
const Cmp& cmp) {
385template <
typename T,
typename Cmp>
386inline T
jpq_poll(std::vector<T>& es,
const Cmp& cmp) {
387 T result = es.front();
395template <
typename T,
typename Cmp>
396inline void jpq_remove_at(std::vector<T>& es, std::size_t i,
const Cmp& cmp) {
397 const std::size_t s = es.size() - 1;
408template <
typename T,
typename Cmp>
410 const std::size_t n = es.size();
411 for (std::size_t i = n >> 1; i-- > 0;)
jpq_sift_down(es, i, es[i], n, cmp);
436 std::uint64_t
id = 0;
451 double c,
const std::vector<double>& weight,
452 std::size_t nclasses) {
453 const std::size_t n = jobs.size();
454 std::vector<double> rates(n, 0.0);
455 if (n == 0)
return rates;
466 if (weighted_by_job) {
468 for (
const PsJob& j : jobs) tot += weight[j.
cls];
470 for (std::size_t i = 0; i < n; ++i)
471 rates[i] = std::min(1.0, weight[jobs[i].cls] * c / tot);
474 }
else if (weighted_by_class) {
475 std::vector<int> per_class(nclasses, 0);
476 for (
const PsJob& j : jobs) ++per_class[j.
cls];
478 for (std::size_t k = 0; k < nclasses; ++k)
479 if (per_class[k] > 0) tot += weight[k];
481 for (std::size_t i = 0; i < n; ++i) {
482 const std::size_t k = jobs[i].cls;
483 rates[i] = std::min(1.0, (weight[k] / tot / per_class[k]) * c);
489 const double share = std::min(1.0, c /
static_cast<double>(n));
490 for (std::size_t i = 0; i < n; ++i) rates[i] = share;
497 if (
static_cast<double>(n) <= c) {
498 for (std::size_t i = 0; i < n; ++i) rates[i] = 1.0;
503 double remaining = c;
504 for (std::set<int>::const_iterator it = prios.begin(); it != prios.end(); ++it) {
505 if (remaining <= 0.0)
break;
508 double tot_job_weight = 0.0;
509 std::vector<int> per_class(nclasses, 0);
510 for (
const PsJob& j : jobs)
513 tot_job_weight += weight[j.
cls];
516 const double allocated = std::min(remaining,
static_cast<double>(count));
517 double tot_class_weight = 0.0;
518 for (std::size_t k = 0; k < nclasses; ++k)
519 if (per_class[k] > 0) tot_class_weight += weight[k];
521 for (std::size_t i = 0; i < n; ++i) {
522 if (jobs[i].priority != p)
continue;
523 const std::size_t k = jobs[i].cls;
525 rates[i] = std::min(1.0, weight[k] * allocated / tot_job_weight);
527 rates[i] = std::min(1.0, (weight[k] / tot_class_weight / per_class[k]) * allocated);
529 rates[i] = allocated / count;
531 remaining -= allocated;
547 double c,
double now) {
548 std::vector<double> works = residuals;
549 std::sort(works.begin(), works.end());
550 const std::size_t n = works.size();
551 double cumulative = 0.0, prev = 0.0;
552 for (std::size_t rank = 0; rank < n; ++rank) {
553 const double w = works[rank];
554 const double rate = std::min(1.0, c /
static_cast<double>(n - rank));
555 if (rate > 0.0) cumulative += (w - prev) / rate;
556 if (w >= target_work)
return now + cumulative;
559 return now + cumulative;
592 const std::vector<double>& started,
const Job& arriving,
593 double c,
double now) {
594 const std::size_t none = in_service.size();
595 if (in_service.empty())
return none;
602 std::size_t best = none;
603 double oldest = std::numeric_limits<double>::infinity();
604 for (std::size_t j = 0; j < in_service.size(); ++j)
605 if (in_service[j].t_arr < oldest) {
606 oldest = in_service[j].t_arr;
616 std::size_t best = none;
617 for (std::size_t j = 0; j < in_service.size(); ++j) {
618 if (in_service[j].priority <= arriving.
priority)
continue;
619 if (best == none || in_service[j].priority > in_service[best].priority ||
620 (in_service[j].priority == in_service[best].priority &&
621 in_service[j].t_arr < in_service[best].t_arr))
624 if (best != none)
return best;
625 for (std::size_t j = 0; j < in_service.size(); ++j)
626 if (in_service[j].priority == arriving.
priority &&
627 (best == none || started[j] < started[best]))
641 std::size_t best = none;
642 for (std::size_t j = 0; j < in_service.size(); ++j) {
643 if (in_service[j].priority <= arriving.
priority)
continue;
644 if (best == none || in_service[j].priority > in_service[best].priority ||
645 (in_service[j].priority == in_service[best].priority &&
646 in_service[j].t_arr > in_service[best].t_arr))
659 std::size_t worst = 0;
660 for (std::size_t j = 1; j < in_service.size(); ++j) {
661 if (prio && in_service[j].priority != in_service[worst].priority) {
662 if (in_service[j].priority > in_service[worst].priority) worst = j;
663 }
else if (in_service[j].remaining > in_service[worst].remaining) {
667 if (prio && arriving.
priority != in_service[worst].priority)
668 return (arriving.
priority < in_service[worst].priority) ? worst : none;
669 return (arriving.
remaining < in_service[worst].remaining) ? worst : none;
672 std::size_t largest = 0;
673 for (std::size_t j = 1; j < in_service.size(); ++j)
674 if (in_service[j].service > in_service[largest].service) largest = j;
675 return (arriving.
service < in_service[largest].service) ? largest : none;
681 std::size_t most = 0;
682 for (std::size_t j = 1; j < in_service.size(); ++j)
683 if (in_service[j].elapsed > in_service[most].elapsed) most = j;
684 return (in_service[most].elapsed > arriving.
elapsed) ? most : none;
687 std::size_t least = 0;
688 for (std::size_t j = 1; j < in_service.size(); ++j)
689 if (in_service[j].remaining < in_service[least].remaining) least = j;
690 return (arriving.
remaining > in_service[least].remaining) ? least : none;
693 std::vector<double> works;
694 for (
const Job& j : in_service) works.push_back(j.
remaining);
697 std::size_t worst = 0;
698 double worst_vft = -std::numeric_limits<double>::infinity();
699 for (std::size_t j = 0; j < in_service.size(); ++j) {
706 return (arrival_vft < worst_vft) ? worst : none;
711 std::size_t latest = 0;
712 for (std::size_t j = 1; j < in_service.size(); ++j)
713 if (in_service[j].deadline > in_service[latest].deadline) latest = j;
714 return (arriving.
deadline < in_service[latest].deadline) ? latest : none;
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
std::size_t preemption_victim(lang::SchedStrategy sched, const std::vector< Job > &in_service, const std::vector< double > &started, const Job &arriving, double c, double now)
Which job in service arriving displaces, or in_service.size() for none.
void jpq_heapify(std::vector< T > &es, const Cmp &cmp)
PriorityQueue.heapify, for a buffer whose keys were rewritten in place.
void jpq_sift_up(std::vector< T > &es, std::size_t k, T x, const Cmp &cmp)
std::size_t jpq_sift_down(std::vector< T > &es, std::size_t k, T x, std::size_t n, const Cmp &cmp)
THE WAITING ROOM IS A java.util.PriorityQueue, ALGORITHM FOR ALGORITHM.
void jpq_offer(std::vector< T > &es, const T &x, const Cmp &cmp)
PriorityQueue.offer: append, then sift the new element up.
bool is_preemptive(lang::SchedStrategy s)
True for the disciplines that interrupt a job already in service.
double fsp_virtual_finish(const std::vector< double > &residuals, double target_work, double c, double now)
FSP's virtual finish time: when target would finish if the station ran processor sharing from now on.
std::vector< double > ps_shares(lang::SchedStrategy sched, const std::vector< PsJob > &jobs, double c, const std::vector< double > &weight, std::size_t nclasses)
The per-job shares of a sharing discipline, in units of one server.
bool is_preemptive_resume(lang::SchedStrategy s)
PREEMPTIVE RESUME, as against preemptive restart.
bool is_ps_family(lang::SchedStrategy s)
True for the disciplines that share the server instead of ordering a queue.
bool is_size_based(lang::SchedStrategy s)
True for the orders whose key is the job's size, residual or attained work.
void jpq_remove_at(std::vector< T > &es, std::size_t i, const Cmp &cmp)
PriorityQueue.removeAt: the last element fills the hole, down then up.
T jpq_poll(std::vector< T > &es, const Cmp &cmp)
PriorityQueue.poll: take the root, sift the LAST element down from it.
Conservation laws of a layered queueing network, enumerated from its structure.
double due
A Delay job's completion instant, read by the track_jobs export.
double deadline
EDD/EDF key.
int sys_cls
The class the current passage STARTED in (the class the Source emitted), -1 when unset.
double region_wait
Time spent parked outside a WAITQ region before entering this station.
double t_arr
arrival instant at this station
std::uint64_t call
The SYNCHRONOUS CALL this job belongs to, 0 when it belongs to none.
double rank
SIRO's random key, drawn at arrival.
double vft
FSP's virtual finish time, recomputed on demand.
bool preset
An init_jobs residual: service is the work still owed, not a fresh draw.
int attempts
How many times this job has already retried from an orbit.
double order_time
Round robin: the instant a job whose quantum expired rejoined the waiting room, which is its buffer o...
double t_sys
instant the current passage started
std::uint64_t tag
The generation its currently scheduled completion was pushed under.
double elapsed
attained service across all its service intervals
double service
the sampled requirement, drawn at arrival
std::int64_t ext
options.init_jobs: the caller's tag for this job, -1 when untagged.
double remaining
residual work; equals service before any preemption
std::uint64_t parent
The fork synchronization this job is a sibling of, 0 when it is not one.
A job being served by a sharing discipline, with the work it still owes.
double total
the sampled requirement
std::uint64_t tag
identifies the departure event scheduled for it
double region_wait
Job::region_wait, carried across the sharing station.
double remaining
residual work, in service-time units
std::uint64_t parent
The fork the job belongs to, carried across the sharing station.
std::int64_t ext
Job::ext, carried across the sharing station.
The waiting-room order of a buffered discipline.
lang::SchedStrategy sched
bool operator()(const Job &a, const Job &b) const
const std::vector< double > * class_mean
mean service per class, for SEPT/LEPT