Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VSs_impls > VSs__MC_shared_impl
view VSs.c @ 40:df464a215387
add implementations of (some) nanos api functions
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Mon, 03 Jun 2013 18:49:19 +0200 |
| parents | a951b38d2cfc |
| children | 8733d1299c3a |
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();
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->deferredSubmitsQ);
448 freeHashTable(semanticEnv->argPtrHashTbl);
449 freeHashTable(semanticEnv->commHashTbl);
450 freeHashTable(semanticEnv->criticalHashTbl);
451 VMS_int__free(semanticEnv->coreIsDone);
452 VMS_int__free(_VMSMasterEnv->semanticEnv);
454 VMS_SS__cleanup_at_end_of_shutdown();
455 }
458 //===========================================================================
460 SlaveVP *
461 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
462 SlaveVP *creatingThd )
463 { VSsSemReq reqData;
465 //the semantic request data is on the stack and disappears when this
466 // call returns -- it's guaranteed to remain in the VP's stack for as
467 // long as the VP is suspended.
468 reqData.reqType = 0; //know type because in a VMS create req
469 reqData.fnPtr = fnPtr;
470 reqData.initData = initData;
471 reqData.callingSlv = creatingThd;
473 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
475 return creatingThd->dataRetFromReq;
476 }
478 /*This is always the last thing done in the code animated by a thread VP.
479 * Normally, this would be the last line of the thread's top level function.
480 * But, if the thread exits from any point, it has to do so by calling
481 * this.
482 *
483 *It simply sends a dissipate request, which handles all the state cleanup.
484 */
485 void
486 VSs__end_thread()
487 {
489 VMS_WL__send_dissipate_req( currVP );
490 }
492 void VSs__run_thread(TopLevelFnPtr fnPtr, void *initData){
493 (*fnPtr)(initData);
494 VSs__end_thread();
495 }
497 //===========================================================================
500 //======================= task submit and end ==============================
502 /*
503 */
504 void VSs__submit_task(VSsTaskType *taskType, void *args, void* deps) {
505 VSsSemReq reqData;
507 reqData.reqType = submit_task;
509 reqData.taskType = taskType;
510 reqData.args = args;
511 reqData.deps = deps;
512 reqData.callingSlv = currVP;
514 reqData.taskID = NULL;
516 VMS_WL__send_sem_request(&reqData, currVP);
517 }
519 int32 *
520 VSs__create_taskID_of_size( int32 numInts)
521 { int32 *taskID;
523 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
524 taskID[0] = numInts;
525 return taskID;
526 }
528 void VSs__submit_task_with_ID(VSsTaskType *taskType, void *args, void* deps, int32 *taskID) {
529 VSsSemReq reqData;
531 reqData.reqType = submit_task;
533 reqData.taskType = taskType;
534 reqData.args = args;
535 reqData.deps = deps;
536 reqData.taskID = taskID;
537 reqData.callingSlv = currVP;
539 VMS_WL__send_sem_request(&reqData, currVP);
540 }
543 /*This call is the last to happen in every task. It causes the slave to
544 * suspend and get the next task out of the task-queue. Notice there is no
545 * assigner here.. only one slave, no slave ReadyQ, and so on..
546 *Can either make the assigner take the next task out of the taskQ, or can
547 * leave all as it is, and make task-end take the next task.
548 *Note: this fits the case in the new VMS for no-context tasks, so will use
549 * the built-in taskQ of new VMS, and should be local and much faster.
550 *
551 *The task-stub is saved in the animSlv, so the request handler will get it
552 * from there, along with the task-type which has arg types, and so on..
553 *
554 * NOTE: if want, don't need to send the animating SlaveVP around..
555 * instead, can make a single slave per core, and coreCtrlr looks up the
556 * slave from having the core number.
557 *
558 *But, to stay compatible with all the other VMS languages, leave it in..
559 */
560 void
561 VSs__end_task()
562 { VSsSemReq reqData;
564 reqData.reqType = end_task;
565 reqData.callingSlv = currVP;
567 VMS_WL__send_sem_request( &reqData, currVP );
568 }
570 void VSs__run_task(TopLevelFnPtr fnPtr, void *initData){
571 (*fnPtr)(initData);
572 VSs__end_task();
573 }
575 void
576 VSs__taskwait()
577 {
578 VSsSemReq reqData;
580 reqData.reqType = taskwait;
581 reqData.callingSlv = currVP;
583 VMS_WL__send_sem_request( &reqData, currVP );
584 }
586 void
587 VSs__taskwait_on(void* ptr){
588 VSsSemReq reqData;
590 reqData.reqType = taskwait_on;
591 reqData.callingSlv = currVP;
593 reqData.args = ptr;
595 VMS_WL__send_sem_request( &reqData, currVP );
596 }
598 void
599 VSs__start_critical(void* lock){
600 VSsSemReq reqData;
602 reqData.reqType = critical_start;
603 reqData.callingSlv = currVP;
605 reqData.criticalID = lock;
607 VMS_WL__send_sem_request( &reqData, currVP );
608 }
610 void
611 VSs__end_critical(void* lock){
612 VSsSemReq reqData;
614 reqData.reqType = critical_end;
615 reqData.callingSlv = currVP;
617 reqData.criticalID = lock;
619 VMS_WL__send_sem_request( &reqData, currVP );
620 }
622 //========================== send and receive ============================
623 //
625 int32 *
626 VSs__give_self_taskID()
627 {
628 return ((VSsSemData*)currVP->semanticData)->taskStub->taskID;
629 }
631 //================================ send ===================================
633 void
634 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID)
635 { VSsSemReq reqData;
637 reqData.reqType = send_type_to;
639 reqData.msg = msg;
640 reqData.msgType = type;
641 reqData.receiverID = receiverID;
642 reqData.senderSlv = currVP;
644 reqData.nextReqInHashEntry = NULL;
646 VMS_WL__send_sem_request( &reqData, currVP );
648 //When come back from suspend, no longer own data reachable from msg
649 }
651 void
652 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID)
653 { VSsSemReq reqData;
655 reqData.reqType = send_from_to;
657 reqData.msg = msg;
658 reqData.senderID = senderID;
659 reqData.receiverID = receiverID;
660 reqData.senderSlv = currVP;
662 reqData.nextReqInHashEntry = NULL;
664 VMS_WL__send_sem_request( &reqData, currVP );
665 }
668 //================================ receive ================================
670 /*The "type" version of send and receive creates a many-to-one relationship.
671 * The sender is anonymous, and many sends can stack up, waiting to be
672 * received. The same receiver can also have send from-to's
673 * waiting for it, and those will be kept separate from the "type"
674 * messages.
675 */
676 void *
677 VSs__receive_type_to( const int32 type, int32* receiverID )
678 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
679 VSsSemReq reqData;
681 reqData.reqType = receive_type_to;
683 reqData.msgType = type;
684 reqData.receiverID = receiverID;
685 reqData.receiverSlv = currVP;
687 reqData.nextReqInHashEntry = NULL;
689 VMS_WL__send_sem_request( &reqData, currVP );
691 return currVP->dataRetFromReq;
692 }
696 /*Call this at the point a receiving task wants in-coming data.
697 * Use this from-to form when know senderID -- it makes a direct channel
698 * between sender and receiver.
699 */
700 void *
701 VSs__receive_from_to( int32 *senderID, int32 *receiverID)
702 {
703 VSsSemReq reqData;
705 reqData.reqType = receive_from_to;
707 reqData.senderID = senderID;
708 reqData.receiverID = receiverID;
709 reqData.receiverSlv = currVP;
711 reqData.nextReqInHashEntry = NULL;
712 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
714 VMS_WL__send_sem_request( &reqData, currVP );
716 return currVP->dataRetFromReq;
717 }
722 //==========================================================================
723 //
724 /*A function singleton is a function whose body executes exactly once, on a
725 * single core, no matter how many times the fuction is called and no
726 * matter how many cores or the timing of cores calling it.
727 *
728 *A data singleton is a ticket attached to data. That ticket can be used
729 * to get the data through the function exactly once, no matter how many
730 * times the data is given to the function, and no matter the timing of
731 * trying to get the data through from different cores.
732 */
734 /*asm function declarations*/
735 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
736 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
738 /*Fn singleton uses ID as index into array of singleton structs held in the
739 * semantic environment.
740 */
741 void
742 VSs__start_fn_singleton( int32 singletonID)
743 {
744 VSsSemReq reqData;
746 //
747 reqData.reqType = singleton_fn_start;
748 reqData.singletonID = singletonID;
750 VMS_WL__send_sem_request( &reqData, currVP );
751 if( currVP->dataRetFromReq ) //will be 0 or addr of label in end singleton
752 {
753 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( currVP );
754 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
755 }
756 }
758 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
759 * The start_data_singleton makes the structure and puts its addr into the
760 * location.
761 */
762 void
763 VSs__start_data_singleton( VSsSingleton **singletonAddr )
764 {
765 VSsSemReq reqData;
767 if( *singletonAddr && (*singletonAddr)->hasFinished )
768 goto JmpToEndSingleton;
770 reqData.reqType = singleton_data_start;
771 reqData.singletonPtrAddr = singletonAddr;
773 VMS_WL__send_sem_request( &reqData, currVP );
774 if( currVP->dataRetFromReq ) //either 0 or end singleton's return addr
775 { //Assembly code changes the return addr on the stack to the one
776 // saved into the singleton by the end-singleton-fn
777 //The return addr is at 0x4(%%ebp)
778 JmpToEndSingleton:
779 asm_write_ret_from_singleton(*singletonAddr);
780 }
781 //now, simply return
782 //will exit either from the start singleton call or the end-singleton call
783 }
785 /*Uses ID as index into array of flags. If flag already set, resumes from
786 * end-label. Else, sets flag and resumes normally.
787 *
788 *Note, this call cannot be inlined because the instr addr at the label
789 * inside is shared by all invocations of a given singleton ID.
790 */
791 void
792 VSs__end_fn_singleton( int32 singletonID )
793 {
794 VSsSemReq reqData;
796 //don't need this addr until after at least one singleton has reached
797 // this function
798 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( currVP );
799 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
801 reqData.reqType = singleton_fn_end;
802 reqData.singletonID = singletonID;
804 VMS_WL__send_sem_request( &reqData, currVP );
806 //EndSingletonInstrAddr:
807 return;
808 }
810 void
811 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr )
812 {
813 VSsSemReq reqData;
815 //don't need this addr until after singleton struct has reached
816 // this function for first time
817 //do assembly that saves the return addr of this fn call into the
818 // data singleton -- that data-singleton can only be given to exactly
819 // one instance in the code of this function. However, can use this
820 // function in different places for different data-singletons.
821 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
824 asm_save_ret_to_singleton(*singletonPtrAddr);
826 reqData.reqType = singleton_data_end;
827 reqData.singletonPtrAddr = singletonPtrAddr;
829 VMS_WL__send_sem_request( &reqData, currVP );
830 }
832 /*This executes the function in the masterVP, so it executes in isolation
833 * from any other copies -- only one copy of the function can ever execute
834 * at a time.
835 *
836 *It suspends to the master, and the request handler takes the function
837 * pointer out of the request and calls it, then resumes the VP.
838 *Only very short functions should be called this way -- for longer-running
839 * isolation, use transaction-start and transaction-end, which run the code
840 * between as work-code.
841 */
842 void
843 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
844 void *data )
845 {
846 VSsSemReq reqData;
848 //
849 reqData.reqType = atomic;
850 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
851 reqData.dataForFn = data;
853 VMS_WL__send_sem_request( &reqData, currVP );
854 }
857 /*This suspends to the master.
858 *First, it looks at the VP's data, to see the highest transactionID that VP
859 * already has entered. If the current ID is not larger, it throws an
860 * exception stating a bug in the code. Otherwise it puts the current ID
861 * there, and adds the ID to a linked list of IDs entered -- the list is
862 * used to check that exits are properly ordered.
863 *Next it is uses transactionID as index into an array of transaction
864 * structures.
865 *If the "VP_currently_executing" field is non-null, then put requesting VP
866 * into queue in the struct. (At some point a holder will request
867 * end-transaction, which will take this VP from the queue and resume it.)
868 *If NULL, then write requesting into the field and resume.
869 */
870 void
871 VSs__start_transaction( int32 transactionID )
872 {
873 VSsSemReq reqData;
875 //
876 reqData.callingSlv = currVP;
877 reqData.reqType = trans_start;
878 reqData.transID = transactionID;
880 VMS_WL__send_sem_request( &reqData, currVP );
881 }
883 /*This suspends to the master, then uses transactionID as index into an
884 * array of transaction structures.
885 *It looks at VP_currently_executing to be sure it's same as requesting VP.
886 * If different, throws an exception, stating there's a bug in the code.
887 *Next it looks at the queue in the structure.
888 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
889 *If something in, gets it, sets VP_currently_executing to that VP, then
890 * resumes both.
891 */
892 void
893 VSs__end_transaction( int32 transactionID )
894 {
895 VSsSemReq reqData;
897 //
898 reqData.callingSlv = currVP;
899 reqData.reqType = trans_end;
900 reqData.transID = transactionID;
902 VMS_WL__send_sem_request( &reqData, currVP );
903 }
905 //======================== Internal ==================================
906 /*
907 */
908 SlaveVP *
909 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
910 SlaveVP *creatingSlv )
911 { VSsSemReq reqData;
913 //the semantic request data is on the stack and disappears when this
914 // call returns -- it's guaranteed to remain in the VP's stack for as
915 // long as the VP is suspended.
916 reqData.reqType = 0; //know type because in a VMS create req
917 reqData.coreToAssignOnto = -1; //means round-robin assign
918 reqData.fnPtr = fnPtr;
919 reqData.initData = initData;
920 reqData.callingSlv = creatingSlv;
922 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
924 return creatingSlv->dataRetFromReq;
925 }
927 SlaveVP *
928 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
929 SlaveVP *creatingSlv, int32 coreToAssignOnto )
930 { VSsSemReq reqData;
932 //the semantic request data is on the stack and disappears when this
933 // call returns -- it's guaranteed to remain in the VP's stack for as
934 // long as the VP is suspended.
935 reqData.reqType = create_slave_w_aff; //not used, May 2012
936 reqData.coreToAssignOnto = coreToAssignOnto;
937 reqData.fnPtr = fnPtr;
938 reqData.initData = initData;
939 reqData.callingSlv = creatingSlv;
941 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
943 return creatingSlv->dataRetFromReq;
944 }
946 int __main_ret;
948 void __entry_point(void* _args) {
949 __main_args* args = (__main_args*) _args;
950 __main_ret = __program_main(args->argc, args->argv);
951 }
953 #undef main
955 int main(int argc, char** argv) {
956 __main_args args;
957 args.argc = argc;
958 args.argv = argv;
959 VSs__create_seed_slave_and_do_work(__entry_point, (void*) &args);
960 return __main_ret;
961 }
