annotate AnimationMaster.c @ 262:aa79134dbc88

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