1function initDefault(self,
nodes)
5% closed classes initialized at ref station
6% running jobs are allocated in
class id order until all
9%refreshStruct(self); % we force update of the model before we initialize
11sn = self.getStruct(
false);
16 nodes = 1:self.getNumberOfNodes;
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);
30 for jst=[refist, setdiff(1:sn.nstations, refist)]
34 if sn.sched(jst) == SchedStrategy.EXT || sn.nodetype(sn.stationToNode(jst)) == NodeType.Place
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;
44 line_error(mfilename, sprintf(
'Cannot place the population of class %d: total station capacity is insufficient.', r));
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
58 switch sn.nodetype(ind)
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;
68 tcc = sn.nvars(ind,2*R+1);
71 if isfield(sn.nodeparam{ind},'retrievalSystemCapacity
') ...
72 && sn.nodeparam{ind}.retrievalSystemCapacity > 0
73 rbw = sn.nodeparam{ind}.nitems;
75 state_i = [state_i, 1:tcc, zeros(1,rbw)]; %#ok<AGROW>
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;
81 state_i = zeros(1,self.getNumberOfClasses);
83 if sn.refstat(r) == ist
84 state_i(r) = sn.njobs(r);
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);
96 if numel(sn.nodeparam) >= ind && isstruct(sn.nodeparam{ind}) ...
97 && isfield(sn.nodeparam{ind}, 'swapGraph
')
98 sg = sn.nodeparam{ind}.swapGraph;
100 hasSwap = ~isempty(sg) && any(sg(:) ~= 0);
101 isClosed = any(isfinite(N));
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);
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));
120 state_i = State.fromMarginalAndStarted(sn, ind, n0(:)', s0(:)
');
123 state_i = State.fromMarginalAndStarted(sn,ind,n0(:)',s0(:)
');
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);
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)];
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)
153 nbuf0(srvclass0) = nbuf0(srvclass0) - 1;
155 state_i = [state_i, State.pollingInit(sn, ind, nbuf0, srvclass0)];
158 line_error(mfilename,sprintf(
'Default initialization failed on station %d.',ind));
160 elseif sn.isstateful(ind) % not a station
161 switch sn.nodetype(ind)
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;
172 state_i = [zeros(1,self.getNumberOfClasses), 1:tcc, zeros(1,rbw)];
174 state_i = zeros(1, self.getNumberOfClasses);
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>
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>
205 %line_error(mfilename,
'Default initialization not available on stateful node %d.',i);
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,:));
219 self.nodes{ind}.setStateSpace([]);
220 self.nodes{ind}.setStatePrior([]);
221 self.nodes{ind}.setState([]);
226if self.isStateValid % problem with example_initState_2
227 self.hasState =
true;
229 line_error(mfilename,sprintf(
'Default initialization failed.'));