Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VSs_impls > VSs__MC_shared_impl
view VSs.c @ 43:8733d1299c3a
add barrier
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Tue, 11 Jun 2013 15:37:02 +0200 |
| parents | 0715109abb08 |
| children | 7e7f37aa2f61 |
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;
80 int32* taskID;
82 VSs__init(); //normal multi-thd
84 semEnv = _VMSMasterEnv->semanticEnv;
86 //VSs starts with one processor, which is put into initial environ,
87 // and which then calls create() to create more, thereby expanding work
88 seedSlv = VSs__create_slave_helper( &VSs__run_thread , fnPtr, initData, semEnv, semEnv->nextCoreToGetNewSlv++ );
89 //NB: this assumes that after VSs_init() nextCoreToGetNewSlv is still 0,
90 // and also that there is more than 1 core.
92 //seed slave is a thread slave, so make a thread's task stub for it
93 // and then make another to stand for the seed's parent task. Make
94 // the parent be already ended, and have one child (the seed). This
95 // will make the dissipate handler do the right thing when the seed
96 // is dissipated.
97 threadTaskStub = create_thread_task_stub( initData);
98 parentTaskStub = create_thread_task_stub( NULL );
99 parentTaskStub->isEnded = TRUE;
100 parentTaskStub->numLiveChildThreads = 1; //so dissipate works for seed
101 threadTaskStub->parentTaskStub = parentTaskStub;
102 threadTaskStub->slaveAssignedTo = seedSlv;
104 taskID = VMS_WL__malloc(2 * sizeof(int32) );
105 taskID[0] = 1;
106 taskID[1] = -1;
107 threadTaskStub->taskID = taskID;
109 semData = (VSsSemData *)seedSlv->semanticData;
110 //seedVP is a thread, so has a permanent task
111 semData->needsTaskAssigned = FALSE;
112 semData->taskStub = threadTaskStub;
113 semData->slaveType = ThreadSlv;
115 resume_slaveVP( seedSlv, semEnv ); //returns right away, just queues Slv
117 VMS_SS__start_the_work_then_wait_until_done(); //normal multi-thd
119 VSs__cleanup_after_shutdown();
120 }
123 int32
124 VSs__giveMinWorkUnitCycles( float32 percentOverhead )
125 {
126 return MIN_WORK_UNIT_CYCLES;
127 }
129 int32
130 VSs__giveIdealNumWorkUnits()
131 {
132 return NUM_ANIM_SLOTS * NUM_CORES;
133 }
135 int32
136 VSs__give_number_of_cores_to_schedule_onto()
137 {
138 return NUM_CORES;
139 }
141 /*For now, use TSC -- later, make these two macros with assembly that first
142 * saves jump point, and second jumps back several times to get reliable time
143 */
144 void
145 VSs__start_primitive()
146 { saveLowTimeStampCountInto( ((VSsSemEnv *)(_VMSMasterEnv->semanticEnv))->
147 primitiveStartTime );
148 }
150 /*Just quick and dirty for now -- make reliable later
151 * will want this to jump back several times -- to be sure cache is warm
152 * because don't want comm time included in calc-time measurement -- and
153 * also to throw out any "weird" values due to OS interrupt or TSC rollover
154 */
155 int32
156 VSs__end_primitive_and_give_cycles()
157 { int32 endTime, startTime;
158 //TODO: fix by repeating time-measurement
159 saveLowTimeStampCountInto( endTime );
160 startTime =((VSsSemEnv*)(_VMSMasterEnv->semanticEnv))->primitiveStartTime;
161 return (endTime - startTime);
162 }
164 //===========================================================================
166 /*Initializes all the data-structures for a VSs system -- but doesn't
167 * start it running yet!
168 *
169 *This runs in the main thread -- before VMS starts up
170 *
171 *This sets up the semantic layer over the VMS system
172 *
173 *First, calls VMS_Setup, then creates own environment, making it ready
174 * for creating the seed processor and then starting the work.
175 */
176 void
177 VSs__init()
178 {
179 VMS_SS__init();
180 //masterEnv, a global var, now is partially set up by init_VMS
181 // after this, have VMS_int__malloc and VMS_int__free available
183 VSs__init_Helper();
184 }
187 void idle_fn(void* data){
188 while(1){
189 VMS_int__suspend_slaveVP_and_send_req(currVP);
190 }
191 }
193 void
194 VSs__init_Helper()
195 { VSsSemEnv *semanticEnv;
196 int32 i, coreNum, slotNum;
197 VSsSemData *semData;
199 //Hook up the semantic layer's plug-ins to the Master virt procr
200 _VMSMasterEnv->requestHandler = &VSs__Request_Handler;
201 _VMSMasterEnv->slaveAssigner = &VSs__assign_slaveVP_to_slot;
203 //create the semantic layer's environment (all its data) and add to
204 // the master environment
205 semanticEnv = VMS_int__malloc( sizeof( VSsSemEnv ) );
206 _VMSMasterEnv->semanticEnv = semanticEnv;
208 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
209 _VMSMasterEnv->counterHandler = &VSs__counter_handler;
210 VSs__init_counter_data_structs();
211 #endif
213 //semanticEnv->shutdownInitiated = FALSE;
214 semanticEnv->coreIsDone = VMS_int__malloc( NUM_CORES * sizeof( bool32 ) );
215 semanticEnv->numCoresDone = 0;
216 //For each animation slot, there is an idle slave, and an initial
217 // slave assigned as the current-task-slave. Create them here.
218 SlaveVP *idleSlv, *slotTaskSlv;
219 for( coreNum = 0; coreNum < NUM_CORES; coreNum++ )
220 { semanticEnv->coreIsDone[coreNum] = FALSE; //use during shutdown
222 for( slotNum = 0; slotNum < NUM_ANIM_SLOTS; ++slotNum )
223 {
224 #ifdef IDLE_SLAVES
225 idleSlv = VSs__create_slave_helper( &VSs__run_thread, &idle_fn, NULL, semanticEnv, 0);
226 idleSlv->coreAnimatedBy = coreNum;
227 idleSlv->animSlotAssignedTo =
228 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
229 semanticEnv->idleSlv[coreNum][slotNum] = idleSlv;
230 #endif
232 slotTaskSlv = VSs__create_slave_helper(&VSs__run_thread, &idle_fn, NULL, semanticEnv, 0);
233 slotTaskSlv->coreAnimatedBy = coreNum;
234 slotTaskSlv->animSlotAssignedTo =
235 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
237 semData = slotTaskSlv->semanticData;
238 semData->needsTaskAssigned = TRUE;
239 semData->slaveType = SlotTaskSlv;
240 semanticEnv->slotTaskSlvs[coreNum][slotNum] = slotTaskSlv;
241 }
242 }
244 //create the ready queues, hash tables used for matching and so forth
245 semanticEnv->slavesReadyToResumeQ = makeVMSQ();
246 semanticEnv->freeExtraTaskSlvQ = makeVMSQ();
247 semanticEnv->taskReadyQ = makeVMSQ();
248 semanticEnv->barrierQ = makeVMSQ();
250 semanticEnv->argPtrHashTbl = makeHashTable32( 20, &free_pointer_entry );
251 semanticEnv->commHashTbl = makeHashTable32( 16, &VMS_int__free );
252 semanticEnv->criticalHashTbl = makeHashTable32( 16, &VMS_int__free );
254 semanticEnv->nextCoreToGetNewSlv = 0;
256 semanticEnv->numInFlightTasks = 0;
257 semanticEnv->deferredSubmitsQ = makeVMSQ();
258 #ifdef EXTERNAL_SCHEDULER
259 VSs__init_ext_scheduler();
260 #endif
261 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
262 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
263 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
264 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
265 {
266 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
267 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
268 semanticEnv->fnSingletons[i].hasFinished = FALSE;
269 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
270 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
271 }
273 semanticEnv->numLiveExtraTaskSlvs = 0; //must be last
274 semanticEnv->numLiveThreadSlvs = 1; //must be last, counts the seed
276 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
277 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
278 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
279 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
280 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
281 semanticEnv->dataDependenciesList = makeListOfArrays(sizeof(Dependency),128);
282 semanticEnv->singletonDependenciesList = makeListOfArrays(sizeof(Dependency),128);
283 semanticEnv->warDependenciesList = makeListOfArrays(sizeof(Dependency),128);
284 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
286 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
287 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
288 #endif
289 }
292 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
293 */
294 void
295 VSs__cleanup_after_shutdown()
296 { VSsSemEnv *semanticEnv;
298 semanticEnv = _VMSMasterEnv->semanticEnv;
300 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
301 FILE* output;
302 int n;
303 char filename[255];
304 //UCC
305 for(n=0;n<255;n++)
306 {
307 sprintf(filename, "./counters/UCC.%d",n);
308 output = fopen(filename,"r");
309 if(output)
310 {
311 fclose(output);
312 }else{
313 break;
314 }
315 }
316 if(n<255){
317 printf("Saving UCC to File: %s ...\n", filename);
318 output = fopen(filename,"w+");
319 if(output!=NULL){
320 set_dependency_file(output);
321 //fprintf(output,"digraph Dependencies {\n");
322 //set_dot_file(output);
323 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
324 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
325 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
326 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
327 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
328 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
329 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
330 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
331 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
332 //fprintf(output,"}\n");
333 fflush(output);
335 } else
336 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
337 } else {
338 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
339 }
340 //Loop Graph
341 for(n=0;n<255;n++)
342 {
343 sprintf(filename, "./counters/LoopGraph.%d",n);
344 output = fopen(filename,"r");
345 if(output)
346 {
347 fclose(output);
348 }else{
349 break;
350 }
351 }
352 if(n<255){
353 printf("Saving LoopGraph to File: %s ...\n", filename);
354 output = fopen(filename,"w+");
355 if(output!=NULL){
356 set_dependency_file(output);
357 //fprintf(output,"digraph Dependencies {\n");
358 //set_dot_file(output);
359 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
360 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
361 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
362 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
363 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
364 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
365 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
366 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
367 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
368 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
369 //fprintf(output,"}\n");
370 fflush(output);
372 } else
373 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
374 } else {
375 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
376 }
379 freeListOfArrays(semanticEnv->unitList);
380 freeListOfArrays(semanticEnv->commDependenciesList);
381 freeListOfArrays(semanticEnv->ctlDependenciesList);
382 freeListOfArrays(semanticEnv->dynDependenciesList);
383 freeListOfArrays(semanticEnv->dataDependenciesList);
384 freeListOfArrays(semanticEnv->warDependenciesList);
385 freeListOfArrays(semanticEnv->singletonDependenciesList);
386 freeListOfArrays(semanticEnv->hwArcs);
388 #endif
389 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
390 FILE* output2;
391 int n2;
392 char filename2[255];
393 for(n2=0;n2<255;n2++)
394 {
395 sprintf(filename2, "./counters/Counters.%d.csv",n2);
396 output2 = fopen(filename2,"r");
397 if(output2)
398 {
399 fclose(output2);
400 }else{
401 break;
402 }
403 }
404 if(n2<255){
405 printf("Saving Counter measurements to File: %s ...\n", filename2);
406 output2 = fopen(filename2,"w+");
407 if(output2!=NULL){
408 set_counter_file(output2);
409 int i;
410 for(i=0;i<NUM_CORES;i++){
411 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
412 fflush(output2);
413 }
415 } else
416 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
417 } else {
418 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
419 }
421 #endif
422 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
423 * nothing to do here */
424 //_VMSMasterEnv->shutdownInitiated = TRUE;
425 int coreIdx, slotIdx;
426 SlaveVP* slotSlv;
427 for (coreIdx = 0; coreIdx < NUM_CORES; coreIdx++) {
428 for (slotIdx = 0; slotIdx < NUM_ANIM_SLOTS; slotIdx++) {
429 slotSlv = semanticEnv->slotTaskSlvs[coreIdx][slotIdx];
430 VMS_int__free(slotSlv->semanticData);
431 VMS_int__dissipate_slaveVP(slotSlv);
432 #ifdef IDLE_SLAVES
433 slotSlv = semanticEnv->idleSlv[coreIdx][slotIdx];
434 VMS_int__free(slotSlv->semanticData);
435 VMS_int__dissipate_slaveVP(slotSlv);
436 #endif
437 }
438 }
439 int i;
440 for (i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++) {
441 freePrivQ(semanticEnv->fnSingletons[i].waitQ);
442 freePrivQ(semanticEnv->transactionStrucs[i].waitingVPQ);
443 }
445 freePrivQ(semanticEnv->freeExtraTaskSlvQ);
446 freePrivQ(semanticEnv->slavesReadyToResumeQ);
447 freePrivQ(semanticEnv->taskReadyQ);
448 freePrivQ(semanticEnv->barrierQ);
449 freePrivQ(semanticEnv->deferredSubmitsQ);
450 freeHashTable(semanticEnv->argPtrHashTbl);
451 freeHashTable(semanticEnv->commHashTbl);
452 freeHashTable(semanticEnv->criticalHashTbl);
453 VMS_int__free(semanticEnv->coreIsDone);
454 VMS_int__free(_VMSMasterEnv->semanticEnv);
456 VMS_SS__cleanup_at_end_of_shutdown();
457 }
460 //===========================================================================
462 SlaveVP *
463 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
464 SlaveVP *creatingThd )
465 { VSsSemReq reqData;
467 //the semantic request data is on the stack and disappears when this
468 // call returns -- it's guaranteed to remain in the VP's stack for as
469 // long as the VP is suspended.
470 reqData.reqType = 0; //know type because in a VMS create req
471 reqData.fnPtr = fnPtr;
472 reqData.initData = initData;
473 reqData.callingSlv = creatingThd;
475 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
477 return creatingThd->dataRetFromReq;
478 }
480 /*This is always the last thing done in the code animated by a thread VP.
481 * Normally, this would be the last line of the thread's top level function.
482 * But, if the thread exits from any point, it has to do so by calling
483 * this.
484 *
485 *It simply sends a dissipate request, which handles all the state cleanup.
486 */
487 void
488 VSs__end_thread()
489 {
491 VMS_WL__send_dissipate_req( currVP );
492 }
494 void VSs__run_thread(TopLevelFnPtr fnPtr, void *initData){
495 (*fnPtr)(initData);
496 VSs__end_thread();
497 }
499 //===========================================================================
502 //======================= task submit and end ==============================
504 /*
505 */
506 void VSs__submit_task(VSsTaskType *taskType, void *args, void* deps) {
507 VSsSemReq reqData;
509 reqData.reqType = submit_task;
511 reqData.taskType = taskType;
512 reqData.args = args;
513 reqData.deps = deps;
514 reqData.callingSlv = currVP;
516 reqData.taskID = NULL;
518 VMS_WL__send_sem_request(&reqData, currVP);
519 }
521 int32 *
522 VSs__create_taskID_of_size( int32 numInts)
523 { int32 *taskID;
525 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
526 taskID[0] = numInts;
527 return taskID;
528 }
530 void VSs__submit_task_with_ID(VSsTaskType *taskType, void *args, void* deps, int32 *taskID) {
531 VSsSemReq reqData;
533 reqData.reqType = submit_task;
535 reqData.taskType = taskType;
536 reqData.args = args;
537 reqData.deps = deps;
538 reqData.taskID = taskID;
539 reqData.callingSlv = currVP;
541 VMS_WL__send_sem_request(&reqData, currVP);
542 }
545 /*This call is the last to happen in every task. It causes the slave to
546 * suspend and get the next task out of the task-queue. Notice there is no
547 * assigner here.. only one slave, no slave ReadyQ, and so on..
548 *Can either make the assigner take the next task out of the taskQ, or can
549 * leave all as it is, and make task-end take the next task.
550 *Note: this fits the case in the new VMS for no-context tasks, so will use
551 * the built-in taskQ of new VMS, and should be local and much faster.
552 *
553 *The task-stub is saved in the animSlv, so the request handler will get it
554 * from there, along with the task-type which has arg types, and so on..
555 *
556 * NOTE: if want, don't need to send the animating SlaveVP around..
557 * instead, can make a single slave per core, and coreCtrlr looks up the
558 * slave from having the core number.
559 *
560 *But, to stay compatible with all the other VMS languages, leave it in..
561 */
562 void
563 VSs__end_task()
564 { VSsSemReq reqData;
566 reqData.reqType = end_task;
567 reqData.callingSlv = currVP;
569 VMS_WL__send_sem_request( &reqData, currVP );
570 }
572 void VSs__run_task(TopLevelFnPtr fnPtr, void *initData){
573 (*fnPtr)(initData);
574 VSs__end_task();
575 }
577 void
578 VSs__taskwait()
579 {
580 VSsSemReq reqData;
582 reqData.reqType = taskwait;
583 reqData.callingSlv = currVP;
585 VMS_WL__send_sem_request( &reqData, currVP );
586 }
588 void
589 VSs__taskwait_on(void* ptr){
590 VSsSemReq reqData;
592 reqData.reqType = taskwait_on;
593 reqData.callingSlv = currVP;
595 reqData.args = ptr;
597 VMS_WL__send_sem_request( &reqData, currVP );
598 }
600 void
601 VSs__start_critical(void* lock){
602 VSsSemReq reqData;
604 reqData.reqType = critical_start;
605 reqData.callingSlv = currVP;
607 reqData.criticalID = lock;
609 VMS_WL__send_sem_request( &reqData, currVP );
610 }
612 void
613 VSs__end_critical(void* lock){
614 VSsSemReq reqData;
616 reqData.reqType = critical_end;
617 reqData.callingSlv = currVP;
619 reqData.criticalID = lock;
621 VMS_WL__send_sem_request( &reqData, currVP );
622 }
624 //========================== send and receive ============================
625 //
627 int32 *
628 VSs__give_self_taskID()
629 {
630 return ((VSsSemData*)currVP->semanticData)->taskStub->taskID;
631 }
633 //================================ send ===================================
635 void
636 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID)
637 { VSsSemReq reqData;
639 reqData.reqType = send_type_to;
641 reqData.msg = msg;
642 reqData.msgType = type;
643 reqData.receiverID = receiverID;
644 reqData.senderSlv = currVP;
646 reqData.nextReqInHashEntry = NULL;
648 VMS_WL__send_sem_request( &reqData, currVP );
650 //When come back from suspend, no longer own data reachable from msg
651 }
653 void
654 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID)
655 { VSsSemReq reqData;
657 reqData.reqType = send_from_to;
659 reqData.msg = msg;
660 reqData.senderID = senderID;
661 reqData.receiverID = receiverID;
662 reqData.senderSlv = currVP;
664 reqData.nextReqInHashEntry = NULL;
666 VMS_WL__send_sem_request( &reqData, currVP );
667 }
670 //================================ receive ================================
672 /*The "type" version of send and receive creates a many-to-one relationship.
673 * The sender is anonymous, and many sends can stack up, waiting to be
674 * received. The same receiver can also have send from-to's
675 * waiting for it, and those will be kept separate from the "type"
676 * messages.
677 */
678 void *
679 VSs__receive_type_to( const int32 type, int32* receiverID )
680 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
681 VSsSemReq reqData;
683 reqData.reqType = receive_type_to;
685 reqData.msgType = type;
686 reqData.receiverID = receiverID;
687 reqData.receiverSlv = currVP;
689 reqData.nextReqInHashEntry = NULL;
691 VMS_WL__send_sem_request( &reqData, currVP );
693 return currVP->dataRetFromReq;
694 }
698 /*Call this at the point a receiving task wants in-coming data.
699 * Use this from-to form when know senderID -- it makes a direct channel
700 * between sender and receiver.
701 */
702 void *
703 VSs__receive_from_to( int32 *senderID, int32 *receiverID)
704 {
705 VSsSemReq reqData;
707 reqData.reqType = receive_from_to;
709 reqData.senderID = senderID;
710 reqData.receiverID = receiverID;
711 reqData.receiverSlv = currVP;
713 reqData.nextReqInHashEntry = NULL;
714 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
716 VMS_WL__send_sem_request( &reqData, currVP );
718 return currVP->dataRetFromReq;
719 }
724 //==========================================================================
725 //
726 /*A function singleton is a function whose body executes exactly once, on a
727 * single core, no matter how many times the fuction is called and no
728 * matter how many cores or the timing of cores calling it.
729 *
730 *A data singleton is a ticket attached to data. That ticket can be used
731 * to get the data through the function exactly once, no matter how many
732 * times the data is given to the function, and no matter the timing of
733 * trying to get the data through from different cores.
734 */
736 /*asm function declarations*/
737 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
738 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
740 /*Fn singleton uses ID as index into array of singleton structs held in the
741 * semantic environment.
742 */
743 void
744 VSs__start_fn_singleton( int32 singletonID)
745 {
746 VSsSemReq reqData;
748 //
749 reqData.reqType = singleton_fn_start;
750 reqData.singletonID = singletonID;
752 VMS_WL__send_sem_request( &reqData, currVP );
753 if( currVP->dataRetFromReq ) //will be 0 or addr of label in end singleton
754 {
755 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( currVP );
756 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
757 }
758 }
760 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
761 * The start_data_singleton makes the structure and puts its addr into the
762 * location.
763 */
764 void
765 VSs__start_data_singleton( VSsSingleton **singletonAddr )
766 {
767 VSsSemReq reqData;
769 if( *singletonAddr && (*singletonAddr)->hasFinished )
770 goto JmpToEndSingleton;
772 reqData.reqType = singleton_data_start;
773 reqData.singletonPtrAddr = singletonAddr;
775 VMS_WL__send_sem_request( &reqData, currVP );
776 if( currVP->dataRetFromReq ) //either 0 or end singleton's return addr
777 { //Assembly code changes the return addr on the stack to the one
778 // saved into the singleton by the end-singleton-fn
779 //The return addr is at 0x4(%%ebp)
780 JmpToEndSingleton:
781 asm_write_ret_from_singleton(*singletonAddr);
782 }
783 //now, simply return
784 //will exit either from the start singleton call or the end-singleton call
785 }
787 /*Uses ID as index into array of flags. If flag already set, resumes from
788 * end-label. Else, sets flag and resumes normally.
789 *
790 *Note, this call cannot be inlined because the instr addr at the label
791 * inside is shared by all invocations of a given singleton ID.
792 */
793 void
794 VSs__end_fn_singleton( int32 singletonID )
795 {
796 VSsSemReq reqData;
798 //don't need this addr until after at least one singleton has reached
799 // this function
800 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( currVP );
801 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
803 reqData.reqType = singleton_fn_end;
804 reqData.singletonID = singletonID;
806 VMS_WL__send_sem_request( &reqData, currVP );
808 //EndSingletonInstrAddr:
809 return;
810 }
812 void
813 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr )
814 {
815 VSsSemReq reqData;
817 //don't need this addr until after singleton struct has reached
818 // this function for first time
819 //do assembly that saves the return addr of this fn call into the
820 // data singleton -- that data-singleton can only be given to exactly
821 // one instance in the code of this function. However, can use this
822 // function in different places for different data-singletons.
823 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
826 asm_save_ret_to_singleton(*singletonPtrAddr);
828 reqData.reqType = singleton_data_end;
829 reqData.singletonPtrAddr = singletonPtrAddr;
831 VMS_WL__send_sem_request( &reqData, currVP );
832 }
834 /*This executes the function in the masterVP, so it executes in isolation
835 * from any other copies -- only one copy of the function can ever execute
836 * at a time.
837 *
838 *It suspends to the master, and the request handler takes the function
839 * pointer out of the request and calls it, then resumes the VP.
840 *Only very short functions should be called this way -- for longer-running
841 * isolation, use transaction-start and transaction-end, which run the code
842 * between as work-code.
843 */
844 void
845 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
846 void *data )
847 {
848 VSsSemReq reqData;
850 //
851 reqData.reqType = atomic;
852 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
853 reqData.dataForFn = data;
855 VMS_WL__send_sem_request( &reqData, currVP );
856 }
859 /*This suspends to the master.
860 *First, it looks at the VP's data, to see the highest transactionID that VP
861 * already has entered. If the current ID is not larger, it throws an
862 * exception stating a bug in the code. Otherwise it puts the current ID
863 * there, and adds the ID to a linked list of IDs entered -- the list is
864 * used to check that exits are properly ordered.
865 *Next it is uses transactionID as index into an array of transaction
866 * structures.
867 *If the "VP_currently_executing" field is non-null, then put requesting VP
868 * into queue in the struct. (At some point a holder will request
869 * end-transaction, which will take this VP from the queue and resume it.)
870 *If NULL, then write requesting into the field and resume.
871 */
872 void
873 VSs__start_transaction( int32 transactionID )
874 {
875 VSsSemReq reqData;
877 //
878 reqData.callingSlv = currVP;
879 reqData.reqType = trans_start;
880 reqData.transID = transactionID;
882 VMS_WL__send_sem_request( &reqData, currVP );
883 }
885 /*This suspends to the master, then uses transactionID as index into an
886 * array of transaction structures.
887 *It looks at VP_currently_executing to be sure it's same as requesting VP.
888 * If different, throws an exception, stating there's a bug in the code.
889 *Next it looks at the queue in the structure.
890 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
891 *If something in, gets it, sets VP_currently_executing to that VP, then
892 * resumes both.
893 */
894 void
895 VSs__end_transaction( int32 transactionID )
896 {
897 VSsSemReq reqData;
899 //
900 reqData.callingSlv = currVP;
901 reqData.reqType = trans_end;
902 reqData.transID = transactionID;
904 VMS_WL__send_sem_request( &reqData, currVP );
905 }
907 //======================== Internal ==================================
908 /*
909 */
910 SlaveVP *
911 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
912 SlaveVP *creatingSlv )
913 { VSsSemReq reqData;
915 //the semantic request data is on the stack and disappears when this
916 // call returns -- it's guaranteed to remain in the VP's stack for as
917 // long as the VP is suspended.
918 reqData.reqType = 0; //know type because in a VMS create req
919 reqData.coreToAssignOnto = -1; //means round-robin assign
920 reqData.fnPtr = fnPtr;
921 reqData.initData = initData;
922 reqData.callingSlv = creatingSlv;
924 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
926 return creatingSlv->dataRetFromReq;
927 }
929 SlaveVP *
930 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
931 SlaveVP *creatingSlv, int32 coreToAssignOnto )
932 { VSsSemReq reqData;
934 //the semantic request data is on the stack and disappears when this
935 // call returns -- it's guaranteed to remain in the VP's stack for as
936 // long as the VP is suspended.
937 reqData.reqType = create_slave_w_aff; //not used, May 2012
938 reqData.coreToAssignOnto = coreToAssignOnto;
939 reqData.fnPtr = fnPtr;
940 reqData.initData = initData;
941 reqData.callingSlv = creatingSlv;
943 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
945 return creatingSlv->dataRetFromReq;
946 }
948 int __main_ret;
950 void __entry_point(void* _args) {
951 __main_args* args = (__main_args*) _args;
952 __main_ret = __program_main(args->argc, args->argv);
953 }
955 #undef main
957 int main(int argc, char** argv) {
958 __main_args args;
959 args.argc = argc;
960 args.argv = argv;
961 VSs__create_seed_slave_and_do_work(__entry_point, (void*) &args);
962 return __main_ret;
963 }
