/
external.go
303 lines (256 loc) · 7.69 KB
/
external.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
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
package cuda
import (
"runtime"
"github.com/pkg/errors"
"gorgonia.org/cu"
"gorgonia.org/cu/blas"
"gorgonia.org/cu/dnn"
)
// this file implements all the methods required to fulfil the External interface
var _ External = &Engine{}
const (
// Any address of a variable residing in global memory or returned by one of the
// memory allocation routines from the driver or runtime API is always aligned to at
// least 256 bytes.
//
memalign = 32
scalarAlign = 8
)
// HasFunc returns true if the execution is external (cgo/cuda/openCL) AND the external device contains the function with the given name
func (e *Engine) HasFunc(name string) bool { _, ok := e.f[name]; return ok }
// Sync returns a channel of sync signals
func (e *Engine) Sync() chan struct{} { return e.syncChan }
// Signal signals the machine to do work
func (e *Engine) Signal() {
e.workAvailable <- true
}
// Context returns the BatchedContext
func (e *Engine) Context() *cu.BatchedContext { return &e.c }
// CUDNNContext returns the cuDNN context
func (e *Engine) CUDNNContext() *cudnn.Context { return &e.n }
// BLASContext returns the cuBLAS context
func (e *Engine) BLASContext() *cublas.Standard { return &e.b }
// Modules returns the loaded modules indexed by name
func (e *Engine) Modules() map[string]cu.Module { return e.m }
// Functions returns the loaded functions indexed by name
func (e *Engine) Functions() map[string]cu.Function { return e.f }
// ElemGridSize calculates the gridsize for elementwise operations. n is the number of elements
func (e *Engine) ElemGridSize(n int) (gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ int) {
maxThreads := e.mtpb
maxGridX := e.mgdx
maxGridY := e.mgdy
maxGridZ := e.mgdz
blockDimX = 1
blockDimY = 1
blockDimZ = 1
gridDimX = 1
gridDimY = 1
gridDimZ = 1
blocks := calcBlocks(n, maxThreads)
switch {
case blocks == 1:
blockDimX = n
case blocks >= maxGridX*maxGridY*maxGridZ:
// what kind of monstrosity is this??!
case blocks >= maxGridX*maxGridY:
gridDimX = maxGridX
gridDimY = maxGridY
gridDimZ = calcBlocks(blocks%(maxGridX*maxGridY), maxGridZ)
blockDimX = maxThreads
case blocks >= maxGridX:
gridDimX = maxGridX
gridDimY = calcBlocks(blocks%(maxGridX), maxGridY)
blockDimX = maxThreads
default:
gridDimX = blocks
blockDimX = maxThreads
}
return
}
// Init creates a CUDA engine with the given size for the given device
func (e *Engine) Init(device cu.Device, size int64) (err error) {
e.Lock()
initialized := e.initialized
e.Unlock()
if initialized {
return nil
}
e.Lock()
e.d = device
if err = e.doInit(size); err != nil {
e.Unlock()
err2 := e.Close()
if err2 != nil {
return errors.Wrapf(err, "Failed to initialize CUDA Engine with size %d for device %v. Additionally, there were errors that occurred when cleaning up %v", size, device, err)
}
return errors.Wrapf(err, "Failed to initialize CUDA Engine with size %d for device %v", size, device)
}
e.initialized = true
e.Unlock()
return
}
func (e *Engine) doInit(size int64) (err error) {
e.workAvailable = make(chan bool)
e.syncChan = make(chan struct{})
e.finishChan = make(chan struct{})
e.finishChan2 = make(chan struct{}, 1)
e.a = makeBFC(memalign)
// create and set context
var cuctx cu.CUContext
ctxFlag := cu.SchedAuto
if cuctx, err = e.d.MakeContext(ctxFlag); err != nil {
if err == cu.OutOfMemory {
free, total, err2 := cu.MemInfo()
if err2 != nil {
return errors.Wrapf(err, "Out of memory. Additionally errors were found while retrieving mem info %v", err2)
}
return errors.Wrapf(err, "Out of memory. Free: %v, total %v | %v", free, total, cuctx)
}
return errors.Wrapf(err, "Failed to make context for device %d", e.d)
}
e.c = *(cu.NewBatchedContext(cu.CtxFromCUContext(e.d, cuctx, ctxFlag), e.d))
var attrs []int
if attrs, err = e.d.Attributes(cu.WarpSize, cu.MaxThreadsPerBlock, cu.MaxGridDimX, cu.MaxGridDimY, cu.MaxGridDimZ, cu.MaxBlockDimX, cu.MaxBlockDimY, cu.MaxBlockDimZ); err != nil {
return errors.Wrapf(err, "Failed to get attributes for device %v.", e.d)
}
e.warp = attrs[0]
e.mtpb = attrs[1]
e.mgdx = attrs[2]
e.mgdy = attrs[3]
e.mgdz = attrs[4]
e.mbdx = attrs[5]
e.mbdy = attrs[6]
e.mbdz = attrs[7]
e.m = make(map[string]cu.Module)
e.f = make(map[string]cu.Function)
// actual work to allocate from graphics card
if e.freeMem, e.totalMem, err = cu.MemInfo(); err != nil {
return errors.Wrapf(err, "Failed to get free and total mem for device %v", e.d)
}
// actually reserve memory for the allocator
var allocsize int64 = 2*size + (size / 2) + minAllocSize
if allocsize >= e.freeMem {
return errors.Errorf("Unable to get %v bytes. Free memory available %v", allocsize, e.freeMem)
}
ptr, err := cu.MemAllocManaged(allocsize, cu.AttachGlobal)
if err != nil {
return errors.Wrapf(err, "Failed to allocate %v bytes of managed memory for %v", allocsize, e.d)
}
e.a.reserve(uintptr(ptr), allocsize)
e.n = *(cudnn.NewContext())
go e.Run()
return nil
}
// Close cleans up the machine, and closes all available resources
func (e *Engine) Close() error {
e.Lock()
defer e.Unlock()
e.c.Cleanup() // frees all ancillary allocations in C land
if e.c.Context == nil {
return nil
}
cu.SetCurrentContext(e.c.Context.CUDAContext())
// Unload all modules (and consequently all functions)
for name, mod := range e.m {
if err := mod.Unload(); err != nil {
return errors.Wrapf(err, "Failed to unload module %v", name)
}
}
// Free all CUDA memory
if e.a.start != 0 {
cu.MemFree(cu.DevicePtr(e.a.start))
}
e.a.reset()
closeB := func() error { return e.b.Close() }
if err := e.c.Do(closeB); err != nil {
return errors.Wrap(e.err, "Failed to close cuBLAS context")
}
closeN := func() error { return e.n.Close() }
if err := e.c.Do(closeN); err != nil {
return errors.Wrap(e.err, "Failed to close cuDNN context")
}
if e.workAvailable != nil {
close(e.workAvailable)
}
if err := e.c.Close(); err != nil {
return errors.Wrapf(err, "Failed to cloes CUDA Context ")
}
runtime.Gosched() // make sure everyone has a fair play
e.finishChan <- struct{}{}
e.finishChan2 <- struct{}{} // wait
e.initialized = false
return nil
}
// DoWork sends a signal to the batched CUDA Context to actually do work
func (e *Engine) DoWork() error {
e.c.DoWork()
return e.c.Errors()
}
// Run initialises and starts the engine
func (e *Engine) Run() {
e.Lock()
if e.running {
e.Unlock()
return
}
e.Unlock()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// finish initialization
e.b.Init(cublas.WithContext(&e.c))
// finishChan2 blocks any external commands to engine (like Close) until it's ready to finish.
e.finishChan2 <- struct{}{}
loop:
for {
select {
case <-e.c.WorkAvailable():
e.c.DoWork()
if err := e.c.Errors(); err != nil {
e.Lock()
e.err = err
e.running = false
e.Unlock()
break loop
}
case w := <-e.c.Work():
if w != nil {
err := w()
e.c.ErrChan() <- err
if err != nil {
e.Lock()
e.err = err
e.running = false
e.Unlock()
break loop
}
}
case <-e.finishChan:
break loop
}
}
<-e.finishChan2
}
// blockThread is an easier version of calculating <<threads, blocks>> for CUDA. Useful for debugging
func (e *Engine) blockThread(n, dev int) (blocks, threads int) {
switch {
case n <= 32:
threads = 32
case n <= 64:
threads = 64
case n <= 128:
threads = 128
case n <= 256:
threads = 256
case n <= 512:
threads = 512
default:
threads = 1024
}
blocks = (n + threads - 1) / threads
if blocks < 0 || blocks > 128 {
blocks = 128
}
return
}
// it's just a generic ceiling function. Added here to avoid mixing with any potential ceilInt operation
func calcBlocks(n, maxThreads int) int { return (n + maxThreads - 1) / maxThreads }