Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VSs_impls > VSs__MC_shared_impl
view VSs.c @ 44:7e7f37aa2f61
fix barrier
author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
---|---|
date | Tue, 11 Jun 2013 15:40:51 +0200 |
parents | 8733d1299c3a |
children | 19114b7a2357 |
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 //For each animation slot, there is an idle slave, and an initial
216 // slave assigned as the current-task-slave. Create them here.
217 SlaveVP *idleSlv, *slotTaskSlv;
218 for( coreNum = 0; coreNum < NUM_CORES; coreNum++ )
219 { semanticEnv->coreIsDone[coreNum] = FALSE; //use during shutdown
221 for( slotNum = 0; slotNum < NUM_ANIM_SLOTS; ++slotNum )
222 {
223 #ifdef IDLE_SLAVES
224 idleSlv = VSs__create_slave_helper( &VSs__run_thread, &idle_fn, NULL, semanticEnv, 0);
225 idleSlv->coreAnimatedBy = coreNum;
226 idleSlv->animSlotAssignedTo =
227 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
228 semanticEnv->idleSlv[coreNum][slotNum] = idleSlv;
229 #endif
231 slotTaskSlv = VSs__create_slave_helper(&VSs__run_thread, &idle_fn, NULL, semanticEnv, 0);
232 slotTaskSlv->coreAnimatedBy = coreNum;
233 slotTaskSlv->animSlotAssignedTo =
234 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
236 semData = slotTaskSlv->semanticData;
237 semData->needsTaskAssigned = TRUE;
238 semData->slaveType = SlotTaskSlv;
239 semanticEnv->slotTaskSlvs[coreNum][slotNum] = slotTaskSlv;
240 }
241 }
243 //create the ready queues, hash tables used for matching and so forth
244 semanticEnv->slavesReadyToResumeQ = makeVMSQ();
245 semanticEnv->freeExtraTaskSlvQ = makeVMSQ();
246 semanticEnv->taskReadyQ = makeVMSQ();
247 semanticEnv->barrierQ = makeVMSQ();
249 semanticEnv->argPtrHashTbl = makeHashTable32( 20, &free_pointer_entry );
250 semanticEnv->commHashTbl = makeHashTable32( 16, &VMS_int__free );
251 semanticEnv->criticalHashTbl = makeHashTable32( 16, &VMS_int__free );
253 semanticEnv->nextCoreToGetNewSlv = 0;
255 semanticEnv->numInFlightTasks = 0;
256 semanticEnv->deferredSubmitsQ = makeVMSQ();
257 #ifdef EXTERNAL_SCHEDULER
258 VSs__init_ext_scheduler();
259 #endif
260 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
261 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
262 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
263 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
264 {
265 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
266 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
267 semanticEnv->fnSingletons[i].hasFinished = FALSE;
268 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
269 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
270 }
272 semanticEnv->numLiveExtraTaskSlvs = 0; //must be last
273 semanticEnv->numLiveThreadSlvs = 1; //must be last, counts the seed
275 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
276 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
277 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
278 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
279 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
280 semanticEnv->dataDependenciesList = makeListOfArrays(sizeof(Dependency),128);
281 semanticEnv->singletonDependenciesList = makeListOfArrays(sizeof(Dependency),128);
282 semanticEnv->warDependenciesList = makeListOfArrays(sizeof(Dependency),128);
283 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
285 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
286 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
287 #endif
288 }
291 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
292 */
293 void
294 VSs__cleanup_after_shutdown()
295 { VSsSemEnv *semanticEnv;
297 semanticEnv = _VMSMasterEnv->semanticEnv;
299 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
300 FILE* output;
301 int n;
302 char filename[255];
303 //UCC
304 for(n=0;n<255;n++)
305 {
306 sprintf(filename, "./counters/UCC.%d",n);
307 output = fopen(filename,"r");
308 if(output)
309 {
310 fclose(output);
311 }else{
312 break;
313 }
314 }
315 if(n<255){
316 printf("Saving UCC to File: %s ...\n", filename);
317 output = fopen(filename,"w+");
318 if(output!=NULL){
319 set_dependency_file(output);
320 //fprintf(output,"digraph Dependencies {\n");
321 //set_dot_file(output);
322 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
323 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
324 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
325 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
326 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
327 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
328 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
329 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
330 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
331 //fprintf(output,"}\n");
332 fflush(output);
334 } else
335 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
336 } else {
337 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
338 }
339 //Loop Graph
340 for(n=0;n<255;n++)
341 {
342 sprintf(filename, "./counters/LoopGraph.%d",n);
343 output = fopen(filename,"r");
344 if(output)
345 {
346 fclose(output);
347 }else{
348 break;
349 }
350 }
351 if(n<255){
352 printf("Saving LoopGraph to File: %s ...\n", filename);
353 output = fopen(filename,"w+");
354 if(output!=NULL){
355 set_dependency_file(output);
356 //fprintf(output,"digraph Dependencies {\n");
357 //set_dot_file(output);
358 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
359 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
360 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
361 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
362 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
363 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
364 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
365 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
366 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
367 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
368 //fprintf(output,"}\n");
369 fflush(output);
371 } else
372 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
373 } else {
374 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
375 }
378 freeListOfArrays(semanticEnv->unitList);
379 freeListOfArrays(semanticEnv->commDependenciesList);
380 freeListOfArrays(semanticEnv->ctlDependenciesList);
381 freeListOfArrays(semanticEnv->dynDependenciesList);
382 freeListOfArrays(semanticEnv->dataDependenciesList);
383 freeListOfArrays(semanticEnv->warDependenciesList);
384 freeListOfArrays(semanticEnv->singletonDependenciesList);
385 freeListOfArrays(semanticEnv->hwArcs);
387 #endif
388 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
389 FILE* output2;
390 int n2;
391 char filename2[255];
392 for(n2=0;n2<255;n2++)
393 {
394 sprintf(filename2, "./counters/Counters.%d.csv",n2);
395 output2 = fopen(filename2,"r");
396 if(output2)
397 {
398 fclose(output2);
399 }else{
400 break;
401 }
402 }
403 if(n2<255){
404 printf("Saving Counter measurements to File: %s ...\n", filename2);
405 output2 = fopen(filename2,"w+");
406 if(output2!=NULL){
407 set_counter_file(output2);
408 int i;
409 for(i=0;i<NUM_CORES;i++){
410 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
411 fflush(output2);
412 }
414 } else
415 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
416 } else {
417 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
418 }
420 #endif
421 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
422 * nothing to do here */
423 //_VMSMasterEnv->shutdownInitiated = TRUE;
424 int coreIdx, slotIdx;
425 SlaveVP* slotSlv;
426 for (coreIdx = 0; coreIdx < NUM_CORES; coreIdx++) {
427 for (slotIdx = 0; slotIdx < NUM_ANIM_SLOTS; slotIdx++) {
428 slotSlv = semanticEnv->slotTaskSlvs[coreIdx][slotIdx];
429 VMS_int__free(slotSlv->semanticData);
430 VMS_int__dissipate_slaveVP(slotSlv);
431 #ifdef IDLE_SLAVES
432 slotSlv = semanticEnv->idleSlv[coreIdx][slotIdx];
433 VMS_int__free(slotSlv->semanticData);
434 VMS_int__dissipate_slaveVP(slotSlv);
435 #endif
436 }
437 }
438 int i;
439 for (i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++) {
440 freePrivQ(semanticEnv->fnSingletons[i].waitQ);
441 freePrivQ(semanticEnv->transactionStrucs[i].waitingVPQ);
442 }
444 freePrivQ(semanticEnv->freeExtraTaskSlvQ);
445 freePrivQ(semanticEnv->slavesReadyToResumeQ);
446 freePrivQ(semanticEnv->taskReadyQ);
447 freePrivQ(semanticEnv->barrierQ);
448 freePrivQ(semanticEnv->deferredSubmitsQ);
449 freeHashTable(semanticEnv->argPtrHashTbl);
450 freeHashTable(semanticEnv->commHashTbl);
451 freeHashTable(semanticEnv->criticalHashTbl);
452 VMS_int__free(semanticEnv->coreIsDone);
453 VMS_int__free(_VMSMasterEnv->semanticEnv);
455 VMS_SS__cleanup_at_end_of_shutdown();
456 }
459 //===========================================================================
461 SlaveVP *
462 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
463 SlaveVP *creatingThd )
464 { VSsSemReq reqData;
466 //the semantic request data is on the stack and disappears when this
467 // call returns -- it's guaranteed to remain in the VP's stack for as
468 // long as the VP is suspended.
469 reqData.reqType = 0; //know type because in a VMS create req
470 reqData.fnPtr = fnPtr;
471 reqData.initData = initData;
472 reqData.callingSlv = creatingThd;
474 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
476 return creatingThd->dataRetFromReq;
477 }
479 /*This is always the last thing done in the code animated by a thread VP.
480 * Normally, this would be the last line of the thread's top level function.
481 * But, if the thread exits from any point, it has to do so by calling
482 * this.
483 *
484 *It simply sends a dissipate request, which handles all the state cleanup.
485 */
486 void
487 VSs__end_thread()
488 {
490 VMS_WL__send_dissipate_req( currVP );
491 }
493 void VSs__run_thread(TopLevelFnPtr fnPtr, void *initData){
494 (*fnPtr)(initData);
495 VSs__end_thread();
496 }
498 //===========================================================================
501 //======================= task submit and end ==============================
503 /*
504 */
505 void VSs__submit_task(VSsTaskType *taskType, void *args, void* deps) {
506 VSsSemReq reqData;
508 reqData.reqType = submit_task;
510 reqData.taskType = taskType;
511 reqData.args = args;
512 reqData.deps = deps;
513 reqData.callingSlv = currVP;
515 reqData.taskID = NULL;
517 VMS_WL__send_sem_request(&reqData, currVP);
518 }
520 int32 *
521 VSs__create_taskID_of_size( int32 numInts)
522 { int32 *taskID;
524 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
525 taskID[0] = numInts;
526 return taskID;
527 }
529 void VSs__submit_task_with_ID(VSsTaskType *taskType, void *args, void* deps, int32 *taskID) {
530 VSsSemReq reqData;
532 reqData.reqType = submit_task;
534 reqData.taskType = taskType;
535 reqData.args = args;
536 reqData.deps = deps;
537 reqData.taskID = taskID;
538 reqData.callingSlv = currVP;
540 VMS_WL__send_sem_request(&reqData, currVP);
541 }
544 /*This call is the last to happen in every task. It causes the slave to
545 * suspend and get the next task out of the task-queue. Notice there is no
546 * assigner here.. only one slave, no slave ReadyQ, and so on..
547 *Can either make the assigner take the next task out of the taskQ, or can
548 * leave all as it is, and make task-end take the next task.
549 *Note: this fits the case in the new VMS for no-context tasks, so will use
550 * the built-in taskQ of new VMS, and should be local and much faster.
551 *
552 *The task-stub is saved in the animSlv, so the request handler will get it
553 * from there, along with the task-type which has arg types, and so on..
554 *
555 * NOTE: if want, don't need to send the animating SlaveVP around..
556 * instead, can make a single slave per core, and coreCtrlr looks up the
557 * slave from having the core number.
558 *
559 *But, to stay compatible with all the other VMS languages, leave it in..
560 */
561 void
562 VSs__end_task()
563 { VSsSemReq reqData;
565 reqData.reqType = end_task;
566 reqData.callingSlv = currVP;
568 VMS_WL__send_sem_request( &reqData, currVP );
569 }
571 void VSs__run_task(TopLevelFnPtr fnPtr, void *initData){
572 (*fnPtr)(initData);
573 VSs__end_task();
574 }
576 void
577 VSs__taskwait()
578 {
579 VSsSemReq reqData;
581 reqData.reqType = taskwait;
582 reqData.callingSlv = currVP;
584 VMS_WL__send_sem_request( &reqData, currVP );
585 }
587 void
588 VSs__taskwait_on(void* ptr){
589 VSsSemReq reqData;
591 reqData.reqType = taskwait_on;
592 reqData.callingSlv = currVP;
594 reqData.args = ptr;
596 VMS_WL__send_sem_request( &reqData, currVP );
597 }
599 void
600 VSs__start_critical(void* lock){
601 VSsSemReq reqData;
603 reqData.reqType = critical_start;
604 reqData.callingSlv = currVP;
606 reqData.criticalID = lock;
608 VMS_WL__send_sem_request( &reqData, currVP );
609 }
611 void
612 VSs__end_critical(void* lock){
613 VSsSemReq reqData;
615 reqData.reqType = critical_end;
616 reqData.callingSlv = currVP;
618 reqData.criticalID = lock;
620 VMS_WL__send_sem_request( &reqData, currVP );
621 }
623 //========================== send and receive ============================
624 //
626 int32 *
627 VSs__give_self_taskID()
628 {
629 return ((VSsSemData*)currVP->semanticData)->taskStub->taskID;
630 }
632 //================================ send ===================================
634 void
635 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID)
636 { VSsSemReq reqData;
638 reqData.reqType = send_type_to;
640 reqData.msg = msg;
641 reqData.msgType = type;
642 reqData.receiverID = receiverID;
643 reqData.senderSlv = currVP;
645 reqData.nextReqInHashEntry = NULL;
647 VMS_WL__send_sem_request( &reqData, currVP );
649 //When come back from suspend, no longer own data reachable from msg
650 }
652 void
653 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID)
654 { VSsSemReq reqData;
656 reqData.reqType = send_from_to;
658 reqData.msg = msg;
659 reqData.senderID = senderID;
660 reqData.receiverID = receiverID;
661 reqData.senderSlv = currVP;
663 reqData.nextReqInHashEntry = NULL;
665 VMS_WL__send_sem_request( &reqData, currVP );
666 }
669 //================================ receive ================================
671 /*The "type" version of send and receive creates a many-to-one relationship.
672 * The sender is anonymous, and many sends can stack up, waiting to be
673 * received. The same receiver can also have send from-to's
674 * waiting for it, and those will be kept separate from the "type"
675 * messages.
676 */
677 void *
678 VSs__receive_type_to( const int32 type, int32* receiverID )
679 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
680 VSsSemReq reqData;
682 reqData.reqType = receive_type_to;
684 reqData.msgType = type;
685 reqData.receiverID = receiverID;
686 reqData.receiverSlv = currVP;
688 reqData.nextReqInHashEntry = NULL;
690 VMS_WL__send_sem_request( &reqData, currVP );
692 return currVP->dataRetFromReq;
693 }
697 /*Call this at the point a receiving task wants in-coming data.
698 * Use this from-to form when know senderID -- it makes a direct channel
699 * between sender and receiver.
700 */
701 void *
702 VSs__receive_from_to( int32 *senderID, int32 *receiverID)
703 {
704 VSsSemReq reqData;
706 reqData.reqType = receive_from_to;
708 reqData.senderID = senderID;
709 reqData.receiverID = receiverID;
710 reqData.receiverSlv = currVP;
712 reqData.nextReqInHashEntry = NULL;
713 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
715 VMS_WL__send_sem_request( &reqData, currVP );
717 return currVP->dataRetFromReq;
718 }
723 //==========================================================================
724 //
725 /*A function singleton is a function whose body executes exactly once, on a
726 * single core, no matter how many times the fuction is called and no
727 * matter how many cores or the timing of cores calling it.
728 *
729 *A data singleton is a ticket attached to data. That ticket can be used
730 * to get the data through the function exactly once, no matter how many
731 * times the data is given to the function, and no matter the timing of
732 * trying to get the data through from different cores.
733 */
735 /*asm function declarations*/
736 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
737 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
739 /*Fn singleton uses ID as index into array of singleton structs held in the
740 * semantic environment.
741 */
742 void
743 VSs__start_fn_singleton( int32 singletonID)
744 {
745 VSsSemReq reqData;
747 //
748 reqData.reqType = singleton_fn_start;
749 reqData.singletonID = singletonID;
751 VMS_WL__send_sem_request( &reqData, currVP );
752 if( currVP->dataRetFromReq ) //will be 0 or addr of label in end singleton
753 {
754 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( currVP );
755 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
756 }
757 }
759 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
760 * The start_data_singleton makes the structure and puts its addr into the
761 * location.
762 */
763 void
764 VSs__start_data_singleton( VSsSingleton **singletonAddr )
765 {
766 VSsSemReq reqData;
768 if( *singletonAddr && (*singletonAddr)->hasFinished )
769 goto JmpToEndSingleton;
771 reqData.reqType = singleton_data_start;
772 reqData.singletonPtrAddr = singletonAddr;
774 VMS_WL__send_sem_request( &reqData, currVP );
775 if( currVP->dataRetFromReq ) //either 0 or end singleton's return addr
776 { //Assembly code changes the return addr on the stack to the one
777 // saved into the singleton by the end-singleton-fn
778 //The return addr is at 0x4(%%ebp)
779 JmpToEndSingleton:
780 asm_write_ret_from_singleton(*singletonAddr);
781 }
782 //now, simply return
783 //will exit either from the start singleton call or the end-singleton call
784 }
786 /*Uses ID as index into array of flags. If flag already set, resumes from
787 * end-label. Else, sets flag and resumes normally.
788 *
789 *Note, this call cannot be inlined because the instr addr at the label
790 * inside is shared by all invocations of a given singleton ID.
791 */
792 void
793 VSs__end_fn_singleton( int32 singletonID )
794 {
795 VSsSemReq reqData;
797 //don't need this addr until after at least one singleton has reached
798 // this function
799 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( currVP );
800 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
802 reqData.reqType = singleton_fn_end;
803 reqData.singletonID = singletonID;
805 VMS_WL__send_sem_request( &reqData, currVP );
807 //EndSingletonInstrAddr:
808 return;
809 }
811 void
812 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr )
813 {
814 VSsSemReq reqData;
816 //don't need this addr until after singleton struct has reached
817 // this function for first time
818 //do assembly that saves the return addr of this fn call into the
819 // data singleton -- that data-singleton can only be given to exactly
820 // one instance in the code of this function. However, can use this
821 // function in different places for different data-singletons.
822 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
825 asm_save_ret_to_singleton(*singletonPtrAddr);
827 reqData.reqType = singleton_data_end;
828 reqData.singletonPtrAddr = singletonPtrAddr;
830 VMS_WL__send_sem_request( &reqData, currVP );
831 }
833 /*This executes the function in the masterVP, so it executes in isolation
834 * from any other copies -- only one copy of the function can ever execute
835 * at a time.
836 *
837 *It suspends to the master, and the request handler takes the function
838 * pointer out of the request and calls it, then resumes the VP.
839 *Only very short functions should be called this way -- for longer-running
840 * isolation, use transaction-start and transaction-end, which run the code
841 * between as work-code.
842 */
843 void
844 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
845 void *data )
846 {
847 VSsSemReq reqData;
849 //
850 reqData.reqType = atomic;
851 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
852 reqData.dataForFn = data;
854 VMS_WL__send_sem_request( &reqData, currVP );
855 }
858 /*This suspends to the master.
859 *First, it looks at the VP's data, to see the highest transactionID that VP
860 * already has entered. If the current ID is not larger, it throws an
861 * exception stating a bug in the code. Otherwise it puts the current ID
862 * there, and adds the ID to a linked list of IDs entered -- the list is
863 * used to check that exits are properly ordered.
864 *Next it is uses transactionID as index into an array of transaction
865 * structures.
866 *If the "VP_currently_executing" field is non-null, then put requesting VP
867 * into queue in the struct. (At some point a holder will request
868 * end-transaction, which will take this VP from the queue and resume it.)
869 *If NULL, then write requesting into the field and resume.
870 */
871 void
872 VSs__start_transaction( int32 transactionID )
873 {
874 VSsSemReq reqData;
876 //
877 reqData.callingSlv = currVP;
878 reqData.reqType = trans_start;
879 reqData.transID = transactionID;
881 VMS_WL__send_sem_request( &reqData, currVP );
882 }
884 /*This suspends to the master, then uses transactionID as index into an
885 * array of transaction structures.
886 *It looks at VP_currently_executing to be sure it's same as requesting VP.
887 * If different, throws an exception, stating there's a bug in the code.
888 *Next it looks at the queue in the structure.
889 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
890 *If something in, gets it, sets VP_currently_executing to that VP, then
891 * resumes both.
892 */
893 void
894 VSs__end_transaction( int32 transactionID )
895 {
896 VSsSemReq reqData;
898 //
899 reqData.callingSlv = currVP;
900 reqData.reqType = trans_end;
901 reqData.transID = transactionID;
903 VMS_WL__send_sem_request( &reqData, currVP );
904 }
906 //======================== Internal ==================================
907 /*
908 */
909 SlaveVP *
910 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
911 SlaveVP *creatingSlv )
912 { VSsSemReq reqData;
914 //the semantic request data is on the stack and disappears when this
915 // call returns -- it's guaranteed to remain in the VP's stack for as
916 // long as the VP is suspended.
917 reqData.reqType = 0; //know type because in a VMS create req
918 reqData.coreToAssignOnto = -1; //means round-robin assign
919 reqData.fnPtr = fnPtr;
920 reqData.initData = initData;
921 reqData.callingSlv = creatingSlv;
923 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
925 return creatingSlv->dataRetFromReq;
926 }
928 SlaveVP *
929 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
930 SlaveVP *creatingSlv, int32 coreToAssignOnto )
931 { VSsSemReq reqData;
933 //the semantic request data is on the stack and disappears when this
934 // call returns -- it's guaranteed to remain in the VP's stack for as
935 // long as the VP is suspended.
936 reqData.reqType = create_slave_w_aff; //not used, May 2012
937 reqData.coreToAssignOnto = coreToAssignOnto;
938 reqData.fnPtr = fnPtr;
939 reqData.initData = initData;
940 reqData.callingSlv = creatingSlv;
942 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
944 return creatingSlv->dataRetFromReq;
945 }
947 int __main_ret;
949 void __entry_point(void* _args) {
950 __main_args* args = (__main_args*) _args;
951 __main_ret = __program_main(args->argc, args->argv);
952 }
954 #undef main
956 int main(int argc, char** argv) {
957 __main_args args;
958 args.argc = argc;
959 args.argv = argv;
960 VSs__create_seed_slave_and_do_work(__entry_point, (void*) &args);
961 return __main_ret;
962 }