/
cuda.go
359 lines (303 loc) · 9.17 KB
/
cuda.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
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
// +build cuda
package gorgonia
// for non-cuda builds, look at noextern.go
import (
"log"
"sync"
"github.com/pkg/errors"
"gorgonia.org/cu"
cudnn "gorgonia.org/cu/dnn"
"gorgonia.org/gorgonia/cuda"
"gorgonia.org/tensor"
)
// CUDA tells the package that CUDA is used
const CUDA = true
var (
_ External = &ExternMetadata{}
_ CUDAMachine = &tapeMachine{}
_ CUDAMachine = &lispMachine{}
)
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
)
//go:generate cudagen -same-module
var cudaStdLib []cudaLib
type cudaLib struct {
name string
data string
funcs []string
}
// CUDAMachine is a representation of CUDA capable VMs.
type CUDAMachine interface {
External
Engines() []cuda.Engine
Contexts() []*cu.BatchedContext
CUDNNContexts() []*cudnn.Context
ElemGridSize(n, dev int) (gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ int)
}
// ExternMetadata holds any metadata for CUDA related stuff.
// The slices in there are indexed by deviceID
type ExternMetadata struct {
tensor.Engine
sync.Mutex
// operational stuff
u cu.Device // device currently in use
b batchedBLAS // UNUSED
engines []cuda.Engine
workAvailable chan bool
syncChan chan struct{}
initialized bool
}
// ElemGridSize calculates the gridsize for elementwise operations
func (m *ExternMetadata) ElemGridSize(n, dev int) (gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ int) {
if dev >= len(m.engines) {
// error
}
return m.engines[dev].ElemGridSize(n)
}
// WorkAvailable returns a channel of empty struct, which is used to signal to the VM when there is work available. The VM will then call the DoWork method
func (m *ExternMetadata) WorkAvailable() <-chan bool { return m.workAvailable }
// Sync the channels
func (m *ExternMetadata) Sync() chan struct{} { return m.syncChan }
// DoWork flushes any batched cgo calls. In this build it flushes any batched CUDA calls and any batched CBLAS calls.
func (m *ExternMetadata) DoWork() error {
for _, e := range m.engines {
if err := e.DoWork(); err != nil {
return err
}
}
return nil
}
// Engines ...
func (m *ExternMetadata) Engines() []cuda.Engine { return m.engines }
// Contexts return a slice of contexts that is being used by this CUDAMachine
func (m *ExternMetadata) Contexts() []*cu.BatchedContext {
retVal := make([]*cu.BatchedContext, 0, len(m.engines))
for _, e := range m.engines {
retVal = append(retVal, e.Context())
}
return retVal
}
// CUDNNContexts returns the CUDNN context
func (m *ExternMetadata) CUDNNContexts() []*cudnn.Context {
retVal := make([]*cudnn.Context, 0, len(m.engines))
for _, e := range m.engines {
retVal = append(retVal, e.CUDNNContext())
}
return retVal
}
// Get gets a previously allocated memory slab of the provided size. If no memories of that size exist,
// it returns a NoOpError. The caller is then responsible for allocating the memory themselves.
func (m *ExternMetadata) Get(dev Device, size int64) (tensor.Memory, error) {
d := int(dev)
if d >= len(m.engines) {
return nil, noopError{} // this should not be a noopError
}
return m.engines[dev].Get(size)
}
// GetFromValue allocates a memory on the GPU, and then copies the data over. v MUST be on CPU.
func (m *ExternMetadata) GetFromValue(dev Device, v Value) (tensor.Memory, error) {
d := int(dev)
if d >= len(m.engines) {
return nil, noopError{}
}
memsize := calcMemSize(v.Dtype(), v.Shape())
mem, err := m.engines[dev].Get(memsize)
if err != nil {
return nil, err
}
ptr := cu.DevicePtr(mem.Uintptr())
ctx := m.engines[dev].Context()
ctx.MemcpyHtoD(ptr, valueToPointer(v), memsize)
return cu.DevicePtr(ptr), nil
}
// Put puts a previously allocated memory slab of the provided size back into the pool
func (m *ExternMetadata) Put(dev Device, mem tensor.Memory, size int64) {
d := int(dev)
if d >= len(m.engines) {
return // wat??
}
m.engines[dev].Put(mem, size)
}
// PutValue puts a previously allocated memory slab back into the pool
func (m *ExternMetadata) PutValue(dev Device, v Value) {
d := int(dev)
if d >= len(m.engines) {
return
}
memsize := calcMemSize(v.Dtype(), v.Shape())
m.engines[dev].Put(v, memsize)
}
// Transfer transfers data from device to device.
func (m *ExternMetadata) Transfer(toDev, fromDev Device, v Value, synchronous bool) (retVal Value, err error) {
defer func() {
if synchronous {
m.Signal()
}
}()
memsize := calcMemSize(v.Dtype(), v.Shape())
switch {
case fromDev == CPU && toDev != CPU:
d := int(toDev)
if d > len(m.engines) {
return nil, errors.Errorf("No context for ToDev")
}
ctx := m.engines[d].Context()
var mem tensor.Memory
if mem, err = m.Get(toDev, memsize); err != nil {
return
}
ctx.MemcpyHtoD(cu.DevicePtr(mem.Uintptr()), valueToPointer(v), memsize)
return makeValueFromMem(TypeOf(v), v.Shape(), mem)
case fromDev != CPU && toDev == CPU:
d := int(fromDev)
if d > len(m.engines) {
return nil, errors.Errorf("No context for FromDev")
}
ctx := m.engines[d].Context()
if retVal, err = makeValue(TypeOf(v), v.Shape()); err != nil {
return
}
ctx.MemcpyDtoH(valueToPointer(retVal), cu.DevicePtr(v.Uintptr()), memsize)
return
case fromDev == toDev:
return v, nil
case fromDev != toDev && fromDev != CPU && toDev != CPU:
}
panic("Unreachable")
}
// Signal sends a signal down the workavailable channel, telling the VM to call the DoWork method. Signal is a synchronous method
func (m *ExternMetadata) Signal() {
if m.workAvailable != nil {
m.signal()
<-m.syncChan
}
}
// Reset frees all the memories, and coalesces the allocator
func (m *ExternMetadata) Reset() {
for i := range m.engines {
m.engines[i].ResetAllocator()
}
}
func (m *ExternMetadata) init(sizes []int64) (err error) {
m.Lock()
initialized := m.initialized
m.Unlock()
if initialized {
return nil
}
devices, err := cu.NumDevices()
if err != nil {
return errors.Wrapf(err, "Failed to get number of devices")
}
if devices == 0 {
return errors.New("No Devices Found")
}
cudaLogf("Creating Engines")
m.Lock()
defer m.Unlock()
m.engines = make([]cuda.Engine, len(sizes))
for i := range m.engines {
e := &m.engines[i]
dev, err := cu.GetDevice(i)
if err != nil {
return errors.Wrapf(err, "Failed to get device %d", i)
}
if err = e.Init(dev, sizes[i]); err != nil {
return err
}
ctx := e.Context()
go m.collectWork(i, ctx.WorkAvailable())
}
m.initialized = true
cudaLogf("CUDA initialized. Engines: %v", m.engines)
return nil
}
func (m *ExternMetadata) initFail() {
cudaLogf("Cleanup")
m.engines = nil
if m.workAvailable != nil {
close(m.workAvailable)
}
m.workAvailable = nil
}
// cleanup cleans up the ancillary allocations made during the calling of batched CUDA functions.
func (m *ExternMetadata) cleanup() {
for _, e := range m.engines {
e.Close()
}
}
// collectWork is a muxer for all the channels for the different devices
func (m *ExternMetadata) collectWork(devID int, workAvailable <-chan struct{}) {
for range workAvailable {
m.workAvailable <- false
}
}
// collectBLASWork is a muxer for CBLAS/CuBLAS (if any) and the devices
func (m *ExternMetadata) collectBLASWork() {}
func (m *ExternMetadata) signal() { m.workAvailable <- true }
func (m *ExternMetadata) setEngine(e tensor.Engine) {}
// AddToStdLib allows for custom ops to be included into the "stdlib" of CUDA functions, so that when the VMs are created, they're loaded automatically
// without having to specify extra loading.
func AddToStdLib(name, data string, funcs []string) {
cudaStdLib = append(cudaStdLib, cudaLib{
name: name,
data: data,
funcs: funcs,
})
}
func init() {
log.Println("Using CUDA build")
}
// ValueOnDevice gets the value of the node as a Value but on the desired device. If the node's valud is not on the same device
// as the desired device, a copy will be made.
func (n *Node) ValueOnDevice(toDev Device, extern External) (retVal Value, allocOnExtern bool, err error) {
if n.dataOn == toDev {
return n.Value(), false, nil
}
v := n.Value()
fromDev := n.Device()
var synchronous bool
if toDev == CPU {
synchronous = true
}
if toDev != fromDev && toDev != CPU {
allocOnExtern = true
}
retVal, err = extern.Transfer(toDev, fromDev, v, synchronous)
return
}
// GradOnDevice gets the gradient value of the node as a Value but on the desired device. If the node's valud is not on the same device
// as the desired device, a copy will be made.
func (n *Node) GradOnDevice(toDev Device, extern External) (retVal Value, allocOnExtern bool, err error) {
if n.dataOn == toDev {
retVal, err = n.Grad()
return
}
var d Value
if dv, ok := n.boundTo.(*dualValue); ok {
d = dv.d
} else if n.deriv != nil {
return n.deriv.ValueOnDevice(toDev, extern)
} else {
return nil, false, errors.Errorf("No gradient node/value found for %v", n)
}
if d == nil {
return nil, false, errors.Errorf("No gradient node/value found for %v", n)
}
fromDev := n.Device()
var synchronous bool
if toDev == CPU {
synchronous = true
}
if toDev != CPU && toDev != fromDev {
allocOnExtern = true
}
retVal, err = extern.Transfer(toDev, fromDev, d, synchronous)
return
}