view AnimationMaster.c @ 269:e6a68e7ea63f

Removed the extra level of core controller -- now only one anim slot and master called after every work unit
author Sean Halle <seanhalle@yahoo.com>
date Mon, 14 Jan 2013 16:10:37 -0800
parents e5bd470b562b
children 292393c6bef1
line source
1 /*
2 * Copyright 2010 OpenSourceResearchInstitute
3 *
4 * Licensed under BSD
5 */
9 #include <stdio.h>
10 #include <stddef.h>
12 #include "PR.h"
13 #include "VSs_impl/VSs.h"
15 /*
16 void PRHandle_CreateTask_SL(SlaveVP *slave);
18 void PRHandle_CreateSlave_SL(SlaveVP *slave);
19 void PRHandle_Dissipate_SL(SlaveVP *slave);
20 void PR_int__handle_PRServiceReq_SL(SlaveVP *slave);
21 */
22 inline void PRHandle_CreateTask( PRReqst *req, SlaveVP *slave );
23 inline void PRHandle_EndTask( PRReqst *req, SlaveVP *slave );
24 inline void PRHandle_CreateSlave(PRReqst *req, SlaveVP *slave );
25 void PRHandle_EndSlave( PRReqst *req, SlaveVP *slave );
28 //inline void masterFunction_SingleLang( PRLangEnv *protoLangEnv, AnimSlot *slot );
29 inline void masterFunction( AnimSlot *slot );
30 inline PRProcess * pickAProcess( AnimSlot *slot );
31 inline SlaveVP * assignWork( PRProcess *process, AnimSlot *slot );
33 /*The animationMaster embodies most of the animator of the language. The
34 * animator is what emodies the behavior of language constructs.
35 * As such, it is the animationMaster, in combination with the plugin
36 * functions, that make the language constructs do their behavior.
37 *
38 *Within the code, this is the top-level-function of the masterVPs, and
39 * runs when the coreController has no more slave VPs. It's job is to
40 * refill the animation slots with slaves that have work.
41 *
42 *There are multiple versions of the master, each tuned to a specific
43 * combination of modes. This keeps the master simple, with reduced overhead,
44 * when the application is not using the extra complexity.
45 *
46 *As of Sept 2012, the versions available will be:
47 * 1) Single langauge, which only exposes slaves (such as SSR or Vthread)
48 * 2) Single language, which only exposes tasks (such as pure dataflow)
49 * 3) Single language, which exposes both (like Cilk, StarSs, and OpenMP)
50 * 4) Multi-language, which always assumes both tasks and slaves
51 * 5) Multi-language and multi-process, which also assumes both tasks and slaves
52 *
53 *
54 *
55 */
57 //This version of the master selects one of three loops, depending upon
58 // whether stand-alone single language (just slaves), or standalone with
59 // tasks, or multi-lang (implies multi-process)
60 void animationMaster( void *_environment, SlaveVP *masterVP )
61 {
62 TopEnv *masterEnv = (TopEnv *)_environment;
63 int32 slotIdx;
64 AnimSlot *currSlot;
65 //Used while scanning and filling animation slots
66 AnimSlot **animSlots;
68 //Local copies, for performance
69 int32 thisCoresIdx;
71 //======================== Initializations ========================
72 thisCoresIdx = masterVP->coreAnimatedBy;
73 animSlots = masterEnv->allAnimSlots[thisCoresIdx];
75 HOLISTIC__Insert_Master_Global_Vars;
77 //======================== animationMaster ========================
78 //Have three different modes, and the master behavior is different for
79 // each, so jump to the loop that corresponds to the mode.
80 //
81 while(1)
82 { MEAS__Capture_Pre_Master_Point
83 for( slotIdx = 0; slotIdx < NUM_ANIM_SLOTS; slotIdx++)
84 {
85 currSlot = animSlots[ slotIdx ];
87 masterFunction( currSlot );
88 }
89 MEAS__Capture_Post_Master_Point;
90 masterSwitchToCoreCtlr( masterVP ); //returns when ctlr switches back to master
91 flushRegisters();
92 }
93 }
96 inline
97 void
98 masterFunction( AnimSlot *slot )
99 { //Scan the animation slots
100 int32 magicNumber;
101 SlaveVP *slave;
102 PRLangEnv *langEnv;
103 PRReqst *req;
104 PRProcess *process;
106 //Check if newly-done slave in slot, which will need request handled
107 if( slot->workIsDone )
108 { slot->workIsDone = FALSE;
109 slot->needsWorkAssigned = TRUE;
111 HOLISTIC__Record_AppResponder_start; //TODO: update to check which process for each slot
112 MEAS__startReqHdlr;
115 //process the request made by the slave (held inside slave struc)
116 slave = slot->slaveAssignedToSlot;
117 req = slave->request;
119 //If the requesting slave is a slot slave, and request is not
120 // task-end, then turn it into a free task slave.
121 if( slave->typeOfVP == SlotTaskSlv && req->reqType != TaskEnd )
122 PR_int__replace_with_new_slot_slv( slave );
124 //Handle task create and end first -- they're special cases..
125 switch( req->reqType )
126 { case TaskEnd:
127 { //do PR handler, which calls lang's hdlr and does recycle of
128 // free task slave if needed -- PR handler checks for free task Slv
129 PRHandle_EndTask( req, slave ); break;
130 }
131 case TaskCreate:
132 { //Do PR's create-task handler, which calls the lang's hdlr
133 // PR handler checks for free task Slv
134 PRHandle_CreateTask( req, slave ); break;
135 }
136 case SlvCreate: PRHandle_CreateSlave( req, slave ); break;
137 case SlvDissipate: PRHandle_EndSlave( req, slave ); break;
138 case Service: PR_int__handle_PRServiceReq( slave ); break; //resumes into Service lang env
139 case Hardware: //for future expansion
140 case IO: //for future expansion
141 case OSCall: //for future expansion
142 PR_int__throw_exception("Not implemented", slave, NULL); break;
143 case Language: //normal lang request
144 { magicNumber = req->langMagicNumber;
145 langEnv = PR_PI__give_lang_env_for( slave, magicNumber );
146 (*req->handler)( req->langReq, slave, langEnv );
147 }
148 }
150 MEAS__endReqHdlr;
151 HOLISTIC__Record_AppResponder_end;
152 } //if have request to be handled
154 if( slot->needsWorkAssigned )
155 {
156 HOLISTIC__Record_Assigner_start;
158 //Pick a process to get this slot
159 process = pickAProcess( slot );
161 //Scan lang environs, looking for langEnv with ready work.
162 // call the Assigner for that lang Env, to get a slave for the slot
163 assignWork( process, slot );
165 HOLISTIC__Record_Assigner_end;
166 }//if slot needs slave assigned
167 }
169 /*When several processes exist, use some pattern for picking one to give
170 * the animation slot to.
171 *First, it has to be a process that has work available.
172 *For now, just do a round-robin
173 */
174 inline
175 PRProcess *
176 pickAProcess( AnimSlot *slot )
177 { int32 idx;
178 PRProcess *process;
180 for( idx = _PRTopEnv->currProcessIdx; idx < _PRTopEnv->numProcesses; idx++)
181 {
182 process = _PRTopEnv->processes[ idx ];
183 if( process->numEnvsWithWork != 0 )
184 { _PRTopEnv->currProcessIdx = idx;
185 return process;
186 }
187 }
188 for( idx = 0; idx < _PRTopEnv->currProcessIdx; idx++)
189 {
190 process = _PRTopEnv->processes[ idx ];
191 if( process->numEnvsWithWork != 0 )
192 { _PRTopEnv->currProcessIdx = idx;
193 return process;
194 }
195 }
196 //none found
197 return NULL;
198 }
200 /*This does:
201 * 1) searches the language environments for one with work ready
202 * if finds one, asks its assigner to return work
203 * 2) checks what kind of work: new task, resuming task, resuming slave
204 * if new task, gets the slot slave and assigns task to it and returns slave
205 * else, gets the slave attached to the metaTask and returns that.
206 * 3) if no work found, then prune former task slaves waiting to be recycled.
207 * If no work and no slaves to prune, check for shutdown conditions.
208 *
209 * language env keeps its own work in its own structures, and has its own
210 * assigner. It chooses
211 * However, include a switch that switches-in an override assigner, which
212 * sees all the work in all the language env's. This is most likely
213 * generated by static tools and included in the executable. That means it
214 * has to be called via a registered pointer from here. The idea is that
215 * the static tools know which languages are grouped together.. and the
216 * override enables them to generate a custom assigner that uses info from
217 * all the languages in a unified way.. Don't really expect this to happen,
218 * but am making it possible.
219 */
220 inline
221 SlaveVP *
222 assignWork( PRProcess *process, AnimSlot *slot )
223 { SlaveVP *returnSlv;
224 int32 coreNum, slotNum;
225 PRMetaTask *assignedMetaTask;
227 coreNum = slot->coreSlotIsOn;
229 if( process->overrideAssigner != NULL )
230 { if( process->numEnvsWithWork != 0 )
231 { (*process->overrideAssigner)( process, slot ); //calls PR fn that inserts work into slot
232 goto ReturnAfterAssigningWork; //quit for-loop, cause found work
233 }
234 else
235 goto NoWork;
236 }
238 //If here, then no override assigner, so search language envs for work
239 int32 envIdx, numEnvs; PRLangEnv **langEnvsList, *langEnv;
240 langEnvsList = process->langEnvsList;
241 numEnvs = process->numLangEnvs;
242 for( envIdx = 0; envIdx < numEnvs; envIdx++ ) //keep langEnvs in hash & array
243 { langEnv = langEnvsList[envIdx];
244 if( langEnv->hasWork )
245 { (*langEnv->slaveAssigner)( langEnv, slot ); //assigner calls PR to put slave/task into slot
246 goto ReturnAfterAssigningWork; //quit for-loop, cause found work
247 //NOTE: bad search alg -- should start where left off, then wrap around
248 }
249 }
250 //If reach here, then have searched all langEnv's & none have work..
252 NoWork: //No work, if end up here..
253 {
254 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
255 returnSlv = process->idleSlv[coreNum][slotNum];
257 //things that would normally happen in resume(), but idle VPs
258 // never go there
259 returnSlv->numTimesAssignedToASlot++; //gives each idle unit a unique ID
260 Unit newU;
261 newU.vp = returnSlv->slaveNum;
262 newU.task = returnSlv->numTimesAssignedToASlot;
263 addToListOfArrays(Unit,newU,process->unitList);
265 if (returnSlv->numTimesAssignedToASlot > 1) //make a dependency from prev idle unit
266 { Dependency newD; // to this one
267 newD.from_vp = returnSlv->slaveNum;
268 newD.from_task = returnSlv->numTimesAssignedToASlot - 1;
269 newD.to_vp = returnSlv->slaveNum;
270 newD.to_task = returnSlv->numTimesAssignedToASlot;
271 addToListOfArrays(Dependency, newD ,process->ctlDependenciesList);
272 }
273 #endif
274 HOLISTIC__Record_Assigner_end;
275 return FALSE;
276 }
278 ReturnAfterAssigningWork: //All paths goto here.. to provide single point for holistic..
279 {
280 HOLISTIC__Record_Assigner_end;
281 return TRUE;
282 }
283 }
287 /*This is first thing called when creating a slave.. it hands off to the
288 * langlet's creator, then adds updates of its own..
289 *
290 *There's a question of things like lang data, meta tasks, and such..
291 *In creator, only PR related things happen, and things for the langlet whose
292 * creator construct was used.
293 *
294 *Other langlets still get a chance to create langData -- but by registering a
295 * "createLangData" handler in the langEnv. When a construct of the langlet
296 * calls "PR__give_lang_data()", if there is no langData for that langlet,
297 * the PR will call the creator in the langlet's langEnv, place whatever it
298 * makes as the langData in that slave for that langlet, and return that langData
299 *
300 *So, as far as counting things, a langlet is only allowed to count creation
301 * of slaves it creates itself.. may have to change this later.. add a way for
302 * langlet to register a trigger Fn called each time a slave gets created..
303 * need more experience with what langlets will do at create time.. think Cilk
304 * has interesting create behavior.. not sure how that will differ in light
305 * of true tasks and langlet approach. Look at it after all done and start
306 * modifying the langs to be langlets..
307 *
308 *PR itself needs to create the slave, then update numLiveSlaves in process,
309 * copy processID from requestor to newly created
310 */
311 inline
312 void
313 PRHandle_CreateSlave( PRReqst *req, SlaveVP *slave )
314 { SlaveVP *newSlv;
315 PRProcess *process;
316 PRLangEnv *protoLangEnv;
318 process = slave->processSlaveIsIn;
319 protoLangEnv = PR_int__give_proto_lang_env_for_slave__ML( slave, req->langMagicNumber );
321 // newSlv = PR_int__create_slave( req->topLevelFn, req->initData );
323 //create slv has diff prototype than standard reqst hdlr
324 newSlv =
325 (*req->createHdlr)(req->langReq, slave, PR_int__give_lang_env(protoLangEnv));
327 newSlv->typeOfVP = GenericSlv;
328 newSlv->processSlaveIsIn = process;
329 newSlv->ID = req->ID;
330 process->numLiveGenericSlvs += 1;
331 }
333 /*The dissipate handler has to, update the number of slaves of the type, within
334 * the process, and call the langlet handler linked into the request,
335 * and after that returns, then call the PR function that frees the slave state
336 * (or recycles the slave).
337 *
338 *The PR function that frees the slave state has to also free all of the
339 * langData in the slave.. or else reset all of the langDatas.. by, say, marking
340 * them, then in PR__give_langData( magicNum ) call the langlet registered
341 * "resetLangData" Fn.
342 */
343 inline
344 void
345 PRHandle_EndSlave( PRReqst *req, SlaveVP *slave )
346 { PRProcess *process;
347 PRLangEnv *protoLangEnv;
349 process = slave->processSlaveIsIn;
351 //do the language's dissipate handler
352 protoLangEnv = PR_int__give_proto_lang_env_for_slave__ML( slave, slave->request->langMagicNumber );
354 if(req->handler != NULL)
355 (*req->handler)( req->langReq, slave, PR_int__give_lang_env(protoLangEnv) );
357 process->numLiveGenericSlvs -= 1;
358 PR_int__recycle_slave__ML( slave );
360 //check End Of Process Condition
361 if( process->numLiveTasks == 0 &&
362 process->numLiveGenericSlvs == 0 )
363 PR_SS__shutdown_process__ML( process );
364 }
366 /*Create task is a special form, that has PR behavior in addition to plugin
367 * behavior. Master calls this first, and it then calls the plugin's
368 * create task handler.
369 *
370 *Note: the requesting slave must be either generic slave or free task slave
371 */
372 inline
373 void
374 PRHandle_CreateTask( PRReqst *req, SlaveVP *slave )
375 { PRMetaTask *metaTask;
376 PRProcess *process;
377 PRLangEnv *protoLangEnv;
378 void *task;
380 process = slave->processSlaveIsIn;
382 protoLangEnv = PR_int__give_proto_lang_env_for_slave__ML( slave,
383 req->langMagicNumber );
385 //Do the langlet's create-task handler, which keeps the task
386 // inside the langlet's lang env, but returns the langMetaTask
387 // so PR can put stuff into the prolog
388 task =
389 (*req->createHdlr)(req->langReq, slave, PR_int__give_lang_env(protoLangEnv) );
390 metaTask = PR_int__give_prolog_of_task( task );
391 metaTask->ID = req->ID; //may be NULL
392 metaTask->topLevelFn = req->topLevelFn;
393 metaTask->initData = req->initData;
395 process->numLiveTasks += 1;
397 return;
398 }
400 /*When a task ends, are two scenarios: 1) task ran to completion, or 2) task
401 * suspended at some point in its code.
402 *For 1, just decr count of live tasks (and check for end condition) -- the
403 * master loop will decide what goes into the slot freed up by this task end,
404 * so, here, don't worry about assigning a new task to the slot slave.
405 *For 2, the task's slot slave has been converted to a free task slave, which
406 * now has nothing more to do, so send it to the recycle Q (which includes
407 * freeing all the langData and meta task structs alloc'd for it). Then
408 * decrement the live task count and check end condition.
409 *
410 *PR has to update count of live tasks, and check end of process condition.
411 * The "main" can invoke constructs that wait for a process to end, so when
412 * end detected, have to resume what's waiting..
413 *Thing is, that wait involves the main OS thread. That means
414 * PR internals have to do OS thread signaling. Want to do that in the
415 * core controller, which has the original stack of an OS thread. So the
416 * end process handling happens in the core controller.
417 *
418 *So here, when detect process end, signal to the core controller, which will
419 * then do the condition variable notify to the OS thread that's waiting.
420 *
421 *Note: slave may be either a slot slave or a free task slave.
422 */
423 inline
424 void
425 PRHandle_EndTask( PRReqst *req, SlaveVP *requestingSlv )
426 { void *langEnv;
427 PRProcess *process;
428 void *langMetaTask;
430 langEnv = PR_int__give_lang_env_of_req__ML( req, requestingSlv ); //magic num in req
431 langMetaTask = PR_int__give_lang_meta_task_from_slave__ML( requestingSlv, req->langMagicNumber);
433 //Do the langlet's request handler
434 //Want to keep PR structs hidden from plugin, so extract langReq..
435 (*req->handler)( req->langReq, requestingSlv, langEnv );
437 //Now that the langlet's done with it, recycle the slave if it's a freeTaskSlv
438 if( requestingSlv->typeOfVP == FreeTaskSlv )
439 PR_int__recycle_slave__ML( requestingSlv );
441 process->numLiveTasks -= 1;
443 //check End Of Process Condition
444 if( process->numLiveTasks == 0 &&
445 process->numLiveGenericSlvs == 0 )
446 { //Tell the core controller to do wakeup of any waiting OS thread
447 PR_SS__shutdown_process__ML( process );
448 }
449 }