LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
fj_serialization.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_API_FJ_SERIALIZATION_H
6#define LINE_API_FJ_SERIALIZATION_H
7
8/**
9 * @file
10 * @ingroup api_fj
11 * Blocking probability and pseudoserver delay of serialization phases.
12 *
13 * Templated port of matlab/src/api/fj/fj_serialization.m.
14 *
15 * A serialization phase is a stretch of execution protected by an exclusive
16 * lock, so at most one of the M circulating jobs may occupy it. Treating the
17 * other M-1 jobs as independently placed in proportion to the residence times,
18 *
19 * P_s(M) = 1 - [ 1 - R_s(M)/R(M) ]^(M-1), R(M) = R_0 + sum_s R_s(M),
20 *
21 * and the delay charged at the pseudoserver is alpha R_s(M), with alpha = 1/2
22 * for an arrival uniform in a lightly utilized phase, the regime in which the
23 * approximation is stated.
24 */
25
26#include <cstddef>
27#include <vector>
28
30#include "line/num/number.h"
31#include "line/util/error.h"
32
33namespace line {
34namespace fj {
35
36/** [P, delay, Rtot] of fj_serialization. */
37template <class T>
39 std::vector<T> P;
40 std::vector<T> delay;
42};
43
44/**
45 * @brief Blocking probability and pseudoserver delay of serialization phases.
46 *
47 * @param Rs mean residence time inside each serialization phase
48 * @param R0 mean residence time in the nonserialized phase
49 * @param M number of circulating jobs, M >= 1
50 * @param alpha fraction of the phase charged to a blocked job, in [0,1]
51 * @return the blocking probabilities, the pseudoserver delays and the cycle time
52 */
53template <class T>
54FJSerializationResult<T> fj_serialization(const std::vector<T>& Rs, const T& R0, unsigned M,
55 const T& alpha = num_traits<T>::from_double(0.5)) {
56 const std::size_t S = Rs.size();
57 if (S < 1) throw InputError("fj_serialization: at least one serialization phase is required");
58 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
59 for (std::size_t s = 0; s < S; ++s)
60 if (Rs[s] < zero)
61 throw InputError("fj_serialization: the phase residence times must be non-negative");
62 if (R0 < zero)
63 throw InputError("fj_serialization: the nonserialized residence time must be non-negative");
64 if (M < 1) throw InputError("fj_serialization: M must be a positive integer");
65 if (alpha < zero || alpha > one)
66 throw InputError("fj_serialization: alpha must lie in [0,1]");
67
68 T R = R0;
69 for (std::size_t s = 0; s < S; ++s) R += Rs[s];
70 if (!(R > zero))
71 throw NumericError("fj_serialization: the total residence time vanished");
72
74 out.P.resize(S);
75 out.delay.resize(S);
76 out.Rtot = R;
77 for (std::size_t s = 0; s < S; ++s) {
78 T pw = one;
79 for (unsigned e = 0; e + 1 < M; ++e) pw *= (one - Rs[s] / R);
80 out.P[s] = one - pw;
81 out.delay[s] = out.P[s] * (alpha * Rs[s]);
82 out.Rtot += out.delay[s];
83 }
84 return out;
85}
86
87} // namespace fj
88} // namespace line
89
90#endif // LINE_API_FJ_SERIALIZATION_H
InputError(const std::string &what)
Definition error.h:39
NumericError(const std::string &what)
Definition error.h:45
The exception types the port throws.
Shared return types and arithmetic helpers for the templated fork-join port.
FJSerializationResult< T > fj_serialization(const std::vector< T > &Rs, const T &R0, unsigned M, const T &alpha=num_traits< T >::from_double(0.5))
Blocking probability and pseudoserver delay of serialization phases.
Number-type abstraction for the templated API port.
[P, delay, Rtot] of fj_serialization.