-
Notifications
You must be signed in to change notification settings - Fork 5
/
parallel_test.go
110 lines (101 loc) · 3.14 KB
/
parallel_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
package gocudnn_test
import (
"sync"
"testing"
gocudnn "github.com/dereklstinson/gocudnn"
"github.com/dereklstinson/gocudnn/cudart"
"github.com/dereklstinson/gocudnn/gocu"
"github.com/dereklstinson/cutil"
)
func TestParrallel(t *testing.T) {
check := func(e error) {
if e != nil {
t.Error(e)
}
}
ndevs, err := cudart.GetDeviceCount()
check(err)
handles := make([]*gocudnn.Handle, ndevs)
workers := make([]*gocu.Worker, ndevs)
MemManager := make([]*cudart.MemManager, ndevs)
var wg sync.WaitGroup
for i := range handles {
wg.Add(1)
go func(i int, err error) {
workers[i] = gocu.NewWorker(cudart.CreateDevice(int32(i)))
handles[i] = gocudnn.CreateHandleEX(workers[i], true)
MemManager[i], err = cudart.CreateMemManager(workers[i])
check(err)
wg.Done()
}(i, err)
}
wg.Wait()
var cmode gocudnn.ConvolutionMode //Creating Flags
var frmt gocudnn.TensorFormat //Creating Flags
var dtype gocudnn.DataType //Creating Flags
cmode.CrossCorrelation() //Setting Flags
frmt.NCHW() //Setting Flags
dtype.Float() //Setting Flags
inputdims := []int32{12, 3, 32, 32}
filterdims := []int32{256, 3, 5, 5} //Assumming this is an input image
biasdims := []int32{1, 256, 1, 1}
inputD, err := gocudnn.CreateTensorDescriptor()
check(err)
filterD, err := gocudnn.CreateFilterDescriptor()
check(err)
biasD, err := gocudnn.CreateTensorDescriptor()
check(err)
outputD, err := gocudnn.CreateTensorDescriptor()
check(inputD.Set(frmt, dtype, inputdims, nil))
check(filterD.Set(dtype, frmt, filterdims))
check(biasD.Set(frmt, dtype, biasdims, nil))
insib, err := inputD.GetSizeInBytes()
check(err)
fsib, err := filterD.GetSizeInBytes()
check(err)
bsib, err := biasD.GetSizeInBytes()
check(err)
convD, err := gocudnn.CreateConvolutionDescriptor()
check(err)
check(convD.Set(cmode, dtype, []int32{2, 2}, []int32{1, 1}, []int32{1, 1}))
outdims, err := convD.GetOutputDims(inputD, filterD)
check(err)
check(outputD.Set(frmt, dtype, outdims, nil))
outsib, err := outputD.GetSizeInBytes()
check(err)
inputMEM := make([]cutil.Mem, len(MemManager))
filterMEM := make([]cutil.Mem, len(MemManager))
biasMEM := make([]cutil.Mem, len(MemManager))
outputMEM := make([]cutil.Mem, len(MemManager))
for i, m := range MemManager {
wg.Add(1)
go func(i int, m *cudart.MemManager, err error) {
inputMEM[i], err = m.Malloc(insib)
check(err)
filterMEM[i], err = m.Malloc(fsib)
check(err)
biasMEM[i], err = m.Malloc(bsib)
check(err)
outputMEM[i], err = m.Malloc(outsib)
check(err)
wg.Done()
}(i, m, err)
}
wg.Wait()
var algopref gocudnn.ConvolutionForwardPref
algopref.PreferFastest()
for i, h := range handles {
wg.Add(1)
go func(i int, h *gocudnn.Handle, err error) {
algo, err := convD.GetForwardAlgorithm(h, inputD, filterD, outputD, algopref, 0)
check(err)
wspacesib, err := convD.GetForwardWorkspaceSize(h, inputD, filterD, outputD, algo)
check(err)
wspacemem, err := MemManager[i].Malloc(wspacesib)
check(err)
check(convD.Forward(h, 1, inputD, inputMEM[i], filterD, filterMEM[i], algo, wspacemem, wspacesib, 0, outputD, outputMEM[i]))
wg.Done()
}(i, h, err)
}
wg.Wait()
}