LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
afterEventCache.m
1function [outspace, outrate, outprob, eventCache] = afterEventCache(sn, ind, event, class, isSimulation, eventCache, R, space_buf, space_srv, space_var, key)
2% job arrives in class, then reads and moves into hit or miss
3% class, then departs
4
5outspace = [];
6outrate = [];
7outprob = 1;
8switch event
9 case EventType.ARV
10 space_srv(:,class) = space_srv(:,class) + 1;
11 outspace = [space_srv, space_var]; % buf is empty
12 outrate = -1*ones(size(outspace,1)); % passive action, rate is unspecified
13 case EventType.DEP
14 if space_srv(class)>0
15 space_srv(:,class) = space_srv(:,class) - 1;
16 switch sn.routing(ind,class)
17 case RoutingStrategy.RROBIN
18 idx = find(space_var(sum(sn.nvars(ind,1:(R+class)))) == sn.nodeparam{ind}{class}.outlinks);
19 if idx < length(sn.nodeparam{ind}{class}.outlinks)
20 space_var(sum(sn.nvars(ind,1:(R+class)))) = sn.nodeparam{ind}{class}.outlinks(idx+1);
21 else
22 space_var(sum(sn.nvars(ind,1:(R+class)))) = sn.nodeparam{ind}{class}.outlinks(1);
23 end
24 end
25 outspace = [space_srv, space_var]; % buf is empty
26 outrate = GlobalConstants.Immediate*ones(size(outspace,1)); % immediate action
27 end
28 case EventType.READ
29 n = sn.nodeparam{ind}.nitems; % n items
30 m = sn.nodeparam{ind}.itemcap; % capacity
31 ac = sn.nodeparam{ind}.accost; % access cost
32 hitclass = sn.nodeparam{ind}.hitclass;
33 missclass = sn.nodeparam{ind}.missclass;
34 h = length(m);
35 replacement_id = sn.nodeparam{ind}.replacestrat;
36 if space_srv(class)>0 && sum(space_srv)==1 % a job of class is in
37 p = sn.nodeparam{ind}.pread{class};
38 en = space_srv(:,class) > 0;
39 space_srv_k = [];
40 space_var_k = [];
41 outrate = [];
42 if any(en)
43 for e=find(en)'
44 if isSimulation
45 % pick one item
46 kset = 1 + max([0,find( rand > cumsum(p) )]);
47 % pick one entry list for cache miss
48 % do not move this entry
49 l = 1 + max([0,find( rand > cumsum(ac{class,kset}(1,:)) )]);
50 else
51 kset = 1:n;
52 end
53 for k=kset % request to item k
54 space_srv_e = space_srv(e,:);
55 space_srv_e(class) = space_srv_e(class) - 1;
56 var = space_var(e,:);
57 posk = find(k==var,1,'first');
58
59 if isempty(posk) % CACHE MISS, can enter any list based on accessCost
60 space_srv_e(missclass(class)) = space_srv_e(missclass(class)) + 1;
61 switch replacement_id
62 case {ReplacementStrategy.FIFO, ReplacementStrategy.LRU, ReplacementStrategy.SFIFO}
63 if isSimulation
64 listidx = l - 1; % l is accessCost column index, listidx is actual list (1-indexed)
65 if listidx > 0 % only cache if listidx is valid (l >= 2)
66 varp = var;
67 varp(cpos(listidx,2):cpos(listidx,m(listidx))) = var(cpos(listidx,1):cpos(listidx,m(listidx)-1));
68 varp(cpos(listidx,1)) = k; % head of list listidx
69 space_srv_k = [space_srv_k; space_srv_e];
70 space_var_k = [space_var_k; varp];
71 %% no p(k) weighting since that goes in the outprob vec
72 outrate(end+1,1) = GlobalConstants.Immediate;
73 outprob(end+1,1) = ac{class,k}(1,l) * p(k);
74 else
75 % Cache reject (l=1): pass through without caching
76 space_srv_k = [space_srv_k; space_srv_e];
77 space_var_k = [space_var_k; var];
78 outrate(end+1,1) = GlobalConstants.Immediate;
79 outprob(end+1,1) = ac{class,k}(1,l) * p(k);
80 end
81 else
82 for l=2:(h+1) % iterate over all possible target lists (columns 2 to h+1)
83 listidx = l - 1; % l is accessCost column index, listidx is actual list (1-indexed)
84 varp = var;
85 varp(cpos(listidx,2):cpos(listidx,m(listidx))) = var(cpos(listidx,1):cpos(listidx,m(listidx)-1));
86 varp(cpos(listidx,1)) = k; % head of list listidx
87 space_srv_k = [space_srv_k; space_srv_e];
88 space_var_k = [space_var_k; varp];
89 outrate(end+1,1) = ac{class,k}(1,l) * p(k) * GlobalConstants.Immediate;
90 outprob(end+1,1) = 1;
91 end
92 end
93 case ReplacementStrategy.RR
94 if isSimulation
95 listidx = l - 1; % l is accessCost column index, listidx is actual list (1-indexed)
96 if listidx > 0 % only cache if listidx is valid (l >= 2)
97 varp = var; % var'
98 r = randi(m(listidx),1,1);
99 varp(cpos(listidx,r)) = k;
100 space_srv_k = [space_srv_k; space_srv_e];
101 space_var_k = [space_var_k; (varp)];
102 outrate(end+1,1) = GlobalConstants.Immediate;
103 outprob(end+1,1) = ac{class,k}(1,l) * p(k);
104 else
105 % Cache reject (l=1): pass through without caching
106 space_srv_k = [space_srv_k; space_srv_e];
107 space_var_k = [space_var_k; var];
108 outrate(end+1,1) = GlobalConstants.Immediate;
109 outprob(end+1,1) = ac{class,k}(1,l) * p(k);
110 end
111 else
112 for l=2:(h+1) % iterate over all possible target lists
113 listidx = l - 1; % l is accessCost column index, listidx is actual list (1-indexed)
114 for r=1:m(listidx) % random position in list listidx
115 varp = var;
116 varp(cpos(listidx,r)) = k;
117 space_srv_k = [space_srv_k; space_srv_e];
118 space_var_k = [space_var_k; (varp)];
119 outrate(end+1,1) = ac{class,k}(1,l) * p(k)/m(listidx) * GlobalConstants.Immediate;
120 end
121 end
122 end
123 end
124 elseif posk <= sum(m(1:h-1)) % CACHE HIT in list i < h, move to list i+1
125 space_srv_e(hitclass(class)) = space_srv_e(hitclass(class)) + 1;
126 i = min(find(posk <= cumsum(m)));
127 j = posk - sum(m(1:i-1));
128
129 switch replacement_id
130 case ReplacementStrategy.FIFO
131 if isSimulation
132 varp = var;
133 inew = i+probchoose(ac{class,k}(1+i,(1+i):end)/sum(ac{class,k}(1+i,(1+i):end)))-1; % can choose i
134 if inew~=i
135 varp(cpos(i,j)) = var(cpos(inew,m(inew)));
136 varp(cpos(inew,2):cpos(inew,m(inew))) = var(cpos(inew,1):cpos(inew,m(inew)-1));
137 varp(cpos(inew,1)) = k;
138 end
139 %varp(cpos(i,j)) = var(cpos(i+1,m(i+1)));
140 %varp(cpos(i+1,2):cpos(i+1,m(i+1))) = var(cpos(i+1,1):cpos(i+1,m(i+1)-1));
141 %varp(cpos(i+1,1)) = k;
142
143 space_srv_k = [space_srv_k; space_srv_e];
144 space_var_k = [space_var_k; varp];
145 outrate(end+1,1) = GlobalConstants.Immediate;
146 outprob(end+1,1) = ac{class,k}(1+i,1+inew) * p(k);
147 else
148 for inew = i:h
149 varp = var;
150 varp(cpos(i,j)) = var(cpos(inew,m(inew)));
151 varp(cpos(inew,2):cpos(inew,m(inew))) = var(cpos(inew,1):cpos(inew,m(inew)-1));
152 varp(cpos(inew,1)) = k;
153 space_srv_k = [space_srv_k; space_srv_e];
154 space_var_k = [space_var_k; varp];
155 outrate(end+1,1) = ac{class,k}(1+i,1+inew) * p(k) * GlobalConstants.Immediate;
156 end
157 end
158 case ReplacementStrategy.RR
159 if isSimulation
160 inew = i+probchoose(ac{class,k}(1+i,(1+i):end)/sum(ac{class,k}(1+i,(1+i):end)))-1; % can choose i
161 varp = var;
162 r = randi(m(inew),1,1);
163 varp(cpos(i,j)) = var(cpos(inew,r));
164 varp(cpos(inew,r)) = k;
165 space_srv_k = [space_srv_k; space_srv_e];
166 space_var_k = [space_var_k; varp];
167 outrate(end+1,1) = GlobalConstants.Immediate;
168 outprob(end+1,1) = ac{class,k}(1+i,1+inew) * p(k)/m(inew);
169 else
170 for inew = i:h
171 for r=1:m(inew) % random position in new list
172 varp = var;
173 varp(cpos(i,j)) = var(cpos(inew,r));
174 varp(cpos(inew,r)) = k;
175 space_srv_k = [space_srv_k; space_srv_e];
176 space_var_k = [space_var_k; varp];
177 outrate(end+1,1) = ac{class,k}(1+i,1+inew) * p(k)/m(inew) * GlobalConstants.Immediate;
178 end
179 end
180 end
181 case {ReplacementStrategy.LRU, ReplacementStrategy.SFIFO}
182 if isSimulation
183 varp = var;
184 inew = i+probchoose(ac{class,k}(1+i,(1+i):end)/sum(ac{class,k}(1+i,(1+i):end)))-1; % can choose i
185 varp(cpos(i,2):cpos(i,j)) = var(cpos(i,1):cpos(i,j-1));
186 varp(cpos(i,1)) = var(cpos(inew,m(inew)));
187 varp(cpos(inew,2):cpos(inew,m(inew))) = var(cpos(inew,1):cpos(inew,m(inew)-1));
188 varp(cpos(inew,1)) = k;
189 space_srv_k = [space_srv_k; space_srv_e];
190 space_var_k = [space_var_k; varp];
191 outrate(end+1,1) = GlobalConstants.Immediate;
192 outprob(end+1,1) = ac{class,k}(1+i,1+inew) * p(k);
193 else
194 for inew = i:h
195 varp = var;
196 varp(cpos(i,2):cpos(i,j)) = var(cpos(i,1):cpos(i,j-1));
197 varp(cpos(i,1)) = var(cpos(inew,m(inew)));
198 varp(cpos(inew,2):cpos(inew,m(inew))) = var(cpos(inew,1):cpos(inew,m(inew)-1));
199 varp(cpos(inew,1)) = k;
200 space_srv_k = [space_srv_k; space_srv_e];
201 space_var_k = [space_var_k; varp];
202 outrate(end+1,1) = ac{class,k}(1+i,1+inew) * p(k) * GlobalConstants.Immediate;
203 end
204 end
205 end
206 else % CACHE HIT in list h
207 space_srv_e(hitclass(class)) = space_srv_e(hitclass(class)) + 1;
208 i=h;
209 j = posk - sum(m(1:i-1));
210 switch replacement_id
211 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO, ReplacementStrategy.SFIFO}
212 space_srv_k = [space_srv_k; space_srv_e];
213 space_var_k = [space_var_k; var];
214 if isSimulation
215 outrate(end+1,1) = GlobalConstants.Immediate;
216 outprob(end+1,1) = p(k);
217 else
218 outrate(end+1,1) = p(k) * GlobalConstants.Immediate;
219 end
220 case ReplacementStrategy.LRU
221 varp = var;
222 varp(cpos(h,2):cpos(h,j)) = var(cpos(h,1):cpos(h,j-1));
223 varp(cpos(h,1)) = var(cpos(h,j));
224 space_srv_k = [space_srv_k; space_srv_e];
225 space_var_k = [space_var_k; varp];
226 if isSimulation
227 outrate(end+1,1) = GlobalConstants.Immediate;
228 outprob(end+1,1) = p(k);
229 else
230 outrate(end+1,1) = p(k) * GlobalConstants.Immediate;
231 end
232 end
233 end
234 end
235 end
236 % if state is unchanged, still add with rate 0
237 outspace = [space_srv_k, space_var_k];
238 end
239 end
240end
241 function pos = cpos(i,j)
242 % POS = CPOS(I,J)
243
244 pos = sum(m(1:i-1)) + j;
245 end
246
247end
248