-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.cpp
2750 lines (1373 loc) · 60 KB
/
start.cpp
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
#include <stdio.h>
//#include "mhead.h"
#include <string> // std::string, std::to_string
#include <bits/stdc++.h>
//#include<map>
using namespace std;
#define maxPCISpeed 266
#define maxPCISpeedPerSlot 62
#define block_size 128
//define PacketSize 480
#define switch_distance 1000
#define brick_distance 1000
#define speed 200000000
//#define int long long int
int num_of_cores=1;
int network_bandwidth=1000000;
int bandwidth=1000000;
int switch_bandwidth=1000000;
int cyclenumber;
int numRequests;
int number_of_instructions;
int frequency=100000000;
int presentcycle=-1;
int Q_limit=1000;
int PacketSize=128;
deque<pair<pair<int,int>,int>> Tx1,Tx2;
deque<pair<pair<int,int>,int>> Rx1,Rx2;
deque<pair<pair<int,int>,int>> switchTx1;
deque<pair<pair<int,int>,int>> switchRx1;
deque<pair<pair<int,int>,int>> pci_Tx1;
deque<pair<pair<int,int>,int>> pci_Rx1;
deque<pair<pair<int,int>,int>> memTx1,memTx2;
deque<pair<pair<int,int>,int>> memRx1,memRx2;
struct cmpByStringLength {
bool operator()(const std::string& a, const std::string& b) const {
istringstream ss(a);
string word1; // for storing each word
istringstream ss2(b);
string word2;
ss>>word1;
ss2>>word2;
//cout<<"word "<<word1<<" "<<word2<<endl;
return stoi(word1)<stoi(word2);
}
};
// ...
//std::map<std::string, std::string, cmpByStringLength> myMap;
map<string, vector<int> ,cmpByStringLength> dc;
deque<int> threadTx1,threadTx2,threadRx1,threadRx2,thread_switchTx1,thread_switchRx1,thread_memTx1,
thread_memTx2,thread_memRx1,thread_memRx2,thread_pci_Rx1,thread_pci_Tx1;
float CPUClock;
float l1_store_hit;
float l1_load_hit;
float l2_store_hit;
float l2_load_hit;
float llc_load_hit;
float llc_store_hit;
//https://www.youtube.com/watch?v=_Je8K-JcYms&t=1103s
//https://www.erg.abdn.ac.uk/users/gorry/course/lan-pages/nic.html
//https://medium.com/software-design/why-software-developers-should-care-about-cpu-caches-8da04355bb8a
//https://s3-eu-west-1.amazonaws.com/dredbox/IBM_samos2017.pdf
int level_1_cache()
{
//design specific--- direct mapped -- Set Assosiative --- Fully Assosiative
return 4;
}
int level_2_cache()
{
//
return 11;
}
int last_level_cache()
{
return 39;
}
int local_memory()
{
return 105;
}
//This module1 used to simulate deleys by caches
// int module_cache()
// {
// level_1_cache();
// level_2_cache();
// last_level_cache();
// local_memory();
// }
int total_avg_pci_cycles=-1;
int y=-1;
int sub_module_pci_data_phase()
{
float effectiveBw,d,extra_latency;
//effectiveBw=bandwidth*(d/(d+extra_latency));
effectiveBw=bandwidth;
int data_size=256;
double fraction=(PacketSize/(double)effectiveBw);
//cout<<" fr "<< fraction<<" "<<effectiveBw<<"\n";
int num_of_cycles_required=abs(fraction)*frequency;
// cout<<"num of cycles "<<num_of_cycles_required<<"\n";
//remaining task:::::
//end_phase
int endphase=1; ///sending out frame down signal
num_of_cycles_required+=endphase;
if(total_avg_pci_cycles!=-1)
{
total_avg_pci_cycles=num_of_cycles_required+5;
total_avg_pci_cycles*=2;
}
return num_of_cycles_required;
}
int sub_module_pci_addr_phase()
{
// for medium speed device it takes 5-6 clcok cycles
int activate_frame=1;
int set_C_BE_line=1;
int set_A0_A31_lines=1;
//initiator sets IRDY (initiator ready ) and that of target sets TRDY ( target ready)
int IRDY=1;
int TRDY=1;
//
// set appropriate values to these vars according to device
//
//
int t=activate_frame+set_C_BE_line+set_A0_A31_lines+IRDY+TRDY;
return activate_frame+set_C_BE_line+set_A0_A31_lines+IRDY+TRDY;
}
int module_pci( int current ,int fl)
{
// address phase ------> data phase
int v= current+sub_module_pci_addr_phase() + sub_module_pci_data_phase();
return v;
}
//this function adds entries with apropriate deleys to Tx
int odd=1;
void push_entry(int cnum,int fl,int original_cnum,int thread)
{
//int presentcycle=-1;
if(presentcycle!=-1)
{
presentcycle=max(presentcycle+1,cnum+1);
}
else{
presentcycle=cnum+1;
}
if(odd&1)
{
Tx1.push_back(make_pair(make_pair(original_cnum,presentcycle),fl));
threadTx1.push_back(thread);
odd-=1;
//
//
// cout<<"\n"<<presentcycle<<" "<<original_cnum<<endl;
}
else{
Tx2.push_back(make_pair(make_pair(original_cnum,presentcycle),fl));
threadTx2.push_back(thread);
odd+=1;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//odd=1;
//presentcycle=-1
void push_entry2(int cnum,int fl,int original_cnum,int thread)
{
//int presentcycle=-1;
if(presentcycle!=-1)
{
presentcycle=max(presentcycle+1,cnum+1);
}
else{
presentcycle=cnum+1;
}
pci_Rx1.push_back(make_pair(make_pair(original_cnum,presentcycle),fl));
thread_pci_Rx1.push_back(thread);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::map<int, int> mp;
int request_preperation_steering()
{
//map<int, int> mapper;
// mapper.insert(make_pair (100,14));
// mapper.insert(make_pair (200,12));
// mapper.insert(make_pair (300,10));
// mapper.insert(make_pair (400,8));
// mapper.insert(make_pair (500,4));
// mapper.insert(make_pair (1000,2));
// insert elements in random order
mp.insert({ 100, 24 });
mp.insert({ 200, 19 });
mp.insert({ 300, 17 });
mp.insert({ 400, 15 });
mp.insert({ 500, 13 });
mp.insert({ 1000, 11 });
mp.insert({ 100000, 7 });
// when 2 is present
auto it = mp.lower_bound(bandwidth/1000000);
//auto it = lower_bound(bandwidth);
return (*it).second;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int request_preperation_steering2()
{
//map<int, int> mapper;
// mapper.insert(make_pair (100,14));
// mapper.insert(make_pair (200,12));
// mapper.insert(make_pair (300,10));
// mapper.insert(make_pair (400,8));
// mapper.insert(make_pair (500,4));
// mapper.insert(make_pair (1000,2));
// insert elements in random order
mp.insert({ 100, 15 });
mp.insert({ 200, 20 });
mp.insert({ 300, 15 });
mp.insert({ 400, 9 });
mp.insert({ 500, 5 });
mp.insert({ 1000, 4 });
mp.insert({ 100000000, 3 });
// when 2 is present
auto it = mp.lower_bound(bandwidth/1000000);
//auto it = lower_bound(bandwidth);
return (*it).second;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void module_nic_at_compute_brick(int cnum,int fl,int original_cnum,int thread)
{
int extra_cycles=request_preperation_steering();
push_entry(cnum+extra_cycles,fl,original_cnum,thread);
//as entries are pushed into deques
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void module_pci_controller_at_compute_brick(int cnum,int fl,int original_cnum,int thread)
{
int extra_cycles=request_preperation_steering2();
push_entry2(cnum+extra_cycles,fl,original_cnum,thread);
//as entries are pushed into deques
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int br=-1,loop=1;
int nic_init_cost=0;
int check=1;
void process_entries_at_nic(int type)
{
if(type==1)
{
int turn =1; //used for round robin scheduling
int pcn=-1;
while(true)
{
if(Tx1.empty() && Tx2.empty())
{
break;
}
if(turn&1)
{
if(!Tx1.empty())
{
int started=Tx1.front().first.first;
int curr=Tx1.front().first.second;
int fl=Tx1.front().second ;
pcn=max(pcn+1,curr+1);
//int loading_cycles=(int)((long long)(PacketSize*frequency)/(double)network_bandwidth);//*frequency;
double c=PacketSize/(double)network_bandwidth;
int loading_cycles=abs(c)*frequency;
if(loop==1)
{
nic_init_cost=curr-started;
//cout<<nic_init_cost<<" "<<loading_cycles<<" "<<endl;
loop++;
}
int reaching_switch_cycles=((float)switch_distance/(float)speed)*frequency;
switchRx1.push_back(make_pair(make_pair(started,pcn+loading_cycles+reaching_switch_cycles),fl));
//
//cout<<" load cyc "<<loading_cycles<<" switch load "<<reaching_switch_cycles<<" "<<((float)PacketSize/network_bandwidth)<<" \n ";
int thread=threadTx1.front();
thread_switchRx1.push_back(thread);
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
dc[str3].push_back(pcn);
threadTx1.pop_front();
Tx1.pop_front();
}
else
{
int started=Tx2.front().first.first;
int curr=Tx2.front().first.second;
int fl=Tx2.front().second ;
pcn=max(pcn+1,curr+1);
double c=PacketSize/(double)network_bandwidth;
int loading_cycles=abs(c)*frequency;
int reaching_switch_cycles=((float)switch_distance/(float)speed)*frequency;
switchRx1.push_back(make_pair(make_pair(started,pcn+loading_cycles+reaching_switch_cycles),fl));
int thread=threadTx2.front();
thread_switchRx1.push_back(thread);
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
dc[str3].push_back(pcn);
threadTx2.pop_front();
Tx2.pop_front();
}
}
else
{
if(!Tx2.empty())
{
int started=Tx2.front().first.first;
int curr=Tx2.front().first.second;
int fl=Tx2.front().second ;
pcn=max(pcn+1,curr+1);
double c=PacketSize/(double)network_bandwidth;
int loading_cycles=abs(c)*frequency;
int reaching_switch_cycles=((float)switch_distance/(float)speed)*frequency;
switchRx1.push_back(make_pair(make_pair(started,pcn+loading_cycles+reaching_switch_cycles),fl));
int thread=threadTx2.front();
thread_switchRx1.push_back(thread);
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
dc[str3].push_back(pcn);
threadTx2.pop_front();
Tx2.pop_front();
}
else
{
int started=Tx1.front().first.first;
int curr=Tx1.front().first.second;
int fl=Tx1.front().second ;
pcn=max(pcn+1,curr+1);
double c=PacketSize/(double)network_bandwidth;
int loading_cycles=abs(c)*frequency;
int reaching_switch_cycles=((float)switch_distance/(float)speed)*frequency;
switchRx1.push_back(make_pair(make_pair(started,pcn+loading_cycles+reaching_switch_cycles),fl));
int thread=threadTx1.front();
thread_switchRx1.push_back(thread);
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
dc[str3].push_back(pcn);
threadTx1.pop_front();
Tx1.pop_front();
}
}
turn^=1;
}
}
else
{
int turn =1; //used for round robin scheduling
int present_cn=-1;
while(true)
{
if(Rx1.empty() && Rx2.empty())
{
break;
}
if(turn&1)
{
if(!Rx1.empty())
{
int started=Rx1.front().first.first;
int curr=Rx1.front().first.second;
int fl=Rx1.front().second ;
present_cn=max(present_cn+1,curr+1);
double c=PacketSize/(double)network_bandwidth;
int loading_cycles=abs(c)*frequency;
//int reaching_switch_cycles=((float)switch_distance/(float)speed)*frequency;
// switchRx1.push_back(make_pair(make_pair(started,present_cn+loading_cycles+reaching_switch_cycles),fl));
int thread=threadRx1.front();
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
dc[str3].push_back(present_cn+nic_init_cost);
// dc[str3].push_back_back(present_cn);
threadRx1.pop_front();
Rx1.pop_front();
}
else
{
int started=Rx2.front().first.first;
int curr=Rx2.front().first.second;
int fl=Rx2.front().second ;
present_cn=max(present_cn+1,curr+1);
double c=PacketSize/(double)network_bandwidth;
int loading_cycles=abs(c)*frequency;
// int reaching_switch_cycles=((float)switch_distance/(float)speed)*frequency;
// switchRx1.push_back(make_pair(make_pair(started,present_cn+loading_cycles+reaching_switch_cycles),fl));
int thread=threadRx2.front();
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
dc[str3].push_back(present_cn+nic_init_cost);
threadRx2.pop_front();
Rx2.pop_front();
}
}
else
{
if(!Rx2.empty())
{
int started=Rx2.front().first.first;
int curr=Rx2.front().first.second;
int fl=Rx2.front().second ;
present_cn=max(present_cn+1,curr+1);
double c=PacketSize/(double)network_bandwidth;
int loading_cycles=abs(c)*frequency;
// int reaching_switch_cycles=((float)switch_distance/(float)speed)*frequency;
// switchRx1.push(make_pair(make_pair(started,present_cn+loading_cycles+reaching_switch_cycles),fl));
int thread=threadRx2.front();
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
dc[str3].push_back(present_cn+nic_init_cost);
threadRx2.pop_front();
Rx2.pop_front();
}
else
{
int started=Rx1.front().first.first;
int curr=Rx1.front().first.second;
int fl=Rx1.front().second ;
present_cn=max(present_cn+1,curr+1);
int loading_cycles=((float)PacketSize/(float)network_bandwidth)*frequency;
// int reaching_switch_cycles=((float)switch_distance/(float)speed)*frequency;
// switchRx1.push(make_pair(make_pair(started,present_cn+loading_cycles+reaching_switch_cycles),fl));
int thread=threadRx1.front();
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
dc[str3].push_back(present_cn+nic_init_cost);
threadRx1.pop_front();
Rx1.pop_front();
}
}
turn^=1;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void process_entries_at_module_switch(int type)
{
int base_deley=2; //to remove headers and to add appropriate headers
if(type==1)
{
int p_cn=-1;
while(true)
{
if(switchRx1.empty())
{
break;
}
int turn =1;
int started=switchRx1.front().first.first ;
int curr=switchRx1.front().first.second ;
int fl=switchRx1.front().second;
int thread=thread_switchRx1.front();
int q_count=0;
for (int i=0;i<Tx1.size();i++)
{
if(switchRx1[i].first.first<presentcycle)
{
q_count+=1;
if(q_count*128>Q_limit*1000)
switchRx1.erase(Tx1.begin()+i);
}
}
p_cn=max(p_cn+1,curr+1);
int reaching_memb_cycles=((float)switch_distance/(float)speed)*frequency;
// int transfer_cycles=((float)PacketSize/(float)network_bandwidth)*frequency;
double c=PacketSize/(double)switch_bandwidth;
int transfer_cycles=abs(c)*frequency;
if(turn&1)
{
memRx1.push_back(make_pair(make_pair(started,p_cn+transfer_cycles+reaching_memb_cycles),fl));
thread_memRx1.push_back(thread);
turn^=1;
}
else
{
memRx2.push_back(make_pair(make_pair(started,p_cn+transfer_cycles+reaching_memb_cycles),fl));
thread_memRx2.push_back(thread);
turn^=1;
}
string str1= to_string(started);
string str2= to_string(thread);
string str3=str1+" "+str2;
// if(check==1)
// {
// cout<<transfer_cycles<<endl;
// check++;
// }
dc[str3].push_back(p_cn);