-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
200 lines (163 loc) · 3.83 KB
/
main.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
package main
import (
"fmt"
"strings"
"github.com/MasterOfBinary/go-opencl/opencl"
)
const (
deviceType = opencl.DeviceTypeAll
dataSize = 128
programCode = `
kernel void kern(global float* out, global float* a, global float* b)
{
size_t i = get_global_id(0);
out[i] = a[i] + b[i];
}
`
)
func printHeader(name string) {
fmt.Println(strings.ToUpper(name))
for _ = range name {
fmt.Print("=")
}
fmt.Println()
}
func printInfo(platform opencl.Platform, device opencl.Device) {
var platformName string
err := platform.GetInfo(opencl.PlatformName, &platformName)
if err != nil {
panic(err)
}
var vendor string
err = device.GetInfo(opencl.DeviceVendor, &vendor)
if err != nil {
panic(err)
}
fmt.Println()
printHeader("Using")
fmt.Println("Platform:", platformName)
fmt.Println("Vendor: ", vendor)
}
func main() {
platforms, err := opencl.GetPlatforms()
if err != nil {
panic(err)
}
printHeader("Platforms")
foundDevice := false
var platform opencl.Platform
var device opencl.Device
var name string
for _, curPlatform := range platforms {
err = curPlatform.GetInfo(opencl.PlatformName, &name)
if err != nil {
panic(err)
}
var devices []opencl.Device
devices, err = curPlatform.GetDevices(deviceType)
if err != nil {
panic(err)
}
// Use the first available device
if len(devices) > 0 && !foundDevice {
var available bool
err = devices[0].GetInfo(opencl.DeviceAvailable, &available)
if err == nil && available {
platform = curPlatform
device = devices[0]
foundDevice = true
}
}
version := curPlatform.GetVersion()
fmt.Printf("Name: %v, devices: %v, version: %v\n", name, len(devices), version)
}
if !foundDevice {
panic("No device found")
}
printInfo(platform, device)
var context opencl.Context
context, err = device.CreateContext()
if err != nil {
panic(err)
}
defer context.Release()
var commandQueue opencl.CommandQueue
commandQueue, err = context.CreateCommandQueue(device)
if err != nil {
panic(err)
}
defer commandQueue.Release()
var program opencl.Program
program, err = context.CreateProgramWithSource(programCode)
if err != nil {
panic(err)
}
defer program.Release()
var log string
err = program.Build(device, &log)
if err != nil {
fmt.Println(log)
panic(err)
}
kernel, err := program.CreateKernel("kern")
if err != nil {
panic(err)
}
defer kernel.Release()
buffer, err := context.CreateBuffer([]opencl.MemFlags{opencl.MemWriteOnly}, dataSize*4)
if err != nil {
panic(err)
}
defer buffer.Release()
buffer1, err := context.CreateBuffer([]opencl.MemFlags{opencl.MemReadOnly}, dataSize*4)
if err != nil {
panic(err)
}
defer buffer1.Release()
buffer2, err := context.CreateBuffer([]opencl.MemFlags{opencl.MemReadOnly}, dataSize*4)
if err != nil {
panic(err)
}
defer buffer2.Release()
err = kernel.SetArg(0, buffer.Size(), &buffer)
if err != nil {
panic(err)
}
err = kernel.SetArg(1, buffer1.Size(), &buffer1)
if err != nil {
panic(err)
}
err = kernel.SetArg(2, buffer2.Size(), &buffer2)
if err != nil {
panic(err)
}
write_data := make([]float32, dataSize)
for i := 0; i < dataSize; i++ {
write_data[i] = float32(i)
}
err = commandQueue.EnqueueWriteBuffer(buffer1, true, write_data)
if err != nil {
panic(err)
}
err = commandQueue.EnqueueWriteBuffer(buffer2, true, write_data)
if err != nil {
panic(err)
}
err = commandQueue.EnqueueNDRangeKernel(kernel, 1, []uint64{dataSize})
if err != nil {
panic(err)
}
commandQueue.Flush()
commandQueue.Finish()
data := make([]float32, dataSize)
err = commandQueue.EnqueueReadBuffer(buffer, true, data)
if err != nil {
panic(err)
}
fmt.Println()
printHeader("Output")
for _, item := range data {
fmt.Printf("%v ", item)
}
fmt.Println()
}