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 /**
116 * A SetupTask: a server that powers down when idle and pays to restart.
117 *
118 * `setup` is charged to the first arrival that finds the server off;
119 * `delayoff` is the idle timer that has to expire before it goes off, so a
120 * long delay-off makes the setup rare. Both are ordinary Task properties in
121 * the reference (`Task.setSetupTime`/`setDelayOffTime`), so this is a plain
122 * task with the two times attached rather than a distinct kind.
123 */
124 void setup_time(const std::string& task_name, const Distrib<T>& setup,
125 const Distrib<T>& delayoff) {
126 if (setup.disabled)
127 throw InputError("LqnBuilder::setup_time: '" + task_name + "' has no setup time");
128 if (delayoff.disabled)
129 throw InputError(
130 "LqnBuilder::setup_time: '" + task_name +
131 "' has a setup time but no delay-off time; a server that never shuts down pays "
132 "the setup at most once and the reference declines to model it");
133 detail::RawTask<T>& t = m_.tasks[find_task(task_name)];
134 t.setuptime = setup;
135 t.delayofftime = delayoff;
136 }
137
138 /**
139 * An ItemEntry: the entry a cache read enters, over `cardinality` items.
140 *
141 * `popularity` is the pmf of the read over those items. The reference takes
142 * a discrete Distribution (a Zipf, typically) and reads only its pmf; this
143 * port has no discrete-distribution type, so the pmf is given directly.
144 */
145 std::size_t item_entry(const std::string& name, const std::string& on_task,
146 std::size_t cardinality, const std::vector<T>& popularity) {
147 if (cardinality == 0)
148 throw InputError("LqnBuilder::item_entry: '" + name + "' indexes no item");
149 if (popularity.size() != cardinality)
150 throw InputError("LqnBuilder::item_entry: '" + name +
151 "' needs one popularity per item");
152 const std::size_t e = entry(name, on_task);
153 m_.entries[e].cardinality = cardinality;
154 m_.entries[e].popularity = popularity;
155 return e;
156 }
157
158 /** Add an entry on a task. */
159 std::size_t entry(const std::string& name, const std::string& on_task) {
160 detail::RawEntry<T> e;
161 e.name = name;
162 e.task_slot = find_task(on_task);
163 m_.entries.push_back(e);
164 return m_.entries.size() - 1;
165 }
166
167 /** An open arrival stream at an entry. */
168 void open_arrival(const std::string& entry_name, const Distrib<T>& d) {
169 detail::RawEntry<T>& e = m_.entries[find_entry(entry_name)];
170 e.has_arrival = true;
171 e.arrival = d;
172 }
173
174 /** Add an activity on a task, with its host demand. */
175 std::size_t activity(const std::string& name, const Distrib<T>& hostdem,
176 const std::string& on_task) {
177 detail::RawActivity<T> a;
178 a.name = name;
179 a.hostdem = hostdem;
180 a.thinktime = Distrib<T>::immediate();
181 a.task_slot = find_task(on_task);
182 a.phase = 1;
183 m_.acts.push_back(a);
184 return m_.acts.size() - 1;
185 }
186
187 /** Bind an activity to an entry: it is the entry's first activity. */
188 void bound_to(const std::string& act, const std::string& entry_name) {
189 m_.acts[find_act(act)].bound_to_entry = entry_name;
190 }
191
192 /** A synchronous call from an activity to an entry of another task. */
193 void sync_call(const std::string& act, const std::string& dest_entry, const T& mean) {
194 m_.acts[find_act(act)].sync_calls.push_back({dest_entry, mean});
195 }
196
197 /** An asynchronous call from an activity to an entry of another task. */
198 void async_call(const std::string& act, const std::string& dest_entry, const T& mean) {
199 m_.acts[find_act(act)].async_calls.push_back({dest_entry, mean});
200 }
201
202 /**
203 * ONE synchronous call per invocation, its destination CYCLING over the
204 * targets in the order given (`synchCallRoundRobin`).
205 *
206 * The members are ordinary sync calls of mean `mean/n`, so the aggregate
207 * call rate is `mean` either way; what round robin removes is the variance
208 * of the branching, which is what smooths the target queues. Needs at least
209 * two targets -- a group of one is not a dispatch decision.
210 */
211 void sync_call_round_robin(const std::string& act,
212 const std::vector<std::string>& dest_entries, const T& mean) {
213 add_call_group(act, dest_entries, mean, lang::RoutingStrategy::RROBIN,
214 "sync_call_round_robin");
215 }
216
217 /** As above, with the least loaded target taking the call (`synchCallJSQ`). */
218 void sync_call_jsq(const std::string& act, const std::vector<std::string>& dest_entries,
219 const T& mean) {
220 add_call_group(act, dest_entries, mean, lang::RoutingStrategy::JSQ, "sync_call_jsq");
221 }
222
223 /**
224 * Forwarding: whenever `src_entry` is invoked, with probability `prob` the
225 * request is handed onward to `dest_entry` instead of `src_entry` replying
226 * -- lqn_finalize (shared with the .lqnx reader, see lqn_reader.h) turns
227 * this into a CallType::FWD call and SolverLN's lqn_fwd_rendezvous rewrite
228 * (lqn_helpers.h) flattens it into a caller-side pseudo rendezvous.
229 */
230 void forward(const std::string& src_entry, const std::string& dest_entry, const T& prob) {
231 detail::RawEntry<T>& e = m_.entries[find_entry(src_entry)];
232 e.fwd_dest.push_back(dest_entry);
233 e.fwd_prob.push_back(prob);
234 }
235
236 /** Mark an activity as the one that replies to an entry. */
237 void replies_to(const std::string& act, const std::string& entry_name) {
238 m_.entries[find_entry(entry_name)].reply_activities.push_back(act);
239 }
240
241 /** An activity think time, in series with the host demand. */
242 void act_think_time(const std::string& act, const Distrib<T>& d) {
243 m_.acts[find_act(act)].thinktime = d;
244 }
245
246 // ---- precedences ------------------------------------------------------
247
248 /** pre -> post, a plain sequence. */
249 void serial(const std::string& pre, const std::string& post) {
250 detail::RawPrecedence<T> p;
251 p.pretype = PrecedenceType::PRE_SEQ;
252 p.posttype = PrecedenceType::POST_SEQ;
253 p.preacts.push_back(pre);
254 p.postacts.push_back(post);
255 add_prec(pre, p);
256 }
257
258 /** pre -> every post, concurrently. */
259 void and_fork(const std::string& pre, const std::vector<std::string>& posts) {
260 detail::RawPrecedence<T> p;
261 p.pretype = PrecedenceType::PRE_SEQ;
262 p.posttype = PrecedenceType::POST_AND;
263 p.preacts.push_back(pre);
264 p.postacts = posts;
265 add_prec(pre, p);
266 }
267
268 /** all pres (or `quorum` of them) -> post. */
269 void and_join(const std::vector<std::string>& pres, const std::string& post,
270 std::size_t quorum = 0) {
271 detail::RawPrecedence<T> p;
272 p.pretype = PrecedenceType::PRE_AND;
273 p.posttype = PrecedenceType::POST_SEQ;
274 p.preacts = pres;
275 p.postacts.push_back(post);
276 if (quorum > 0) {
277 p.has_quorum = true;
278 p.quorum = quorum;
279 }
280 add_prec(pres.at(0), p);
281 }
282
283 /** pre -> one of the posts, with the given branch probabilities. */
284 void or_fork(const std::string& pre, const std::vector<std::string>& posts,
285 const std::vector<T>& probs) {
286 if (posts.size() != probs.size())
287 throw InputError("LqnBuilder::or_fork: one probability per branch is required");
288 detail::RawPrecedence<T> p;
289 p.pretype = PrecedenceType::PRE_SEQ;
290 p.posttype = PrecedenceType::POST_OR;
291 p.preacts.push_back(pre);
292 p.postacts = posts;
293 p.postparams = probs;
294 add_prec(pre, p);
295 }
296
297 /**
298 * `ActivityPrecedence.CacheAccess(pre, {hit, miss})`.
299 *
300 * `pre` reads the cache; the job leaves it on the HIT branch or the MISS
301 * branch. POST_CACHE lands on the two successors, not on `pre` -- that is
302 * how getStruct.m records a post type, and what SolverLN keys on.
303 */
304 void cache_access(const std::string& pre, const std::string& hit, const std::string& miss) {
305 detail::RawPrecedence<T> p;
306 p.pretype = PrecedenceType::PRE_SEQ;
307 p.posttype = PrecedenceType::POST_CACHE;
308 p.preacts.push_back(pre);
309 p.postacts.push_back(hit);
310 p.postacts.push_back(miss);
311 add_prec(pre, p);
312 }
313
314 /** any of the pres -> post. */
315 void or_join(const std::vector<std::string>& pres, const std::string& post) {
316 detail::RawPrecedence<T> p;
317 p.pretype = PrecedenceType::PRE_OR;
318 p.posttype = PrecedenceType::POST_SEQ;
319 p.preacts = pres;
320 p.postacts.push_back(post);
321 // PRE_OR carries a probability per branch in MATLAB; a plain or-join
322 // leaves them unset, which getStruct reads as absent
323 add_prec(pres.at(0), p);
324 }
325
326 /**
327 * pre -> body, repeated `count` times in expectation, then -> end.
328 *
329 * The body list is the loop body in order; `end` is the activity the loop
330 * exits to. This is MATLAB's ActivityPrecedence.Loop(pre, body, count),
331 * whose postacts vector is body followed by end.
332 */
333 void loop(const std::string& pre, const std::vector<std::string>& body,
334 const std::string& end, const T& count) {
335 detail::RawPrecedence<T> p;
336 p.pretype = PrecedenceType::PRE_SEQ;
337 p.posttype = PrecedenceType::POST_LOOP;
338 p.preacts.push_back(pre);
339 p.postacts = body;
340 p.postacts.push_back(end);
341 p.postparams.assign(body.size(), count);
342 add_prec(pre, p);
343 }
344
345 // ---- admission constraints --------------------------------------------
346
347 /**
348 * `elem.addConstraint(operands, coeffs, cap)`: sum(coeffs .* n(operands)) <= cap.
349 *
350 * `elem` is a task (whose operands are its entries) or a host (whose
351 * operands are its tasks). Port of LayeredNetworkElement.addConstraint;
352 * the operands stay NAMED until build(), because their column order is the
353 * task's entriesof / the host's tasksof and neither exists yet.
354 */
355 void add_constraint(const std::string& elem, const std::vector<std::string>& operands,
356 const std::vector<T>& coeffs, const T& cap) {
357 if (operands.size() != coeffs.size())
358 throw InputError("LqnBuilder::add_constraint: one coefficient per operand is required");
359 if (operands.empty())
360 throw InputError("LqnBuilder::add_constraint: the constraint names no operand");
361 for (std::size_t i = 0; i < operands.size(); ++i)
362 for (std::size_t j = i + 1; j < operands.size(); ++j)
363 if (operands[i] == operands[j])
364 throw InputError("LqnBuilder::add_constraint: operand '" + operands[i] +
365 "' appears twice");
366 detail::RawLinConRow<T> row;
367 row.names = operands;
368 row.coeffs = coeffs;
369 row.cap = cap;
370 linconrows_of(elem).push_back(row);
371 }
372
373 /**
374 * The positional form, `elem.setConstraint(A, b)`.
375 *
376 * Only the column COUNT is checked, at build() time, as the reference does:
377 * the columns are the element's entries or tasks in declaration order.
378 */
379 void set_constraint(const std::string& elem, const Matrix<T>& A, const std::vector<T>& b) {
380 if (A.rows() != b.size())
381 throw InputError("LqnBuilder::set_constraint: A and b disagree on the number of rows");
382 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
383 if (m_.tasks[i].name == elem) {
384 m_.tasks[i].lincon_A = A;
385 m_.tasks[i].lincon_b = b;
386 return;
387 }
388 for (std::size_t i = 0; i < m_.procs.size(); ++i)
389 if (m_.procs[i].name == elem) {
390 m_.proc_lincon[i] = std::make_pair(A, b);
391 return;
392 }
393 throw InputError("LqnBuilder::set_constraint: unknown task or processor '" + elem + "'");
394 }
395
396 /**
397 * `elem.setLoadDependence(alpha)`: alpha(n) scales the rate of the layer
398 * station of ELEM when it holds n jobs in total, on top of the multiplicity.
399 */
400 void set_load_dependence(const std::string& elem, const std::vector<T>& alpha) {
401 assert_rate_dependent(elem, "Load");
402 if (alpha.empty())
403 throw InputError("LqnBuilder::set_load_dependence: alpha is empty");
404 if (is_task(elem)) {
405 m_.tasks[find_task(elem)].lldscaling = alpha;
406 } else {
407 m_.proc_lldscaling[find_proc(elem)] = alpha;
408 }
409 }
410
411 /**
412 * `elem.setClassDependence(beta, peak)`: the product-form handle, whose
413 * argument is the per-OPERAND population of ELEM -- task j of a processor,
414 * entry j of a task, in declaration order.
415 */
416 void set_class_dependence(const std::string& elem, const CdScaling<T>& beta,
417 const std::vector<T>& peak) {
418 assert_rate_dependent(elem, "Class");
419 assert_dependence_handle(beta, peak, "Class");
420 if (is_task(elem)) {
421 const std::size_t t = find_task(elem);
422 m_.tasks[t].cdscaling = beta;
423 m_.tasks[t].cdscalingpeak = peak;
424 } else {
425 const std::size_t p = find_proc(elem);
426 m_.proc_cdscaling[p] = beta;
427 m_.proc_cdscalingpeak[p] = peak;
428 }
429 }
430
431 /**
432 * `elem.setJointDependence(eta, peak)`: the non-product-form handle, read at
433 * the whole per-operand vector, so solvers treat it as an approximation.
434 */
435 void set_joint_dependence(const std::string& elem, const CdScaling<T>& eta,
436 const std::vector<T>& peak) {
437 assert_rate_dependent(elem, "Joint");
438 assert_dependence_handle(eta, peak, "Joint");
439 assert_no_pools(elem, "a joint dependence");
440 if (is_task(elem)) {
441 const std::size_t t = find_task(elem);
442 m_.tasks[t].jdscaling = eta;
443 m_.tasks[t].jdscalingpeak = peak;
444 } else {
445 const std::size_t p = find_proc(elem);
446 m_.proc_jdscaling[p] = eta;
447 m_.proc_jdscalingpeak[p] = peak;
448 }
449 }
450
451 /**
452 * `elem.addServerType(ServerType(pool, count, compatible))`: one pool of
453 * COUNT identical servers, each running at RATE, eligible for the operands
454 * named in COMPATIBLE.
455 *
456 * The operands are resolved by name against the element's own operand list
457 * at build() time, so a pool may name a task or an entry that is declared
458 * later. Pools accumulate; SolverLN lowers the whole declaration to the
459 * activated-server rate.
460 */
461 void add_server_type(const std::string& elem, const std::string& pool, double count,
462 const std::vector<std::string>& compatible, const T& rate) {
463 assert_rate_dependent(elem, "Compatibility");
464 assert_no_jd(elem);
465 if (count < 1)
466 throw InputError("LqnBuilder::add_server_type: pool '" + pool +
467 "' must hold at least one server");
468 if (compatible.empty())
469 throw InputError("LqnBuilder::add_server_type: pool '" + pool +
470 "' is compatible with no operand, so it can never serve");
471 detail::RawServerPool<T> sp;
472 sp.name = pool;
473 sp.count = count;
474 sp.rate = rate;
475 sp.compatible = compatible;
476 raw_pools_of(elem).push_back(sp);
477 }
478
479 /** Flatten into the struct SolverLN consumes. */
480 LqnStruct<T> build() const { return lqn_finalize(m_); }
481
482 const LqnModel<T>& model() const { return m_; }
483
484private:
485 LqnModel<T> m_;
486
487 std::size_t find_proc(const std::string& n) const {
488 for (std::size_t i = 0; i < m_.procs.size(); ++i)
489 if (m_.procs[i].name == n) return i;
490 throw InputError("LqnBuilder: unknown processor '" + n + "'");
491 }
492 std::size_t find_task(const std::string& n) const {
493 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
494 if (m_.tasks[i].name == n) return i;
495 throw InputError("LqnBuilder: unknown task '" + n + "'");
496 }
497 std::size_t find_entry(const std::string& n) const {
498 for (std::size_t i = 0; i < m_.entries.size(); ++i)
499 if (m_.entries[i].name == n) return i;
500 throw InputError("LqnBuilder: unknown entry '" + n + "'");
501 }
502 std::size_t find_act(const std::string& n) const {
503 for (std::size_t i = 0; i < m_.acts.size(); ++i)
504 if (m_.acts[i].name == n) return i;
505 throw InputError("LqnBuilder: unknown activity '" + n + "'");
506 }
507 /** The constraint-row list of a task or a host, by name. */
508 std::vector<detail::RawLinConRow<T>>& linconrows_of(const std::string& n) {
509 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
510 if (m_.tasks[i].name == n) return m_.tasks[i].linconrows;
511 for (std::size_t i = 0; i < m_.procs.size(); ++i)
512 if (m_.procs[i].name == n) return m_.proc_linconrows[i];
513 throw InputError("LqnBuilder: unknown task or processor '" + n + "'");
514 }
515
516 bool is_task(const std::string& n) const {
517 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
518 if (m_.tasks[i].name == n) return true;
519 return false;
520 }
521
522 /** The declared pool list of a task or a host, by name. */
523 std::vector<detail::RawServerPool<T>>& raw_pools_of(const std::string& n) {
524 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
525 if (m_.tasks[i].name == n) return m_.tasks[i].pools;
526 for (std::size_t i = 0; i < m_.procs.size(); ++i)
527 if (m_.procs[i].name == n) return m_.proc_pools[i];
528 throw InputError("LqnBuilder: unknown task or processor '" + n + "'");
529 }
530
531 /**
532 * Only a Task or a Host becomes a layer STATION, and only a PS or FCFS one
533 * admits a rate scaling. Twin of LayeredNetworkElement.assertRateDependent.
534 */
535 void assert_rate_dependent(const std::string& elem, const char* what) const {
536 SchedStrategy sched = SchedStrategy::NONE;
537 bool found = false;
538 for (std::size_t i = 0; i < m_.tasks.size() && !found; ++i)
539 if (m_.tasks[i].name == elem) {
540 sched = m_.tasks[i].sched;
541 found = true;
542 }
543 for (std::size_t i = 0; i < m_.procs.size() && !found; ++i)
544 if (m_.procs[i].name == elem) {
545 sched = m_.procs[i].sched;
546 found = true;
547 }
548 if (!found)
549 throw InputError(std::string(what) +
550 "-dependence can only be set on a Task or a Host, which are the only "
551 "elements that become server stations in a layer; '" +
552 elem + "' is neither");
553 if (sched != SchedStrategy::PS && sched != SchedStrategy::FCFS)
554 throw InputError(std::string(what) +
555 "-dependence supported only for processor sharing (PS) and "
556 "first-come first-serve (FCFS) servers, but '" +
557 elem + "' is scheduled otherwise");
558 }
559
560 /** The peak rate is a model input; without it utilization has no normalizer. */
561 void assert_dependence_handle(const CdScaling<T>& f, const std::vector<T>& peak,
562 const char* what) const {
563 if (!f)
564 throw InputError(std::string(what) + "-dependence needs a handle");
565 if (peak.empty())
566 throw InputError(std::string(what) +
567 "-dependence needs a peak rate per operand, which normalizes "
568 "utilization as U = T*S/peak");
569 }
570
571 /** One rate law per server: pools and an explicit handle would both claim it. */
572 void assert_no_pools(const std::string& elem, const char* what) {
573 if (!raw_pools_of(elem).empty())
574 throw InputError("LqnBuilder: '" + elem + "' already declares server pools, which are "
575 "themselves a rate law, so it cannot also take " + what);
576 }
577
578 void assert_no_jd(const std::string& elem) {
579 bool has = false;
580 for (std::size_t i = 0; i < m_.tasks.size(); ++i)
581 if (m_.tasks[i].name == elem && m_.tasks[i].jdscaling) has = true;
582 for (std::size_t i = 0; i < m_.procs.size(); ++i)
583 if (m_.procs[i].name == elem && m_.proc_jdscaling.count(i)) has = true;
584 if (has)
585 throw InputError("LqnBuilder: '" + elem + "' already declares a joint dependence, so "
586 "it cannot also declare server pools, which are a rate law of their "
587 "own");
588 }
589
590 /** A precedence belongs to the task owning its activities. */
591 void add_prec(const std::string& anchor_act, const detail::RawPrecedence<T>& p) {
592 m_.tasks[m_.acts[find_act(anchor_act)].task_slot].precedences.push_back(p);
593 }
594
595 /**
596 * A routed group: n ordinary sync calls of mean/n, plus the record that
597 * they are one dispatch. Splitting the mean is the reference's own
598 * `addCallGroup` (Activity.m), and it is what keeps the aggregate call rate
599 * equal to the probabilistic twin's.
600 */
601 void add_call_group(const std::string& act, const std::vector<std::string>& dest_entries,
602 const T& mean, lang::RoutingStrategy rs, const char* who) {
603 if (dest_entries.size() < 2)
604 throw InputError(std::string("LqnBuilder::") + who +
605 " needs at least two target entries: a group of one is not a "
606 "dispatch decision");
607 const T share = T(mean / num_traits<T>::from_int(int(dest_entries.size())));
608 for (const std::string& d : dest_entries) sync_call(act, d, share);
609 m_.acts[find_act(act)].call_groups.push_back(std::make_pair(rs, dest_entries));
610 }
611};
612
613} // namespace lqn
614} // namespace line
615
616#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.
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 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 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 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:389
std::function< std::vector< T >(const std::vector< T > &)> CdScaling
A class-dependent scaling map, sn.cdscaling.
Definition lang_types.h:639
ReplacementStrategy
Cache replacement policies, with the values of MATLAB ReplacementStrategy.
Definition lang_types.h:378
LqnStruct< T > lqn_finalize(const LqnModel< T > &m)
Port of @LayeredNetwork/getStruct.m: flatten the model into its struct.
Definition lqn_reader.h:432
static Distrib immediate()
The Immediate singleton.
Definition lang_types.h:846
The intermediate model, and the second stage that flattens it.
Definition lqn_reader.h:399
std::vector< detail::RawTask< T > > tasks
Definition lqn_reader.h:401
std::vector< detail::RawProc > procs
Definition lqn_reader.h:400
std::vector< detail::RawEntry< T > > entries
Definition lqn_reader.h:402