view VSs.c @ 3:468b8638ff92

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