5#ifndef LINE_LANG_LQN_LQN_BUILDER_H
6#define LINE_LANG_LQN_LQN_BUILDER_H
55 p.mult = sched == SchedStrategy::INF ? std::numeric_limits<double>::infinity() : mult;
56 m_.procs.push_back(p);
57 return m_.procs.size() - 1;
61 std::size_t
host(
const std::string& name,
double mult,
SchedStrategy sched,
double repl = 1.0) {
62 return processor(name, mult, sched, repl);
67 const std::string& on_processor,
double repl = 1.0) {
72 t.mult = sched == SchedStrategy::INF ? std::numeric_limits<double>::infinity() : mult;
74 t.proc_slot = find_proc(on_processor);
75 m_.tasks.push_back(t);
76 return m_.tasks.size() - 1;
88 m_.tasks[find_task(task_name)].thinktime = d;
100 const std::string& on_processor, std::size_t nitems,
104 throw InputError(
"LqnBuilder::cache_task: '" + name +
"' caches no item");
106 throw InputError(
"LqnBuilder::cache_task: '" + name +
"' has no cache list");
108 for (std::size_t i = 0; i < itemcap.size(); ++i) total += itemcap[i];
109 if (total <= 0 ||
static_cast<std::size_t
>(total) >= nitems)
110 throw InputError(
"LqnBuilder::cache_task: '" + name +
111 "' has a total capacity that is not between 1 and nitems-1; "
112 "a cache that holds every item never misses");
113 const std::size_t t =
task(name, mult, sched, on_processor, repl);
114 m_.tasks[t].nitems = nitems;
115 m_.tasks[t].itemcap = itemcap;
116 m_.tasks[t].replacestrat = replacestrat;
122 detail::RawTask<T>& t = m_.tasks[find_task(task_name)];
124 throw InputError(
"LqnBuilder::cache_retrieval: '" + task_name +
"' is not a cache task");
140 throw InputError(
"LqnBuilder::setup_time: '" + task_name +
"' has no setup time");
143 "LqnBuilder::setup_time: '" + task_name +
144 "' has a setup time but no delay-off time; a server that never shuts down pays "
145 "the setup at most once and the reference declines to model it");
146 detail::RawTask<T>& t = m_.tasks[find_task(task_name)];
148 t.delayofftime = delayoff;
158 std::size_t
item_entry(
const std::string& name,
const std::string& on_task,
159 std::size_t cardinality,
const std::vector<T>& popularity) {
160 if (cardinality == 0)
161 throw InputError(
"LqnBuilder::item_entry: '" + name +
"' indexes no item");
162 if (popularity.size() != cardinality)
163 throw InputError(
"LqnBuilder::item_entry: '" + name +
164 "' needs one popularity per item");
165 const std::size_t e =
entry(name, on_task);
166 m_.entries[e].cardinality = cardinality;
167 m_.entries[e].popularity = popularity;
172 std::size_t
entry(
const std::string& name,
const std::string& on_task) {
173 detail::RawEntry<T> e;
175 e.task_slot = find_task(on_task);
176 m_.entries.push_back(e);
177 return m_.entries.size() - 1;
182 detail::RawEntry<T>& e = m_.entries[find_entry(entry_name)];
183 e.has_arrival =
true;
189 const std::string& on_task) {
190 detail::RawActivity<T> a;
194 a.task_slot = find_task(on_task);
196 m_.acts.push_back(a);
197 return m_.acts.size() - 1;
201 void bound_to(
const std::string& act,
const std::string& entry_name) {
202 m_.acts[find_act(act)].bound_to_entry = entry_name;
206 void sync_call(
const std::string& act,
const std::string& dest_entry,
const T& mean) {
207 m_.acts[find_act(act)].sync_calls.push_back({dest_entry, mean});
211 void async_call(
const std::string& act,
const std::string& dest_entry,
const T& mean) {
212 m_.acts[find_act(act)].async_calls.push_back({dest_entry, mean});
225 const std::vector<std::string>& dest_entries,
const T& mean) {
227 "sync_call_round_robin");
231 void sync_call_jsq(
const std::string& act,
const std::vector<std::string>& dest_entries,
244 if (dest_entries.size() < 2)
245 throw InputError(
"LqnBuilder::record_call_group needs at least two target entries: a "
246 "group of one is not a dispatch decision");
247 m_.acts[find_act(act)].call_groups.push_back(std::make_pair(rs, dest_entries));
257 void forward(
const std::string& src_entry,
const std::string& dest_entry,
const T& prob) {
258 detail::RawEntry<T>& e = m_.entries[find_entry(src_entry)];
259 e.fwd_dest.push_back(dest_entry);
260 e.fwd_prob.push_back(prob);
264 void replies_to(
const std::string& act,
const std::string& entry_name) {
266 if (m_.tasks[m_.acts[find_act(act)].task_slot].sched == SchedStrategy::REF)
268 m_.entries[find_entry(entry_name)].reply_activities.push_back(act);
273 m_.acts[find_act(act)].phase = phase;
278 m_.acts[find_act(act)].thinktime = d;
291 m_.tasks[find_task(task_name)].priority = priority;
295 void entry_type(
const std::string& entry_name,
const std::string& type) {
296 m_.entries[find_entry(entry_name)].type = type;
300 void call_order(
const std::string& act,
const std::string& order) {
301 m_.acts[find_act(act)].call_order = detail::call_order_from_text(order);
305 void host_class(
const std::string& proc_name,
bool is_host =
true) {
306 m_.procs[find_proc(proc_name)].is_host_class = is_host;
311 if (!(speed > 0.0) || !std::isfinite(speed))
312 throw InputError(
"speed_factor: the speed factor of '" + proc_name +
"' must be positive and finite");
313 m_.procs[find_proc(proc_name)].speed_factor = speed;
319 void serial(
const std::string& pre,
const std::string& post) {
320 detail::RawPrecedence<T> p;
321 p.pretype = PrecedenceType::PRE_SEQ;
322 p.posttype = PrecedenceType::POST_SEQ;
323 p.preacts.push_back(pre);
324 p.postacts.push_back(post);
329 void and_fork(
const std::string& pre,
const std::vector<std::string>& posts) {
330 detail::RawPrecedence<T> p;
331 p.pretype = PrecedenceType::PRE_SEQ;
332 p.posttype = PrecedenceType::POST_AND;
333 p.preacts.push_back(pre);
339 void and_join(
const std::vector<std::string>& pres,
const std::string& post,
340 std::size_t quorum = 0) {
341 detail::RawPrecedence<T> p;
342 p.pretype = PrecedenceType::PRE_AND;
343 p.posttype = PrecedenceType::POST_SEQ;
345 p.postacts.push_back(post);
350 add_prec(pres.at(0), p);
354 void or_fork(
const std::string& pre,
const std::vector<std::string>& posts,
355 const std::vector<T>& probs) {
356 if (posts.size() != probs.size())
357 throw InputError(
"LqnBuilder::or_fork: one probability per branch is required");
358 detail::RawPrecedence<T> p;
359 p.pretype = PrecedenceType::PRE_SEQ;
360 p.posttype = PrecedenceType::POST_OR;
361 p.preacts.push_back(pre);
363 p.postparams = probs;
374 void cache_access(
const std::string& pre,
const std::string& hit,
const std::string& miss) {
375 detail::RawPrecedence<T> p;
376 p.pretype = PrecedenceType::PRE_SEQ;
377 p.posttype = PrecedenceType::POST_CACHE;
378 p.preacts.push_back(pre);
379 p.postacts.push_back(hit);
380 p.postacts.push_back(miss);
385 void or_join(
const std::vector<std::string>& pres,
const std::string& post) {
386 detail::RawPrecedence<T> p;
387 p.pretype = PrecedenceType::PRE_OR;
388 p.posttype = PrecedenceType::POST_SEQ;
390 p.postacts.push_back(post);
393 add_prec(pres.at(0), p);
403 void loop(
const std::string& pre,
const std::vector<std::string>& body,
404 const std::string& end,
const T& count) {
405 detail::RawPrecedence<T> p;
406 p.pretype = PrecedenceType::PRE_SEQ;
407 p.posttype = PrecedenceType::POST_LOOP;
408 p.preacts.push_back(pre);
410 p.postacts.push_back(end);
411 p.postparams.assign(body.size(), count);
425 void add_constraint(
const std::string& elem,
const std::vector<std::string>& operands,
426 const std::vector<T>& coeffs,
const T& cap) {
427 if (operands.size() != coeffs.size())
428 throw InputError(
"LqnBuilder::add_constraint: one coefficient per operand is required");
429 if (operands.empty())
430 throw InputError(
"LqnBuilder::add_constraint: the constraint names no operand");
431 for (std::size_t i = 0; i < operands.size(); ++i)
432 for (std::size_t j = i + 1; j < operands.size(); ++j)
433 if (operands[i] == operands[j])
434 throw InputError(
"LqnBuilder::add_constraint: operand '" + operands[i] +
436 detail::RawLinConRow<T> row;
437 row.names = operands;
440 linconrows_of(elem).push_back(row);
450 if (A.
rows() != b.size())
451 throw InputError(
"LqnBuilder::set_constraint: A and b disagree on the number of rows");
452 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
453 if (m_.tasks[i].name == elem) {
454 m_.tasks[i].lincon_A = A;
455 m_.tasks[i].lincon_b = b;
458 for (std::size_t i = 0; i < m_.procs.size(); ++i)
459 if (m_.procs[i].name == elem) {
460 m_.proc_lincon[i] = std::make_pair(A, b);
463 throw InputError(
"LqnBuilder::set_constraint: unknown task or processor '" + elem +
"'");
471 assert_rate_dependent(elem,
"Load");
473 throw InputError(
"LqnBuilder::set_load_dependence: alpha is empty");
475 m_.tasks[find_task(elem)].lldscaling = alpha;
477 m_.proc_lldscaling[find_proc(elem)] = alpha;
487 const std::vector<T>& peak) {
488 assert_rate_dependent(elem,
"Class");
489 assert_dependence_handle(beta, peak,
"Class");
491 const std::size_t t = find_task(elem);
492 m_.tasks[t].cdscaling = beta;
493 m_.tasks[t].cdscalingpeak = peak;
495 const std::size_t p = find_proc(elem);
496 m_.proc_cdscaling[p] = beta;
497 m_.proc_cdscalingpeak[p] = peak;
506 const std::vector<T>& peak) {
507 assert_rate_dependent(elem,
"Joint");
508 assert_dependence_handle(eta, peak,
"Joint");
509 assert_no_pools(elem,
"a joint dependence");
511 const std::size_t t = find_task(elem);
512 m_.tasks[t].jdscaling = eta;
513 m_.tasks[t].jdscalingpeak = peak;
515 const std::size_t p = find_proc(elem);
516 m_.proc_jdscaling[p] = eta;
517 m_.proc_jdscalingpeak[p] = peak;
532 const std::vector<std::string>& compatible,
const T& rate) {
533 assert_rate_dependent(elem,
"Compatibility");
536 throw InputError(
"LqnBuilder::add_server_type: pool '" + pool +
537 "' must hold at least one server");
538 if (compatible.empty())
539 throw InputError(
"LqnBuilder::add_server_type: pool '" + pool +
540 "' is compatible with no operand, so it can never serve");
541 detail::RawServerPool<T> sp;
545 sp.compatible = compatible;
546 raw_pools_of(elem).push_back(sp);
557 std::size_t find_proc(
const std::string& n)
const {
558 for (std::size_t i = 0; i < m_.
procs.size(); ++i)
559 if (m_.
procs[i].name == n)
return i;
560 throw InputError(
"LqnBuilder: unknown processor '" + n +
"'");
562 std::size_t find_task(
const std::string& n)
const {
563 for (std::size_t i = 0; i < m_.
tasks.size(); ++i)
564 if (m_.
tasks[i].name == n)
return i;
565 throw InputError(
"LqnBuilder: unknown task '" + n +
"'");
567 std::size_t find_entry(
const std::string& n)
const {
568 for (std::size_t i = 0; i < m_.
entries.size(); ++i)
569 if (m_.
entries[i].name == n)
return i;
570 throw InputError(
"LqnBuilder: unknown entry '" + n +
"'");
572 std::size_t find_act(
const std::string& n)
const {
573 for (std::size_t i = 0; i < m_.acts.size(); ++i)
574 if (m_.acts[i].name == n)
return i;
575 throw InputError(
"LqnBuilder: unknown activity '" + n +
"'");
578 std::vector<detail::RawLinConRow<T>>& linconrows_of(
const std::string& n) {
579 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
580 if (m_.tasks[i].name == n)
return m_.tasks[i].linconrows;
581 for (std::size_t i = 0; i < m_.procs.size(); ++i)
582 if (m_.procs[i].name == n)
return m_.proc_linconrows[i];
583 throw InputError(
"LqnBuilder: unknown task or processor '" + n +
"'");
586 bool is_task(
const std::string& n)
const {
587 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
588 if (m_.tasks[i].name == n)
return true;
593 std::vector<detail::RawServerPool<T>>& raw_pools_of(
const std::string& n) {
594 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
595 if (m_.tasks[i].name == n)
return m_.tasks[i].pools;
596 for (std::size_t i = 0; i < m_.procs.size(); ++i)
597 if (m_.procs[i].name == n)
return m_.proc_pools[i];
598 throw InputError(
"LqnBuilder: unknown task or processor '" + n +
"'");
605 void assert_rate_dependent(
const std::string& elem,
const char* what)
const {
608 for (std::size_t i = 0; i < m_.tasks.size() && !found; ++i)
609 if (m_.tasks[i].name == elem) {
610 sched = m_.tasks[i].sched;
613 for (std::size_t i = 0; i < m_.procs.size() && !found; ++i)
614 if (m_.procs[i].name == elem) {
615 sched = m_.procs[i].sched;
620 "-dependence can only be set on a Task or a Host, which are the only "
621 "elements that become server stations in a layer; '" +
622 elem +
"' is neither");
623 if (sched != SchedStrategy::PS && sched != SchedStrategy::FCFS)
625 "-dependence supported only for processor sharing (PS) and "
626 "first-come first-serve (FCFS) servers, but '" +
627 elem +
"' is scheduled otherwise");
631 void assert_dependence_handle(
const CdScaling<T>& f,
const std::vector<T>& peak,
632 const char* what)
const {
634 throw InputError(std::string(what) +
"-dependence needs a handle");
637 "-dependence needs a peak rate per operand, which normalizes "
638 "utilization as U = T*S/peak");
642 void assert_no_pools(
const std::string& elem,
const char* what) {
643 if (!raw_pools_of(elem).empty())
644 throw InputError(
"LqnBuilder: '" + elem +
"' already declares server pools, which are "
645 "themselves a rate law, so it cannot also take " + what);
648 void assert_no_jd(
const std::string& elem) {
650 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
651 if (m_.tasks[i].name == elem && m_.tasks[i].jdscaling) has =
true;
652 for (std::size_t i = 0; i < m_.procs.size(); ++i)
653 if (m_.procs[i].name == elem && m_.proc_jdscaling.count(i)) has =
true;
655 throw InputError(
"LqnBuilder: '" + elem +
"' already declares a joint dependence, so "
656 "it cannot also declare server pools, which are a rate law of their "
661 void add_prec(
const std::string& anchor_act,
const detail::RawPrecedence<T>& p) {
662 m_.tasks[m_.acts[find_act(anchor_act)].task_slot].precedences.push_back(p);
671 void add_call_group(
const std::string& act,
const std::vector<std::string>& dest_entries,
673 if (dest_entries.size() < 2)
674 throw InputError(std::string(
"LqnBuilder::") + who +
675 " needs at least two target entries: a group of one is not a "
676 "dispatch decision");
677 const T share = T(mean / num_traits<T>::from_int(
int(dest_entries.size())));
678 for (
const std::string& d : dest_entries)
sync_call(act, d, share);
679 m_.acts[find_act(act)].call_groups.push_back(std::make_pair(rs, dest_entries));
void record_call_group(const std::string &act, const std::vector< std::string > &dest_entries, lang::RoutingStrategy rs)
Records the grouping of sync calls the activity ALREADY declares (recordCallGroup).
void sync_call_jsq(const std::string &act, const std::vector< std::string > &dest_entries, const T &mean)
As above, with the least loaded target taking the call (synchCallJSQ).
void bound_to(const std::string &act, const std::string &entry_name)
Bind an activity to an entry: it is the entry's first activity.
std::size_t activity(const std::string &name, const Distrib< T > &hostdem, const std::string &on_task)
Add an activity on a task, with its host demand.
void entry_type(const std::string &entry_name, const std::string &type)
Entry.setType: PH1PH2 (the default) or NONE; carried for the code generators only.
void cache_retrieval(const std::string &task_name, bool on=true)
CacheTask.setRetrieval(true): concurrent misses of one item coalesce on a single fetch.
std::size_t host(const std::string &name, double mult, SchedStrategy sched, double repl=1.0)
Host, the other name MATLAB, the JAR and python give a processor: the same element.
LqnStruct< T > build() const
Flatten into the struct SolverLN consumes.
void act_think_time(const std::string &act, const Distrib< T > &d)
An activity think time, in series with the host demand.
void loop(const std::string &pre, const std::vector< std::string > &body, const std::string &end, const T &count)
pre -> body, repeated count times in expectation, then -> end.
void activity_phase(const std::string &act, int phase)
Activity phase: 1 before the entry replies, 2 after it (the second phase).
void open_arrival(const std::string &entry_name, const Distrib< T > &d)
An open arrival stream at an entry.
void setup_time(const std::string &task_name, const Distrib< T > &setup, const Distrib< T > &delayoff)
A SetupTask: a server that powers down when idle and pays to restart.
std::size_t processor(const std::string &name, double mult, SchedStrategy sched, double repl=1.0)
Add a processor.
void set_joint_dependence(const std::string &elem, const CdScaling< T > &eta, const std::vector< T > &peak)
elem.setJointDependence(eta, peak): the non-product-form handle, read at the whole per-operand vector...
void host_class(const std::string &proc_name, bool is_host=true)
Declare a processor as a Host rather than a Processor: the same element to every solver.
void task_priority(const std::string &task_name, int priority)
Task.setPriority, on the lqns scale the .lqnx priority attribute carries: a LARGER value is served fi...
void replies_to(const std::string &act, const std::string &entry_name)
Mark an activity as the one that replies to an entry.
void forward(const std::string &src_entry, const std::string &dest_entry, const T &prob)
Forwarding: whenever src_entry is invoked, with probability prob the request is handed onward to dest...
void and_fork(const std::string &pre, const std::vector< std::string > &posts)
pre -> every post, concurrently.
void cache_access(const std::string &pre, const std::string &hit, const std::string &miss)
ActivityPrecedence.CacheAccess(pre, {hit, miss}).
std::size_t cache_task(const std::string &name, double mult, SchedStrategy sched, const std::string &on_processor, std::size_t nitems, const std::vector< int > &itemcap, ReplacementStrategy replacestrat, double repl=1.0)
A CacheTask: a task whose entries are looked up in a cache of nitems.
void set_constraint(const std::string &elem, const Matrix< T > &A, const std::vector< T > &b)
The positional form, elem.setConstraint(A, b).
void add_constraint(const std::string &elem, const std::vector< std::string > &operands, const std::vector< T > &coeffs, const T &cap)
elem.addConstraint(operands, coeffs, cap): sum(coeffs .
void async_call(const std::string &act, const std::string &dest_entry, const T &mean)
An asynchronous call from an activity to an entry of another task.
void and_join(const std::vector< std::string > &pres, const std::string &post, std::size_t quorum=0)
all pres (or quorum of them) -> post.
void serial(const std::string &pre, const std::string &post)
pre -> post, a plain sequence.
void sync_call(const std::string &act, const std::string &dest_entry, const T &mean)
A synchronous call from an activity to an entry of another task.
void call_order(const std::string &act, const std::string &order)
Activity.setCallOrder: STOCHASTIC or DETERMINISTIC, any other value reading as STOCHASTIC.
void set_class_dependence(const std::string &elem, const CdScaling< T > &beta, const std::vector< T > &peak)
elem.setClassDependence(beta, peak): the product-form handle, whose argument is the per-OPERAND popul...
void think_time(const std::string &task_name, const Distrib< T > &d)
Set a task's think time.
const LqnModel< T > & model() const
void sync_call_round_robin(const std::string &act, const std::vector< std::string > &dest_entries, const T &mean)
ONE synchronous call per invocation, its destination CYCLING over the targets in the order given (syn...
void add_server_type(const std::string &elem, const std::string &pool, double count, const std::vector< std::string > &compatible, const T &rate)
elem.addServerType(ServerType(pool, count, compatible)): one pool of COUNT identical servers,...
void or_fork(const std::string &pre, const std::vector< std::string > &posts, const std::vector< T > &probs)
pre -> one of the posts, with the given branch probabilities.
void set_load_dependence(const std::string &elem, const std::vector< T > &alpha)
elem.setLoadDependence(alpha): alpha(n) scales the rate of the layer station of ELEM when it holds n ...
void or_join(const std::vector< std::string > &pres, const std::string &post)
any of the pres -> post.
void speed_factor(const std::string &proc_name, double speed)
Processor::setSpeedFactor: the processor serves its activities' demands speed times faster.
std::size_t item_entry(const std::string &name, const std::string &on_task, std::size_t cardinality, const std::vector< T > &popularity)
An ItemEntry: the entry a cache read enters, over cardinality items.
std::size_t entry(const std::string &name, const std::string &on_task)
Add an entry on a task.
std::size_t task(const std::string &name, double mult, SchedStrategy sched, const std::string &on_processor, double repl=1.0)
Add a task on a processor.
The exception types the port throws.
.lqnx -> LqnStruct, a port of matlab/src/lang/layered/@LayeredNetwork/parseXML.m followed by ....
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
RoutingStrategy
Routing strategies, with the values of MATLAB RoutingStrategy.
std::function< std::vector< T >(const std::vector< T > &)> CdScaling
A class-dependent scaling map, sn.cdscaling.
ReplacementStrategy
Cache replacement policies, with the values of MATLAB ReplacementStrategy.
LqnStruct< T > lqn_finalize(const LqnModel< T > &m)
Port of @LayeredNetwork/getStruct.m: flatten the model into its struct.
Conservation laws of a layered queueing network, enumerated from its structure.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
static Distrib immediate()
The point mass at zero.
The intermediate model, and the second stage that flattens it.
std::vector< detail::RawTask< T > > tasks
std::vector< detail::RawProc > procs
std::vector< detail::RawEntry< T > > entries