view VSs.c @ 21:feea343d202f

add support for more OmpSs features
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Mon, 29 Oct 2012 16:57:56 +0100
parents a7ca8f45c1c4
children b787a5234406
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 semanticEnv->criticalSection[i].isOccupied = FALSE;
257 semanticEnv->criticalSection[i].waitQ = makeVMSQ();
258 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
259 semanticEnv->criticalSection[i].previous.vp = 0;
260 semanticEnv->criticalSection[i].previous.task = 0;
261 #endif
262 }
264 semanticEnv->numLiveExtraTaskSlvs = 0; //must be last
265 semanticEnv->numLiveThreadSlvs = 1; //must be last, counts the seed
267 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
268 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
269 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
270 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
271 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
272 semanticEnv->dataDependenciesList = makeListOfArrays(sizeof(Dependency),128);
273 semanticEnv->singletonDependenciesList = makeListOfArrays(sizeof(Dependency),128);
274 semanticEnv->warDependenciesList = makeListOfArrays(sizeof(Dependency),128);
275 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
277 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
278 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
279 #endif
280 }
283 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
284 */
285 void
286 VSs__cleanup_after_shutdown()
287 { VSsSemEnv *semanticEnv;
289 semanticEnv = _VMSMasterEnv->semanticEnv;
291 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
292 FILE* output;
293 int n;
294 char filename[255];
295 //UCC
296 for(n=0;n<255;n++)
297 {
298 sprintf(filename, "./counters/UCC.%d",n);
299 output = fopen(filename,"r");
300 if(output)
301 {
302 fclose(output);
303 }else{
304 break;
305 }
306 }
307 if(n<255){
308 printf("Saving UCC to File: %s ...\n", filename);
309 output = fopen(filename,"w+");
310 if(output!=NULL){
311 set_dependency_file(output);
312 //fprintf(output,"digraph Dependencies {\n");
313 //set_dot_file(output);
314 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
315 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
316 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
317 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
318 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
319 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
320 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
321 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
322 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
323 //fprintf(output,"}\n");
324 fflush(output);
326 } else
327 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
328 } else {
329 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
330 }
331 //Loop Graph
332 for(n=0;n<255;n++)
333 {
334 sprintf(filename, "./counters/LoopGraph.%d",n);
335 output = fopen(filename,"r");
336 if(output)
337 {
338 fclose(output);
339 }else{
340 break;
341 }
342 }
343 if(n<255){
344 printf("Saving LoopGraph to File: %s ...\n", filename);
345 output = fopen(filename,"w+");
346 if(output!=NULL){
347 set_dependency_file(output);
348 //fprintf(output,"digraph Dependencies {\n");
349 //set_dot_file(output);
350 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
351 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
352 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
353 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
354 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
355 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
356 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
357 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
358 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
359 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
360 //fprintf(output,"}\n");
361 fflush(output);
363 } else
364 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
365 } else {
366 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
367 }
370 freeListOfArrays(semanticEnv->unitList);
371 freeListOfArrays(semanticEnv->commDependenciesList);
372 freeListOfArrays(semanticEnv->ctlDependenciesList);
373 freeListOfArrays(semanticEnv->dynDependenciesList);
374 freeListOfArrays(semanticEnv->dataDependenciesList);
375 freeListOfArrays(semanticEnv->warDependenciesList);
376 freeListOfArrays(semanticEnv->singletonDependenciesList);
377 freeListOfArrays(semanticEnv->hwArcs);
379 #endif
380 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
381 FILE* output2;
382 int n2;
383 char filename2[255];
384 for(n2=0;n2<255;n2++)
385 {
386 sprintf(filename2, "./counters/Counters.%d.csv",n2);
387 output2 = fopen(filename2,"r");
388 if(output2)
389 {
390 fclose(output2);
391 }else{
392 break;
393 }
394 }
395 if(n2<255){
396 printf("Saving Counter measurements to File: %s ...\n", filename2);
397 output2 = fopen(filename2,"w+");
398 if(output2!=NULL){
399 set_counter_file(output2);
400 int i;
401 for(i=0;i<NUM_CORES;i++){
402 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
403 fflush(output2);
404 }
406 } else
407 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
408 } else {
409 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
410 }
412 #endif
413 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
414 * nothing to do here
417 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
418 {
419 VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
420 VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
421 }
422 VMS_int__free( semanticEnv->readyVPQs );
424 freeHashTable( semanticEnv->commHashTbl );
425 VMS_int__free( _VMSMasterEnv->semanticEnv );
426 */
427 VMS_SS__cleanup_at_end_of_shutdown();
428 }
431 //===========================================================================
433 SlaveVP *
434 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
435 SlaveVP *creatingThd )
436 { VSsSemReq reqData;
438 //the semantic request data is on the stack and disappears when this
439 // call returns -- it's guaranteed to remain in the VP's stack for as
440 // long as the VP is suspended.
441 reqData.reqType = 0; //know type because in a VMS create req
442 reqData.fnPtr = fnPtr;
443 reqData.initData = initData;
444 reqData.callingSlv = creatingThd;
446 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
448 return creatingThd->dataRetFromReq;
449 }
451 /*This is always the last thing done in the code animated by a thread VP.
452 * Normally, this would be the last line of the thread's top level function.
453 * But, if the thread exits from any point, it has to do so by calling
454 * this.
455 *
456 *It simply sends a dissipate request, which handles all the state cleanup.
457 */
458 void
459 VSs__end_thread( SlaveVP *thdToEnd )
460 {
462 VMS_WL__send_dissipate_req( thdToEnd );
463 }
467 //===========================================================================
470 //======================= task submit and end ==============================
471 /*
472 */
473 void
474 VSs__submit_task( VSsTaskType *taskType, void *args, SlaveVP *animSlv)
475 { VSsSemReq reqData;
477 reqData.reqType = submit_task;
479 reqData.taskType = taskType;
480 reqData.args = args;
481 reqData.callingSlv = animSlv;
483 reqData.taskID = NULL;
485 VMS_WL__send_sem_request( &reqData, animSlv );
486 }
488 int32 *
489 VSs__create_taskID_of_size( int32 numInts, SlaveVP *animSlv )
490 { int32 *taskID;
492 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
493 taskID[0] = numInts;
494 return taskID;
495 }
497 void
498 VSs__submit_task_with_ID( VSsTaskType *taskType, void *args, int32 *taskID,
499 SlaveVP *animSlv)
500 { VSsSemReq reqData;
502 reqData.reqType = submit_task;
504 reqData.taskType = taskType;
505 reqData.args = args;
506 reqData.taskID = taskID;
507 reqData.callingSlv = animSlv;
509 VMS_WL__send_sem_request( &reqData, animSlv );
510 }
513 /*This call is the last to happen in every task. It causes the slave to
514 * suspend and get the next task out of the task-queue. Notice there is no
515 * assigner here.. only one slave, no slave ReadyQ, and so on..
516 *Can either make the assigner take the next task out of the taskQ, or can
517 * leave all as it is, and make task-end take the next task.
518 *Note: this fits the case in the new VMS for no-context tasks, so will use
519 * the built-in taskQ of new VMS, and should be local and much faster.
520 *
521 *The task-stub is saved in the animSlv, so the request handler will get it
522 * from there, along with the task-type which has arg types, and so on..
523 *
524 * NOTE: if want, don't need to send the animating SlaveVP around..
525 * instead, can make a single slave per core, and coreCtrlr looks up the
526 * slave from having the core number.
527 *
528 *But, to stay compatible with all the other VMS languages, leave it in..
529 */
530 void
531 VSs__end_task( SlaveVP *animSlv )
532 { VSsSemReq reqData;
534 reqData.reqType = end_task;
535 reqData.callingSlv = animSlv;
537 VMS_WL__send_sem_request( &reqData, animSlv );
538 }
541 void
542 VSs__taskwait(SlaveVP *animSlv)
543 {
544 VSsSemReq reqData;
546 reqData.reqType = taskwait;
547 reqData.callingSlv = animSlv;
549 VMS_WL__send_sem_request( &reqData, animSlv );
550 }
552 void
553 VSs__taskwait_on(SlaveVP *animSlv,void* ptr){
554 VSsSemReq reqData;
556 reqData.reqType = taskwait_on;
557 reqData.callingSlv = animSlv;
559 reqData.args = ptr;
561 VMS_WL__send_sem_request( &reqData, animSlv );
562 }
564 void
565 VSs__start_critical(SlaveVP *animSlv,int32 name){
566 VSsSemReq reqData;
568 reqData.reqType = critical_start;
569 reqData.callingSlv = animSlv;
571 reqData.criticalID = name;
573 VMS_WL__send_sem_request( &reqData, animSlv );
574 }
576 void
577 VSs__end_critical(SlaveVP *animSlv,int32 name){
578 VSsSemReq reqData;
580 reqData.reqType = critical_end;
581 reqData.callingSlv = animSlv;
583 reqData.criticalID = name;
585 VMS_WL__send_sem_request( &reqData, animSlv );
586 }
588 //========================== send and receive ============================
589 //
591 int32 *
592 VSs__give_self_taskID( SlaveVP *animSlv )
593 {
594 return ((VSsSemData*)animSlv->semanticData)->taskStub->taskID;
595 }
597 //================================ send ===================================
599 void
600 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID,
601 SlaveVP *senderSlv )
602 { VSsSemReq reqData;
604 reqData.reqType = send_type_to;
606 reqData.msg = msg;
607 reqData.msgType = type;
608 reqData.receiverID = receiverID;
609 reqData.senderSlv = senderSlv;
611 reqData.nextReqInHashEntry = NULL;
613 VMS_WL__send_sem_request( &reqData, senderSlv );
615 //When come back from suspend, no longer own data reachable from msg
616 }
618 void
619 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID, SlaveVP *senderSlv )
620 { VSsSemReq reqData;
622 reqData.reqType = send_from_to;
624 reqData.msg = msg;
625 reqData.senderID = senderID;
626 reqData.receiverID = receiverID;
627 reqData.senderSlv = senderSlv;
629 reqData.nextReqInHashEntry = NULL;
631 VMS_WL__send_sem_request( &reqData, senderSlv );
632 }
635 //================================ receive ================================
637 /*The "type" version of send and receive creates a many-to-one relationship.
638 * The sender is anonymous, and many sends can stack up, waiting to be
639 * received. The same receiver can also have send from-to's
640 * waiting for it, and those will be kept separate from the "type"
641 * messages.
642 */
643 void *
644 VSs__receive_type_to( const int32 type, int32* receiverID, SlaveVP *receiverSlv )
645 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
646 VSsSemReq reqData;
648 reqData.reqType = receive_type_to;
650 reqData.msgType = type;
651 reqData.receiverID = receiverID;
652 reqData.receiverSlv = receiverSlv;
654 reqData.nextReqInHashEntry = NULL;
656 VMS_WL__send_sem_request( &reqData, receiverSlv );
658 return receiverSlv->dataRetFromReq;
659 }
663 /*Call this at the point a receiving task wants in-coming data.
664 * Use this from-to form when know senderID -- it makes a direct channel
665 * between sender and receiver.
666 */
667 void *
668 VSs__receive_from_to( int32 *senderID, int32 *receiverID, SlaveVP *receiverSlv )
669 {
670 VSsSemReq reqData;
672 reqData.reqType = receive_from_to;
674 reqData.senderID = senderID;
675 reqData.receiverID = receiverID;
676 reqData.receiverSlv = receiverSlv;
678 reqData.nextReqInHashEntry = NULL;
679 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
681 VMS_WL__send_sem_request( &reqData, receiverSlv );
683 return receiverSlv->dataRetFromReq;
684 }
689 //==========================================================================
690 //
691 /*A function singleton is a function whose body executes exactly once, on a
692 * single core, no matter how many times the fuction is called and no
693 * matter how many cores or the timing of cores calling it.
694 *
695 *A data singleton is a ticket attached to data. That ticket can be used
696 * to get the data through the function exactly once, no matter how many
697 * times the data is given to the function, and no matter the timing of
698 * trying to get the data through from different cores.
699 */
701 /*asm function declarations*/
702 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
703 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
705 /*Fn singleton uses ID as index into array of singleton structs held in the
706 * semantic environment.
707 */
708 void
709 VSs__start_fn_singleton( int32 singletonID, SlaveVP *animSlv )
710 {
711 VSsSemReq reqData;
713 //
714 reqData.reqType = singleton_fn_start;
715 reqData.singletonID = singletonID;
717 VMS_WL__send_sem_request( &reqData, animSlv );
718 if( animSlv->dataRetFromReq ) //will be 0 or addr of label in end singleton
719 {
720 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
721 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
722 }
723 }
725 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
726 * The start_data_singleton makes the structure and puts its addr into the
727 * location.
728 */
729 void
730 VSs__start_data_singleton( VSsSingleton **singletonAddr, SlaveVP *animSlv )
731 {
732 VSsSemReq reqData;
734 if( *singletonAddr && (*singletonAddr)->hasFinished )
735 goto JmpToEndSingleton;
737 reqData.reqType = singleton_data_start;
738 reqData.singletonPtrAddr = singletonAddr;
740 VMS_WL__send_sem_request( &reqData, animSlv );
741 if( animSlv->dataRetFromReq ) //either 0 or end singleton's return addr
742 { //Assembly code changes the return addr on the stack to the one
743 // saved into the singleton by the end-singleton-fn
744 //The return addr is at 0x4(%%ebp)
745 JmpToEndSingleton:
746 asm_write_ret_from_singleton(*singletonAddr);
747 }
748 //now, simply return
749 //will exit either from the start singleton call or the end-singleton call
750 }
752 /*Uses ID as index into array of flags. If flag already set, resumes from
753 * end-label. Else, sets flag and resumes normally.
754 *
755 *Note, this call cannot be inlined because the instr addr at the label
756 * inside is shared by all invocations of a given singleton ID.
757 */
758 void
759 VSs__end_fn_singleton( int32 singletonID, SlaveVP *animSlv )
760 {
761 VSsSemReq reqData;
763 //don't need this addr until after at least one singleton has reached
764 // this function
765 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
766 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
768 reqData.reqType = singleton_fn_end;
769 reqData.singletonID = singletonID;
771 VMS_WL__send_sem_request( &reqData, animSlv );
773 //EndSingletonInstrAddr:
774 return;
775 }
777 void
778 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr, SlaveVP *animSlv )
779 {
780 VSsSemReq reqData;
782 //don't need this addr until after singleton struct has reached
783 // this function for first time
784 //do assembly that saves the return addr of this fn call into the
785 // data singleton -- that data-singleton can only be given to exactly
786 // one instance in the code of this function. However, can use this
787 // function in different places for different data-singletons.
788 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
791 asm_save_ret_to_singleton(*singletonPtrAddr);
793 reqData.reqType = singleton_data_end;
794 reqData.singletonPtrAddr = singletonPtrAddr;
796 VMS_WL__send_sem_request( &reqData, animSlv );
797 }
799 /*This executes the function in the masterVP, so it executes in isolation
800 * from any other copies -- only one copy of the function can ever execute
801 * at a time.
802 *
803 *It suspends to the master, and the request handler takes the function
804 * pointer out of the request and calls it, then resumes the VP.
805 *Only very short functions should be called this way -- for longer-running
806 * isolation, use transaction-start and transaction-end, which run the code
807 * between as work-code.
808 */
809 void
810 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
811 void *data, SlaveVP *animSlv )
812 {
813 VSsSemReq reqData;
815 //
816 reqData.reqType = atomic;
817 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
818 reqData.dataForFn = data;
820 VMS_WL__send_sem_request( &reqData, animSlv );
821 }
824 /*This suspends to the master.
825 *First, it looks at the VP's data, to see the highest transactionID that VP
826 * already has entered. If the current ID is not larger, it throws an
827 * exception stating a bug in the code. Otherwise it puts the current ID
828 * there, and adds the ID to a linked list of IDs entered -- the list is
829 * used to check that exits are properly ordered.
830 *Next it is uses transactionID as index into an array of transaction
831 * structures.
832 *If the "VP_currently_executing" field is non-null, then put requesting VP
833 * into queue in the struct. (At some point a holder will request
834 * end-transaction, which will take this VP from the queue and resume it.)
835 *If NULL, then write requesting into the field and resume.
836 */
837 void
838 VSs__start_transaction( int32 transactionID, SlaveVP *animSlv )
839 {
840 VSsSemReq reqData;
842 //
843 reqData.callingSlv = animSlv;
844 reqData.reqType = trans_start;
845 reqData.transID = transactionID;
847 VMS_WL__send_sem_request( &reqData, animSlv );
848 }
850 /*This suspends to the master, then uses transactionID as index into an
851 * array of transaction structures.
852 *It looks at VP_currently_executing to be sure it's same as requesting VP.
853 * If different, throws an exception, stating there's a bug in the code.
854 *Next it looks at the queue in the structure.
855 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
856 *If something in, gets it, sets VP_currently_executing to that VP, then
857 * resumes both.
858 */
859 void
860 VSs__end_transaction( int32 transactionID, SlaveVP *animSlv )
861 {
862 VSsSemReq reqData;
864 //
865 reqData.callingSlv = animSlv;
866 reqData.reqType = trans_end;
867 reqData.transID = transactionID;
869 VMS_WL__send_sem_request( &reqData, animSlv );
870 }
872 //======================== Internal ==================================
873 /*
874 */
875 SlaveVP *
876 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
877 SlaveVP *creatingSlv )
878 { VSsSemReq reqData;
880 //the semantic request data is on the stack and disappears when this
881 // call returns -- it's guaranteed to remain in the VP's stack for as
882 // long as the VP is suspended.
883 reqData.reqType = 0; //know type because in a VMS create req
884 reqData.coreToAssignOnto = -1; //means round-robin assign
885 reqData.fnPtr = fnPtr;
886 reqData.initData = initData;
887 reqData.callingSlv = creatingSlv;
889 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
891 return creatingSlv->dataRetFromReq;
892 }
894 SlaveVP *
895 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
896 SlaveVP *creatingSlv, int32 coreToAssignOnto )
897 { VSsSemReq reqData;
899 //the semantic request data is on the stack and disappears when this
900 // call returns -- it's guaranteed to remain in the VP's stack for as
901 // long as the VP is suspended.
902 reqData.reqType = create_slave_w_aff; //not used, May 2012
903 reqData.coreToAssignOnto = coreToAssignOnto;
904 reqData.fnPtr = fnPtr;
905 reqData.initData = initData;
906 reqData.callingSlv = creatingSlv;
908 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
910 return creatingSlv->dataRetFromReq;
911 }