LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
lqn_builder.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_LANG_LQN_LQN_BUILDER_H
6#define LINE_LANG_LQN_LQN_BUILDER_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * Build a layered queueing network in code, as the MATLAB constructors do.
12 *
13 * Port of the Processor / Task / Entry / Activity / ActivityPrecedence API in
14 * matlab/src/lang/layered/, feeding the same `lqn_finalize` (getStruct) that
15 * the .lqnx reader feeds.
16 *
17 * WHY THIS EXISTS rather than always going through a file. The .lqnx
18 * interchange is LOSSY for models a script can express. The clearest case is a
19 * think time on a non-reference task: `writeXML` omits it because lqns rejects
20 * the attribute there (see _kb, "lqnx cannot carry non-ref think time"), so
21 * exporting such a model and reading it back silently drops the think time and
22 * changes the answer. matlab/examples/basic/layeredModel/lqn_workflows.m is
23 * exactly that shape -- its T3 has both an infinite-server discipline and a
24 * think time of 10 -- so it cannot be reached through the file format at all.
25 *
26 * ORDER MATTERS, as in the reader: element indices are assigned in declaration
27 * order (all processors, then all tasks, then all entries, then all
28 * activities), and within a task the activities are ordered by declaration.
29 * Declare in the same order as the reference script and the indices agree.
30 *
31 * The builder validates references by name and throws on an unknown one; it
32 * does not silently create elements, because a typo that creates a second
33 * disconnected task is a model that still solves.
34 */
35
36#include <string>
37#include <vector>
38
40#include "line/util/error.h"
41
42namespace line {
43namespace lqn {
44
45template <class T>
47public:
48 /** Add a processor. `mult` may be infinite; INF scheduling forces it so. */
49 std::size_t processor(const std::string& name, double mult, SchedStrategy sched,
50 double repl = 1.0) {
51 detail::RawProc p;
52 p.name = name;
53 p.sched = sched;
54 p.repl = repl;
55 p.mult = sched == SchedStrategy::INF ? std::numeric_limits<double>::infinity() : mult;
56 m_.procs.push_back(p);
57 return m_.procs.size() - 1;
58 }
59
60 /** Add a task on a processor. */
61 std::size_t task(const std::string& name, double mult, SchedStrategy sched,
62 const std::string& on_processor, double repl = 1.0) {
63 detail::RawTask<T> t;
64 t.name = name;
65 t.sched = sched;
66 t.repl = repl;
67 t.mult = sched == SchedStrategy::INF ? std::numeric_limits<double>::infinity() : mult;
68 t.thinktime = Distrib<T>::immediate();
69 t.proc_slot = find_proc(on_processor);
70 m_.tasks.push_back(t);
71 return m_.tasks.size() - 1;
72 }
73
74 /**
75 * Set a task's think time.
76 *
77 * Accepted on ANY task, not only a reference one. That is what the MATLAB
78 * API allows and what the .lqnx writer cannot express; refusing it here to
79 * match the file format would make the builder strictly weaker than the
80 * reference for no gain.
81 */
82 void think_time(const std::string& task_name, const Distrib<T>& d) {
83 m_.tasks[find_task(task_name)].thinktime = d;
84 }
85
86 /**
87 * A CacheTask: a task whose entries are looked up in a cache of `nitems`.
88 *
89 * `itemcap` is the capacity of each cache list, so a plain single-level
90 * cache passes one value. The task itself is an ordinary server in its own
91 * layer; the Cache NODE appears in its HOST's layer, which is where
92 * buildLayersRecursive puts it (`iscachelayer` is a host-layer test).
93 */
94 std::size_t cache_task(const std::string& name, double mult, SchedStrategy sched,
95 const std::string& on_processor, std::size_t nitems,
96 const std::vector<int>& itemcap, ReplacementStrategy replacestrat,
97 double repl = 1.0) {
98 if (nitems == 0)
99 throw InputError("LqnBuilder::cache_task: '" + name + "' caches no item");
100 if (itemcap.empty())
101 throw InputError("LqnBuilder::cache_task: '" + name + "' has no cache list");
102 int total = 0;
103 for (std::size_t i = 0; i < itemcap.size(); ++i) total += itemcap[i];
104 if (total <= 0 || static_cast<std::size_t>(total) >= nitems)
105 throw InputError("LqnBuilder::cache_task: '" + name +
106 "' has a total capacity that is not between 1 and nitems-1; "
107 "a cache that holds every item never misses");
108 const std::size_t t = task(name, mult, sched, on_processor, repl);
109 m_.tasks[t].nitems = nitems;
110 m_.tasks[t].itemcap = itemcap;
111 m_.tasks[t].replacestrat = replacestrat;
112 return t;
113 }
114
115 /** `CacheTask.setRetrieval(true)`: concurrent misses of one item coalesce on a single fetch. */
116 void cache_retrieval(const std::string& task_name, bool on = true) {
117 detail::RawTask<T>& t = m_.tasks[find_task(task_name)];
118 if (t.nitems == 0)
119 throw InputError("LqnBuilder::cache_retrieval: '" + task_name + "' is not a cache task");
120 t.retrieval = on;
121 }
122
123 /**
124 * A SetupTask: a server that powers down when idle and pays to restart.
125 *
126 * `setup` is charged to the first arrival that finds the server off;
127 * `delayoff` is the idle timer that has to expire before it goes off, so a
128 * long delay-off makes the setup rare. Both are ordinary Task properties in
129 * the reference (`Task.setSetupTime`/`setDelayOffTime`), so this is a plain
130 * task with the two times attached rather than a distinct kind.
131 */
132 void setup_time(const std::string& task_name, const Distrib<T>& setup,
133 const Distrib<T>& delayoff) {
134 if (setup.disabled)
135 throw InputError("LqnBuilder::setup_time: '" + task_name + "' has no setup time");
136 if (delayoff.disabled)
137 throw InputError(
138 "LqnBuilder::setup_time: '" + task_name +
139 "' has a setup time but no delay-off time; a server that never shuts down pays "
140 "the setup at most once and the reference declines to model it");
141 detail::RawTask<T>& t = m_.tasks[find_task(task_name)];
142 t.setuptime = setup;
143 t.delayofftime = delayoff;
144 }
145
146 /**
147 * An ItemEntry: the entry a cache read enters, over `cardinality` items.
148 *
149 * `popularity` is the pmf of the read over those items. The reference takes
150 * a discrete Distribution (a Zipf, typically) and reads only its pmf; this
151 * port has no discrete-distribution type, so the pmf is given directly.
152 */
153 std::size_t item_entry(const std::string& name, const std::string& on_task,
154 std::size_t cardinality, const std::vector<T>& popularity) {
155 if (cardinality == 0)
156 throw InputError("LqnBuilder::item_entry: '" + name + "' indexes no item");
157 if (popularity.size() != cardinality)
158 throw InputError("LqnBuilder::item_entry: '" + name +
159 "' needs one popularity per item");
160 const std::size_t e = entry(name, on_task);
161 m_.entries[e].cardinality = cardinality;
162 m_.entries[e].popularity = popularity;
163 return e;
164 }
165
166 /** Add an entry on a task. */
167 std::size_t entry(const std::string& name, const std::string& on_task) {
168 detail::RawEntry<T> e;
169 e.name = name;
170 e.task_slot = find_task(on_task);
171 m_.entries.push_back(e);
172 return m_.entries.size() - 1;
173 }
174
175 /** An open arrival stream at an entry. */
176 void open_arrival(const std::string& entry_name, const Distrib<T>& d) {
177 detail::RawEntry<T>& e = m_.entries[find_entry(entry_name)];
178 e.has_arrival = true;
179 e.arrival = d;
180 }
181
182 /** Add an activity on a task, with its host demand. */
183 std::size_t activity(const std::string& name, const Distrib<T>& hostdem,
184 const std::string& on_task) {
185 detail::RawActivity<T> a;
186 a.name = name;
187 a.hostdem = hostdem;
188 a.thinktime = Distrib<T>::immediate();
189 a.task_slot = find_task(on_task);
190 a.phase = 1;
191 m_.acts.push_back(a);
192 return m_.acts.size() - 1;
193 }
194
195 /** Bind an activity to an entry: it is the entry's first activity. */
196 void bound_to(const std::string& act, const std::string& entry_name) {
197 m_.acts[find_act(act)].bound_to_entry = entry_name;
198 }
199
200 /** A synchronous call from an activity to an entry of another task. */
201 void sync_call(const std::string& act, const std::string& dest_entry, const T& mean) {
202 m_.acts[find_act(act)].sync_calls.push_back({dest_entry, mean});
203 }
204
205 /** An asynchronous call from an activity to an entry of another task. */
206 void async_call(const std::string& act, const std::string& dest_entry, const T& mean) {
207 m_.acts[find_act(act)].async_calls.push_back({dest_entry, mean});
208 }
209
210 /**
211 * ONE synchronous call per invocation, its destination CYCLING over the
212 * targets in the order given (`synchCallRoundRobin`).
213 *
214 * The members are ordinary sync calls of mean `mean/n`, so the aggregate
215 * call rate is `mean` either way; what round robin removes is the variance
216 * of the branching, which is what smooths the target queues. Needs at least
217 * two targets -- a group of one is not a dispatch decision.
218 */
219 void sync_call_round_robin(const std::string& act,
220 const std::vector<std::string>& dest_entries, const T& mean) {
221 add_call_group(act, dest_entries, mean, lang::RoutingStrategy::RROBIN,
222 "sync_call_round_robin");
223 }
224
225 /** As above, with the least loaded target taking the call (`synchCallJSQ`). */
226 void sync_call_jsq(const std::string& act, const std::vector<std::string>& dest_entries,
227 const T& mean) {
228 add_call_group(act, dest_entries, mean, lang::RoutingStrategy::JSQ, "sync_call_jsq");
229 }
230
231 /**
232 * Forwarding: whenever `src_entry` is invoked, with probability `prob` the
233 * request is handed onward to `dest_entry` instead of `src_entry` replying
234 * -- lqn_finalize (shared with the .lqnx reader, see lqn_reader.h) turns
235 * this into a CallType::FWD call and SolverLN's lqn_fwd_rendezvous rewrite
236 * (lqn_helpers.h) flattens it into a caller-side pseudo rendezvous.
237 */
238 void forward(const std::string& src_entry, const std::string& dest_entry, const T& prob) {
239 detail::RawEntry<T>& e = m_.entries[find_entry(src_entry)];
240 e.fwd_dest.push_back(dest_entry);
241 e.fwd_prob.push_back(prob);
242 }
243
244 /** Mark an activity as the one that replies to an entry. */
245 void replies_to(const std::string& act, const std::string& entry_name) {
246 m_.entries[find_entry(entry_name)].reply_activities.push_back(act);
247 }
248
249 /** `Activity` phase: 1 before the entry replies, 2 after it (the second phase). */
250 void activity_phase(const std::string& act, int phase) {
251 m_.acts[find_act(act)].phase = phase;
252 }
253
254 /** An activity think time, in series with the host demand. */
255 void act_think_time(const std::string& act, const Distrib<T>& d) {
256 m_.acts[find_act(act)].thinktime = d;
257 }
258
259 /**
260 * `Task.setPriority`, on the lqns scale the .lqnx `priority` attribute carries:
261 * a LARGER value is served first and the default 0 is the lowest.
262 *
263 * Read only under the priority disciplines (HOL, 'pri' and the other *PRIO
264 * ones), where a host serves the demand of the task with the largest value
265 * first and a task serves the request of the CALLER with the largest value
266 * first. SolverLN maps it to a class priority max - prio (ln_host_task_priorities).
267 */
268 void task_priority(const std::string& task_name, int priority) {
269 m_.tasks[find_task(task_name)].priority = priority;
270 }
271
272 /** `Entry.setType`: PH1PH2 (the default) or NONE; carried for the code generators only. */
273 void entry_type(const std::string& entry_name, const std::string& type) {
274 m_.entries[find_entry(entry_name)].type = type;
275 }
276
277 /** `Activity.setCallOrder`: STOCHASTIC or DETERMINISTIC, any other value reading as STOCHASTIC. */
278 void call_order(const std::string& act, const std::string& order) {
279 m_.acts[find_act(act)].call_order = detail::call_order_from_text(order);
280 }
281
282 /** Declare a processor as a `Host` rather than a `Processor`: the same element to every solver. */
283 void host_class(const std::string& proc_name, bool is_host = true) {
284 m_.procs[find_proc(proc_name)].is_host_class = is_host;
285 }
286
287 // ---- precedences ------------------------------------------------------
288
289 /** pre -> post, a plain sequence. */
290 void serial(const std::string& pre, const std::string& post) {
291 detail::RawPrecedence<T> p;
292 p.pretype = PrecedenceType::PRE_SEQ;
293 p.posttype = PrecedenceType::POST_SEQ;
294 p.preacts.push_back(pre);
295 p.postacts.push_back(post);
296 add_prec(pre, p);
297 }
298
299 /** pre -> every post, concurrently. */
300 void and_fork(const std::string& pre, const std::vector<std::string>& posts) {
301 detail::RawPrecedence<T> p;
302 p.pretype = PrecedenceType::PRE_SEQ;
303 p.posttype = PrecedenceType::POST_AND;
304 p.preacts.push_back(pre);
305 p.postacts = posts;
306 add_prec(pre, p);
307 }
308
309 /** all pres (or `quorum` of them) -> post. */
310 void and_join(const std::vector<std::string>& pres, const std::string& post,
311 std::size_t quorum = 0) {
312 detail::RawPrecedence<T> p;
313 p.pretype = PrecedenceType::PRE_AND;
314 p.posttype = PrecedenceType::POST_SEQ;
315 p.preacts = pres;
316 p.postacts.push_back(post);
317 if (quorum > 0) {
318 p.has_quorum = true;
319 p.quorum = quorum;
320 }
321 add_prec(pres.at(0), p);
322 }
323
324 /** pre -> one of the posts, with the given branch probabilities. */
325 void or_fork(const std::string& pre, const std::vector<std::string>& posts,
326 const std::vector<T>& probs) {
327 if (posts.size() != probs.size())
328 throw InputError("LqnBuilder::or_fork: one probability per branch is required");
329 detail::RawPrecedence<T> p;
330 p.pretype = PrecedenceType::PRE_SEQ;
331 p.posttype = PrecedenceType::POST_OR;
332 p.preacts.push_back(pre);
333 p.postacts = posts;
334 p.postparams = probs;
335 add_prec(pre, p);
336 }
337
338 /**
339 * `ActivityPrecedence.CacheAccess(pre, {hit, miss})`.
340 *
341 * `pre` reads the cache; the job leaves it on the HIT branch or the MISS
342 * branch. POST_CACHE lands on the two successors, not on `pre` -- that is
343 * how getStruct.m records a post type, and what SolverLN keys on.
344 */
345 void cache_access(const std::string& pre, const std::string& hit, const std::string& miss) {
346 detail::RawPrecedence<T> p;
347 p.pretype = PrecedenceType::PRE_SEQ;
348 p.posttype = PrecedenceType::POST_CACHE;
349 p.preacts.push_back(pre);
350 p.postacts.push_back(hit);
351 p.postacts.push_back(miss);
352 add_prec(pre, p);
353 }
354
355 /** any of the pres -> post. */
356 void or_join(const std::vector<std::string>& pres, const std::string& post) {
357 detail::RawPrecedence<T> p;
358 p.pretype = PrecedenceType::PRE_OR;
359 p.posttype = PrecedenceType::POST_SEQ;
360 p.preacts = pres;
361 p.postacts.push_back(post);
362 // PRE_OR carries a probability per branch in MATLAB; a plain or-join
363 // leaves them unset, which getStruct reads as absent
364 add_prec(pres.at(0), p);
365 }
366
367 /**
368 * pre -> body, repeated `count` times in expectation, then -> end.
369 *
370 * The body list is the loop body in order; `end` is the activity the loop
371 * exits to. This is MATLAB's ActivityPrecedence.Loop(pre, body, count),
372 * whose postacts vector is body followed by end.
373 */
374 void loop(const std::string& pre, const std::vector<std::string>& body,
375 const std::string& end, const T& count) {
376 detail::RawPrecedence<T> p;
377 p.pretype = PrecedenceType::PRE_SEQ;
378 p.posttype = PrecedenceType::POST_LOOP;
379 p.preacts.push_back(pre);
380 p.postacts = body;
381 p.postacts.push_back(end);
382 p.postparams.assign(body.size(), count);
383 add_prec(pre, p);
384 }
385
386 // ---- admission constraints --------------------------------------------
387
388 /**
389 * `elem.addConstraint(operands, coeffs, cap)`: sum(coeffs .* n(operands)) <= cap.
390 *
391 * `elem` is a task (whose operands are its entries) or a host (whose
392 * operands are its tasks). Port of LayeredNetworkElement.addConstraint;
393 * the operands stay NAMED until build(), because their column order is the
394 * task's entriesof / the host's tasksof and neither exists yet.
395 */
396 void add_constraint(const std::string& elem, const std::vector<std::string>& operands,
397 const std::vector<T>& coeffs, const T& cap) {
398 if (operands.size() != coeffs.size())
399 throw InputError("LqnBuilder::add_constraint: one coefficient per operand is required");
400 if (operands.empty())
401 throw InputError("LqnBuilder::add_constraint: the constraint names no operand");
402 for (std::size_t i = 0; i < operands.size(); ++i)
403 for (std::size_t j = i + 1; j < operands.size(); ++j)
404 if (operands[i] == operands[j])
405 throw InputError("LqnBuilder::add_constraint: operand '" + operands[i] +
406 "' appears twice");
407 detail::RawLinConRow<T> row;
408 row.names = operands;
409 row.coeffs = coeffs;
410 row.cap = cap;
411 linconrows_of(elem).push_back(row);
412 }
413
414 /**
415 * The positional form, `elem.setConstraint(A, b)`.
416 *
417 * Only the column COUNT is checked, at build() time, as the reference does:
418 * the columns are the element's entries or tasks in declaration order.
419 */
420 void set_constraint(const std::string& elem, const Matrix<T>& A, const std::vector<T>& b) {
421 if (A.rows() != b.size())
422 throw InputError("LqnBuilder::set_constraint: A and b disagree on the number of rows");
423 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
424 if (m_.tasks[i].name == elem) {
425 m_.tasks[i].lincon_A = A;
426 m_.tasks[i].lincon_b = b;
427 return;
428 }
429 for (std::size_t i = 0; i < m_.procs.size(); ++i)
430 if (m_.procs[i].name == elem) {
431 m_.proc_lincon[i] = std::make_pair(A, b);
432 return;
433 }
434 throw InputError("LqnBuilder::set_constraint: unknown task or processor '" + elem + "'");
435 }
436
437 /**
438 * `elem.setLoadDependence(alpha)`: alpha(n) scales the rate of the layer
439 * station of ELEM when it holds n jobs in total, on top of the multiplicity.
440 */
441 void set_load_dependence(const std::string& elem, const std::vector<T>& alpha) {
442 assert_rate_dependent(elem, "Load");
443 if (alpha.empty())
444 throw InputError("LqnBuilder::set_load_dependence: alpha is empty");
445 if (is_task(elem)) {
446 m_.tasks[find_task(elem)].lldscaling = alpha;
447 } else {
448 m_.proc_lldscaling[find_proc(elem)] = alpha;
449 }
450 }
451
452 /**
453 * `elem.setClassDependence(beta, peak)`: the product-form handle, whose
454 * argument is the per-OPERAND population of ELEM -- task j of a processor,
455 * entry j of a task, in declaration order.
456 */
457 void set_class_dependence(const std::string& elem, const CdScaling<T>& beta,
458 const std::vector<T>& peak) {
459 assert_rate_dependent(elem, "Class");
460 assert_dependence_handle(beta, peak, "Class");
461 if (is_task(elem)) {
462 const std::size_t t = find_task(elem);
463 m_.tasks[t].cdscaling = beta;
464 m_.tasks[t].cdscalingpeak = peak;
465 } else {
466 const std::size_t p = find_proc(elem);
467 m_.proc_cdscaling[p] = beta;
468 m_.proc_cdscalingpeak[p] = peak;
469 }
470 }
471
472 /**
473 * `elem.setJointDependence(eta, peak)`: the non-product-form handle, read at
474 * the whole per-operand vector, so solvers treat it as an approximation.
475 */
476 void set_joint_dependence(const std::string& elem, const CdScaling<T>& eta,
477 const std::vector<T>& peak) {
478 assert_rate_dependent(elem, "Joint");
479 assert_dependence_handle(eta, peak, "Joint");
480 assert_no_pools(elem, "a joint dependence");
481 if (is_task(elem)) {
482 const std::size_t t = find_task(elem);
483 m_.tasks[t].jdscaling = eta;
484 m_.tasks[t].jdscalingpeak = peak;
485 } else {
486 const std::size_t p = find_proc(elem);
487 m_.proc_jdscaling[p] = eta;
488 m_.proc_jdscalingpeak[p] = peak;
489 }
490 }
491
492 /**
493 * `elem.addServerType(ServerType(pool, count, compatible))`: one pool of
494 * COUNT identical servers, each running at RATE, eligible for the operands
495 * named in COMPATIBLE.
496 *
497 * The operands are resolved by name against the element's own operand list
498 * at build() time, so a pool may name a task or an entry that is declared
499 * later. Pools accumulate; SolverLN lowers the whole declaration to the
500 * activated-server rate.
501 */
502 void add_server_type(const std::string& elem, const std::string& pool, double count,
503 const std::vector<std::string>& compatible, const T& rate) {
504 assert_rate_dependent(elem, "Compatibility");
505 assert_no_jd(elem);
506 if (count < 1)
507 throw InputError("LqnBuilder::add_server_type: pool '" + pool +
508 "' must hold at least one server");
509 if (compatible.empty())
510 throw InputError("LqnBuilder::add_server_type: pool '" + pool +
511 "' is compatible with no operand, so it can never serve");
512 detail::RawServerPool<T> sp;
513 sp.name = pool;
514 sp.count = count;
515 sp.rate = rate;
516 sp.compatible = compatible;
517 raw_pools_of(elem).push_back(sp);
518 }
519
520 /** Flatten into the struct SolverLN consumes. */
521 LqnStruct<T> build() const { return lqn_finalize(m_); }
522
523 const LqnModel<T>& model() const { return m_; }
524
525private:
526 LqnModel<T> m_;
527
528 std::size_t find_proc(const std::string& n) const {
529 for (std::size_t i = 0; i < m_.procs.size(); ++i)
530 if (m_.procs[i].name == n) return i;
531 throw InputError("LqnBuilder: unknown processor '" + n + "'");
532 }
533 std::size_t find_task(const std::string& n) const {
534 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
535 if (m_.tasks[i].name == n) return i;
536 throw InputError("LqnBuilder: unknown task '" + n + "'");
537 }
538 std::size_t find_entry(const std::string& n) const {
539 for (std::size_t i = 0; i < m_.entries.size(); ++i)
540 if (m_.entries[i].name == n) return i;
541 throw InputError("LqnBuilder: unknown entry '" + n + "'");
542 }
543 std::size_t find_act(const std::string& n) const {
544 for (std::size_t i = 0; i < m_.acts.size(); ++i)
545 if (m_.acts[i].name == n) return i;
546 throw InputError("LqnBuilder: unknown activity '" + n + "'");
547 }
548 /** The constraint-row list of a task or a host, by name. */
549 std::vector<detail::RawLinConRow<T>>& linconrows_of(const std::string& n) {
550 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
551 if (m_.tasks[i].name == n) return m_.tasks[i].linconrows;
552 for (std::size_t i = 0; i < m_.procs.size(); ++i)
553 if (m_.procs[i].name == n) return m_.proc_linconrows[i];
554 throw InputError("LqnBuilder: unknown task or processor '" + n + "'");
555 }
556
557 bool is_task(const std::string& n) const {
558 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
559 if (m_.tasks[i].name == n) return true;
560 return false;
561 }
562
563 /** The declared pool list of a task or a host, by name. */
564 std::vector<detail::RawServerPool<T>>& raw_pools_of(const std::string& n) {
565 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
566 if (m_.tasks[i].name == n) return m_.tasks[i].pools;
567 for (std::size_t i = 0; i < m_.procs.size(); ++i)
568 if (m_.procs[i].name == n) return m_.proc_pools[i];
569 throw InputError("LqnBuilder: unknown task or processor '" + n + "'");
570 }
571
572 /**
573 * Only a Task or a Host becomes a layer STATION, and only a PS or FCFS one
574 * admits a rate scaling. Twin of LayeredNetworkElement.assertRateDependent.
575 */
576 void assert_rate_dependent(const std::string& elem, const char* what) const {
577 SchedStrategy sched = SchedStrategy::NONE;
578 bool found = false;
579 for (std::size_t i = 0; i < m_.tasks.size() && !found; ++i)
580 if (m_.tasks[i].name == elem) {
581 sched = m_.tasks[i].sched;
582 found = true;
583 }
584 for (std::size_t i = 0; i < m_.procs.size() && !found; ++i)
585 if (m_.procs[i].name == elem) {
586 sched = m_.procs[i].sched;
587 found = true;
588 }
589 if (!found)
590 throw InputError(std::string(what) +
591 "-dependence can only be set on a Task or a Host, which are the only "
592 "elements that become server stations in a layer; '" +
593 elem + "' is neither");
594 if (sched != SchedStrategy::PS && sched != SchedStrategy::FCFS)
595 throw InputError(std::string(what) +
596 "-dependence supported only for processor sharing (PS) and "
597 "first-come first-serve (FCFS) servers, but '" +
598 elem + "' is scheduled otherwise");
599 }
600
601 /** The peak rate is a model input; without it utilization has no normalizer. */
602 void assert_dependence_handle(const CdScaling<T>& f, const std::vector<T>& peak,
603 const char* what) const {
604 if (!f)
605 throw InputError(std::string(what) + "-dependence needs a handle");
606 if (peak.empty())
607 throw InputError(std::string(what) +
608 "-dependence needs a peak rate per operand, which normalizes "
609 "utilization as U = T*S/peak");
610 }
611
612 /** One rate law per server: pools and an explicit handle would both claim it. */
613 void assert_no_pools(const std::string& elem, const char* what) {
614 if (!raw_pools_of(elem).empty())
615 throw InputError("LqnBuilder: '" + elem + "' already declares server pools, which are "
616 "themselves a rate law, so it cannot also take " + what);
617 }
618
619 void assert_no_jd(const std::string& elem) {
620 bool has = false;
621 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
622 if (m_.tasks[i].name == elem && m_.tasks[i].jdscaling) has = true;
623 for (std::size_t i = 0; i < m_.procs.size(); ++i)
624 if (m_.procs[i].name == elem && m_.proc_jdscaling.count(i)) has = true;
625 if (has)
626 throw InputError("LqnBuilder: '" + elem + "' already declares a joint dependence, so "
627 "it cannot also declare server pools, which are a rate law of their "
628 "own");
629 }
630
631 /** A precedence belongs to the task owning its activities. */
632 void add_prec(const std::string& anchor_act, const detail::RawPrecedence<T>& p) {
633 m_.tasks[m_.acts[find_act(anchor_act)].task_slot].precedences.push_back(p);
634 }
635
636 /**
637 * A routed group: n ordinary sync calls of mean/n, plus the record that
638 * they are one dispatch. Splitting the mean is the reference's own
639 * `addCallGroup` (Activity.m), and it is what keeps the aggregate call rate
640 * equal to the probabilistic twin's.
641 */
642 void add_call_group(const std::string& act, const std::vector<std::string>& dest_entries,
643 const T& mean, lang::RoutingStrategy rs, const char* who) {
644 if (dest_entries.size() < 2)
645 throw InputError(std::string("LqnBuilder::") + who +
646 " needs at least two target entries: a group of one is not a "
647 "dispatch decision");
648 const T share = T(mean / num_traits<T>::from_int(int(dest_entries.size())));
649 for (const std::string& d : dest_entries) sync_call(act, d, share);
650 m_.acts[find_act(act)].call_groups.push_back(std::make_pair(rs, dest_entries));
651 }
652};
653
654} // namespace lqn
655} // namespace line
656
657#endif // LINE_LANG_LQN_LQN_BUILDER_H
InputError(const std::string &what)
Definition error.h:39
std::size_t rows() const
Definition matrix.h:89
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.
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.
Definition lqn_builder.h:49
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.
Definition lqn_builder.h:94
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.
Definition lqn_builder.h:82
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.
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.
Definition lqn_builder.h:61
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.
Definition lang_types.h:181
RoutingStrategy
Routing strategies, with the values of MATLAB RoutingStrategy.
Definition lang_types.h:391
std::function< std::vector< T >(const std::vector< T > &)> CdScaling
A class-dependent scaling map, sn.cdscaling.
Definition lang_types.h:731
ReplacementStrategy
Cache replacement policies, with the values of MATLAB ReplacementStrategy.
Definition lang_types.h:380
LqnStruct< T > lqn_finalize(const LqnModel< T > &m)
Port of @LayeredNetwork/getStruct.m: flatten the model into its struct.
Definition lqn_reader.h:444
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:52
static Distrib immediate()
The Immediate singleton.
Definition lang_types.h:977
The intermediate model, and the second stage that flattens it.
Definition lqn_reader.h:409
std::vector< detail::RawTask< T > > tasks
Definition lqn_reader.h:413
std::vector< detail::RawProc > procs
Definition lqn_reader.h:412
std::vector< detail::RawEntry< T > > entries
Definition lqn_reader.h:414