-
Notifications
You must be signed in to change notification settings - Fork 9
/
gridbuilder.go
190 lines (162 loc) · 3.8 KB
/
gridbuilder.go
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
package kernels
import "github.com/sarchlab/mgpusim/v3/insts"
// WGFilterFunc is a filter
type WGFilterFunc func(
*HsaKernelDispatchPacket,
*WorkGroup,
) bool
// KernelLaunchInfo includes the necessary information to launch a kernel.
type KernelLaunchInfo struct {
CodeObject *insts.HsaCo
Packet *HsaKernelDispatchPacket
PacketAddr uint64
WGFilter WGFilterFunc
}
// A GridBuilder is the unit that can build a grid and its internal structure
// from a kernel and its launch parameters.
type GridBuilder interface {
SetKernel(info KernelLaunchInfo)
NumWG() int
NextWG() *WorkGroup
Skip(n int)
}
// NewGridBuilder creates a default grid builder
func NewGridBuilder() GridBuilder {
return &gridBuilderImpl{}
}
type gridBuilderImpl struct {
hsaco *insts.HsaCo
packet *HsaKernelDispatchPacket
filter WGFilterFunc
packetAddr uint64
numWG int
xid, yid, zid int
}
func (b *gridBuilderImpl) SetKernel(
info KernelLaunchInfo,
) {
b.hsaco = info.CodeObject
b.packet = info.Packet
b.packetAddr = info.PacketAddr
b.filter = info.WGFilter
b.xid = 0
b.yid = 0
b.zid = 0
b.countWG()
}
func (b *gridBuilderImpl) Skip(n int) {
for i := 0; i < n; i++ {
b.NextWG()
}
}
func (b *gridBuilderImpl) countWG() {
x := int(b.packet.GridSizeX-1)/int(b.packet.WorkgroupSizeX) + 1
y := int(b.packet.GridSizeY-1)/int(b.packet.WorkgroupSizeY) + 1
z := int(b.packet.GridSizeZ-1)/int(b.packet.WorkgroupSizeZ) + 1
if b.filter == nil {
b.numWG = x * y * z
return
}
b.numWG = 0
for i := 0; i < x; i++ {
for j := 0; j < y; j++ {
for k := 0; k < z; k++ {
wg := WorkGroup{
IDX: i,
IDY: j,
IDZ: k,
}
if b.filter(b.packet, &wg) {
b.numWG++
}
}
}
}
}
func (b *gridBuilderImpl) NumWG() int {
return b.numWG
}
func (b *gridBuilderImpl) NextWG() *WorkGroup {
wg := NewWorkGroup()
for {
xLeft := int(b.packet.GridSizeX) - b.xid*int(b.packet.WorkgroupSizeX)
yLeft := int(b.packet.GridSizeY) - b.yid*int(b.packet.WorkgroupSizeY)
zLeft := int(b.packet.GridSizeZ) - b.zid*int(b.packet.WorkgroupSizeZ)
if xLeft <= 0 || yLeft <= 0 || zLeft <= 0 {
return nil
}
xToAllocate := min(xLeft, int(b.packet.WorkgroupSizeX))
yToAllocate := min(yLeft, int(b.packet.WorkgroupSizeY))
zToAllocate := min(zLeft, int(b.packet.WorkgroupSizeZ))
wg.Packet = b.packet
wg.CodeObject = b.hsaco
wg.SizeX = int(b.packet.WorkgroupSizeX)
wg.SizeY = int(b.packet.WorkgroupSizeY)
wg.SizeZ = int(b.packet.WorkgroupSizeZ)
wg.IDX = b.xid
wg.IDY = b.yid
wg.IDZ = b.zid
wg.CurrSizeX = xToAllocate
wg.CurrSizeY = yToAllocate
wg.CurrSizeZ = zToAllocate
b.xid++
xLeft -= xToAllocate
if xLeft <= 0 {
b.xid = 0
b.yid++
yLeft -= yToAllocate
if yLeft <= 0 {
b.yid = 0
b.zid++
}
}
if b.filter == nil {
break
}
if b.filter(b.packet, wg) {
break
}
}
b.spawnWorkItems(wg)
b.formWavefronts(wg)
return wg
}
func (b *gridBuilderImpl) spawnWorkItems(wg *WorkGroup) {
for z := 0; z < wg.CurrSizeZ; z++ {
for y := 0; y < wg.CurrSizeY; y++ {
for x := 0; x < wg.CurrSizeX; x++ {
wi := new(WorkItem)
wi.WG = wg
wi.IDX = x
wi.IDY = y
wi.IDZ = z
wg.WorkItems = append(wg.WorkItems, wi)
}
}
}
}
func (b *gridBuilderImpl) formWavefronts(wg *WorkGroup) {
var wf *Wavefront
wavefrontSize := 64
for i, wi := range wg.WorkItems {
wg := wi.WG
inWGID := wi.IDZ*wg.SizeX*wg.SizeY + wi.IDY*wg.SizeX + wi.IDX
if inWGID%wavefrontSize == 0 {
wf = NewWavefront()
wf.FirstWiFlatID = wg.WorkItems[i].FlattenedID()
wf.CodeObject = b.hsaco
wf.Packet = b.packet
wf.PacketAddress = b.packetAddr
wf.WG = wg
wg.Wavefronts = append(wg.Wavefronts, wf)
}
wf.WorkItems = append(wf.WorkItems, wi)
wf.InitExecMask |= 1 << uint32(inWGID%wavefrontSize)
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}