forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_math_cuda.go
258 lines (217 loc) · 6.33 KB
/
op_math_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
// +build cuda
package gorgonia
import (
"fmt"
"unsafe"
"github.com/chewxy/cu"
)
// module names
const (
elemBinOpMod = "elembinop"
elemUnaryOpMod = "elemunaryop"
)
func (op elemUnaryOp) CallsExtern() bool { return true }
func (op elemUnaryOp) CUDADo(extern External, dev Device, prealloc Value, inputs ...Value) (retVal Value, err error) {
if err = checkArity(op, len(inputs)); err != nil {
return
}
cudaLogf("CUDADoing %v | prealloc %v | %v", op, prealloc, inputs)
enterLoggingContext()
defer leaveLoggingContext()
// check
cudaLogf("checking if input is scalar")
a := inputs[0]
dt := a.Dtype()
// build name
name := fmt.Sprintf("%v.%v_f%d", elemUnaryOpMod, op.unaryOpType(), int(dt.Size())*8)
if !extern.HasFunc(name) {
cudaLogf("extern does not have func %q", name)
extern.Signal()
if retVal, err = op.do(a); err != nil {
return
}
if prealloc == nil {
return
}
return Copy(prealloc, retVal)
}
machine := extern.(CUDAMachine)
fn := machine.Functions()[name][int(dev)]
ctx := machine.Contexts()[int(dev)]
if prealloc == nil {
prealloc = a
}
var mem cu.DevicePtr
if prealloc.Uintptr() == a.Uintptr() && a.Shape().Eq(prealloc.Shape()) {
mem = cu.DevicePtr(a.Uintptr())
} else {
mem = cu.DevicePtr(prealloc.Uintptr())
memSize := int64(a.MemSize())
memA := cu.DevicePtr(a.Uintptr())
ctx.Memcpy(mem, memA, memSize)
}
size := logicalSize(a.Shape())
// blocks, threads := machine.(*tapeMachine).blockThread(int(size), int(dev))
gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ := machine.ElemGridSize(int(size), int(dev))
args := []unsafe.Pointer{
unsafe.Pointer(&mem),
unsafe.Pointer(&size),
}
cudaLogf("gx %d, gy %d, gz %d | bx %d by %d, bz %d", gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ)
cudaLogf("CUDADO %q, Mem: %v size %v, args %v", name, mem, size, args)
cudaLogf("LaunchKernel Params. mem: %v. Size %v", mem, size)
ctx.LaunchAndSync(fn, gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ, 0, cu.Stream(0), args)
// ctx.LaunchAndSync(fn, blocks, 1, 1, threads, 1, 1, 0, cu.Stream(0), args)
return prealloc, nil
}
func (op elemBinOp) CallsExtern() bool { return true }
func (op elemBinOp) CUDADo(extern External, dev Device, prealloc Value, inputs ...Value) (retVal Value, err error) {
if err = checkArity(op, len(inputs)); err != nil {
return
}
cudaLogf("CUDADoing %v", op)
enterLoggingContext()
defer leaveLoggingContext()
a := inputs[0]
b := inputs[1]
as := a.Shape()
bs := b.Shape()
var vv, vs, sv, ss bool
dt := a.Dtype()
opName := ʘBinOpNames[op.binOpType()]
var name string
switch {
case as.IsScalar() && bs.IsScalar():
ss = true
name = fmt.Sprintf("%v.%v_ss_f%d", elemBinOpMod, opName, int(dt.Size())*8)
case as.IsScalar() && !bs.IsScalar():
sv = true
name = fmt.Sprintf("%v.%v_sv_f%d", elemBinOpMod, opName, int(dt.Size())*8)
case !as.IsScalar() && bs.IsScalar():
vs = true
name = fmt.Sprintf("%v.%v_vs_f%d", elemBinOpMod, opName, int(dt.Size())*8)
case !as.IsScalar() && !bs.IsScalar():
vv = true
name = fmt.Sprintf("%v.%v_vv_f%d", elemBinOpMod, opName, int(dt.Size())*8)
}
machine := extern.(CUDAMachine)
ctx := machine.Contexts()[int(dev)]
var mem, memB cu.DevicePtr
var size int64
cudaLogf("a: 0x%x b 0x%x", a.Uintptr(), b.Uintptr())
cudaLogf("a %v, b%v", a, b)
switch {
case vv, vs, ss:
if prealloc == nil {
mem = cu.DevicePtr(a.Uintptr())
retVal = a
size = int64(logicalSize(a.Shape()))
} else {
mem = cu.DevicePtr(prealloc.Uintptr())
memA := cu.DevicePtr(a.Uintptr())
memSize := int64(a.MemSize())
ctx.Memcpy(mem, memA, memSize)
size = int64(logicalSize(prealloc.Shape()))
retVal = prealloc
}
memB = cu.DevicePtr(b.Uintptr())
cudaLogf("HERE")
case sv:
if prealloc == nil {
mem = cu.DevicePtr(b.Uintptr())
retVal = b
size = int64(logicalSize(b.Shape()))
memB = cu.DevicePtr(b.Uintptr())
} else {
mem = cu.DevicePtr(a.Uintptr())
preallocMem := cu.DevicePtr(prealloc.Uintptr())
B := cu.DevicePtr(b.Uintptr())
memSize := int64(b.MemSize())
ctx.Memcpy(preallocMem, B, memSize)
size = int64(logicalSize(prealloc.Shape()))
retVal = prealloc
memB = preallocMem
}
}
hasFn := extern.HasFunc(name)
if !hasFn {
cudaLogf("NoFn: %q", name)
extern.Signal()
cudaLogf("DONE. Prealloc \n%v", prealloc)
if prealloc != nil {
return op.UsePreallocDo(prealloc, inputs...)
}
if op.retSame {
return op.UsePreallocDo(retVal, inputs...)
}
cudaLogf("Using DO - Prealloc %v", retVal)
return op.Do(inputs...)
}
fn := machine.Functions()[name][int(dev)]
var args []unsafe.Pointer
cudaLogf("%v mem %v, memB %v", op, mem, memB)
gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ := machine.ElemGridSize(int(size), int(dev))
args = []unsafe.Pointer{
unsafe.Pointer(&mem),
unsafe.Pointer(&memB),
unsafe.Pointer(&size),
}
cudaLogf("CUDADO %q, size %v", name, size)
cudaLogf("LaunchKernel params. mem: %v memB: %v size: %v", mem, memB, size)
cudaLogf("%d, %d, %d, %d, %d, %d", gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ)
ctx.LaunchAndSync(fn, gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ, 0, cu.Stream(0), args)
return
}
// NewAddOp creates a new *ExternalOp that wraps a add op
func NewAddOp(a, b *Node, ctx ExecutionContext) *ExternalOp {
add := newElemBinOp(addOpType, a, b)
op := NewExternalOp(add, ctx, nil)
if a.Device() == CPU && b.Device() == CPU {
op.Device = CPU
return op
}
if a.Device() != CPU {
op.Device = a.Device()
return op
}
if b.Device() != CPU {
op.Device = b.Device()
return op
}
return op
}
// NewSubOp creates a new *ExternalOp that wraps a sub op
func NewSubOp(a, b *Node, ctx ExecutionContext) *ExternalOp {
sub := newEBOByType(subOpType, a.t, b.t)
op := NewExternalOp(sub, ctx, nil)
if a.Device() == CPU && b.Device() == CPU {
op.Device = CPU
return op
}
if a.Device() != CPU {
op.Device = a.Device()
return op
}
if b.Device() != CPU {
op.Device = b.Device()
return op
}
return op
}
func NewHadamardProdOp(a, b *Node, ctx ExecutionContext) *ExternalOp {
mul := newEBOByType(mulOpType, a.t, b.t)
op := NewExternalOp(mul, ctx, nil)
if a.Device() == CPU && b.Device() == CPU {
op.Device = CPU
return op
}
if a.Device() != CPU {
op.Device = a.Device()
return op
}
if b.Device() != CPU {
op.Device = b.Device()
return op
}
return op
}