VMS/VMS_Implementations/VMS_impls/VMS__MC_shared_impl

annotate AnimationMaster.c @ 256:2bcf37fd50c6

Prev msg should have been: isolated valgrind with a new compiler switch use SERVICES__TURN_ON_VALGRIND in order to compile valgrind support into vmalloc
author Sean Halle <seanhalle@yahoo.com>
date Mon, 10 Sep 2012 01:26:51 -0700
parents b95711c6965c
children f5b110414453 999f2966a3e5
rev   line source
seanhalle@230 1 /*
seanhalle@230 2 * Copyright 2010 OpenSourceStewardshipFoundation
seanhalle@230 3 *
seanhalle@230 4 * Licensed under BSD
seanhalle@230 5 */
seanhalle@230 6
seanhalle@230 7
seanhalle@230 8
seanhalle@230 9 #include <stdio.h>
seanhalle@230 10 #include <stddef.h>
seanhalle@230 11
seanhalle@230 12 #include "VMS.h"
seanhalle@230 13
seanhalle@230 14
seanhalle@230 15
seanhalle@230 16 /*The animationMaster embodies most of the animator of the language. The
seanhalle@230 17 * animator is what emodies the behavior of language constructs.
seanhalle@230 18 * As such, it is the animationMaster, in combination with the plugin
seanhalle@230 19 * functions, that make the language constructs do their behavior.
seanhalle@230 20 *
seanhalle@230 21 *Within the code, this is the top-level-function of the masterVPs, and
seanhalle@230 22 * runs when the coreController has no more slave VPs. It's job is to
seanhalle@230 23 * refill the animation slots with slaves.
seanhalle@230 24 *
seanhalle@230 25 *To do this, it scans the animation slots for just-completed slaves.
seanhalle@230 26 * Each of these has a request in it. So, the master hands each to the
seanhalle@230 27 * plugin's request handler.
seanhalle@230 28 *Each request represents a language construct that has been encountered
seanhalle@230 29 * by the application code in the slave. Passing the request to the
seanhalle@230 30 * request handler is how that language construct's behavior gets invoked.
seanhalle@230 31 * The request handler then performs the actions of the construct's
seanhalle@230 32 * behavior. So, the request handler encodes the behavior of the
seanhalle@230 33 * language's parallelism constructs, and performs that when the master
seanhalle@230 34 * hands it a slave containing a request to perform that construct.
seanhalle@230 35 *
seanhalle@230 36 *On a shared-memory machine, the behavior of parallelism constructs
seanhalle@230 37 * equals control, over order of execution of code. Hence, the behavior
seanhalle@230 38 * of the language constructs performed by the request handler is to
seanhalle@230 39 * choose the order that slaves get animated, and thereby control the
seanhalle@230 40 * order that application code in the slaves executes.
seanhalle@230 41 *
seanhalle@230 42 *To control order of animation of slaves, the request handler has a
seanhalle@230 43 * semantic environment that holds data structures used to hold slaves
seanhalle@230 44 * and choose when they're ready to be animated.
seanhalle@230 45 *
seanhalle@230 46 *Once a slave is marked as ready to be animated by the request handler,
seanhalle@230 47 * it is the second plugin function, the Assigner, which chooses the core
seanhalle@230 48 * the slave gets assigned to for animation. Hence, the Assigner doesn't
seanhalle@230 49 * perform any of the semantic behavior of language constructs, rather
seanhalle@230 50 * it gives the language a chance to improve performance. The performance
seanhalle@230 51 * of application code is strongly related to communication between
seanhalle@230 52 * cores. On shared-memory machines, communication is caused during
seanhalle@230 53 * execution of code, by memory accesses, and how much depends on contents
seanhalle@230 54 * of caches connected to the core executing the code. So, the placement
seanhalle@230 55 * of slaves determines the communication caused during execution of the
seanhalle@230 56 * slave's code.
seanhalle@230 57 *The point of the Assigner, then, is to use application information during
seanhalle@230 58 * execution of the program, to make choices about slave placement onto
seanhalle@230 59 * cores, with the aim to put slaves close to caches containing the data
seanhalle@230 60 * used by the slave's code.
seanhalle@230 61 *
seanhalle@230 62 *==========================================================================
seanhalle@230 63 *In summary, the animationMaster scans the slots, finds slaves
seanhalle@230 64 * just-finished, which hold requests, pass those to the request handler,
seanhalle@230 65 * along with the semantic environment, and the request handler then manages
seanhalle@230 66 * the structures in the semantic env, which controls the order of
seanhalle@230 67 * animation of slaves, and so embodies the behavior of the language
seanhalle@230 68 * constructs.
seanhalle@230 69 *The animationMaster then rescans the slots, offering each empty one to
seanhalle@230 70 * the Assigner, along with the semantic environment. The Assigner chooses
seanhalle@230 71 * among the ready slaves in the semantic Env, finding the one best suited
seanhalle@230 72 * to be animated by that slot's associated core.
seanhalle@230 73 *
seanhalle@230 74 *==========================================================================
seanhalle@230 75 *Implementation Details:
seanhalle@230 76 *
seanhalle@230 77 *There is a separate masterVP for each core, but a single semantic
seanhalle@230 78 * environment shared by all cores. Each core also has its own scheduling
seanhalle@230 79 * slots, which are used to communicate slaves between animationMaster and
seanhalle@230 80 * coreController. There is only one global variable, _VMSMasterEnv, which
seanhalle@230 81 * holds the semantic env and other things shared by the different
seanhalle@230 82 * masterVPs. The request handler and Assigner are registered with
seanhalle@230 83 * the animationMaster by the language's init function, and a pointer to
seanhalle@230 84 * each is in the _VMSMasterEnv. (There are also some pthread related global
seanhalle@230 85 * vars, but they're only used during init of VMS).
seanhalle@230 86 *VMS gains control over the cores by essentially "turning off" the OS's
seanhalle@230 87 * scheduler, using pthread pin-to-core commands.
seanhalle@230 88 *
seanhalle@230 89 *The masterVPs are created during init, with this animationMaster as their
seanhalle@230 90 * top level function. The masterVPs use the same SlaveVP data structure,
seanhalle@230 91 * even though they're not slave VPs.
seanhalle@230 92 *A "seed slave" is also created during init -- this is equivalent to the
seanhalle@230 93 * "main" function in C, and acts as the entry-point to the VMS-language-
seanhalle@230 94 * based application.
seanhalle@230 95 *The masterVPs shared a single system-wide master-lock, so only one
seanhalle@230 96 * masterVP may be animated at a time.
seanhalle@230 97 *The core controllers access _VMSMasterEnv to get the masterVP, and when
seanhalle@230 98 * they start, the slots are all empty, so they run their associated core's
seanhalle@230 99 * masterVP. The first of those to get the master lock sees the seed slave
seanhalle@230 100 * in the shared semantic environment, so when it runs the Assigner, that
seanhalle@230 101 * returns the seed slave, which the animationMaster puts into a scheduling
seanhalle@230 102 * slot then switches to the core controller. That then switches the core
seanhalle@230 103 * over to the seed slave, which then proceeds to execute language
seanhalle@230 104 * constructs to create more slaves, and so on. Each of those constructs
seanhalle@230 105 * causes the seed slave to suspend, switching over to the core controller,
seanhalle@230 106 * which eventually switches to the masterVP, which executes the
seanhalle@230 107 * request handler, which uses VMS primitives to carry out the creation of
seanhalle@230 108 * new slave VPs, which are marked as ready for the Assigner, and so on..
seanhalle@230 109 *
seanhalle@230 110 *On animation slots, and system behavior:
seanhalle@230 111 * A request may linger in a animation slot for a long time while
seanhalle@230 112 * the slaves in the other slots are animated. This only becomes a problem
seanhalle@230 113 * when such a request is a choke-point in the constraints, and is needed
seanhalle@230 114 * to free work for *other* cores. To reduce this occurance, the number
seanhalle@230 115 * of animation slots should be kept low. In balance, having multiple
seanhalle@230 116 * animation slots amortizes the overhead of switching to the masterVP and
seanhalle@230 117 * executing the animationMaster code, which drives for more than one. In
seanhalle@230 118 * practice, the best balance should be discovered by profiling.
seanhalle@230 119 */
seanhalle@230 120 void animationMaster( void *initData, SlaveVP *masterVP )
seanhalle@230 121 {
seanhalle@230 122 //Used while scanning and filling animation slots
seanhalle@230 123 int32 slotIdx, numSlotsFilled;
seanhalle@235 124 AnimSlot *currSlot, **animSlots;
seanhalle@230 125 SlaveVP *assignedSlaveVP; //the slave chosen by the assigner
seanhalle@230 126
seanhalle@230 127 //Local copies, for performance
seanhalle@230 128 MasterEnv *masterEnv;
seanhalle@230 129 SlaveAssigner slaveAssigner;
seanhalle@230 130 RequestHandler requestHandler;
seanhalle@230 131 void *semanticEnv;
seanhalle@230 132 int32 thisCoresIdx;
nengel@238 133
seanhalle@230 134 //======================== Initializations ========================
seanhalle@230 135 masterEnv = (MasterEnv*)_VMSMasterEnv;
seanhalle@230 136
seanhalle@230 137 thisCoresIdx = masterVP->coreAnimatedBy;
seanhalle@235 138 animSlots = masterEnv->allAnimSlots[thisCoresIdx];
seanhalle@230 139
seanhalle@230 140 requestHandler = masterEnv->requestHandler;
seanhalle@230 141 slaveAssigner = masterEnv->slaveAssigner;
seanhalle@230 142 semanticEnv = masterEnv->semanticEnv;
nengel@238 143
nengel@238 144 HOLISTIC__Insert_Master_Global_Vars;
seanhalle@230 145
seanhalle@230 146 //======================== animationMaster ========================
seanhalle@230 147 while(1){
seanhalle@230 148
seanhalle@230 149 MEAS__Capture_Pre_Master_Point
seanhalle@230 150
seanhalle@230 151 //Scan the animation slots
seanhalle@230 152 numSlotsFilled = 0;
seanhalle@236 153 for( slotIdx = 0; slotIdx < NUM_ANIM_SLOTS; slotIdx++)
seanhalle@230 154 {
seanhalle@235 155 currSlot = animSlots[ slotIdx ];
seanhalle@230 156
nengel@239 157 //Check if newly-done slave in slot, which will need request handled
seanhalle@230 158 if( currSlot->workIsDone )
seanhalle@230 159 {
seanhalle@230 160 currSlot->workIsDone = FALSE;
seanhalle@230 161 currSlot->needsSlaveAssigned = TRUE;
nengel@238 162
nengel@238 163 HOLISTIC__Record_AppResponder_start;
seanhalle@230 164 MEAS__startReqHdlr;
seanhalle@230 165
seanhalle@230 166 //process the requests made by the slave (held inside slave struc)
seanhalle@230 167 (*requestHandler)( currSlot->slaveAssignedToSlot, semanticEnv );
seanhalle@230 168
nengel@238 169 HOLISTIC__Record_AppResponder_end;
seanhalle@230 170 MEAS__endReqHdlr;
seanhalle@230 171 }
seanhalle@230 172 //If slot empty, hand to Assigner to fill with a slave
seanhalle@230 173 if( currSlot->needsSlaveAssigned )
seanhalle@230 174 { //Call plugin's Assigner to give slot a new slave
nengel@238 175 HOLISTIC__Record_Assigner_start;
seanhalle@230 176 assignedSlaveVP =
seanhalle@230 177 (*slaveAssigner)( semanticEnv, currSlot );
seanhalle@230 178
seanhalle@230 179 //put the chosen slave into slot, and adjust flags and state
seanhalle@230 180 if( assignedSlaveVP != NULL )
seanhalle@230 181 { currSlot->slaveAssignedToSlot = assignedSlaveVP;
seanhalle@235 182 assignedSlaveVP->animSlotAssignedTo = currSlot;
seanhalle@230 183 currSlot->needsSlaveAssigned = FALSE;
seanhalle@230 184 numSlotsFilled += 1;
nengel@238 185
nengel@238 186 HOLISTIC__Record_Assigner_end;
seanhalle@230 187 }
seanhalle@230 188 }
seanhalle@230 189 }
seanhalle@230 190
seanhalle@230 191 MEAS__Capture_Post_Master_Point;
seanhalle@230 192
seanhalle@231 193 masterSwitchToCoreCtlr( masterVP );
seanhalle@230 194 flushRegisters();
seanhalle@235 195 DEBUG__printf(FALSE,"came back after switch to core -- so lock released!");
seanhalle@232 196 }//while(1)
seanhalle@230 197 }
seanhalle@230 198