-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathScatteredReadCost.cpp
218 lines (163 loc) · 6.33 KB
/
ScatteredReadCost.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
// See how many compute instructions one needs to cover the latency of a memory read message
#include "HAXWell.h"
#include "GENCoder.h"
#include "GENDisassembler.h"
#include "GENIsa.h"
#include <vector>
#include "Misc.h"
#define FILENAME "Scatter.csv"
#define REPEAT_COUNT 32
#define SAME_ADDRESS
static void ConstructShader( std::vector<GEN::Instruction>& ops, size_t nThreadsPerGroup )
{
// write address is threadgroup_id*threads_per_group*2 + 2*local_thread_id
//
ops.push_back(
GEN::DoMath( 8, GEN::OP_XOR, GEN::DT_U32, 4,4,4 )
);
// address mul
ops.push_back(
GEN::BinaryInstruction( 1, GEN::OP_MUL,
GEN::DestOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,4,8),1,1,1 ) ),
GEN::SourceOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,0,4),1,1,1) ),
GEN::SourceOperand( GEN::DT_U32 , nThreadsPerGroup*2 )
)
);
// copy EOT payload
ops.push_back(
GEN::UnaryInstruction( 8, GEN::OP_MOV,
GEN::DestOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(127),8,8,1 ) ),
GEN::SourceOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(0),8,8,1) )
)
);
// address offset
ops.push_back( // assume address offset (2*local) passed in curbe
GEN::BinaryInstruction( 1, GEN::OP_ADD,
GEN::DestOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,4,8),1,1,1 ) ),
GEN::SourceOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,4,8),1,1,1) ),
GEN::SourceOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(1),1,1,1))
)
);
#ifdef SAME_ADDRESS
ops.push_back(
GEN::RegMoveIMM( 9, 0 )
);
#endif
// read timestamp register
ops.push_back(
GEN::UnaryInstruction( 2, GEN::OP_MOV,
GEN::DestOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,6,0),2,2,1 ) ),
GEN::SourceOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_TIMESTAMP,0),2,2,1))
)
);
///////////////////////////////////////////////////
for( size_t i=0; i<REPEAT_COUNT; i++ )
{
ops.push_back(
GEN::DWordScatteredReadSIMD8( HAXWell::BIND_TABLE_BASE + 1, 2, 3 )
);
// use the read result
ops.push_back(
GEN::RegMove(GEN::REG_GPR,7,GEN::REG_GPR,3)
);
}
///////////////////////////////////////////////////
// read timestamp again to get delta
ops.push_back(
GEN::UnaryInstruction( 2, GEN::OP_MOV,
GEN::DestOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,7,0),2,2,1 ) ),
GEN::SourceOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_TIMESTAMP,0),2,2,1))
)
);
// collect timestamps in output reg
ops.push_back(
GEN::UnaryInstruction( 2, GEN::OP_MOV,
GEN::DestOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,5,0),2,2,1 ) ),
GEN::SourceOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,6,0),2,2,1))
)
);
ops.push_back(
GEN::UnaryInstruction( 2, GEN::OP_MOV,
GEN::DestOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,5,8),2,2,1 ) ),
GEN::SourceOperand( GEN::DT_U32, GEN::RegisterRegion( GEN::DirectRegReference(GEN::REG_GPR,7,0),2,2,1))
)
);
// write output (address in r4, data in r5)
ops.push_back(
GEN::OWordDualBlockWrite( HAXWell::BIND_TABLE_BASE, 4 )
);
ops.push_back(
GEN::SendEOT(127)
);
}
double ScatterReadTest( size_t nDivergent )
{
size_t nGroups = 32;
size_t nThreadsPerGroup=60;
std::vector<GEN::Instruction> ops;
ConstructShader(ops,nThreadsPerGroup);
GEN::Encoder enc;
HAXWell::Blob isa;
isa.SetLength( enc.GetBufferSize(ops.size()) );
isa.SetLength( enc.Encode( isa.GetBytes(), ops.data(), ops.size() ) );
//PrintISA(stdout,isa);
int CURBE[HAXWell::MAX_DISPATCH_COUNT][2][8];
for( size_t i=0; i<HAXWell::MAX_DISPATCH_COUNT; i++ )
{
// first CURBE reg is offset into timings buffer
for( size_t j=0; j<8; j++ )
CURBE[i][0][j] = 2*i;
// second CURBE reg is per-channel read offsets for scatter read
for( size_t j=0; j<8; j++ )
CURBE[i][1][j] = 8*i + j;
// send a certain number of them to completely different cache lines
for( size_t j=0; j<nDivergent; j++ )
CURBE[i][1][j] += 16*j;
#ifdef SAME_ADDRESS // nah, use zero instead....
for( size_t j=0; j<8; j++ )
CURBE[i][1][j] = 0;
#endif
}
HAXWell::ShaderArgs args;
args.nDispatchThreadCount = nThreadsPerGroup;
args.nSIMDMode = 16;
args.nCURBEAllocsPerThread = 2;
args.pCURBE = CURBE;
args.nIsaLength = isa.GetLength();
args.pIsa = isa.GetBytes();
HAXWell::ShaderHandle hShader = HAXWell::CreateShader(args);
HAXWell::BufferHandle hBuffers[] =
{
HAXWell::CreateBuffer( 0, 32*nGroups*args.nDispatchThreadCount ),
HAXWell::CreateBuffer( 0, 32*nGroups*args.nDispatchThreadCount*1024 ) ,
};
HAXWell::DispatchShader( hShader, hBuffers,2,nGroups );
HAXWell::Finish();
unsigned int* pBuff = (unsigned int*)HAXWell::MapBuffer(hBuffers[0]);
size_t nAvg=0;
for( size_t i=0; i<nThreadsPerGroup*nGroups; i++ )
{
unsigned int startlo = pBuff[8*i];
unsigned int endlo = pBuff[8*i+2];
nAvg += endlo-startlo;
}
HAXWell::UnmapBuffer(hBuffers[0]);
HAXWell::ReleaseBuffer(hBuffers[0]);
HAXWell::ReleaseBuffer(hBuffers[1]);
HAXWell::ReleaseShader(hShader);
return (nAvg)/(double)(nGroups*nThreadsPerGroup);
}
void ScatteredReadTest()
{
FILE* fp = fopen(FILENAME, "w" );
fprintf(fp, "cachelines, shader latency\n");
for( size_t i=0; i<8; i++ )
{
double tm;
tm = ScatterReadTest(i);
tm += ScatterReadTest(i);
tm += ScatterReadTest(i);
fprintf(fp, "%u,%f\n", i+1,tm/3 );
}
fclose(fp);
}