LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
initDefault.m
1function initDefault(self, nodes)
2% INITDEFAULT(NODES)
3
4% open classes empty
5% closed classes initialized at ref station
6% running jobs are allocated in class id order until all
7% servers are busy
8
9%refreshStruct(self); % we force update of the model before we initialize
10
11sn = self.getStruct(false);
12
13R = sn.nclasses;
14N = sn.njobs';
15if nargin < 2
16 nodes = 1:self.getNumberOfNodes;
17end
18
19% see _kb/04-networkstruct.md (initDefault.m/spaceGenerator.m) for rationale
20nplace = zeros(sn.nstations, R);
21totplace = zeros(sn.nstations, 1);
22for r=find(isfinite(N))'
23 refist = sn.refstat(r);
24 if sn.nodetype(sn.stationToNode(refist)) == NodeType.Place
25 nplace(refist,r) = N(r);
26 totplace(refist) = totplace(refist) + N(r);
27 continue
28 end
29 remaining = N(r);
30 for jst=[refist, setdiff(1:sn.nstations, refist)]
31 if remaining == 0
32 break
33 end
34 if sn.sched(jst) == SchedStrategy.EXT || sn.nodetype(sn.stationToNode(jst)) == NodeType.Place
35 continue
36 end
37 avail = min(sn.classcap(jst,r) - nplace(jst,r), sn.cap(jst) - totplace(jst));
38 take = min(remaining, max(0, avail));
39 nplace(jst,r) = nplace(jst,r) + take;
40 totplace(jst) = totplace(jst) + take;
41 remaining = remaining - take;
42 end
43 if remaining > 0
44 line_error(mfilename, sprintf('Cannot place the population of class %d: total station capacity is insufficient.', r));
45 end
46end
47
48for ind=nodes
49 if sn.isstation(ind)
50 ist = sn.nodeToStation(ind);
51 n0 = nplace(ist,:); % number of jobs in the initial state
52 s0 = zeros(1,length(N)); % number of active servers in the initial state
53 s = sn.nservers(ist); % allocate
54 for r=find(isfinite(N))' % for all closed classes
55 s0(r) = min(n0(r),s);
56 s = s - s0(r);
57 end
58 switch sn.nodetype(ind)
59 case NodeType.Cache
60 state_i = State.fromMarginalAndStarted(sn,ind,n0(:)',s0(:)');
61 % Cache state width = totalCacheCapacity + (per-item retrieval bitmap).
62 % Initialize the cache region with items 1..totalCacheCapacity and the
63 % retrieval-system region (one column per item) with zeros (nothing
64 % being retrieved). The bitmap is omitted when no retrieval system.
65 if isfield(sn.nodeparam{ind}, 'totalCacheCapacity')
66 tcc = sn.nodeparam{ind}.totalCacheCapacity;
67 else
68 tcc = sn.nvars(ind,2*R+1);
69 end
70 rbw = 0;
71 if isfield(sn.nodeparam{ind},'retrievalSystemCapacity') ...
72 && sn.nodeparam{ind}.retrievalSystemCapacity > 0
73 rbw = sn.nodeparam{ind}.nitems;
74 end
75 state_i = [state_i, 1:tcc, zeros(1,rbw)]; %#ok<AGROW>
76 case NodeType.Place
77 if sum(self.nodes{ind}.state)>0
78 % if the user pre-loaded manually some jobs, keep them
79 state_i = self.nodes{ind}.state;
80 else
81 state_i = zeros(1,self.getNumberOfClasses);
82 for r=1:sn.nclasses
83 if sn.refstat(r) == ist
84 state_i(r) = sn.njobs(r);
85 else
86 state_i(r) = 0;
87 end
88 end
89 end
90 otherwise
91 if sn.isstation(ind) && sn.sched(ist) == SchedStrategy.PAS
92 % see _kb/04-networkstruct.md (initDefault.m/spaceGenerator.m) for rationale
93 userState = self.nodes{ind}.getState();
94 hasUser = ~isempty(userState) && any(userState(:) > 0);
95 sg = [];
96 if numel(sn.nodeparam) >= ind && isstruct(sn.nodeparam{ind}) ...
97 && isfield(sn.nodeparam{ind}, 'swapGraph')
98 sg = sn.nodeparam{ind}.swapGraph;
99 end
100 hasSwap = ~isempty(sg) && any(sg(:) ~= 0);
101 isClosed = any(isfinite(N));
102 if hasUser
103 nUser = zeros(1, R);
104 present = userState(userState > 0);
105 for r = 1:R, nUser(r) = sum(present == r); end
106 sU = zeros(1, R); ss = sn.nservers(ist);
107 for r = 1:R, sU(r) = min(nUser(r), ss); ss = ss - sU(r); end
108 space_i = State.fromMarginalAndStarted(sn, ind, nUser, sU);
109 W = size(space_i, 2);
110 urow = zeros(1, W);
111 cols = min(numel(userState), W);
112 urow(1:cols) = userState(1:cols);
113 % keep the user placement as the initial state (row 1)
114 state_i = unique([urow; space_i], 'rows', 'stable');
115 elseif hasSwap && isClosed && sum(n0) > 1
116 line_error(mfilename, sprintf(['A closed pass-and-swap station with a non-empty swapping ' ...
117 'graph requires an explicit initial job placement (station %d). Call setState on the ' ...
118 'station with the ordered class list (oldest first) before solving.'], ind));
119 else
120 state_i = State.fromMarginalAndStarted(sn, ind, n0(:)', s0(:)');
121 end
122 else
123 state_i = State.fromMarginalAndStarted(sn,ind,n0(:)',s0(:)');
124 if sn.isstation(ind)
125 for r=1:sn.nclasses
126 switch sn.procid(sn.nodeToStation(ind),r)
127 case {ProcessType.MAP, ProcessType.MMPP2}
128 % Markov-modulated service: append the phase-restart
129 % slot tracked in sn.nvars (see refreshLocalVars)
130 %state_i = State.cartesian(state_i, [1:sn.phases(i,r)]');
131 state_i = State.cartesian(state_i, 1);
132 end
133 end
134 end
135 end
136 end
137 for r=1:sn.nclasses
138 switch sn.routing(ind,r)
139 case {RoutingStrategy.RROBIN, RoutingStrategy.WRROBIN}
140 % start from first connected queue
141 state_i = [state_i, find(sn.connmatrix(ind,:),1)];
142 end
143 end
144 if sn.sched(ist) == SchedStrategy.POLLING
145 % The polling controller trails the routing variables, matching the
146 % nvars column order used by State.fromMarginal.
147 srvclass0 = find(s0 > 0, 1);
148 if isempty(srvclass0)
149 srvclass0 = 0;
150 end
151 nbuf0 = n0;
152 if srvclass0 > 0
153 nbuf0(srvclass0) = nbuf0(srvclass0) - 1;
154 end
155 state_i = [state_i, State.pollingInit(sn, ind, nbuf0, srvclass0)];
156 end
157 if isempty(state_i)
158 line_error(mfilename,sprintf('Default initialization failed on station %d.',ind));
159 end
160 elseif sn.isstateful(ind) % not a station
161 switch sn.nodetype(ind)
162 case NodeType.Cache
163 % [class counts | cache contents (items 1..tcc) | per-item retrieval
164 % bitmap (zeros, nothing being retrieved)]. The bitmap (one column per
165 % item) is omitted when no retrieval system is configured.
166 tcc = self.nodes{ind}.totalCacheCapacity;
167 if self.nodes{ind}.retrievalSystemCapacity > 0
168 rbw = self.nodes{ind}.items.nitems;
169 else
170 rbw = 0;
171 end
172 state_i = [zeros(1,self.getNumberOfClasses), 1:tcc, zeros(1,rbw)];
173 case NodeType.Router
174 state_i = zeros(1, self.getNumberOfClasses);
175 for r=1:sn.nclasses
176 switch sn.routing(ind,r)
177 case RoutingStrategy.RROBIN
178 % RR slot holds destination value; start at first connected queue
179 state_i = [state_i, find(sn.connmatrix(ind,:),1)]; %#ok<AGROW>
180 case RoutingStrategy.WRROBIN
181 % WRR slot holds POSITION in weighted_outlinks; start at 1
182 state_i = [state_i, 1]; %#ok<AGROW>
183 end
184 end
185 case NodeType.Fork
186 % Stateful Fork (FJ tag-augmented copies only): per-class
187 % count of parent jobs held before the fork firing
188 state_i = zeros(1, self.getNumberOfClasses);
189 case NodeType.Transition
190 % Differently from a server, the first nmodes states in a
191 % transitions count the servers that are not enabled and
192 % the last nodes count servers that just fired.
193 % This is required as the local state does not encode the
194 % buffer hence the enabling condition is not available.
195 state_i = sn.nodeparam{ind}.nmodeservers;
196 % For infinite servers, use large finite value for state (SSA needs finite states)
197 state_i(isinf(state_i)) = GlobalConstants.MaxInt();
198 % For non-Markovian distributions, firingphases is NaN - treat as 1 phase
199 firingphases = sn.nodeparam{ind}.firingphases;
200 firingphases(isnan(firingphases)) = 1;
201 state_i = [state_i, zeros(1, sum(firingphases)), zeros(size(state_i))]; %#ok<AGROW>
202 otherwise
203 state_i = [];
204 end
205 %line_error(mfilename,'Default initialization not available on stateful node %d.',i);
206 end
207
208 if sn.isstateful(ind) % not a station
209 if size(state_i,1)==1
210 self.nodes{ind}.setStateSpace(state_i);
211 self.nodes{ind}.setStatePrior(1);
212 self.nodes{ind}.setState(state_i);
213 elseif size(state_i,1)>1
214 prior_state_i = zeros(1,size(state_i,1)); prior_state_i(1) = 1;
215 self.nodes{ind}.setStateSpace(state_i);
216 self.nodes{ind}.setStatePrior(prior_state_i);
217 self.nodes{ind}.setState(state_i(1,:));
218 else
219 self.nodes{ind}.setStateSpace([]);
220 self.nodes{ind}.setStatePrior([]);
221 self.nodes{ind}.setState([]);
222 end
223 end
224end
225
226if self.isStateValid % problem with example_initState_2
227 self.hasState = true;
228else
229 line_error(mfilename,sprintf('Default initialization failed.'));
230end
231end
Definition fjtag.m:161