Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VSs_impls > VSs__MC_shared_impl
view VSs.c @ 19:58a71af04cd1
version for Tamer
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Mon, 17 Sep 2012 11:07:07 +0200 |
| parents | f83fff8bd4b2 |
| children | a7ca8f45c1c4 |
line source
1 /*
2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <malloc.h>
11 #include "Queue_impl/PrivateQueue.h"
12 #include "Hash_impl/PrivateHash.h"
14 #include "VSs.h"
15 #include "Measurement/VSs_Counter_Recording.h"
17 //==========================================================================
19 void
20 VSs__init();
22 void
23 VSs__init_Helper();
24 //==========================================================================
28 //===========================================================================
31 /*These are the library functions *called in the application*
32 *
33 *There's a pattern for the outside sequential code to interact with the
34 * VMS_HW code.
35 *The VMS_HW system is inside a boundary.. every VSs system is in its
36 * own directory that contains the functions for each of the processor types.
37 * One of the processor types is the "seed" processor that starts the
38 * cascade of creating all the processors that do the work.
39 *So, in the directory is a file called "EntryPoint.c" that contains the
40 * function, named appropriately to the work performed, that the outside
41 * sequential code calls. This function follows a pattern:
42 *1) it calls VSs__init()
43 *2) it creates the initial data for the seed processor, which is passed
44 * in to the function
45 *3) it creates the seed VSs processor, with the data to start it with.
46 *4) it calls startVSsThenWaitUntilWorkDone
47 *5) it gets the returnValue from the transfer struc and returns that
48 * from the function
49 *
50 *For now, a new VSs system has to be created via VSs__init every
51 * time an entry point function is called -- later, might add letting the
52 * VSs system be created once, and let all the entry points just reuse
53 * it -- want to be as simple as possible now, and see by using what makes
54 * sense for later..
55 */
59 //===========================================================================
61 /*This is the "border crossing" function -- the thing that crosses from the
62 * outside world, into the VMS_HW world. It initializes and starts up the
63 * VMS system, then creates one processor from the specified function and
64 * puts it into the readyQ. From that point, that one function is resp.
65 * for creating all the other processors, that then create others, and so
66 * forth.
67 *When all the processors, including the seed, have dissipated, then this
68 * function returns. The results will have been written by side-effect via
69 * pointers read from, or written into initData.
70 *
71 *NOTE: no Threads should exist in the outside program that might touch
72 * any of the data reachable from initData passed in to here
73 */
74 void
75 VSs__create_seed_slave_and_do_work( TopLevelFnPtr fnPtr, void *initData )
76 { VSsSemEnv *semEnv;
77 SlaveVP *seedSlv;
78 VSsSemData *semData;
79 VSsTaskStub *threadTaskStub, *parentTaskStub;
81 VSs__init(); //normal multi-thd
83 semEnv = _VMSMasterEnv->semanticEnv;
85 //VSs starts with one processor, which is put into initial environ,
86 // and which then calls create() to create more, thereby expanding work
87 seedSlv = VSs__create_slave_helper( fnPtr, initData,
88 semEnv, semEnv->nextCoreToGetNewSlv++ );
90 //seed slave is a thread slave, so make a thread's task stub for it
91 // and then make another to stand for the seed's parent task. Make
92 // the parent be already ended, and have one child (the seed). This
93 // will make the dissipate handler do the right thing when the seed
94 // is dissipated.
95 threadTaskStub = create_thread_task_stub( initData );
96 parentTaskStub = create_thread_task_stub( NULL );
97 parentTaskStub->isEnded = TRUE;
98 parentTaskStub->numLiveChildThreads = 1; //so dissipate works for seed
99 threadTaskStub->parentTaskStub = parentTaskStub;
100 threadTaskStub->slaveAssignedTo = seedSlv;
102 semData = (VSsSemData *)seedSlv->semanticData;
103 //seedVP is a thread, so has a permanent task
104 semData->needsTaskAssigned = FALSE;
105 semData->taskStub = threadTaskStub;
106 semData->slaveType = ThreadSlv;
108 resume_slaveVP( seedSlv, semEnv ); //returns right away, just queues Slv
110 VMS_SS__start_the_work_then_wait_until_done(); //normal multi-thd
112 VSs__cleanup_after_shutdown();
113 }
116 int32
117 VSs__giveMinWorkUnitCycles( float32 percentOverhead )
118 {
119 return MIN_WORK_UNIT_CYCLES;
120 }
122 int32
123 VSs__giveIdealNumWorkUnits()
124 {
125 return NUM_ANIM_SLOTS * NUM_CORES;
126 }
128 int32
129 VSs__give_number_of_cores_to_schedule_onto()
130 {
131 return NUM_CORES;
132 }
134 /*For now, use TSC -- later, make these two macros with assembly that first
135 * saves jump point, and second jumps back several times to get reliable time
136 */
137 void
138 VSs__start_primitive()
139 { saveLowTimeStampCountInto( ((VSsSemEnv *)(_VMSMasterEnv->semanticEnv))->
140 primitiveStartTime );
141 }
143 /*Just quick and dirty for now -- make reliable later
144 * will want this to jump back several times -- to be sure cache is warm
145 * because don't want comm time included in calc-time measurement -- and
146 * also to throw out any "weird" values due to OS interrupt or TSC rollover
147 */
148 int32
149 VSs__end_primitive_and_give_cycles()
150 { int32 endTime, startTime;
151 //TODO: fix by repeating time-measurement
152 saveLowTimeStampCountInto( endTime );
153 startTime =((VSsSemEnv*)(_VMSMasterEnv->semanticEnv))->primitiveStartTime;
154 return (endTime - startTime);
155 }
157 //===========================================================================
159 /*Initializes all the data-structures for a VSs system -- but doesn't
160 * start it running yet!
161 *
162 *This runs in the main thread -- before VMS starts up
163 *
164 *This sets up the semantic layer over the VMS system
165 *
166 *First, calls VMS_Setup, then creates own environment, making it ready
167 * for creating the seed processor and then starting the work.
168 */
169 void
170 VSs__init()
171 {
172 VMS_SS__init();
173 //masterEnv, a global var, now is partially set up by init_VMS
174 // after this, have VMS_int__malloc and VMS_int__free available
176 VSs__init_Helper();
177 }
180 void idle_fn(void* data, SlaveVP *animatingSlv){
181 while(1){
182 VMS_int__suspend_slaveVP_and_send_req(animatingSlv);
183 }
184 }
186 void
187 VSs__init_Helper()
188 { VSsSemEnv *semanticEnv;
189 int32 i, coreNum, slotNum;
190 VSsSemData *semData;
192 //Hook up the semantic layer's plug-ins to the Master virt procr
193 _VMSMasterEnv->requestHandler = &VSs__Request_Handler;
194 _VMSMasterEnv->slaveAssigner = &VSs__assign_slaveVP_to_slot;
196 //create the semantic layer's environment (all its data) and add to
197 // the master environment
198 semanticEnv = VMS_int__malloc( sizeof( VSsSemEnv ) );
199 _VMSMasterEnv->semanticEnv = semanticEnv;
201 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
202 _VMSMasterEnv->counterHandler = &VSs__counter_handler;
203 VSs__init_counter_data_structs();
204 #endif
206 semanticEnv->shutdownInitiated = FALSE;
207 semanticEnv->coreIsDone = VMS_int__malloc( NUM_CORES * sizeof( bool32 ) );
208 //For each animation slot, there is an idle slave, and an initial
209 // slave assigned as the current-task-slave. Create them here.
210 SlaveVP *idleSlv, *slotTaskSlv;
211 for( coreNum = 0; coreNum < NUM_CORES; coreNum++ )
212 { semanticEnv->coreIsDone[coreNum] = FALSE; //use during shutdown
214 for( slotNum = 0; slotNum < NUM_ANIM_SLOTS; ++slotNum )
215 { idleSlv = VSs__create_slave_helper( &idle_fn, NULL, semanticEnv, 0);
216 idleSlv->coreAnimatedBy = coreNum;
217 idleSlv->animSlotAssignedTo =
218 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
219 semanticEnv->idleSlv[coreNum][slotNum] = idleSlv;
221 slotTaskSlv = VSs__create_slave_helper( &idle_fn, NULL, semanticEnv, 0);
222 slotTaskSlv->coreAnimatedBy = coreNum;
223 slotTaskSlv->animSlotAssignedTo =
224 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
226 semData = slotTaskSlv->semanticData;
227 semData->needsTaskAssigned = TRUE;
228 semData->slaveType = SlotTaskSlv;
229 semanticEnv->slotTaskSlvs[coreNum][slotNum] = slotTaskSlv;
230 }
231 }
233 //create the ready queues, hash tables used for matching and so forth
234 semanticEnv->slavesReadyToResumeQ = makeVMSQ();
235 semanticEnv->freeExtraTaskSlvQ = makeVMSQ();
236 semanticEnv->taskReadyQ = makeVMSQ();
238 semanticEnv->argPtrHashTbl = makeHashTable32( 16, &VMS_int__free );
239 semanticEnv->commHashTbl = makeHashTable32( 16, &VMS_int__free );
241 semanticEnv->nextCoreToGetNewSlv = 0;
243 #ifdef EXTERNAL_SCHEDULER
244 VSs__init_ext_scheduler();
245 #endif
246 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
247 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
248 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
249 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
250 {
251 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
252 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
253 semanticEnv->fnSingletons[i].hasFinished = FALSE;
254 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
255 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
256 }
258 semanticEnv->numLiveExtraTaskSlvs = 0; //must be last
259 semanticEnv->numLiveThreadSlvs = 1; //must be last, counts the seed
261 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
262 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
263 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
264 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
265 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
266 semanticEnv->dataDependenciesList = makeListOfArrays(sizeof(Dependency),128);
267 semanticEnv->singletonDependenciesList = makeListOfArrays(sizeof(Dependency),128);
268 semanticEnv->warDependenciesList = makeListOfArrays(sizeof(Dependency),128);
269 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
271 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
272 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
273 #endif
274 }
277 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
278 */
279 void
280 VSs__cleanup_after_shutdown()
281 { VSsSemEnv *semanticEnv;
283 semanticEnv = _VMSMasterEnv->semanticEnv;
284 FILE* output;
285 int n;
286 char filename[255];
287 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
288 //UCC
289 for(n=0;n<255;n++)
290 {
291 sprintf(filename, "./counters/UCC.%d",n);
292 output = fopen(filename,"r");
293 if(output)
294 {
295 fclose(output);
296 }else{
297 break;
298 }
299 }
300 if(n<255){
301 printf("Saving UCC to File: %s ...\n", filename);
302 output = fopen(filename,"w+");
303 if(output!=NULL){
304 set_dependency_file(output);
305 //fprintf(output,"digraph Dependencies {\n");
306 //set_dot_file(output);
307 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
308 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
309 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
310 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
311 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
312 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
313 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
314 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
315 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
316 //fprintf(output,"}\n");
317 fflush(output);
319 } else
320 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
321 } else {
322 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
323 }
324 //Loop Graph
325 for(n=0;n<255;n++)
326 {
327 sprintf(filename, "./counters/LoopGraph.%d",n);
328 output = fopen(filename,"r");
329 if(output)
330 {
331 fclose(output);
332 }else{
333 break;
334 }
335 }
336 if(n<255){
337 printf("Saving LoopGraph to File: %s ...\n", filename);
338 output = fopen(filename,"w+");
339 if(output!=NULL){
340 set_dependency_file(output);
341 //fprintf(output,"digraph Dependencies {\n");
342 //set_dot_file(output);
343 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
344 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
345 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
346 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
347 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
348 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
349 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
350 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
351 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
352 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
353 //fprintf(output,"}\n");
354 fflush(output);
356 } else
357 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
358 } else {
359 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
360 }
363 freeListOfArrays(semanticEnv->unitList);
364 freeListOfArrays(semanticEnv->commDependenciesList);
365 freeListOfArrays(semanticEnv->ctlDependenciesList);
366 freeListOfArrays(semanticEnv->dynDependenciesList);
367 freeListOfArrays(semanticEnv->dataDependenciesList);
368 freeListOfArrays(semanticEnv->warDependenciesList);
369 freeListOfArrays(semanticEnv->singletonDependenciesList);
370 freeListOfArrays(semanticEnv->hwArcs);
372 #endif
373 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
374 for(n=0;n<255;n++)
375 {
376 sprintf(filename, "./counters/Counters.%d.csv",n);
377 output = fopen(filename,"r");
378 if(output)
379 {
380 fclose(output);
381 }else{
382 break;
383 }
384 }
385 if(n<255){
386 printf("Saving Counter measurements to File: %s ...\n", filename);
387 output = fopen(filename,"w+");
388 if(output!=NULL){
389 set_counter_file(output);
390 int i;
391 for(i=0;i<NUM_CORES;i++){
392 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
393 fflush(output);
394 }
396 } else
397 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
398 } else {
399 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
400 }
402 #endif
403 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
404 * nothing to do here
407 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
408 {
409 VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
410 VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
411 }
412 VMS_int__free( semanticEnv->readyVPQs );
414 freeHashTable( semanticEnv->commHashTbl );
415 VMS_int__free( _VMSMasterEnv->semanticEnv );
416 */
417 VMS_SS__cleanup_at_end_of_shutdown();
418 }
421 //===========================================================================
423 SlaveVP *
424 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
425 SlaveVP *creatingThd )
426 { VSsSemReq reqData;
428 //the semantic request data is on the stack and disappears when this
429 // call returns -- it's guaranteed to remain in the VP's stack for as
430 // long as the VP is suspended.
431 reqData.reqType = 0; //know type because in a VMS create req
432 reqData.fnPtr = fnPtr;
433 reqData.initData = initData;
434 reqData.callingSlv = creatingThd;
436 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
438 return creatingThd->dataRetFromReq;
439 }
441 /*This is always the last thing done in the code animated by a thread VP.
442 * Normally, this would be the last line of the thread's top level function.
443 * But, if the thread exits from any point, it has to do so by calling
444 * this.
445 *
446 *It simply sends a dissipate request, which handles all the state cleanup.
447 */
448 void
449 VSs__end_thread( SlaveVP *thdToEnd )
450 { VSsSemData *semData;
452 VMS_WL__send_dissipate_req( thdToEnd );
453 }
457 //===========================================================================
460 //======================= task submit and end ==============================
461 /*
462 */
463 void
464 VSs__submit_task( VSsTaskType *taskType, void *args, SlaveVP *animSlv)
465 { VSsSemReq reqData;
467 reqData.reqType = submit_task;
469 reqData.taskType = taskType;
470 reqData.args = args;
471 reqData.callingSlv = animSlv;
473 reqData.taskID = NULL;
475 VMS_WL__send_sem_request( &reqData, animSlv );
476 }
478 inline int32 *
479 VSs__create_taskID_of_size( int32 numInts, SlaveVP *animSlv )
480 { int32 *taskID;
482 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
483 taskID[0] = numInts;
484 return taskID;
485 }
487 void
488 VSs__submit_task_with_ID( VSsTaskType *taskType, void *args, int32 *taskID,
489 SlaveVP *animSlv)
490 { VSsSemReq reqData;
492 reqData.reqType = submit_task;
494 reqData.taskType = taskType;
495 reqData.args = args;
496 reqData.taskID = taskID;
497 reqData.callingSlv = animSlv;
499 VMS_WL__send_sem_request( &reqData, animSlv );
500 }
503 /*This call is the last to happen in every task. It causes the slave to
504 * suspend and get the next task out of the task-queue. Notice there is no
505 * assigner here.. only one slave, no slave ReadyQ, and so on..
506 *Can either make the assigner take the next task out of the taskQ, or can
507 * leave all as it is, and make task-end take the next task.
508 *Note: this fits the case in the new VMS for no-context tasks, so will use
509 * the built-in taskQ of new VMS, and should be local and much faster.
510 *
511 *The task-stub is saved in the animSlv, so the request handler will get it
512 * from there, along with the task-type which has arg types, and so on..
513 *
514 * NOTE: if want, don't need to send the animating SlaveVP around..
515 * instead, can make a single slave per core, and coreCtrlr looks up the
516 * slave from having the core number.
517 *
518 *But, to stay compatible with all the other VMS languages, leave it in..
519 */
520 void
521 VSs__end_task( SlaveVP *animSlv )
522 { VSsSemReq reqData;
524 reqData.reqType = end_task;
525 reqData.callingSlv = animSlv;
527 VMS_WL__send_sem_request( &reqData, animSlv );
528 }
531 void
532 VSs__taskwait(SlaveVP *animSlv)
533 {
534 VSsSemReq reqData;
536 reqData.reqType = taskwait;
537 reqData.callingSlv = animSlv;
539 VMS_WL__send_sem_request( &reqData, animSlv );
540 }
544 //========================== send and receive ============================
545 //
547 inline int32 *
548 VSs__give_self_taskID( SlaveVP *animSlv )
549 {
550 return ((VSsSemData*)animSlv->semanticData)->taskStub->taskID;
551 }
553 //================================ send ===================================
555 void
556 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID,
557 SlaveVP *senderSlv )
558 { VSsSemReq reqData;
560 reqData.reqType = send_type_to;
562 reqData.msg = msg;
563 reqData.msgType = type;
564 reqData.receiverID = receiverID;
565 reqData.senderSlv = senderSlv;
567 reqData.nextReqInHashEntry = NULL;
569 VMS_WL__send_sem_request( &reqData, senderSlv );
571 //When come back from suspend, no longer own data reachable from msg
572 }
574 void
575 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID, SlaveVP *senderSlv )
576 { VSsSemReq reqData;
578 reqData.reqType = send_from_to;
580 reqData.msg = msg;
581 reqData.senderID = senderID;
582 reqData.receiverID = receiverID;
583 reqData.senderSlv = senderSlv;
585 reqData.nextReqInHashEntry = NULL;
587 VMS_WL__send_sem_request( &reqData, senderSlv );
588 }
591 //================================ receive ================================
593 /*The "type" version of send and receive creates a many-to-one relationship.
594 * The sender is anonymous, and many sends can stack up, waiting to be
595 * received. The same receiver can also have send from-to's
596 * waiting for it, and those will be kept separate from the "type"
597 * messages.
598 */
599 void *
600 VSs__receive_type_to( const int32 type, int32* receiverID, SlaveVP *receiverSlv )
601 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
602 VSsSemReq reqData;
604 reqData.reqType = receive_type_to;
606 reqData.msgType = type;
607 reqData.receiverID = receiverID;
608 reqData.receiverSlv = receiverSlv;
610 reqData.nextReqInHashEntry = NULL;
612 VMS_WL__send_sem_request( &reqData, receiverSlv );
614 return receiverSlv->dataRetFromReq;
615 }
619 /*Call this at the point a receiving task wants in-coming data.
620 * Use this from-to form when know senderID -- it makes a direct channel
621 * between sender and receiver.
622 */
623 void *
624 VSs__receive_from_to( int32 *senderID, int32 *receiverID, SlaveVP *receiverSlv )
625 {
626 VSsSemReq reqData;
628 reqData.reqType = receive_from_to;
630 reqData.senderID = senderID;
631 reqData.receiverID = receiverID;
632 reqData.receiverSlv = receiverSlv;
634 reqData.nextReqInHashEntry = NULL;
635 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
637 VMS_WL__send_sem_request( &reqData, receiverSlv );
639 return receiverSlv->dataRetFromReq;
640 }
645 //==========================================================================
646 //
647 /*A function singleton is a function whose body executes exactly once, on a
648 * single core, no matter how many times the fuction is called and no
649 * matter how many cores or the timing of cores calling it.
650 *
651 *A data singleton is a ticket attached to data. That ticket can be used
652 * to get the data through the function exactly once, no matter how many
653 * times the data is given to the function, and no matter the timing of
654 * trying to get the data through from different cores.
655 */
657 /*asm function declarations*/
658 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
659 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
661 /*Fn singleton uses ID as index into array of singleton structs held in the
662 * semantic environment.
663 */
664 void
665 VSs__start_fn_singleton( int32 singletonID, SlaveVP *animSlv )
666 {
667 VSsSemReq reqData;
669 //
670 reqData.reqType = singleton_fn_start;
671 reqData.singletonID = singletonID;
673 VMS_WL__send_sem_request( &reqData, animSlv );
674 if( animSlv->dataRetFromReq ) //will be 0 or addr of label in end singleton
675 {
676 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
677 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
678 }
679 }
681 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
682 * The start_data_singleton makes the structure and puts its addr into the
683 * location.
684 */
685 void
686 VSs__start_data_singleton( VSsSingleton **singletonAddr, SlaveVP *animSlv )
687 {
688 VSsSemReq reqData;
690 if( *singletonAddr && (*singletonAddr)->hasFinished )
691 goto JmpToEndSingleton;
693 reqData.reqType = singleton_data_start;
694 reqData.singletonPtrAddr = singletonAddr;
696 VMS_WL__send_sem_request( &reqData, animSlv );
697 if( animSlv->dataRetFromReq ) //either 0 or end singleton's return addr
698 { //Assembly code changes the return addr on the stack to the one
699 // saved into the singleton by the end-singleton-fn
700 //The return addr is at 0x4(%%ebp)
701 JmpToEndSingleton:
702 asm_write_ret_from_singleton(*singletonAddr);
703 }
704 //now, simply return
705 //will exit either from the start singleton call or the end-singleton call
706 }
708 /*Uses ID as index into array of flags. If flag already set, resumes from
709 * end-label. Else, sets flag and resumes normally.
710 *
711 *Note, this call cannot be inlined because the instr addr at the label
712 * inside is shared by all invocations of a given singleton ID.
713 */
714 void
715 VSs__end_fn_singleton( int32 singletonID, SlaveVP *animSlv )
716 {
717 VSsSemReq reqData;
719 //don't need this addr until after at least one singleton has reached
720 // this function
721 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
722 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
724 reqData.reqType = singleton_fn_end;
725 reqData.singletonID = singletonID;
727 VMS_WL__send_sem_request( &reqData, animSlv );
729 EndSingletonInstrAddr:
730 return;
731 }
733 void
734 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr, SlaveVP *animSlv )
735 {
736 VSsSemReq reqData;
738 //don't need this addr until after singleton struct has reached
739 // this function for first time
740 //do assembly that saves the return addr of this fn call into the
741 // data singleton -- that data-singleton can only be given to exactly
742 // one instance in the code of this function. However, can use this
743 // function in different places for different data-singletons.
744 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
747 asm_save_ret_to_singleton(*singletonPtrAddr);
749 reqData.reqType = singleton_data_end;
750 reqData.singletonPtrAddr = singletonPtrAddr;
752 VMS_WL__send_sem_request( &reqData, animSlv );
753 }
755 /*This executes the function in the masterVP, so it executes in isolation
756 * from any other copies -- only one copy of the function can ever execute
757 * at a time.
758 *
759 *It suspends to the master, and the request handler takes the function
760 * pointer out of the request and calls it, then resumes the VP.
761 *Only very short functions should be called this way -- for longer-running
762 * isolation, use transaction-start and transaction-end, which run the code
763 * between as work-code.
764 */
765 void
766 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
767 void *data, SlaveVP *animSlv )
768 {
769 VSsSemReq reqData;
771 //
772 reqData.reqType = atomic;
773 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
774 reqData.dataForFn = data;
776 VMS_WL__send_sem_request( &reqData, animSlv );
777 }
780 /*This suspends to the master.
781 *First, it looks at the VP's data, to see the highest transactionID that VP
782 * already has entered. If the current ID is not larger, it throws an
783 * exception stating a bug in the code. Otherwise it puts the current ID
784 * there, and adds the ID to a linked list of IDs entered -- the list is
785 * used to check that exits are properly ordered.
786 *Next it is uses transactionID as index into an array of transaction
787 * structures.
788 *If the "VP_currently_executing" field is non-null, then put requesting VP
789 * into queue in the struct. (At some point a holder will request
790 * end-transaction, which will take this VP from the queue and resume it.)
791 *If NULL, then write requesting into the field and resume.
792 */
793 void
794 VSs__start_transaction( int32 transactionID, SlaveVP *animSlv )
795 {
796 VSsSemReq reqData;
798 //
799 reqData.callingSlv = animSlv;
800 reqData.reqType = trans_start;
801 reqData.transID = transactionID;
803 VMS_WL__send_sem_request( &reqData, animSlv );
804 }
806 /*This suspends to the master, then uses transactionID as index into an
807 * array of transaction structures.
808 *It looks at VP_currently_executing to be sure it's same as requesting VP.
809 * If different, throws an exception, stating there's a bug in the code.
810 *Next it looks at the queue in the structure.
811 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
812 *If something in, gets it, sets VP_currently_executing to that VP, then
813 * resumes both.
814 */
815 void
816 VSs__end_transaction( int32 transactionID, SlaveVP *animSlv )
817 {
818 VSsSemReq reqData;
820 //
821 reqData.callingSlv = animSlv;
822 reqData.reqType = trans_end;
823 reqData.transID = transactionID;
825 VMS_WL__send_sem_request( &reqData, animSlv );
826 }
828 //======================== Internal ==================================
829 /*
830 */
831 SlaveVP *
832 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
833 SlaveVP *creatingSlv )
834 { VSsSemReq reqData;
836 //the semantic request data is on the stack and disappears when this
837 // call returns -- it's guaranteed to remain in the VP's stack for as
838 // long as the VP is suspended.
839 reqData.reqType = 0; //know type because in a VMS create req
840 reqData.coreToAssignOnto = -1; //means round-robin assign
841 reqData.fnPtr = fnPtr;
842 reqData.initData = initData;
843 reqData.callingSlv = creatingSlv;
845 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
847 return creatingSlv->dataRetFromReq;
848 }
850 SlaveVP *
851 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
852 SlaveVP *creatingSlv, int32 coreToAssignOnto )
853 { VSsSemReq reqData;
855 //the semantic request data is on the stack and disappears when this
856 // call returns -- it's guaranteed to remain in the VP's stack for as
857 // long as the VP is suspended.
858 reqData.reqType = create_slave_w_aff; //not used, May 2012
859 reqData.coreToAssignOnto = coreToAssignOnto;
860 reqData.fnPtr = fnPtr;
861 reqData.initData = initData;
862 reqData.callingSlv = creatingSlv;
864 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
866 return creatingSlv->dataRetFromReq;
867 }
