Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view MasterLoop.c @ 55:3bac84e4e56e
Works with correct matrix mult Nov 4 -- switch animators macros, many updates
Changed all queues back to VMSQ variants #defines
correct, protected, work-stealing, with compiler switch in and out
| author | Me |
|---|---|
| date | Thu, 04 Nov 2010 18:13:18 -0700 |
| parents | 42dd44df1bb0 |
| children | 984f7d78bfdf dd3e60aeae26 |
line source
1 /*
2 * Copyright 2010 OpenSourceStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
9 #include <stdio.h>
10 #include <stddef.h>
12 #include "VMS.h"
15 //===========================================================================
16 void inline
17 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
18 VirtProcr *masterPr );
20 //===========================================================================
24 /*This code is animated by the virtual Master processor.
25 *
26 *Polls each sched slot exactly once, hands any requests made by a newly
27 * done slave to the "request handler" plug-in function
28 *
29 *Any slots that need a virt procr assigned are given to the "schedule"
30 * plug-in function, which tries to assign a virt procr (slave) to it.
31 *
32 *When all slots needing a processor have been given to the schedule plug-in,
33 * a fraction of the procrs successfully scheduled are put into the
34 * work queue, then a continuation of this function is put in, then the rest
35 * of the virt procrs that were successfully scheduled.
36 *
37 *The first thing the continuation does is busy-wait until the previous
38 * animation completes. This is because an (unlikely) continuation may
39 * sneak through queue before previous continuation is done putting second
40 * part of scheduled slaves in, which is the only race condition.
41 *
42 */
44 /*May 29, 2010 -- birth a Master during init so that first core loop to
45 * start running gets it and does all the stuff for a newly born --
46 * from then on, will be doing continuation, but do suspension self
47 * directly at end of master loop
48 *So VMS__init just births the master virtual processor same way it births
49 * all the others -- then does any extra setup needed and puts it into the
50 * work queue.
51 *However means have to make masterEnv a global static volatile the same way
52 * did with readyToAnimateQ in core loop. -- for performance, put the
53 * jump to the core loop directly in here, and have it directly jump back.
54 *
55 *
56 *Aug 18, 2010 -- Going to a separate MasterVP for each core, to see if this
57 * avoids the suspected bug in the system stack that causes bizarre faults
58 * at random places in the system code.
59 *
60 *So, this function is coupled to each of the MasterVPs, -- meaning this
61 * function can't rely on a particular stack and frame -- each MasterVP that
62 * animates this function has a different one.
63 *
64 *At this point, the masterLoop does not write itself into the queue anymore,
65 * instead, the coreLoop acquires the masterLock when it has nothing to
66 * animate, and then animates its own masterLoop. However, still try to put
67 * several AppVPs into the queue to amortize the startup cost of switching
68 * to the MasterVP. Note, don't have to worry about latency of requests much
69 * because most requests generate work for same core -- only latency issue
70 * is case when other cores starved and one core's requests generate work
71 * for them -- so keep max in queue to 3 or 4..
72 */
73 void masterLoop( void *initData, VirtProcr *animatingPr )
74 {
75 int32 slotIdx, numSlotsFilled;
76 VirtProcr *schedVirtPr;
77 SchedSlot *currSlot, **schedSlots;
78 MasterEnv *masterEnv;
79 VMSQueueStruc *readyToAnimateQ;
81 SlaveScheduler slaveScheduler;
82 RequestHandler requestHandler;
83 void *semanticEnv;
85 int32 thisCoresIdx;
86 VirtProcr *masterPr;
87 volatile VirtProcr *volatileMasterPr;
89 volatileMasterPr = animatingPr;
90 masterPr = volatileMasterPr; //used to force re-define after jmp
92 //First animation of each MasterVP will in turn animate this part
93 // of setup code.. (VP creator sets up the stack as if this function
94 // was called normally, but actually get here by jmp)
95 //So, setup values about stack ptr, jmp pt and all that
96 masterPr->nextInstrPt = &&masterLoopStartPt;
99 //Note, got rid of writing the stack and frame ptr up here, because
100 // only one
101 // core can ever animate a given MasterVP, so don't need to communicate
102 // new frame and stack ptr to the MasterVP storage before a second
103 // version of that MasterVP can get animated on a different core.
104 //Also got rid of the busy-wait.
107 masterLoopStartPt:
108 //============================= MEASUREMENT STUFF ========================
109 #ifdef MEAS__TIME_MASTER
110 //Total Master time includes one coreloop time -- just assume the core
111 // loop time is same for Master as for AppVPs, even though it will be
112 // smaller due to high predictability of the fixed jmp.
113 saveLowTimeStampCountInto( masterPr->startMasterTSCLow );
114 #endif
115 //========================================================================
117 masterEnv = _VMSMasterEnv;
119 //GCC may optimize so doesn't always re-define from frame-storage
120 masterPr = volatileMasterPr; //just to make sure after jmp
121 thisCoresIdx = masterPr->coreAnimatedBy;
122 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx];
123 schedSlots = masterEnv->allSchedSlots[thisCoresIdx];
125 requestHandler = masterEnv->requestHandler;
126 slaveScheduler = masterEnv->slaveScheduler;
127 semanticEnv = masterEnv->semanticEnv;
130 //Poll each slot's Done flag
131 numSlotsFilled = 0;
132 for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++)
133 {
134 currSlot = schedSlots[ slotIdx ];
136 if( currSlot->workIsDone )
137 {
138 currSlot->workIsDone = FALSE;
139 currSlot->needsProcrAssigned = TRUE;
141 //process requests from slave to master
142 (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv );
143 }
144 if( currSlot->needsProcrAssigned )
145 { //give slot a new virt procr
146 schedVirtPr =
147 (*slaveScheduler)( semanticEnv, thisCoresIdx );
149 if( schedVirtPr != NULL )
150 { currSlot->procrAssignedToSlot = schedVirtPr;
151 schedVirtPr->schedSlot = currSlot;
152 currSlot->needsProcrAssigned = FALSE;
153 numSlotsFilled += 1;
155 writeVMSQ( schedVirtPr, readyToAnimateQ );
156 }
157 }
158 }
161 #ifdef USE_WORK_STEALING
162 //If no slots filled, means no more work, look for work to steal.
163 if( numSlotsFilled == 0 )
164 { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterPr );
165 }
166 #endif
169 #ifdef MEAS__TIME_MASTER
170 saveLowTimeStampCountInto( masterPr->endMasterTSCLow );
171 #endif
174 masterSwitchToCoreLoop( masterPr )
175 }
179 /*This has a race condition -- the coreloops are accessing their own queues
180 * at the same time that this work-stealer on a different core is trying to
181 */
182 void inline
183 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
184 VirtProcr *masterPr )
185 {
186 VirtProcr *stolenPr;
187 int32 coreIdx, i;
188 VMSQueueStruc *currQ;
190 stolenPr = NULL;
191 coreIdx = masterPr->coreAnimatedBy;
192 for( i = 0; i < NUM_CORES -1; i++ )
193 {
194 if( coreIdx >= NUM_CORES -1 )
195 { coreIdx = 0;
196 }
197 else
198 { coreIdx++;
199 }
200 currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
201 if( numInVMSQ( currQ ) > 0 )
202 { stolenPr = readVMSQ (currQ );
203 break;
204 }
205 }
207 if( stolenPr != NULL )
208 { currSlot->procrAssignedToSlot = stolenPr;
209 stolenPr->schedSlot = currSlot;
210 currSlot->needsProcrAssigned = FALSE;
212 writeVMSQ( stolenPr, readyToAnimateQ );
213 }
214 }
216 /*This algorithm makes the common case fast. Make the coreloop passive,
217 * and show its progress. Make the stealer control a gate that coreloop
218 * has to pass.
219 *To avoid interference, only one stealer at a time. Use a global
220 * stealer-lock.
221 *
222 *The pattern is based on a gate -- stealer shuts the gate, then monitors
223 * to be sure any already past make it all the way out, before starting.
224 *So, have a "progress" measure just before the gate, then have two after it,
225 * one is in a "waiting room" outside the gate, the other is at the exit.
226 *Then, the stealer first shuts the gate, then checks the progress measure
227 * outside it, then looks to see if the progress measure at the exit is the
228 * same. If yes, it knows the protected area is empty 'cause no other way
229 * to get in and the last to get in also exited.
230 *If the progress measure at the exit is not the same, then the stealer goes
231 * into a loop checking both the waiting-area and the exit progress-measures
232 * until one of them shows the same as the measure outside the gate. Might
233 * as well re-read the measure outside the gate each go around, just to be
234 * sure. It is guaranteed that one of the two will eventually match the one
235 * outside the gate.
236 *
237 *Here's an informal proof of correctness:
238 *The gate can be closed at any point, and have only four cases:
239 * 1) coreloop made it past the gate-closing but not yet past the exit
240 * 2) coreloop made it past the pre-gate progress update but not yet past
241 * the gate,
242 * 3) coreloop is right before the pre-gate update
243 * 4) coreloop is past the exit and far from the pre-gate update.
244 *
245 * Covering the cases in reverse order,
246 * 4) is not a problem -- stealer will read pre-gate progress, see that it
247 * matches exit progress, and the gate is closed, so stealer can proceed.
248 * 3) stealer will read pre-gate progress just after coreloop updates it..
249 * so stealer goes into a loop until the coreloop causes wait-progress
250 * to match pre-gate progress, so then stealer can proceed
251 * 2) same as 3..
252 * 1) stealer reads pre-gate progress, sees that it's different than exit,
253 * so goes into loop until exit matches pre-gate, now it knows coreloop
254 * is not in protected and cannot get back in, so can proceed.
255 *
256 *Implementation for the stealer:
257 *
258 *First, acquire the stealer lock -- only cores with no work to do will
259 * compete to steal, so not a big performance penalty having only one --
260 * will rarely have multiple stealers in a system with plenty of work -- and
261 * in a system with little work, it doesn't matter.
262 *
263 *Note, have single-reader, single-writer pattern for all variables used to
264 * communicate between stealer and victims
265 *
266 *So, scan the queues of the core loops, until find non-empty. Each core
267 * has its own list that it scans. The list goes in order from closest to
268 * furthest core, so it steals first from close cores. Later can add
269 * taking info from the app about overlapping footprints, and scan all the
270 * others then choose work with the most footprint overlap with the contents
271 * of this core's cache.
272 *
273 *Now, have a victim want to take work from. So, shut the gate in that
274 * coreloop, by setting the "gate closed" var on its stack to TRUE.
275 *Then, read the core's pre-gate progress and compare to the core's exit
276 * progress.
277 *If same, can proceed to take work from the coreloop's queue. When done,
278 * write FALSE to gate closed var.
279 *If different, then enter a loop that reads the pre-gate progress, then
280 * compares to exit progress then to wait progress. When one of two
281 * matches, proceed. Take work from the coreloop's queue. When done,
282 * write FALSE to the gate closed var.
283 *
284 */
285 void inline
286 gateProtected_stealWorkInto( SchedSlot *currSlot,
287 VMSQueueStruc *myReadyToAnimateQ,
288 VirtProcr *masterPr )
289 {
290 VirtProcr *stolenPr;
291 int32 coreIdx, i, haveAVictim, gotLock;
292 VMSQueueStruc *victimsQ;
294 volatile GateStruc *vicGate;
295 int32 coreMightBeInProtected;
299 //see if any other cores have work available to steal
300 haveAVictim = FALSE;
301 coreIdx = masterPr->coreAnimatedBy;
302 for( i = 0; i < NUM_CORES -1; i++ )
303 {
304 if( coreIdx >= NUM_CORES -1 )
305 { coreIdx = 0;
306 }
307 else
308 { coreIdx++;
309 }
310 victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
311 if( numInVMSQ( victimsQ ) > 0 )
312 { haveAVictim = TRUE;
313 vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ];
314 break;
315 }
316 }
317 if( !haveAVictim ) return; //no work to steal, exit
319 //have a victim core, now get the stealer-lock
320 gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock),
321 UNLOCKED, LOCKED );
322 if( !gotLock ) return; //go back to core loop, which will re-start master
325 //====== Start Gate-protection =======
326 vicGate->gateClosed = TRUE;
327 coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress;
328 while( coreMightBeInProtected )
329 { //wait until sure
330 if( vicGate->preGateProgress == vicGate->waitProgress )
331 coreMightBeInProtected = FALSE;
332 if( vicGate->preGateProgress == vicGate->exitProgress )
333 coreMightBeInProtected = FALSE;
334 }
336 stolenPr = readVMSQ ( victimsQ );
338 vicGate->gateClosed = FALSE;
339 //======= End Gate-protection =======
342 if( stolenPr != NULL ) //victim could have been in protected and taken
343 { currSlot->procrAssignedToSlot = stolenPr;
344 stolenPr->schedSlot = currSlot;
345 currSlot->needsProcrAssigned = FALSE;
347 writeVMSQ( stolenPr, myReadyToAnimateQ );
348 }
350 //unlock the work stealing lock
351 _VMSMasterEnv->workStealingLock = UNLOCKED;
352 }
