forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
115 lines (90 loc) · 2.89 KB
/
example_test.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
package gocudnn_test
import (
"fmt"
"runtime"
"github.com/dereklstinson/GoCudnn/gocu"
"github.com/dereklstinson/GoCudnn/cudart"
gocudnn "github.com/dereklstinson/GoCudnn"
)
//ExampleActivationD of doing the activation function
func ExampleActivationD() {
runtime.LockOSThread()
check := func(e error) {
if e != nil {
panic(e)
}
}
h := gocudnn.CreateHandle(true) //Using go garbage collector
ActOp, err := gocudnn.CreateActivationDescriptor()
check(err)
var AMode gocudnn.ActivationMode //Activation Mode Flag
var NanMode gocudnn.NANProp //Nan Propagation Flag
err = ActOp.Set(AMode.Relu(), NanMode.Propigate(), 20)
check(err)
am, nm, coef, err := ActOp.Get() //Gets the calues that where set
if am != AMode.Relu() || nm != NanMode.Propigate() || coef != 20 {
panic("am!=Amode.Relu()||nm !=NanMode.Propigate()||coef!=20")
}
//Dummy Variables
//Check TensorD to find out how to make xD,yD and x and y
var x, y *gocu.CudaPtr
var xD, yD *gocudnn.TensorD
err = ActOp.Forward(h, 1, xD, x, 0, yD, y)
check(err)
}
//ExampleTensorD shows tomake a tensor
func ExampleTensorD() {
//Need to lock os thread.
runtime.LockOSThread()
check := func(e error) {
if e != nil {
panic(e)
}
}
//Creating a blocking stream
cs, err := cudart.CreateBlockingStream()
check(err)
//Create Device
dev, err := cudart.CreateDevice(1)
check(err)
//Make an Allocator
CudaMemManager, err := cudart.CreateMemManager(cs, dev) //cs could be nil . Check out cudart package on more about streams
check(err)
//Tensor
var tflg gocudnn.TensorFormat //Flag for tensor
var dtflg gocudnn.DataType //Flag for tensor
xD, err := gocudnn.CreateTensorDescriptor()
// Setting Tensor
err = xD.Set(tflg.NCHW(), dtflg.Float(), []int32{20, 1, 1, 1}, nil)
check(err)
//Gets SIB for tensor memory on device
xSIB, err := xD.GetSizeInBytes()
check(err)
//Allocating memory to device and returning pointer to device memory
x, err := CudaMemManager.Malloc(xSIB)
//Create some host mem to copy to cuda memory
hostmem := make([]float32, xSIB/4)
//You can fill it
for i := range hostmem {
hostmem[i] = float32(i)
}
//Convert the slice to GoMem
hostptr, err := gocu.MakeGoMem(hostmem)
//Copy hostmem to x
CudaMemManager.Copy(x, hostptr, xSIB) // This allocotor syncs the cuda stream after every copy.
// You can make your own custom one. This was a default one
// to help others get going. Some "extra" functions beyond the api
// require an allocator.
//if not using an allocator sync the stream before changing the host mem right after a mem copy. It could cause problems.
err = cs.Sync()
check(err)
//Zero out the golang host mem.
for i := range hostmem {
hostmem[i] = float32(0)
}
//do some tensor stuff can return vals to host mem by doing another copy
err = CudaMemManager.Copy(hostptr, x, xSIB)
check(err)
fmt.Println(hostmem)
//Output: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
}