view AnimationMaster.c @ 257:f5b110414453

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