-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathss-agent.cpp
More file actions
1724 lines (1543 loc) · 71.9 KB
/
ss-agent.cpp
File metadata and controls
1724 lines (1543 loc) · 71.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, University of Delaware
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <atomic>
#include <chrono>
#include <random>
#include <condition_variable>
#include <thread>
#include <chrono>
#include <ctime>
#include <ratio>
#include <iostream>
#include <climits>
#include "ss-agent.h"
extern "C" {
#include "ss-math.h"
#include "ss-temp.h"
}
#include "ss-msr.h"
#include "sa-api.h"
thread_local struct AgentMap agent;
AgentMap* agentMap[N_UNITS_IN_CHIP*N_BLOCKS_IN_UNIT];
std::atomic<u64> done; // incremented to agent count signals the simulation as finished.
std::chrono::high_resolution_clock::time_point simulationStartTime; //simulation start time.
std::atomic<s64> dram_ports[N_UNITS_IN_CHIP];
#if EXECUTION_TIMES == 1 || EXECUTION_TIMES == 3 || EXECUTION_TIMES == 2
thread_local struct Times
{
double roles;
double rolesChip;
double rolesUnit;
double rolesBlock;
double simulation;
double barriers;
}times;
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point start2;
std::chrono::high_resolution_clock::time_point stop;
std::chrono::high_resolution_clock::time_point stop2;
#endif
thread_local struct LeafNodeState
{
u8 stub;
FLOAT_TYPE powerGoal;
std::deque <saMetadata> parentBuffer;
bool underControl;
void push(saMetadata& msg)
{
if(!parentBuffer.empty())
{
saMetadata* msg2send = &parentBuffer.front();
u64 attr1, attr2;
saGetMetadata(&msg, SA_ATR_METADATA_TYPE, &attr1);
saGetMetadata(msg2send, SA_ATR_METADATA_TYPE, &attr2);
if (attr1 == attr2)
{
parentBuffer.pop_front();
}
}
parentBuffer.push_back(msg);
}
void flush()
{
/*Flushing the parent*/
//check if there is any message to send
if(!parentBuffer.empty())
{
saLocation parentSlot = saGetParentLocation();
saMetadata * msg2send;
msg2send = &parentBuffer.front();
if (saSendMetadata(parentSlot,(void*)msg2send)==0)
{
parentBuffer.pop_front();
}
}
}
} leafNodeState = {0};
thread_local struct BranchNodeState
{
FLOAT_TYPE temperatureAvg;
FLOAT_TYPE temperatureVar;
FLOAT_TYPE temperatureSD;
FLOAT_TYPE temperatureSkew;
FLOAT_TYPE temperatureMap[N_BLOCKS_IN_UNIT];
FLOAT_TYPE powerTotal;
FLOAT_TYPE powerAvg;
FLOAT_TYPE powerSD;
FLOAT_TYPE powerVar;
FLOAT_TYPE powerSkew;
FLOAT_TYPE powerMap[N_BLOCKS_IN_UNIT];
FLOAT_TYPE powerGoal;
FLOAT_TYPE currentMultipliers[N_CORES_IN_BLOCK];
std::deque <saMetadata> parentBuffer;
std::deque <saMetadata> childBuffers[N_BLOCKS_IN_UNIT];
bool underControl;
void push(saMetadata& msg)
{
if(!parentBuffer.empty())
{
saMetadata* msg2send = &parentBuffer.front();
u64 attr1, attr2;
saGetMetadata(&msg, SA_ATR_METADATA_TYPE, &attr1);
saGetMetadata(msg2send, SA_ATR_METADATA_TYPE, &attr2);
if (attr1 == attr2)
{
parentBuffer.pop_front();
}
}
parentBuffer.push_back(msg);
}
//Flush method try to send the information in the buffers
void flush()
{
/*Flushing the children buffers*/
for(u64 child=0; child<N_BLOCKS_IN_UNIT; child++)
{
//if there is any message to send.
if (!childBuffers[child].empty())
{
saLocation childSlot = saGetChildLocation(child);
saMetadata * msg2send;
msg2send = &childBuffers[child].front();
if (saSendMetadata(childSlot,(void*)msg2send)==0)
{
childBuffers[child].pop_front();
}
}
}
/*Flushing the parent*/
//check if there is any message to send
if(!parentBuffer.empty())
{
saLocation parentSlot = saGetParentLocation();
saMetadata * msg2send;
msg2send = &parentBuffer.front();
if (saSendMetadata(parentSlot,(void*)msg2send)==0)
{
parentBuffer.pop_front();
}
}
}
} branchNodeState = {0}; //space for unit roles + Chip role.
thread_local struct RootNodeState
{
FLOAT_TYPE temperatureAvg;
FLOAT_TYPE temperatureVar;
FLOAT_TYPE temperatureSD;
FLOAT_TYPE temperatureSkew;
FLOAT_TYPE temperatureAvgMap[N_UNITS_IN_CHIP];
FLOAT_TYPE temperatureVarMap[N_UNITS_IN_CHIP];
FLOAT_TYPE temperatureSDMap[N_UNITS_IN_CHIP];
FLOAT_TYPE temperatureSkewMap[N_UNITS_IN_CHIP];
FLOAT_TYPE powerTotal; // our total.
FLOAT_TYPE powerAvg;
FLOAT_TYPE powerVar;
FLOAT_TYPE powerSD;
FLOAT_TYPE powerSkew;
FLOAT_TYPE powerTotalMap[N_UNITS_IN_CHIP]; // for previous level.
FLOAT_TYPE powerAvgMap[N_UNITS_IN_CHIP];
FLOAT_TYPE powerVarMap[N_UNITS_IN_CHIP];
FLOAT_TYPE powerSDMap[N_UNITS_IN_CHIP];
FLOAT_TYPE powerSkewMap[N_UNITS_IN_CHIP];
FLOAT_TYPE powerGoal;
FLOAT_TYPE currentMultipliers[N_UNITS_IN_CHIP];
std::deque <saMetadata> childBuffers[N_UNITS_IN_CHIP];
void flush()
{
/*Flushing the children buffers*/
for(u64 child=0; child<N_BLOCKS_IN_UNIT; child++)
{
//if there is any message to send.
if (!childBuffers[child].empty())
{
saLocation childSlot = saGetChildLocation(child);
saMetadata * msg2send;
msg2send = &childBuffers[child].front();
if (saSendMetadata(childSlot,(void*)msg2send)==0)
{
childBuffers[child].pop_front();
}
}
}
}
} rootNodeState = {0}; //space for unit roles + Chip role.
auto inline initializeAgent() -> void
{
//XXX FIXME WARNING The casting may represent problems in different architectures.
//XXX The operation is false if id-width is unsigned long and is compared to -1.
//XXX casting necessary but may represent problems in the future.
assert(agent.bid <= USHRT_MAX && "agent.bid too big: cast to long will potentially result in negative value");
//Helpers to calculate neighbours assuming periodicity (blk calculations).
#define TOP_NEIGHBOR_PERIODIC(id, width, height) \
((long)(id-width) > -1 ? (long)id-width : (long)(id-width)+width*height)
#define BTM_NEIGHBOR_PERIODIC(id, width, height) \
(id+width < width*height ? id+width : id+width-width*height)
#define LFT_NEIGHBOR_PERIODIC(id, width, height) \
(id%width != 0 ? id-1 : id-1+width)
#define RHT_NEIGHBOR_PERIODIC(id, width, height) \
((id+1)%width != 0 ? id+1 : id+1-width)
//Helpers to calculate neighbours assuming non-periodicity (unit calculations).
#define EAGENT_NOT_FOUND (u64)-1
#define TOP_NEIGHBOR(id, width, height) \
(((long)(id-width) > -1) ? id-width : EAGENT_NOT_FOUND)
#define BTM_NEIGHBOR(id, width, height, count) \
((id+width < width*height) && (id+width < count) ? id+width : EAGENT_NOT_FOUND)
#define LFT_NEIGHBOR(id, width, height) \
((id%width != 0) ? id-1 : EAGENT_NOT_FOUND)
#define RHT_NEIGHBOR(id, width, height, count) \
(((id+1)%width != 0) && (id+1 < count) ? id+1 : EAGENT_NOT_FOUND)
//assign temperature.
agent.temperature = 50;
//initialize the underControl flag
leafNodeState.underControl = false;
branchNodeState.underControl = false;
//Initialize some SA related data. and the multipliers of the roots for the controller.
for (u64 i=0; i<N_UNITS_IN_CHIP; i++)
{
branchNodeState.temperatureMap[i] = 50.0;
}
for (u64 i=0; i<N_BLOCKS_IN_UNIT; i++)
{
rootNodeState.temperatureAvgMap[i] = 50.0;
rootNodeState.currentMultipliers[i]=1;
}
for (u64 i=0; i<N_CORES_IN_BLOCK; i++)
{
branchNodeState.currentMultipliers[i]=1;
}
//Neighbor ids.
u64 n_uid = agent.uid; u64 n_bid;
//Find top neighbor.
n_bid = TOP_NEIGHBOR_PERIODIC(agent.bid, chip_layout.unit_width_num_blocks, chip_layout.unit_height_num_blocks);
n_uid = (n_bid >= agent.bid) ? TOP_NEIGHBOR(agent.uid, chip_layout.chip_width_num_units, chip_layout.chip_height_num_units) : agent.uid; //check if in another unit.
if (n_uid != EAGENT_NOT_FOUND) {
agent.topNeighbor = agentMap[n_uid*N_BLOCKS_IN_UNIT+n_bid];
///printf("\t%d \n", n_uid*N_BLOCKS_IN_UNIT+n_bid);
}
//Find left neighbor.
n_bid = LFT_NEIGHBOR_PERIODIC(agent.bid, chip_layout.unit_width_num_blocks, chip_layout.unit_height_num_blocks);
n_uid = (n_bid >= agent.bid) ? LFT_NEIGHBOR(agent.uid, chip_layout.chip_width_num_units, chip_layout.chip_height_num_units) : agent.uid; //check if in another unit.
if (n_uid != EAGENT_NOT_FOUND) {
agent.lftNeighbor = agentMap[n_uid*N_BLOCKS_IN_UNIT+n_bid];
///printf("%d", n_uid*N_BLOCKS_IN_UNIT+n_bid);
}
///printf("\t%d ", tid);
//Find right neighbor.
n_bid = RHT_NEIGHBOR_PERIODIC(agent.bid, chip_layout.unit_width_num_blocks, chip_layout.unit_height_num_blocks);
n_uid = (n_bid <= agent.bid) ? RHT_NEIGHBOR(agent.uid, chip_layout.chip_width_num_units, chip_layout.chip_height_num_units, N_UNITS_IN_CHIP) : agent.uid; //check if in another unit.
if (n_uid != EAGENT_NOT_FOUND) {
agent.rhtNeighbor = agentMap[n_uid*N_BLOCKS_IN_UNIT+n_bid];
///printf("\t%d ", n_uid*N_BLOCKS_IN_UNIT+n_bid);
}
///printf("\n");
//Find bottom neighbor.
n_bid = BTM_NEIGHBOR_PERIODIC(agent.bid, chip_layout.unit_width_num_blocks, (u64)chip_layout.unit_height_num_blocks);
n_uid = (n_bid <= agent.bid) ? BTM_NEIGHBOR(agent.uid, chip_layout.chip_width_num_units, (u64)chip_layout.chip_height_num_units, N_UNITS_IN_CHIP) : agent.uid; //check if in another unit.
if (n_uid != EAGENT_NOT_FOUND) {
agent.btmNeighbor = agentMap[n_uid*N_BLOCKS_IN_UNIT+n_bid];
///printf("\t%d\n", n_uid*N_BLOCKS_IN_UNIT+n_bid);
}
}
auto inline pushWorkRoundRobin() -> void
{
//push task into queue more than once if MULTIPLIER specified
for(u64 taskMultiplier = 0; taskMultiplier<TASK_MULTIPLIER; taskMultiplier++)
{
u64 taskNumber = 0;
//If I still have instructions to schedule.
while (taskNumber < taskPool.size())
{
for(u64 i = 0 ; i < N_BLOCKS_IN_UNIT*N_UNITS_IN_CHIP; i++)
{
//Push 8 tasks to the same block for it to work on.
for(u64 j = 0 ; j < 8 && taskNumber<taskPool.size() ; j++)
{
agentMap[i]->taskQueue.push_back(taskPool[taskNumber]); // add front to another queue (copies).
taskNumber++;
}
}
}
}
taskPool.clear();
taskPool.shrink_to_fit();
}
auto inline scheduleWork() -> void
{
for(u64 i = 0 ; i < N_BLOCKS_IN_UNIT*N_UNITS_IN_CHIP; i++)
{
u64 taskNumber = 0;
//Cycle each XE scheduling tasks.
while (taskNumber < agentMap[i]->taskQueue.size())
{
for(u64 j=0; j<N_CORES_IN_BLOCK; j++)
{
//add new task to be executed in the next cycle if any available.
if(taskNumber < agentMap[i]->taskQueue.size())
{
//The new task is the task that is in front of the in the taskQueue of the agent.
agentMap[i]->xe[j].taskQueue.push_back(agentMap[i]->taskQueue[taskNumber++]);
}
else
{
agentMap[i]->taskQueue.clear();
agentMap[i]->taskQueue.shrink_to_fit();
break;
}
}
}
agentMap[i]->taskQueue.clear();
agentMap[i]->taskQueue.shrink_to_fit();
}
}
auto inline ExecuteWork() -> void
{
if(agent.done == true)
return;
if(((FLOAT_TYPE)readClockMSR()*INST_PER_MEGA_INST/((FLOAT_TYPE)MAX_XE_CLOCK_SPEED_MHZ*1000)) >= MAX_TIME && agent.done == false)
{
// tell everyone we finished.
done++;
agent.done = true; // mark us as done so we don't increment again.
return;
}
bool executedWork = false;
FLOAT_TYPE energyDelta[N_CORES_IN_BLOCK] = {0.0};
static thread_local s64 port[N_CORES_IN_BLOCK] = {-1, -1, -1, -1, -1, -1, -1, -1};
//Cycle each XE scheduling tasks.
for(u64 i=0; i<N_CORES_IN_BLOCK; i++)
{
u16& state = agent.xe[i].state; //grab the state.
TaskType*& task = agent.xe[i].task; //grab the current task.
u64& instCounter = agent.xe[i].instCounter; //Grab the current Instruction Counter within the task
u64& taskCounter = agent.xe[i].taskCounter; //Grab the current Task Counter within the task
InstType& currentInstruction = agent.xe[i].currentInstruction; //Grab the current instruction
if(taskCounter <= agent.xe[i].taskQueue.size())
{
if(currentInstruction.latency <= 0)
{
if(task == NULL || instCounter >= task->instructions.size())
{
if (taskCounter == agent.xe[i].taskQueue.size())
continue;
#if DEBUG == 0
task = &taskSet.lookup(agent.xe[i].taskQueue[(taskCounter++)]);
#else
task = &taskSet.lookup(agent.xe[i].taskQueue.at((taskCounter++)));
#endif
instCounter = 0;
//Statistics.
agent.statistics.tasksExecuted++;
}
currentInstruction=instructionSet.lookup(task->instructions[(instCounter)++]);
assert(currentInstruction.latency > 0);
//Statistics.
agent.statistics.instsExecuted++;
}
executedWork = true;
if(currentInstruction.type > 0 && port[i] <= 0)
{
bool failed_acquire = false;
if(dram_ports[agent.uid].load(std::memory_order_relaxed) > 0)
{
failed_acquire = true;
port[i] = dram_ports[agent.uid].fetch_sub(1);
}
if(port[i] <= 0)
{
if(failed_acquire)
dram_ports[agent.uid]++;
if (state == XE_STATE_FULL)
energyDelta[i] = noopInstruction.fullStateEnergy;
else if (state == XE_STATE_HALF)
energyDelta[i] = noopInstruction.halfStateEnergy;
continue;
}
}
//
if (state == XE_STATE_FULL)
{
//Execution of instructions FULL DVFS STATE
//Decrement latency of the current instruction by the full state multiplier.
currentInstruction.latency -= currentInstruction.multiplier;
if(currentInstruction.type > 0)
{
currentInstruction.type -= currentInstruction.multiplier;
if(currentInstruction.type <= 0)
{
port[i] = -1;
dram_ports[agent.uid]++;
}
}
energyDelta[i] = currentInstruction.fullStateEnergy;
continue;
}
else if (state == XE_STATE_HALF)
{
//Execution of instructions HALF DVFS STATE
//Decrement latency of the current instruction by 1.
currentInstruction.latency--;
if(currentInstruction.type > 0)
{
currentInstruction.type--;
if(currentInstruction.type <= 0)
{
port[i] = -1;
dram_ports[agent.uid]++;
}
}
energyDelta[i] = currentInstruction.halfStateEnergy;
continue;
}
}
}
//done working.
//if((executedWork==false && agent.done == false))
//{
// // tell everyone we finished.
// done++;
// agent.done = true; // mark us as done so we don't increment again.
//}
FLOAT_TYPE energy = 0.0;
for(u64 i=0; i<N_CORES_IN_BLOCK; i++)
energy+=energyDelta[i];
//Push energy to rolling window for power computations.
agent.energyWindow.push_front(energy);
if (agent.energyWindow.size() >= ROLLING_ENERGY_WINDOW)
{
if (agent.accumulatedEnergy == 0.0)
{
for(u64 i=0; i<ROLLING_ENERGY_WINDOW; i++)
agent.accumulatedEnergy += agent.energyWindow[i];
}
else
{
agent.accumulatedEnergy -= agent.energyWindow.back();
agent.accumulatedEnergy += agent.energyWindow.front();
}
agent.energyWindow.pop_back();
}
updateTemperatureMSR();
}
auto inline adjustMultiplier(FLOAT_TYPE& powerTotal, FLOAT_TYPE& powerGoal, FLOAT_TYPE& multiplier, s64& dampener) -> bool
{
if(dampener < 0)
dampener = 0;
if ((powerTotal>powerGoal*1.10 || powerTotal<powerGoal*0.90) && dampener == 0)
{
multiplier -= ((powerTotal/powerGoal - 1.0) * powerTotal) * 0.008;
dampener = DAMPENER*2;
}
else if ((powerTotal>powerGoal*1.05 || powerTotal<powerGoal*0.95) && dampener == 0)
{
multiplier -= ((powerTotal/powerGoal - 1.0) * powerTotal) * 0.000025;
dampener = DAMPENER;
}
else if ((powerTotal>powerGoal*1.03 || powerTotal<powerGoal*0.97) && dampener == 0)
{
multiplier -= ((powerTotal/powerGoal - 1.0) * powerTotal) * 0.000025;
dampener = DAMPENER;
}
else if ((powerTotal>powerGoal*1.02 || powerTotal<powerGoal*0.98) && dampener == 0)
{
multiplier -= ((powerTotal/powerGoal - 1.0) * powerTotal) * 0.000025;
dampener = DAMPENER;
}
else
{
dampener--;
return false;
}
return true;
}
unsigned int seed = 0;
auto inline chipControl() -> void
{
if(readClockMSR()*INST_PER_MEGA_INST < MAX_XE_CLOCK_SPEED_MHZ * 1e5)
return;
#if ENABLE_ADAPT_POLICY == 0
//do nothing
#elif ENABLE_ADAPT_POLICY == 1
//We would like to check if our current total power is above the power budget, if so, modify the multiplier and update the power budget of an aleatory unit
//If the current total power is above the powerGoal plus 10%
static thread_local u64 waitCycles = 1;
waitCycles--;
static thread_local s64 dampener = 0;
if (!waitCycles)
{
waitCycles = CHIP_CONTROL_CLOCK;
//Pick up a unit randomly
int randomUnit=rand_r(&seed)%N_UNITS_IN_CHIP;
if(adjustMultiplier(rootNodeState.powerTotal, rootNodeState.powerGoal, rootNodeState.currentMultipliers[randomUnit], dampener))
{
//Message to be sent
saBlkCtrlMetadata msgPowerGoal = SA_BLK_CTRL_METADATA_INITIALIZER;
u64 typeIns = SA_ATR_FUB_CTRL_POWER_GOAL_VALID;
//Type of control Power Goal. Value node.powerGoal/N_BLOCKS_IN_UNIT
FLOAT_TYPE powerPerBlock=rootNodeState.powerGoal*rootNodeState.currentMultipliers[randomUnit]/N_UNITS_IN_CHIP;
saSetMetadata(&msgPowerGoal, SA_ATR_FUB_CTRL, &typeIns);
saSetMetadata(&msgPowerGoal, SA_ATR_FUB_POWER_GOAL, &powerPerBlock);
rootNodeState.childBuffers[randomUnit].push_back(*reinterpret_cast<saMetadata*>(&msgPowerGoal));
}
}
#endif
}
auto inline chipRole() -> void
{
if(agent.done == true)
return;
agent.role = ROLE_STATE_CHIP;
auto& node = rootNodeState; //get node state.
if (agent.bid == 0 && agent.uid == 0) // Congratulations you were picked as the controller in your chip.
{
//Temperature related.
node.temperatureAvg = computeAverage(node.temperatureAvgMap, N_UNITS_IN_CHIP);
FLOAT_TYPE avgOfVar = computeAverage(node.temperatureVarMap, N_UNITS_IN_CHIP);
node.temperatureVar = avgOfVar + computeVariance(node.temperatureAvgMap,
node.temperatureAvg, N_UNITS_IN_CHIP);
node.temperatureSD = sqrt(node.temperatureVar);
node.temperatureSkew = computeAverage(node.temperatureSkewMap, N_UNITS_IN_CHIP)
+ computeSkew(node.temperatureAvgMap, node.temperatureAvg, N_UNITS_IN_CHIP)
+ 3*computeCovariance(node.temperatureAvgMap, node.temperatureAvg,
node.temperatureVarMap, avgOfVar, N_UNITS_IN_CHIP);
//Power related -- note that that these statistics ignore lower level data points.
node.powerTotal = 0.0;
for (u64 i=0; i<N_UNITS_IN_CHIP; i++)
node.powerTotal += node.powerTotalMap[i];
node.powerAvg = node.powerTotal/N_UNITS_IN_CHIP;
node.powerVar = computeVariance(node.powerTotalMap,
node.powerAvg, N_UNITS_IN_CHIP);
node.powerSD = sqrt(node.powerVar);
node.powerSkew = computeSkew(node.powerTotalMap,
node.powerAvg, N_UNITS_IN_CHIP);
//CONTROL POLICY
chipControl();
/*We check the mail */
//Iterate over child mailboxes.
for(u64 child=0; child<N_UNITS_IN_CHIP; child++)
{
//Grab child slot location.
saLocation childSlot = saGetChildLocation(child);
//Fetch message from slot.
saMetadata msg;
if(saRecvMetadata(childSlot,(void*)&msg) == 0)
{
//Success.
if (*(u64*)&msg != 0)
{
//Found message.
u64 attr;
///FIXME: add metadata APIs for checking this type of thing?
//Retrieve metadata type.
saGetMetadata(&msg, SA_ATR_METADATA_TYPE, &attr);
switch(attr)
{
case SA_METADATA_TYPE_AGG_INFO:
{
//Record temperature data (implicit unpacking in the API).
{
//record aggregated data.
saGetMetadata(&msg, SA_ATR_AGG_TEMP_MEAN, &node.temperatureAvgMap[child]);
saGetMetadata(&msg, SA_ATR_AGG_TEMP_SD, &node.temperatureSDMap[child]);
node.temperatureVarMap[child] = node.temperatureSDMap[child]*node.temperatureSDMap[child];
saGetMetadata(&msg, SA_ATR_AGG_TEMP_SKEW, &node.temperatureSkewMap[child]);
}
//Record power data (implicit unpacking in the API).
{
//record aggregated data.
saGetMetadata(&msg, SA_ATR_AGG_POW_MEAN, &node.powerAvgMap[child]);
node.powerTotalMap[child] = node.powerAvgMap[child]*N_BLOCKS_IN_UNIT;
saGetMetadata(&msg, SA_ATR_AGG_POW_SD, & node.powerSDMap[child]);
node.powerVarMap[child] = node.powerSDMap[child]*node.powerSDMap[child];
saGetMetadata(&msg, SA_ATR_AGG_POW_SKEW, &node.powerSkewMap[child]);
}
break;
}
default:
printf("unt%ld.blk%ld: WARNING: chip role received unknown metadata message!\n", agent.uid, agent.bid);
}
}
}
}
{
//If the power goal change, set the powerGoal variable and send a message to the units.
if (node.powerGoal != POWER_GOAL)
{
node.powerGoal = POWER_GOAL;
fprintf(agent.logfile, "[RMD_CONTROL_EVENT] [CHIP_POWER_GOAL_CHANGE] %f at cycle %ld \n", node.powerGoal, readClockMSR()*INST_PER_MEGA_INST);
for (u64 child = 0 ; child < N_UNITS_IN_CHIP ; child++)
{
//Message to be sent
saBlkCtrlMetadata ctrlMsg = SA_BLK_CTRL_METADATA_INITIALIZER;
u64 typeIns = SA_ATR_FUB_CTRL_POWER_GOAL_VALID;
//Type of control Power Goal. Value node.powerGoal/N_UNITS_IN_CHIP
FLOAT_TYPE powerPerUnit = node.powerGoal/N_UNITS_IN_CHIP;
saSetMetadata(&ctrlMsg, SA_ATR_FUB_CTRL, &typeIns);
saSetMetadata(&ctrlMsg, SA_ATR_FUB_POWER_GOAL, &powerPerUnit);
node.childBuffers[child].push_back(*reinterpret_cast<saMetadata*>(&ctrlMsg));
}
}
//Compute aggregate statistics.
//#if LOGGING_LEVEL == 1
////Print statistics to the log.
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Temperature Average of %.2lfC at cycle %ld.\n",
// TEMPERATURE_OPERATION-node.temperatureAvg, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Temperature Variance of %.2lfC^2 at cycle %ld.\n",
// node.temperatureVar, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Temperature SD of %.2lfC at cycle %ld.\n",
// node.temperatureSD, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Temperature Skew of %.2lfC^3 at cycle %ld.\n",
// node.temperatureSkew, readClockMSR()*INST_PER_MEGA_INST);
//#endif
//#if LOGGING_LEVEL == 1
////Print statistics to the log.
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Power Total of %lfW at cycle %ld.\n",
// node.powerTotal, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Power Average of %lfW at cycle %ld.\n",
// node.powerAvg, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Power Variance of %lfW^2 at cycle %ld.\n",
// node.powerVar, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Power SD of %lfW at cycle %ld.\n",
// node.powerSD, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Chip] Power Skew of %lfW^3 at cycle %ld.\n",
// node.powerSkew, readClockMSR()*INST_PER_MEGA_INST);
//#endif
}
}
//Flush messages
node.flush();
}
auto inline unitControl() -> void
{
if(readClockMSR()*INST_PER_MEGA_INST < MAX_XE_CLOCK_SPEED_MHZ * 1e5)
return;
if (!branchNodeState.underControl)
{
#if ENABLE_ADAPT_POLICY == 0
//Do nothing
#elif ENABLE_ADAPT_POLICY == 1
if ((readClockMSR()*INST_PER_MEGA_INST) % (UNIT_CONTROL_CLOCK + agent.id*100) == 0)
{
static thread_local s64 dampener = 1;
static thread_local unsigned int seed = agent.id+10;
int randomUnit=rand_r(&seed)%N_CORES_IN_BLOCK;
if(adjustMultiplier(branchNodeState.powerTotal, branchNodeState.powerGoal, branchNodeState.currentMultipliers[randomUnit], dampener))
{
//Message to be sent
saBlkCtrlMetadata msgPowerGoal = SA_BLK_CTRL_METADATA_INITIALIZER;
u64 typeIns = SA_ATR_FUB_CTRL_POWER_GOAL_VALID;
//Type of control Power Goal. Value node.powerGoal/N_BLOCKS_IN_UNIT
FLOAT_TYPE powerPerBlock= branchNodeState.powerGoal*branchNodeState.currentMultipliers[randomUnit]/N_BLOCKS_IN_UNIT;
saSetMetadata(&msgPowerGoal, SA_ATR_FUB_CTRL, &typeIns);
saSetMetadata(&msgPowerGoal, SA_ATR_FUB_POWER_GOAL, &powerPerBlock);
branchNodeState.childBuffers[randomUnit].push_back(*reinterpret_cast<saMetadata*>(&msgPowerGoal));
}
}
#endif
}
}
auto inline unitRole() -> void
{
if(agent.done == true)
return;
agent.role = ROLE_STATE_UNIT;
auto& node = branchNodeState; // get node state.
static thread_local FLOAT_TYPE oldTemp[N_BLOCKS_IN_UNIT] = {0.0};
static thread_local FLOAT_TYPE oldPower[N_BLOCKS_IN_UNIT] = {0.0};
//Check if we should send data to chip agent.
bool sendData = false;
if (agent.bid == 0) //Congratulations you were picked as the controller of your unit.
{
//Compute aggregate statistics.
//Temperature related.
node.temperatureAvg = computeAverage(node.temperatureMap,
N_BLOCKS_IN_UNIT);
node.temperatureVar = computeVariance(node.temperatureMap,
node.temperatureAvg, N_BLOCKS_IN_UNIT);
node.temperatureSD = sqrt(node.temperatureVar);
node.temperatureSkew = computeSkew(node.temperatureMap,
node.temperatureAvg, N_BLOCKS_IN_UNIT);
//Power related.
node.powerTotal = 0.0;
for (u64 i=0; i<N_BLOCKS_IN_UNIT; i++)
node.powerTotal += node.powerMap[i];
node.powerAvg = node.powerTotal/N_BLOCKS_IN_UNIT;
node.powerVar = computeVariance(node.powerMap,
node.powerAvg, N_BLOCKS_IN_UNIT);
node.powerSD = sqrt(node.powerVar);
node.powerSkew = computeSkew(node.powerMap,
node.powerAvg, N_BLOCKS_IN_UNIT);
//control policy
unitControl();
/*We check the mail*/
//Grab parent slot location.
saLocation parentSlot = saGetParentLocation();
//Fetch message from slot.
saMetadata msg;
if(saRecvMetadata(parentSlot,(void*)&msg) == 0)
{
sendData = true;
//Success.
if (*(u64*)&msg != 0)
{
//Found message.
u64 attr;
///FIXME: add metadata APIs for checking this type of thing?
//Retrieve metadata type.
saGetMetadata(&msg, SA_ATR_METADATA_TYPE, &attr);
switch(attr)
{
//Incoming control instruction.
case SA_METADATA_TYPE_BLK_CTRL:
{
//Grab the type of instruction.
u64 typeIns;
saGetMetadata(&msg, SA_ATR_FUB_CTRL, &typeIns);
switch (typeIns)
{
//Change the power goal
case SA_ATR_FUB_CTRL_POWER_GOAL_VALID:
//Set power goal to the value in the message. Inform the change to the childrens.
FLOAT_TYPE newPowerGoal;
saGetMetadata(&msg, SA_ATR_FUB_POWER_GOAL, &newPowerGoal);
if (node.powerGoal != newPowerGoal)
{
node.powerGoal = newPowerGoal*UNIT_POWER_GOAL_SCALE;
fprintf(agent.logfile, "[RMD_CONTROL_EVENT] [UNIT_POWER_GOAL_CHANGE] %f at cycle %ld \n", node.powerGoal, readClockMSR()*INST_PER_MEGA_INST);
//Set a new power goal for the children.
for (u64 i = 0 ; i < N_BLOCKS_IN_UNIT ; i++)
{
//Message to be sent
saBlkCtrlMetadata msgPowerGoal = SA_BLK_CTRL_METADATA_INITIALIZER;
typeIns = SA_ATR_FUB_CTRL_POWER_GOAL_VALID;
//Type of control Power Goal. Value node.powerGoal/N_BLOCKS_IN_UNIT
FLOAT_TYPE powerPerBlock=node.powerGoal/N_BLOCKS_IN_UNIT;
saSetMetadata(&msgPowerGoal, SA_ATR_FUB_CTRL, &typeIns);
saSetMetadata(&msgPowerGoal, SA_ATR_FUB_POWER_GOAL, &powerPerBlock);
node.childBuffers[i].push_back(*reinterpret_cast<saMetadata*>(&msgPowerGoal));
}
}
break;
default:
printf("unt%ld.blk%ld: WARNING: unit role received unknown control message!\n", agent.uid, agent.bid);
}
break;
}
}
}
}
//Iterate over child mailboxes.
for(u64 child=0; child<N_BLOCKS_IN_UNIT; child++)
{
//Grab child slot location.
saLocation childSlot = saGetChildLocation(child);
//Fetch message from slot.
saMetadata msg;
if(saRecvMetadata(childSlot,(void*)&msg) == 0)
{
sendData = true;
//Success.
if (*(u64*)&msg != 0)
{
//Found message.
u64 attr;
///FIXME: add metadata APIs for checking this type of thing?
//Retrieve metadata type.
saGetMetadata(&msg, SA_ATR_METADATA_TYPE, &attr);
switch(attr)
{
//Incoming block information packet.
case SA_METADATA_TYPE_BLK_INFO:
{
//Decode and handle the temperature (implicit unpacking in the API).
{
// Grab temperature directly (No need to unpack).
saGetMetadata(&msg, SA_ATR_FUB_TEMP_SCORE, &node.temperatureMap[child]);
// //Compute real temperature from delta.
// FLOAT_TYPE temperature = TEMPERATURE_OPERATION - node.temperatureMap[child];
//
// if(temperature > 90.0)
// {
// // send clock off message to block
// saBlkCtrlMetadata ctrlMsg = SA_BLK_CTRL_METADATA_INITIALIZER;
// attr = SA_ATR_FUB_DVFS_SCORE_NONE;
// saSetMetadata(&ctrlMsg, SA_ATR_FUB_DVFS_SCORE, &attr);
// node.childBuffers[child].push_back(*reinterpret_cast<saMetadata*>(&ctrlMsg));
// }
// else if(temperature > 70.0)
// {
// // send dvfs message to block
// saBlkCtrlMetadata ctrlMsg = SA_BLK_CTRL_METADATA_INITIALIZER;
// attr = SA_ATR_FUB_DVFS_SCORE_HALF;
// saSetMetadata(&ctrlMsg, SA_ATR_FUB_DVFS_SCORE, &attr);
// node.childBuffers[child].push_back(*reinterpret_cast<saMetadata*>(&ctrlMsg));
// }
}
//Decode and handle the power.
{
// Grab power.
saGetMetadata(&msg, SA_ATR_FUB_POW_SCORE, &node.powerMap[child]);
}
sendData = true;
break;
}
default:
printf("unt%ld.blk%ld: WARNING: unit role received unknown metadata message!\n", agent.uid, agent.bid);
}
}
}
}
for(u64 child=0; child<N_BLOCKS_IN_UNIT; child++)
{
if (fabs(oldTemp[child]-node.temperatureMap[child]) > TEMPERATURE_SWING || fabs(oldPower[child]-node.powerMap[child]) > POWER_SWING)
{
sendData = true;
break;
}
}
if (sendData == true)
{
for(u64 child=0; child<N_BLOCKS_IN_UNIT; child++)
{
oldTemp[child] = node.temperatureMap[child];
oldPower[child] = node.powerMap[child];
}
//#if LOGGING_LEVEL == 1
////Print statistics to the log.
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Temperature Average of %.2lfC at cycle %ld.\n",
// TEMPERATURE_OPERATION-node.temperatureAvg, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Temperature Variance of %.2lfC^2 at cycle %ld.\n",
// node.temperatureVar, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Temperature SD of %.2lfC at cycle %ld.\n",
// node.temperatureSD, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Temperature Skew of %.2lfC^3 at cycle %ld.\n",
// node.temperatureSkew, readClockMSR()*INST_PER_MEGA_INST);
//#endif
//#if LOGGING_LEVEL == 1
////Print statistics to the log.
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Power Total of %lfW at cycle %ld.\n",
// node.powerTotal, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Power Average of %lfW at cycle %ld.\n",
// node.powerAvg, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Power Variance of %lfW^2 at cycle %ld.\n",
// node.powerVar, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Power SD of %lfW at cycle %ld.\n",
// node.powerSD, readClockMSR()*INST_PER_MEGA_INST);
//fprintf(agent.logfile, "[RMD_TRACE_AGGREGATE] [Unit] Power Skew of %lfW^3 at cycle %ld.\n",
// node.powerSkew, readClockMSR()*INST_PER_MEGA_INST);
//#endif
//Transfer our aggregate temperatures.
{
saMetadata msg = SA_AGG_INFO_METADATA_INITIALIZER;
//Push temperature and power to next level CE (packing is transparently done in the API).
saSetMetadata(&msg, SA_ATR_AGG_TEMP_MEAN, &node.temperatureAvg);
saSetMetadata(&msg, SA_ATR_AGG_TEMP_SD, &node.temperatureSD);
saSetMetadata(&msg, SA_ATR_AGG_TEMP_SKEW, &node.temperatureSkew);
saSetMetadata(&msg, SA_ATR_AGG_POW_MEAN, &node.powerAvg);
saSetMetadata(&msg, SA_ATR_AGG_POW_SD, &node.powerSD);
saSetMetadata(&msg, SA_ATR_AGG_POW_SKEW, &node.powerSkew);
node.push(msg);
}
}
}
//flush messages
node.flush();
}
auto inline blockControl() -> void
{
//FLOAT_TYPE tempDelta = readTemperatureMSR(); //read from MSR (should be 7 bits max).
//FLOAT_TYPE temperature = TEMPERATURE_JUNCTION - tempDelta;
//Turn on all units for work.
// if (temperature < 51) //cool as a whistle.
// {