Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VSs_impls > VSs__MC_shared_impl
view VSs.c @ 2:f2ed1c379fe7
code nearly complete.. about to begin debugging
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Wed, 30 May 2012 15:02:38 -0700 |
| parents | 67a3a05a39c0 |
| children | 468b8638ff92 |
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 "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 *seedPr;
79 VSs__init(); //normal multi-thd
81 semEnv = _VMSMasterEnv->semanticEnv;
83 //VSs starts with one processor, which is put into initial environ,
84 // and which then calls create() to create more, thereby expanding work
85 seedPr = VSs__create_slave_helper( fnPtr, initData,
86 semEnv, semEnv->nextCoreToGetNewPr++ );
88 resume_slaveVP( seedPr, semEnv );
90 VMS_SS__start_the_work_then_wait_until_done(); //normal multi-thd
92 VSs__cleanup_after_shutdown();
93 }
96 int32
97 VSs__giveMinWorkUnitCycles( float32 percentOverhead )
98 {
99 return MIN_WORK_UNIT_CYCLES;
100 }
102 int32
103 VSs__giveIdealNumWorkUnits()
104 {
105 return NUM_ANIM_SLOTS * NUM_CORES;
106 }
108 int32
109 VSs__give_number_of_cores_to_schedule_onto()
110 {
111 return NUM_CORES;
112 }
114 /*For now, use TSC -- later, make these two macros with assembly that first
115 * saves jump point, and second jumps back several times to get reliable time
116 */
117 void
118 VSs__start_primitive()
119 { saveLowTimeStampCountInto( ((VSsSemEnv *)(_VMSMasterEnv->semanticEnv))->
120 primitiveStartTime );
121 }
123 /*Just quick and dirty for now -- make reliable later
124 * will want this to jump back several times -- to be sure cache is warm
125 * because don't want comm time included in calc-time measurement -- and
126 * also to throw out any "weird" values due to OS interrupt or TSC rollover
127 */
128 int32
129 VSs__end_primitive_and_give_cycles()
130 { int32 endTime, startTime;
131 //TODO: fix by repeating time-measurement
132 saveLowTimeStampCountInto( endTime );
133 startTime =((VSsSemEnv*)(_VMSMasterEnv->semanticEnv))->primitiveStartTime;
134 return (endTime - startTime);
135 }
137 //===========================================================================
139 /*Initializes all the data-structures for a VSs system -- but doesn't
140 * start it running yet!
141 *
142 *This runs in the main thread -- before VMS starts up
143 *
144 *This sets up the semantic layer over the VMS system
145 *
146 *First, calls VMS_Setup, then creates own environment, making it ready
147 * for creating the seed processor and then starting the work.
148 */
149 void
150 VSs__init()
151 {
152 VMS_SS__init();
153 //masterEnv, a global var, now is partially set up by init_VMS
154 // after this, have VMS_int__malloc and VMS_int__free available
156 VSs__init_Helper();
157 }
160 void idle_fn(void* data, SlaveVP *animatingSlv){
161 while(1){
162 VMS_int__suspend_slaveVP_and_send_req(animatingSlv);
163 }
164 }
166 void
167 VSs__init_Helper()
168 { VSsSemEnv *semanticEnv;
169 PrivQueueStruc **readyVPQs;
170 int coreIdx, i, j;
172 //Hook up the semantic layer's plug-ins to the Master virt procr
173 _VMSMasterEnv->requestHandler = &VSs__Request_Handler;
174 _VMSMasterEnv->slaveAssigner = &VSs__assign_slaveVP_to_slot;
175 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
176 _VMSMasterEnv->counterHandler = &VSs__counter_handler;
177 #endif
179 //create the semantic layer's environment (all its data) and add to
180 // the master environment
181 semanticEnv = VMS_int__malloc( sizeof( VSsSemEnv ) );
182 _VMSMasterEnv->semanticEnv = semanticEnv;
184 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
185 VSs__init_counter_data_structs();
186 #endif
187 semanticEnv->shutdownInitiated = FALSE;
188 for(i=0;i<NUM_CORES;++i){
189 for(j=0;j<NUM_ANIM_SLOTS;++j){
190 semanticEnv->idlePr[i][j] = VMS_int__create_slaveVP(&idle_fn,NULL);
191 semanticEnv->idlePr[i][j]->coreAnimatedBy = i;
192 }
193 }
195 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
196 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
197 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
198 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
199 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
200 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
202 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
203 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
204 #endif
206 //create the ready queue, hash tables used for pairing send to receive
207 // and so forth
208 //TODO: add hash tables for pairing sends with receives, and
209 // initialize the data ownership system
210 readyVPQs = VMS_int__malloc( NUM_CORES * sizeof(PrivQueueStruc *) );
212 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
213 {
214 readyVPQs[ coreIdx ] = makeVMSQ();
215 }
217 semanticEnv->readyVPQs = readyVPQs;
219 semanticEnv->nextCoreToGetNewPr = 0;
220 semanticEnv->numSlaveVP = 0;
222 semanticEnv->argPtrHashTbl = makeHashTable( 1<<16, &VMS_int__free );//start big
224 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
225 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
226 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
227 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
228 {
229 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
230 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
231 semanticEnv->fnSingletons[i].hasFinished = FALSE;
232 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
233 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
234 }
235 }
238 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
239 */
240 void
241 VSs__cleanup_after_shutdown()
242 { VSsSemEnv *semanticEnv;
244 semanticEnv = _VMSMasterEnv->semanticEnv;
246 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
247 //UCC
248 FILE* output;
249 int n;
250 char filename[255];
251 for(n=0;n<255;n++)
252 {
253 sprintf(filename, "./counters/UCC.%d",n);
254 output = fopen(filename,"r");
255 if(output)
256 {
257 fclose(output);
258 }else{
259 break;
260 }
261 }
262 if(n<255){
263 printf("Saving UCC to File: %s ...\n", filename);
264 output = fopen(filename,"w+");
265 if(output!=NULL){
266 set_dependency_file(output);
267 //fprintf(output,"digraph Dependencies {\n");
268 //set_dot_file(output);
269 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
270 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
271 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
272 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
273 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
274 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
275 //fprintf(output,"}\n");
276 fflush(output);
278 } else
279 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
280 } else {
281 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
282 }
283 //Loop Graph
284 for(n=0;n<255;n++)
285 {
286 sprintf(filename, "./counters/LoopGraph.%d",n);
287 output = fopen(filename,"r");
288 if(output)
289 {
290 fclose(output);
291 }else{
292 break;
293 }
294 }
295 if(n<255){
296 printf("Saving LoopGraph to File: %s ...\n", filename);
297 output = fopen(filename,"w+");
298 if(output!=NULL){
299 set_dependency_file(output);
300 //fprintf(output,"digraph Dependencies {\n");
301 //set_dot_file(output);
302 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
303 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
304 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
305 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
306 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
307 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
308 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
309 //fprintf(output,"}\n");
310 fflush(output);
312 } else
313 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
314 } else {
315 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
316 }
319 freeListOfArrays(semanticEnv->unitList);
320 freeListOfArrays(semanticEnv->commDependenciesList);
321 freeListOfArrays(semanticEnv->ctlDependenciesList);
322 freeListOfArrays(semanticEnv->dynDependenciesList);
324 #endif
325 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
326 for(n=0;n<255;n++)
327 {
328 sprintf(filename, "./counters/Counters.%d.csv",n);
329 output = fopen(filename,"r");
330 if(output)
331 {
332 fclose(output);
333 }else{
334 break;
335 }
336 }
337 if(n<255){
338 printf("Saving Counter measurements to File: %s ...\n", filename);
339 output = fopen(filename,"w+");
340 if(output!=NULL){
341 set_counter_file(output);
342 int i;
343 for(i=0;i<NUM_CORES;i++){
344 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
345 fflush(output);
346 }
348 } else
349 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
350 } else {
351 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
352 }
354 #endif
355 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
356 * nothing to do here
359 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
360 {
361 VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
362 VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
363 }
364 VMS_int__free( semanticEnv->readyVPQs );
366 freeHashTable( semanticEnv->commHashTbl );
367 VMS_int__free( _VMSMasterEnv->semanticEnv );
368 */
369 VMS_SS__cleanup_at_end_of_shutdown();
370 }
373 //===========================================================================
375 /*
376 */
377 SlaveVP *
378 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
379 SlaveVP *creatingPr )
380 { VSsSemReq reqData;
382 //the semantic request data is on the stack and disappears when this
383 // call returns -- it's guaranteed to remain in the VP's stack for as
384 // long as the VP is suspended.
385 reqData.reqType = 0; //know type because in a VMS create req
386 reqData.coreToAssignOnto = -1; //means round-robin assign
387 reqData.fnPtr = fnPtr;
388 reqData.initData = initData;
389 reqData.callingSlv = creatingPr;
391 VMS_WL__send_create_slaveVP_req( &reqData, creatingPr );
393 return creatingPr->dataRetFromReq;
394 }
396 SlaveVP *
397 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
398 SlaveVP *creatingPr, int32 coreToAssignOnto )
399 { VSsSemReq reqData;
401 //the semantic request data is on the stack and disappears when this
402 // call returns -- it's guaranteed to remain in the VP's stack for as
403 // long as the VP is suspended.
404 reqData.reqType = create_slave;
405 reqData.coreToAssignOnto = coreToAssignOnto;
406 reqData.fnPtr = fnPtr;
407 reqData.initData = initData;
408 reqData.callingSlv = creatingPr;
410 VMS_WL__send_create_slaveVP_req( &reqData, creatingPr );
412 return creatingPr->dataRetFromReq;
413 }
416 void
417 VSs__dissipate_slave( SlaveVP *slaveToDissipate )
418 {
419 VMS_WL__send_dissipate_req( slaveToDissipate );
420 }
423 //===========================================================================
426 //===========================================================================
427 /*Returns a taskID, which can be used to communicate between tasks with
428 * send-receive, or to use other kinds of constructs with tasks.
429 */
430 int32
431 VSs__submit_task( VSsTaskType *taskType, void *args, SlaveVP *animSlv)
432 { VSsSemReq reqData;
434 reqData.reqType = submit_task;
435 reqData.callingSlv = animSlv;
436 reqData.taskType = taskType;
437 reqData.args = args;
440 VMS_WL__send_sem_request( &reqData, animSlv );
441 return animSlv->dataRetFromReq;
442 }
444 /*NOTE: if want, don't need to send the animating SlaveVP around..
445 * instead, can make a single slave per core, and coreCtrlr looks up the
446 * slave from having the core number.
447 *
448 *But, to stay compatible with all the other VMS languages, leave it in..
449 *
450 *This call is the last to happen in every task. It causes the slave to
451 * suspend and get the next task out of the task-queue. Notice there is no
452 * assigner here.. only one slave, no slave ReadyQ, and so on..
453 *Can either make the assigner take the next task out of the taskQ, or can
454 * leave all as it is, and make task-end take the next task.
455 *Note: this fits the case in the new VMS for no-context tasks, so will use
456 * the built-in taskQ of new VMS, and should be local and much faster.
457 *
458 *The task-stub is saved in the animSlv, so the request handler will get it
459 * from there, along with the task-type which has arg types, and so on..
460 */
461 void
462 VSs__end_task( SlaveVP *animSlv )
463 { VSsSemReq reqData;
465 reqData.reqType = end_task;
466 reqData.callingSlv = animSlv;
468 VMS_WL__send_sem_request( &reqData, animSlv );
469 }
471 //==========================================================================
472 //
473 /*A function singleton is a function whose body executes exactly once, on a
474 * single core, no matter how many times the fuction is called and no
475 * matter how many cores or the timing of cores calling it.
476 *
477 *A data singleton is a ticket attached to data. That ticket can be used
478 * to get the data through the function exactly once, no matter how many
479 * times the data is given to the function, and no matter the timing of
480 * trying to get the data through from different cores.
481 */
483 /*asm function declarations*/
484 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
485 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
487 /*Fn singleton uses ID as index into array of singleton structs held in the
488 * semantic environment.
489 */
490 void
491 VSs__start_fn_singleton( int32 singletonID, SlaveVP *animPr )
492 {
493 VSsSemReq reqData;
495 //
496 reqData.reqType = singleton_fn_start;
497 reqData.singletonID = singletonID;
499 VMS_WL__send_sem_request( &reqData, animPr );
500 if( animPr->dataRetFromReq ) //will be 0 or addr of label in end singleton
501 {
502 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animPr );
503 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
504 }
505 }
507 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
508 * The start_data_singleton makes the structure and puts its addr into the
509 * location.
510 */
511 void
512 VSs__start_data_singleton( VSsSingleton **singletonAddr, SlaveVP *animPr )
513 {
514 VSsSemReq reqData;
516 if( *singletonAddr && (*singletonAddr)->hasFinished )
517 goto JmpToEndSingleton;
519 reqData.reqType = singleton_data_start;
520 reqData.singletonPtrAddr = singletonAddr;
522 VMS_WL__send_sem_request( &reqData, animPr );
523 if( animPr->dataRetFromReq ) //either 0 or end singleton's return addr
524 { //Assembly code changes the return addr on the stack to the one
525 // saved into the singleton by the end-singleton-fn
526 //The return addr is at 0x4(%%ebp)
527 JmpToEndSingleton:
528 asm_write_ret_from_singleton(*singletonAddr);
529 }
530 //now, simply return
531 //will exit either from the start singleton call or the end-singleton call
532 }
534 /*Uses ID as index into array of flags. If flag already set, resumes from
535 * end-label. Else, sets flag and resumes normally.
536 *
537 *Note, this call cannot be inlined because the instr addr at the label
538 * inside is shared by all invocations of a given singleton ID.
539 */
540 void
541 VSs__end_fn_singleton( int32 singletonID, SlaveVP *animPr )
542 {
543 VSsSemReq reqData;
545 //don't need this addr until after at least one singleton has reached
546 // this function
547 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animPr );
548 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
550 reqData.reqType = singleton_fn_end;
551 reqData.singletonID = singletonID;
553 VMS_WL__send_sem_request( &reqData, animPr );
555 EndSingletonInstrAddr:
556 return;
557 }
559 void
560 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr, SlaveVP *animPr )
561 {
562 VSsSemReq reqData;
564 //don't need this addr until after singleton struct has reached
565 // this function for first time
566 //do assembly that saves the return addr of this fn call into the
567 // data singleton -- that data-singleton can only be given to exactly
568 // one instance in the code of this function. However, can use this
569 // function in different places for different data-singletons.
570 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
573 asm_save_ret_to_singleton(*singletonPtrAddr);
575 reqData.reqType = singleton_data_end;
576 reqData.singletonPtrAddr = singletonPtrAddr;
578 VMS_WL__send_sem_request( &reqData, animPr );
579 }
581 /*This executes the function in the masterVP, so it executes in isolation
582 * from any other copies -- only one copy of the function can ever execute
583 * at a time.
584 *
585 *It suspends to the master, and the request handler takes the function
586 * pointer out of the request and calls it, then resumes the VP.
587 *Only very short functions should be called this way -- for longer-running
588 * isolation, use transaction-start and transaction-end, which run the code
589 * between as work-code.
590 */
591 void
592 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
593 void *data, SlaveVP *animPr )
594 {
595 VSsSemReq reqData;
597 //
598 reqData.reqType = atomic;
599 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
600 reqData.dataForFn = data;
602 VMS_WL__send_sem_request( &reqData, animPr );
603 }
606 /*This suspends to the master.
607 *First, it looks at the VP's data, to see the highest transactionID that VP
608 * already has entered. If the current ID is not larger, it throws an
609 * exception stating a bug in the code. Otherwise it puts the current ID
610 * there, and adds the ID to a linked list of IDs entered -- the list is
611 * used to check that exits are properly ordered.
612 *Next it is uses transactionID as index into an array of transaction
613 * structures.
614 *If the "VP_currently_executing" field is non-null, then put requesting VP
615 * into queue in the struct. (At some point a holder will request
616 * end-transaction, which will take this VP from the queue and resume it.)
617 *If NULL, then write requesting into the field and resume.
618 */
619 void
620 VSs__start_transaction( int32 transactionID, SlaveVP *animPr )
621 {
622 VSsSemReq reqData;
624 //
625 reqData.callingSlv = animPr;
626 reqData.reqType = trans_start;
627 reqData.transID = transactionID;
629 VMS_WL__send_sem_request( &reqData, animPr );
630 }
632 /*This suspends to the master, then uses transactionID as index into an
633 * array of transaction structures.
634 *It looks at VP_currently_executing to be sure it's same as requesting VP.
635 * If different, throws an exception, stating there's a bug in the code.
636 *Next it looks at the queue in the structure.
637 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
638 *If something in, gets it, sets VP_currently_executing to that VP, then
639 * resumes both.
640 */
641 void
642 VSs__end_transaction( int32 transactionID, SlaveVP *animPr )
643 {
644 VSsSemReq reqData;
646 //
647 reqData.callingSlv = animPr;
648 reqData.reqType = trans_end;
649 reqData.transID = transactionID;
651 VMS_WL__send_sem_request( &reqData, animPr );
652 }
