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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
|
/*
* shader.h
*
* Copyright (c) 2009 by Tor M. Aamodt, Wilson W. L. Fung, Ali Bakhoda,
* George L. Yuan, Ivan Sham, Henry Wong, Dan O'Connor, Henry Tran and the
* University of British Columbia
* Vancouver, BC V6T 1Z4
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT BY DOWNLOADING GPGPU-SIM, YOU ARE AGREEING TO THESE
* TERMS AND CONDITIONS.
*
* 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 OWNERS 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.
*
* NOTE: The files libcuda/cuda_runtime_api.c and src/cuda-sim/cuda-math.h
* are derived from the CUDA Toolset available from http://www.nvidia.com/cuda
* (property of NVIDIA). The files benchmarks/BlackScholes/ and
* benchmarks/template/ are derived from the CUDA SDK available from
* http://www.nvidia.com/cuda (also property of NVIDIA). The files from
* src/intersim/ are derived from Booksim (a simulator provided with the
* textbook "Principles and Practices of Interconnection Networks" available
* from http://cva.stanford.edu/books/ppin/). As such, those files are bound by
* the corresponding legal terms and conditions set forth separately (original
* copyright notices are left in files from these sources and where we have
* modified a file our copyright notice appears before the original copyright
* notice).
*
* Using this version of GPGPU-Sim requires a complete installation of CUDA
* which is distributed seperately by NVIDIA under separate terms and
* conditions. To use this version of GPGPU-Sim with OpenCL requires a
* recent version of NVIDIA's drivers which support OpenCL.
*
* 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 University of British Columbia nor the names of
* its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* 4. This version of GPGPU-SIM is distributed freely for non-commercial use only.
*
* 5. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 6. GPGPU-SIM was developed primarily by Tor M. Aamodt, Wilson W. L. Fung,
* Ali Bakhoda, George L. Yuan, at the University of British Columbia,
* Vancouver, BC V6T 1Z4
*/
#ifndef SHADER_H
#define SHADER_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <bitset>
#include <utility>
#include <algorithm>
#include <deque>
#include "../cuda-sim/ptx.tab.h"
#include "../cuda-sim/dram_callback.h"
#include "delayqueue.h"
#include "stack.h"
#include "dram.h"
#include "../abstract_hardware_model.h"
#include "scoreboard.h"
#include "mem_fetch.h"
#include "stats.h"
#include "gpu-cache.h"
#define NO_OP_FLAG 0xFF
/* READ_PACKET_SIZE:
bytes: 6 address (flit can specify chanel so this gives up to ~2GB/channel, so good for now),
2 bytes [shaderid + mshrid](14 bits) + req_size(0-2 bits if req_size variable) - so up to 2^14 = 16384 mshr total
*/
#define READ_PACKET_SIZE 8
//WRITE_PACKET_SIZE: bytes: 6 address, 2 miscelaneous.
#define WRITE_PACKET_SIZE 8
#define WRITE_MASK_SIZE 8
//Set a hard limit of 32 CTAs per shader [cuda only has 8]
#define MAX_CTA_PER_SHADER 32
class thread_ctx_t {
public:
class ptx_thread_info *m_functional_model_thread_state;
unsigned m_cta_id; // hardware CTA this thread belongs
// per thread stats (ac stands for accumulative).
unsigned n_insn;
unsigned n_insn_ac;
unsigned n_l1_mis_ac;
unsigned n_l1_mrghit_ac;
unsigned n_l1_access_ac;
};
class shd_warp_t {
public:
shd_warp_t( class shader_core_ctx *shader, unsigned warp_size)
: m_shader(shader), m_warp_size(warp_size)
{
m_stores_outstanding=0;
m_inst_in_pipeline=0;
reset();
}
void reset()
{
assert( m_stores_outstanding==0);
assert( m_inst_in_pipeline==0);
m_imiss_pending=false;
m_warp_id=(unsigned)-1;
n_completed = m_warp_size;
m_n_atomic=0;
m_membar=false;
m_done_exit=false;
m_last_fetch=0;
m_next=0;
}
void init( address_type start_pc, unsigned cta_id, unsigned wid, unsigned active )
{
m_cta_id=cta_id;
m_warp_id=wid;
m_next_pc=start_pc;
assert( n_completed >= active );
assert( n_completed <= m_warp_size);
n_completed -= active; // active threads are not yet completed
}
bool done();
bool waiting();
bool done_exit() const { return m_done_exit; }
void set_done_exit() { m_done_exit=true; }
void print( FILE *fout ) const;
void print_ibuffer( FILE *fout ) const;
unsigned get_n_completed() const { return n_completed; }
void inc_n_completed() { n_completed++; }
void set_last_fetch( unsigned long long sim_cycle ) { m_last_fetch=sim_cycle; }
unsigned get_n_atomic() const { return m_n_atomic; }
void inc_n_atomic() { m_n_atomic++; }
void dec_n_atomic(unsigned n) { m_n_atomic-=n; }
void set_membar() { m_membar=true; }
void clear_membar() { m_membar=false; }
bool get_membar() const { return m_membar; }
address_type get_pc() const { return m_next_pc; }
void set_next_pc( address_type pc ) { m_next_pc = pc; }
void ibuffer_fill( unsigned slot, const warp_inst_t *pI )
{
assert(slot < IBUFFER_SIZE );
m_ibuffer[slot].m_inst=pI;
m_ibuffer[slot].m_valid=true;
m_next=0;
}
bool ibuffer_empty() const
{
for( unsigned i=0; i < IBUFFER_SIZE; i++)
if(m_ibuffer[i].m_valid)
return false;
return true;
}
void ibuffer_flush()
{
for(unsigned i=0;i<IBUFFER_SIZE;i++) {
if( m_ibuffer[i].m_valid )
dec_inst_in_pipeline();
m_ibuffer[i].m_inst=NULL;
m_ibuffer[i].m_valid=false;
}
}
const warp_inst_t *ibuffer_next_inst() { return m_ibuffer[m_next].m_inst; }
bool ibuffer_next_valid() { return m_ibuffer[m_next].m_valid; }
void ibuffer_free()
{
m_ibuffer[m_next].m_inst = NULL;
m_ibuffer[m_next].m_valid = false;
}
void ibuffer_step() { m_next = (m_next+1)%IBUFFER_SIZE; }
bool imiss_pending() const { return m_imiss_pending; }
void set_imiss_pending() { m_imiss_pending=true; }
void clear_imiss_pending() { m_imiss_pending=false; }
bool stores_done() const { return m_stores_outstanding == 0; }
void inc_store_req() { m_stores_outstanding++; }
void dec_store_req()
{
assert( m_stores_outstanding > 0 );
m_stores_outstanding--;
}
bool inst_in_pipeline() const { return m_inst_in_pipeline > 0; }
void inc_inst_in_pipeline() { m_inst_in_pipeline++; }
void dec_inst_in_pipeline()
{
assert( m_inst_in_pipeline > 0 );
m_inst_in_pipeline--;
}
unsigned get_cta_id() const { return m_cta_id; }
private:
static const unsigned IBUFFER_SIZE=2;
class shader_core_ctx *m_shader;
unsigned m_cta_id;
unsigned m_warp_id;
unsigned m_warp_size;
address_type m_next_pc;
unsigned n_completed; // number of threads in warp completed
bool m_imiss_pending;
struct ibuffer_entry {
ibuffer_entry() { m_valid = false; m_inst = NULL; }
const warp_inst_t *m_inst;
bool m_valid;
};
ibuffer_entry m_ibuffer[IBUFFER_SIZE];
unsigned m_next;
unsigned m_n_atomic; // number of outstanding atomic operations
bool m_membar; // if true, warp is waiting at memory barrier
bool m_done_exit; // true once thread exit has been registered for threads in this warp
unsigned long long m_last_fetch;
unsigned m_stores_outstanding; // number of store requests sent but not yet acknowledged
unsigned m_inst_in_pipeline;
};
inline unsigned hw_tid_from_wid(unsigned wid, unsigned warp_size, unsigned i){return wid * warp_size + i;};
inline unsigned wid_from_hw_tid(unsigned tid, unsigned warp_size){return tid/warp_size;};
// bounded stack that implements pdom reconvergence (see MICRO'07 paper)
class pdom_warp_ctx_t {
public:
pdom_warp_ctx_t( unsigned wid, class shader_core_ctx *shdr );
void reset();
void launch( address_type start_pc, unsigned active_mask );
void pdom_update_warp_mask();
unsigned get_active_mask() const;
void get_pdom_stack_top_info( unsigned *pc, unsigned *rpc ) const;
unsigned get_rp() const;
void print(FILE*fp) const;
private:
unsigned m_warp_id;
class shader_core_ctx *m_shader;
unsigned m_stack_top;
unsigned m_warp_size;
address_type *m_pc;
unsigned int *m_active_mask;
address_type *m_recvg_pc;
unsigned int *m_calldepth;
unsigned long long *m_branch_div_cycle;
};
const unsigned WARP_PER_CTA_MAX = 32;
typedef std::bitset<WARP_PER_CTA_MAX> warp_set_t;
int register_bank(int regnum, int wid, unsigned num_banks, unsigned bank_warp_shift);
class shader_core_ctx;
class opndcoll_rfu_t { // operand collector based register file unit
public:
// constructors
opndcoll_rfu_t()
{
m_num_collectors=0;
m_num_banks=0;
m_cu = NULL;
m_shader=NULL;
m_num_ports=0;
}
void add_port( unsigned num_collector_units,
warp_inst_t **input_port,
warp_inst_t **output_port );
void init( unsigned num_banks, shader_core_ctx *shader );
// modifiers
bool writeback( const warp_inst_t &warp ); // might cause stall
void step()
{
dispatch_ready_cu();
allocate_reads();
for( unsigned p=0; p < m_num_ports; p++ )
allocate_cu( p );
process_banks();
}
void dump( FILE *fp ) const
{
fprintf(fp,"\n");
fprintf(fp,"Operand Collector State:\n");
for( unsigned n=0; n < m_num_collectors; n++ ) {
fprintf(fp," CU-%2u: ", n);
m_cu[n].dump(fp,m_shader);
}
m_arbiter.dump(fp);
}
shader_core_ctx *shader_core() { return m_shader; }
private:
void process_banks()
{
m_arbiter.reset_alloction();
}
void dispatch_ready_cu();
void allocate_cu( unsigned port );
void allocate_reads();
// types
class collector_unit_t;
class op_t {
public:
op_t() { m_valid = false; }
op_t( collector_unit_t *cu, unsigned op, unsigned reg, unsigned num_banks, unsigned bank_warp_shift )
{
m_valid = true;
m_warp=NULL;
m_cu = cu;
m_operand = op;
m_register = reg;
m_bank = register_bank(reg,cu->get_warp_id(),num_banks,bank_warp_shift);
}
op_t( const warp_inst_t *warp, unsigned reg, unsigned num_banks, unsigned bank_warp_shift )
{
m_valid=true;
m_warp=warp;
m_register=reg;
m_cu=NULL;
m_operand = -1;
m_bank = register_bank(reg,warp->warp_id(),num_banks,bank_warp_shift);
}
// accessors
bool valid() const { return m_valid; }
unsigned get_reg() const
{
assert( m_valid );
return m_register;
}
unsigned get_wid() const
{
if( m_warp ) return m_warp->warp_id();
else if( m_cu ) return m_cu->get_warp_id();
else abort();
return 0;
}
unsigned get_oc_id() const { return m_cu->get_id(); }
unsigned get_bank() const { return m_bank; }
unsigned get_operand() const { return m_operand; }
void dump(FILE *fp) const
{
if(m_cu)
fprintf(fp," <R%u, CU:%u, w:%02u> ", m_register,m_cu->get_id(),m_cu->get_warp_id());
else if( !m_warp->empty() )
fprintf(fp," <R%u, wid:%02u> ", m_register,m_warp->warp_id() );
}
std::string get_reg_string() const
{
char buffer[64];
snprintf(buffer,64,"R%u", m_register);
return std::string(buffer);
}
// modifiers
void reset() { m_valid = false; }
private:
bool m_valid;
collector_unit_t *m_cu;
const warp_inst_t *m_warp;
unsigned m_operand; // operand offset in instruction. e.g., add r1,r2,r3; r2 is oprd 0, r3 is 1 (r1 is dst)
unsigned m_register;
unsigned m_bank;
};
enum alloc_t {
NO_ALLOC,
READ_ALLOC,
WRITE_ALLOC,
};
class allocation_t {
public:
allocation_t() { m_allocation = NO_ALLOC; }
bool is_read() const { return m_allocation==READ_ALLOC; }
bool is_write() const {return m_allocation==WRITE_ALLOC; }
bool is_free() const {return m_allocation==NO_ALLOC; }
void dump(FILE *fp) const {
if( m_allocation == NO_ALLOC ) { fprintf(fp,"<free>"); }
else if( m_allocation == READ_ALLOC ) { fprintf(fp,"rd: "); m_op.dump(fp); }
else if( m_allocation == WRITE_ALLOC ) { fprintf(fp,"wr: "); m_op.dump(fp); }
fprintf(fp,"\n");
}
void alloc_read( const op_t &op ) { assert(is_free()); m_allocation=READ_ALLOC; m_op=op; }
void alloc_write( const op_t &op ) { assert(is_free()); m_allocation=WRITE_ALLOC; m_op=op; }
void reset() { m_allocation = NO_ALLOC; }
private:
enum alloc_t m_allocation;
op_t m_op;
};
class arbiter_t {
public:
// constructors
arbiter_t()
{
m_queue=NULL;
m_allocated_bank=NULL;
m_allocator_rr_head=NULL;
_inmatch=NULL;
_outmatch=NULL;
_request=NULL;
}
void init( unsigned num_cu, unsigned num_banks )
{
m_num_collectors = num_cu;
m_num_banks = num_banks;
_inmatch = new int[ m_num_banks ];
_outmatch = new int[ m_num_collectors ];
_request = new int*[ m_num_banks ];
for(unsigned i=0; i<m_num_banks;i++)
_request[i] = new int[m_num_collectors];
m_queue = new std::list<op_t>[num_banks];
m_allocated_bank = new allocation_t[num_banks];
m_allocator_rr_head = new unsigned[num_cu];
for( unsigned n=0; n<num_cu;n++ )
m_allocator_rr_head[n] = n%num_banks;
reset_alloction();
}
// accessors
void dump(FILE *fp) const
{
fprintf(fp,"\n");
fprintf(fp," Arbiter State:\n");
fprintf(fp," requests:\n");
for( unsigned b=0; b<m_num_banks; b++ ) {
fprintf(fp," bank %u : ", b );
std::list<op_t>::const_iterator o = m_queue[b].begin();
for(; o != m_queue[b].end(); o++ ) {
o->dump(fp);
}
fprintf(fp,"\n");
}
fprintf(fp," grants:\n");
for(unsigned b=0;b<m_num_banks;b++) {
fprintf(fp," bank %u : ", b );
m_allocated_bank[b].dump(fp);
}
fprintf(fp,"\n");
}
// modifiers
std::list<op_t> allocate_reads();
void add_read_requests( collector_unit_t *cu )
{
const op_t *src = cu->get_operands();
for( unsigned i=0; i<MAX_REG_OPERANDS; i++) {
const op_t &op = src[i];
if( op.valid() ) {
unsigned bank = op.get_bank();
m_queue[bank].push_back(op);
}
}
}
bool bank_idle( unsigned bank ) const
{
return m_allocated_bank[bank].is_free();
}
void allocate_bank_for_write( unsigned bank, const op_t &op )
{
assert( bank < m_num_banks );
m_allocated_bank[bank].alloc_write(op);
}
void allocate_for_read( unsigned bank, const op_t &op )
{
assert( bank < m_num_banks );
m_allocated_bank[bank].alloc_read(op);
}
void reset_alloction()
{
for( unsigned b=0; b < m_num_banks; b++ )
m_allocated_bank[b].reset();
}
private:
unsigned m_num_banks;
unsigned m_num_collectors;
allocation_t *m_allocated_bank; // bank # -> register that wins
std::list<op_t> *m_queue;
unsigned *m_allocator_rr_head; // cu # -> next bank to check for request (rr-arb)
unsigned m_last_cu; // first cu to check while arb-ing banks (rr)
int *_inmatch;
int *_outmatch;
int **_request;
};
class collector_unit_t {
public:
// constructors
collector_unit_t()
{
m_free = true;
m_warp = NULL;
m_src_op = new op_t[MAX_REG_OPERANDS];
m_not_ready.reset();
m_warp_id = -1;
m_num_banks = 0;
m_bank_warp_shift = 0;
}
// accessors
bool ready() const;
const op_t *get_operands() const { return m_src_op; }
void dump(FILE *fp, const shader_core_ctx *shader ) const;
unsigned get_warp_id() const { return m_warp_id; }
unsigned get_id() const { return m_cuid; } // returns CU hw id
// modifiers
void init(unsigned n,
warp_inst_t **port,
unsigned num_banks,
unsigned log2_warp_size,
const core_config *config,
opndcoll_rfu_t *rfu );
void allocate( warp_inst_t *&pipeline_reg );
void collect_operand( unsigned op )
{
m_not_ready.reset(op);
}
void dispatch();
private:
bool m_free;
unsigned m_cuid; // collector unit hw id
warp_inst_t **m_port; // pipeline register to issue to when ready
unsigned m_warp_id;
warp_inst_t *m_warp;
op_t *m_src_op;
std::bitset<MAX_REG_OPERANDS> m_not_ready;
unsigned m_num_banks;
unsigned m_bank_warp_shift;
opndcoll_rfu_t *m_rfu;
};
class dispatch_unit_t {
public:
dispatch_unit_t()
{
m_last_cu=0;
m_num_collectors=0;
m_collector_units=NULL;
m_next_cu=0;
}
void init( unsigned num_collectors )
{
m_num_collectors = num_collectors;
m_collector_units = new collector_unit_t * [num_collectors];
m_next_cu=0;
}
void add_cu( collector_unit_t *cu )
{
assert(m_next_cu<m_num_collectors);
m_collector_units[m_next_cu] = cu;
m_next_cu++;
}
collector_unit_t *find_ready()
{
for( unsigned n=0; n < m_num_collectors; n++ ) {
unsigned c=(m_last_cu+n+1)%m_num_collectors;
if( m_collector_units[c]->ready() ) {
m_last_cu=c;
return m_collector_units[c];
}
}
return NULL;
}
private:
unsigned m_num_collectors;
collector_unit_t **m_collector_units;
unsigned m_last_cu; // dispatch ready cu's rr
unsigned m_next_cu; // for initialization
};
// opndcoll_rfu_t data members
unsigned m_num_collectors;
unsigned m_num_banks;
unsigned m_bank_warp_shift;
unsigned m_warp_size;
collector_unit_t *m_cu;
arbiter_t m_arbiter;
unsigned m_num_ports;
std::vector<warp_inst_t**> m_input;
std::vector<warp_inst_t**> m_output;
std::vector<unsigned> m_num_collector_units;
warp_inst_t **m_alu_port;
typedef std::map<warp_inst_t**/*port*/,dispatch_unit_t> port_to_du_t;
port_to_du_t m_dispatch_units;
std::map<warp_inst_t**,std::list<collector_unit_t*> > m_free_cu;
shader_core_ctx *m_shader;
};
class barrier_set_t {
public:
barrier_set_t( unsigned max_warps_per_core, unsigned max_cta_per_core );
// during cta allocation
void allocate_barrier( unsigned cta_id, warp_set_t warps );
// during cta deallocation
void deallocate_barrier( unsigned cta_id );
typedef std::map<unsigned, warp_set_t > cta_to_warp_t;
// individual warp hits barrier
void warp_reaches_barrier( unsigned cta_id, unsigned warp_id );
// fetching a warp
bool available_for_fetch( unsigned warp_id ) const;
// warp reaches exit
void warp_exit( unsigned warp_id );
// assertions
bool warp_waiting_at_barrier( unsigned warp_id ) const;
// debug
void dump() const;
private:
unsigned m_max_cta_per_core;
unsigned m_max_warps_per_core;
cta_to_warp_t m_cta_to_warps;
warp_set_t m_warp_active;
warp_set_t m_warp_at_barrier;
};
struct insn_latency_info {
unsigned pc;
unsigned long latency;
};
struct ifetch_buffer_t {
ifetch_buffer_t() { m_valid=false; }
ifetch_buffer_t( address_type pc, unsigned nbytes, unsigned warp_id )
{
m_valid=true;
m_pc=pc;
m_nbytes=nbytes;
m_warp_id=warp_id;
}
bool m_valid;
address_type m_pc;
unsigned m_nbytes;
unsigned m_warp_id;
};
class shader_core_config;
class simd_function_unit {
public:
simd_function_unit( const shader_core_config *config );
~simd_function_unit() { delete m_dispatch_reg; }
// modifiers
virtual void issue( warp_inst_t *&inst ) { move_warp(m_dispatch_reg,inst); }
virtual void cycle() = 0;
// accessors
virtual unsigned clock_multiplier() const { return 1; }
virtual bool can_issue( const warp_inst_t & ) const { return m_dispatch_reg->empty(); }
virtual bool stallable() const = 0;
virtual void print( FILE *fp ) const
{
fprintf(fp,"%s dispatch= ", m_name.c_str() );
m_dispatch_reg->print(fp);
}
protected:
std::string m_name;
const shader_core_config *m_config;
warp_inst_t *m_dispatch_reg;
};
class pipelined_simd_unit : public simd_function_unit {
public:
pipelined_simd_unit( warp_inst_t **result_port, const shader_core_config *config, unsigned max_latency );
//modifiers
virtual void cycle()
{
if( !m_pipeline_reg[0]->empty() )
move_warp(*m_result_port,m_pipeline_reg[0]); // non-stallable pipeline
for( unsigned stage=0; (stage+1)<m_pipeline_depth; stage++ )
move_warp(m_pipeline_reg[stage], m_pipeline_reg[stage+1]);
if( !m_dispatch_reg->empty() ) {
if( !m_dispatch_reg->dispatch_delay() ) {
int start_stage = m_dispatch_reg->latency - m_dispatch_reg->initiation_interval;
move_warp(m_pipeline_reg[start_stage],m_dispatch_reg);
}
}
}
virtual void issue( warp_inst_t *&inst )
{
move_warp(m_dispatch_reg,inst);
}
// accessors
virtual bool stallable() const { return false; }
virtual bool can_issue( const warp_inst_t &inst ) const
{
return simd_function_unit::can_issue(inst);
}
virtual void print(FILE *fp) const
{
simd_function_unit::print(fp);
for( int s=m_pipeline_depth-1; s>=0; s-- ) {
if( !m_pipeline_reg[s]->empty() ) {
fprintf(fp," %s[%2d] ", m_name.c_str(), s );
m_pipeline_reg[s]->print(fp);
}
}
}
protected:
unsigned m_pipeline_depth;
warp_inst_t **m_pipeline_reg;
warp_inst_t **m_result_port;
};
class sfu : public pipelined_simd_unit
{
public:
sfu( warp_inst_t **result_port, const shader_core_config *config );
virtual bool can_issue( const warp_inst_t &inst ) const
{
switch(inst.op) {
case SFU_OP: break;
case ALU_SFU_OP: break;
default: return false;
}
return pipelined_simd_unit::can_issue(inst);
}
};
class sp_unit : public pipelined_simd_unit
{
public:
sp_unit( warp_inst_t **result_port, const shader_core_config *config );
virtual bool can_issue( const warp_inst_t &inst ) const
{
switch(inst.op) {
case SFU_OP: return false;
case LOAD_OP: return false;
case STORE_OP: return false;
case MEMORY_BARRIER_OP: return false;
default: break;
}
return pipelined_simd_unit::can_issue(inst);
}
};
class simt_core_cluster;
class shader_memory_interface;
class cache_t;
class ldst_unit: public pipelined_simd_unit {
public:
ldst_unit( shader_memory_interface *icnt,
shader_core_ctx *core,
opndcoll_rfu_t *operand_collector,
Scoreboard *scoreboard,
const shader_core_config *config,
const memory_config *mem_config,
class shader_core_stats *stats,
unsigned sid, unsigned tpc );
// modifiers
virtual void cycle();
void fill( mem_fetch *mf );
void flush();
void writeback();
// accessors
virtual unsigned clock_multiplier() const;
virtual bool can_issue( const warp_inst_t &inst ) const
{
switch(inst.op) {
case LOAD_OP: break;
case STORE_OP: break;
case MEMORY_BARRIER_OP: break;
default: return false;
}
return simd_function_unit::can_issue(inst);
}
virtual bool stallable() const { return true; }
bool response_buffer_full() const;
void print(FILE *fout) const;
private:
bool shared_cycle( warp_inst_t &inst, mem_stage_stall_type &rc_fail, mem_stage_access_type &fail_type);
bool constant_cycle( warp_inst_t &inst, mem_stage_stall_type &rc_fail, mem_stage_access_type &fail_type);
bool texture_cycle( warp_inst_t &inst, mem_stage_stall_type &rc_fail, mem_stage_access_type &fail_type);
bool memory_cycle( warp_inst_t &inst, mem_stage_stall_type &rc_fail, mem_stage_access_type &fail_type);
mem_stage_stall_type process_memory_access_queue( cache_t *cache, warp_inst_t &inst );
mem_fetch *create_data_mem_fetch(const warp_inst_t &inst, const mem_access_t &access);
const memory_config *m_memory_config;
class shader_memory_interface *m_icnt;
class shader_core_ctx *m_core;
unsigned m_sid;
unsigned m_tpc;
tex_cache *m_L1T; // texture cache
read_only_cache *m_L1C; // constant cache
std::map<unsigned/*warp_id*/, std::map<unsigned/*regnum*/,unsigned/*count*/> > m_pending_writes;
std::list<mem_fetch*> m_response_fifo;
opndcoll_rfu_t *m_operand_collector;
Scoreboard *m_scoreboard;
mem_fetch *m_next_global;
warp_inst_t m_next_wb;
unsigned m_writeback_arb; // round-robin arbiter for writeback contention between L1T, L1C, shared
unsigned m_num_writeback_clients;
enum mem_stage_stall_type m_mem_rc;
shader_core_stats *m_stats;
};
enum pipeline_stage_name_t {
ID_OC_SP=0,
ID_OC_SFU,
ID_OC_MEM,
OC_EX_SP,
OC_EX_SFU,
OC_EX_MEM,
EX_WB,
N_PIPELINE_STAGES
};
struct shader_core_config : public core_config
{
void init()
{
int ntok = sscanf(gpgpu_shader_core_pipeline_opt,"%d:%d",
&n_thread_per_shader,
&warp_size);
if(ntok != 2) {
printf("GPGPU-Sim uArch: error while parsing configuration string gpgpu_shader_core_pipeline_opt\n");
abort();
}
max_warps_per_shader = n_thread_per_shader/warp_size;
assert( !(n_thread_per_shader % warp_size) );
max_sfu_latency = 32;
max_sp_latency = 32;
m_L1I_config.init();
m_L1T_config.init();
m_L1C_config.init();
gpgpu_cache_texl1_linesize = m_L1T_config.get_line_sz();
gpgpu_cache_constl1_linesize = m_L1C_config.get_line_sz();
m_valid = true;
}
void reg_options(class OptionParser * opp );
unsigned max_cta( const kernel_info_t &k ) const;
unsigned num_shader() const { return n_simt_clusters*n_simt_cores_per_cluster; }
// data
char *gpgpu_shader_core_pipeline_opt;
bool gpgpu_perfect_mem;
enum divergence_support_t model;
unsigned n_thread_per_shader;
unsigned max_warps_per_shader;
unsigned max_cta_per_core; //Limit on number of concurrent CTAs in shader core
cache_config m_L1I_config;
cache_config m_L1T_config;
cache_config m_L1C_config;
bool gpgpu_dwf_reg_bankconflict;
int gpgpu_operand_collector_num_units_sp;
int gpgpu_operand_collector_num_units_sfu;
int gpgpu_operand_collector_num_units_mem;
//Shader core resources
unsigned gpgpu_shader_registers;
int gpgpu_warpdistro_shader;
unsigned gpgpu_num_reg_banks;
unsigned gpu_max_cta_per_shader; // TODO: modify this for fermi... computed based upon kernel
// resource usage; used in shader_core_ctx::translate_local_memaddr
bool gpgpu_reg_bank_use_warp_id;
bool gpgpu_local_mem_map;
int gpu_padded_cta_size;
unsigned max_sp_latency;
unsigned max_sfu_latency;
unsigned n_simt_cores_per_cluster;
unsigned n_simt_clusters;
unsigned n_simt_ejection_buffer_size;
unsigned ldst_unit_response_queue_size;
unsigned mem2device(unsigned memid) const { return memid + n_simt_clusters; }
};
struct shader_core_stats_pod {
unsigned gpgpu_n_load_insn;
unsigned gpgpu_n_store_insn;
unsigned gpgpu_n_shmem_insn;
unsigned gpgpu_n_tex_insn;
unsigned gpgpu_n_const_insn;
unsigned gpgpu_n_param_insn;
unsigned gpgpu_n_shmem_bkconflict;
unsigned gpgpu_n_cache_bkconflict;
int gpgpu_n_intrawarp_mshr_merge;
unsigned gpgpu_n_cmem_portconflict;
unsigned gpu_stall_shd_mem_breakdown[N_MEM_STAGE_ACCESS_TYPE][N_MEM_STAGE_STALL_TYPE];
unsigned gpu_reg_bank_conflict_stalls;
unsigned *shader_cycle_distro;
unsigned *last_shader_cycle_distro;
unsigned L1_write_miss;
unsigned L1_read_miss;
unsigned L1_texture_miss;
unsigned L1_const_miss;
unsigned L1_write_hit_on_miss;
unsigned L1_writeback;
unsigned long long gpu_sim_insn_no_ld_const;
unsigned long long gpu_completed_thread;
unsigned gpgpu_commit_pc_beyond_two;
unsigned gpu_stall_shd_mem;
unsigned gpu_stall_sh2icnt;
int *num_warps_issuable;
int *num_warps_issuable_pershader;
//memory access classification
int gpgpu_n_mem_read_local;
int gpgpu_n_mem_write_local;
int gpgpu_n_mem_texture;
int gpgpu_n_mem_const;
int gpgpu_n_mem_read_global;
int gpgpu_n_mem_write_global;
int gpgpu_n_mem_read_inst;
unsigned made_write_mfs;
unsigned made_read_mfs;
};
class shader_core_stats : private shader_core_stats_pod {
public:
shader_core_stats( const shader_core_config *config )
{
m_config = config;
shader_core_stats_pod *pod = this;
memset(pod,0,sizeof(shader_core_stats_pod));
num_warps_issuable = (int*) calloc(config->max_warps_per_shader+1, sizeof(int));
num_warps_issuable_pershader = (int*) calloc(config->n_simt_clusters*config->n_simt_cores_per_cluster, sizeof(int));
shader_cycle_distro = (unsigned int*) calloc(config->warp_size+3, sizeof(unsigned int));
last_shader_cycle_distro = (unsigned int*) calloc(m_config->warp_size+3, sizeof(unsigned int));
}
void new_grid()
{
gpu_sim_insn_no_ld_const = 0;
gpu_completed_thread = 0;
}
void visualizer_print( gzFile visualizer_file );
unsigned long long get_gpu_completed_thread() const { return gpu_completed_thread; }
void print( FILE *fout ) const;
private:
const shader_core_config *m_config;
friend class shader_core_ctx;
friend class ldst_unit;
friend class simt_core_cluster;
};
class shader_core_ctx : public core_t
{
public:
shader_core_ctx( class gpgpu_sim *gpu,
class simt_core_cluster *cluster,
unsigned shader_id,
unsigned tpc_id,
const struct shader_core_config *config,
const struct memory_config *mem_config,
shader_core_stats *stats );
void issue_block2core( class kernel_info_t &kernel );
void get_pdom_stack_top_info( unsigned tid, unsigned *pc, unsigned *rpc ) const;
bool ptx_thread_done( unsigned hw_thread_id ) const;
class ptx_thread_info *get_thread_state( unsigned hw_thread_id );
void mem_instruction_stats(const warp_inst_t &inst);
virtual void warp_exit( unsigned warp_id );
virtual bool warp_waiting_at_barrier( unsigned warp_id ) const;
virtual class gpgpu_sim *get_gpu();
void set_at_memory_barrier( unsigned warp_id );
bool warp_waiting_at_mem_barrier( unsigned warp_id );
void allocate_barrier( unsigned cta_id, warp_set_t warps );
void deallocate_barrier( unsigned cta_id );
void decrement_atomic_count( unsigned wid, unsigned n );
void cycle();
void reinit(unsigned start_thread, unsigned end_thread, bool reset_not_completed );
void init_warps(unsigned cta_id, unsigned start_thread, unsigned end_thread);
void cache_flush();
void display_pdom_state(FILE *fout, int mask ) const;
void display_pipeline( FILE *fout, int print_mem, int mask3bit ) const;
void register_cta_thread_exit(int cta_num );
bool fetch_unit_response_buffer_full() const;
void accept_fetch_response( mem_fetch *mf );
bool ldst_unit_response_buffer_full();
void accept_ldst_unit_response( class mem_fetch * mf );
void store_ack( class mem_fetch *mf );
class ptx_thread_info* get_functional_thread( unsigned tid ) { return m_thread[tid].m_functional_model_thread_state; }
std::list<unsigned> get_regs_written( const inst_t &fvt ) const;
const shader_core_config *get_config() const { return m_config; }
unsigned get_num_sim_insn() const { return m_num_sim_insn; }
int get_not_completed() const { return m_not_completed; }
unsigned get_n_diverge() const { return m_n_diverge; }
unsigned get_thread_n_insn( unsigned tid ) const { return m_thread[tid].n_insn; }
unsigned get_thread_n_insn_ac( unsigned tid ) const { return m_thread[tid].n_insn_ac; }
unsigned get_thread_n_l1_mis_ac( unsigned tid ) const { return m_thread[tid].n_l1_mis_ac; }
unsigned get_thread_n_l1_mrghit_ac( unsigned tid ) const { return m_thread[tid].n_l1_mrghit_ac; }
unsigned get_thread_n_l1_access_ac( unsigned tid ) const { return m_thread[tid].n_l1_access_ac; }
unsigned get_n_active_cta() const { return m_n_active_cta; }
void inc_store_req( unsigned warp_id) { m_warp[warp_id].inc_store_req(); }
void dec_inst_in_pipeline( unsigned warp_id ) { m_warp[warp_id].dec_inst_in_pipeline(); }
private:
void dump_istream_state( FILE *fout ) const;
address_type next_pc( int tid ) const;
void fetch();
void decode();
void issue_warp( warp_inst_t *&warp, const warp_inst_t *pI, unsigned active_mask, unsigned warp_id );
void func_exec_inst( warp_inst_t &inst );
address_type translate_local_memaddr(address_type localaddr, unsigned tid, unsigned num_shader );
void execute();
void writeback();
void print_stage(unsigned int stage, FILE *fout) const;
// general information
unsigned m_sid; // shader id
unsigned m_tpc; // texture processor cluster id (aka, node id when using interconnect concentration)
const shader_core_config *m_config;
const memory_config *m_memory_config;
class simt_core_cluster *m_cluster;
class gpgpu_sim *m_gpu;
// statistics
shader_core_stats *m_stats;
unsigned int m_num_sim_insn; // number of instructions committed by this shader core
unsigned int m_n_diverge; // number of divergence occurred in this shader
// CTA scheduling / hardware thread allocation
int m_n_active_cta; // number of Cooperative Thread Arrays (blocks) currently running on this shader.
int m_cta_status[MAX_CTA_PER_SHADER]; // CTAs status
int m_not_completed; // number of threads to be completed (==0 when all thread on this core completed)
// thread contexts
thread_ctx_t *m_thread; // functional state, per thread fetch state
// interconnect interface
shader_memory_interface *m_icnt;
// fetch
read_only_cache *m_L1I; // instruction cache
int m_last_warp_fetched;
// decode/dispatch
int m_last_warp_issued;
std::vector<shd_warp_t> m_warp; // per warp information array
barrier_set_t m_barriers;
ifetch_buffer_t m_inst_fetch_buffer;
pdom_warp_ctx_t **m_pdom_warp; // pdom reconvergence context for each warp
warp_inst_t** m_pipeline_reg;
Scoreboard *m_scoreboard;
opndcoll_rfu_t m_operand_collector;
// execute
unsigned m_num_function_units;
enum pipeline_stage_name_t *m_dispatch_port;
enum pipeline_stage_name_t *m_issue_port;
simd_function_unit **m_fu; // stallable pipelines should be last in this array
ldst_unit *m_ldst_unit;
static const unsigned MAX_ALU_LATENCY = 64;
std::bitset<MAX_ALU_LATENCY> m_result_bus;
};
class simt_core_cluster {
public:
simt_core_cluster( class gpgpu_sim *gpu,
unsigned cluster_id,
const struct shader_core_config *config,
const struct memory_config *mem_config,
shader_core_stats *stats );
void core_cycle();
void icnt_cycle();
void reinit();
unsigned issue_block2core( class kernel_info_t &kernel );
void cache_flush();
bool icnt_injection_buffer_full(unsigned size, bool write);
void icnt_inject_request_packet(class mem_fetch *mf);
void get_pdom_stack_top_info( unsigned sid, unsigned tid, unsigned *pc, unsigned *rpc ) const;
unsigned max_cta( const kernel_info_t &kernel );
unsigned get_not_completed() const;
unsigned get_n_active_cta() const;
gpgpu_sim *get_gpu() { return m_gpu; }
void display_pipeline( unsigned sid, FILE *fout, int print_mem, int mask );
private:
unsigned sid_to_cid( unsigned sid ) const { return sid % m_config->n_simt_cores_per_cluster; }
unsigned cid_to_sid( unsigned cid ) const { return m_cluster_id*m_config->n_simt_cores_per_cluster + cid; }
unsigned m_cluster_id;
gpgpu_sim *m_gpu;
const shader_core_config *m_config;
shader_core_stats *m_stats;
shader_core_ctx **m_core;
unsigned m_cta_issue_next_core;
std::list<mem_fetch*> m_response_fifo;
};
class shader_memory_interface : public mem_fetch_interface {
public:
shader_memory_interface( shader_core_ctx *core, simt_core_cluster *cluster ) { m_core=core; m_cluster=cluster; }
virtual bool full( unsigned size, bool write ) const
{
return m_cluster->icnt_injection_buffer_full(size,write);
}
virtual void push(mem_fetch *mf)
{
if( !mf->get_inst().empty() )
m_core->mem_instruction_stats(mf->get_inst()); // not I$-fetch
m_cluster->icnt_inject_request_packet(mf);
}
private:
shader_core_ctx *m_core;
simt_core_cluster *m_cluster;
};
#endif /* SHADER_H */
|