-
Notifications
You must be signed in to change notification settings - Fork 2
/
ocl.go
192 lines (151 loc) · 4.21 KB
/
ocl.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
package ocl
/*
#include <stdlib.h>
#include <CL/cl.h>
static char**makeCharArray(int size) {
return calloc(sizeof(char*), size);
}
static void setArrayString(char **a, char *s, int n) {
a[n] = s;
}
static void freeCharArray(char **a, int size) {
int i;
for (i = 0; i < size; i++)
free(a[i]);
free(a);
}
static cl_context_properties platform2property(cl_platform_id pid) {
return (cl_context_properties)pid;
}
*/
import "C"
import "unsafe"
import "fmt"
import "runtime"
const (
CL_CONTEXT_PLATFORM = C.CL_CONTEXT_PLATFORM
)
//constants for clGetPlatformInfo function
const (
CL_PLATFORM_PROFILE = C.CL_PLATFORM_PROFILE
CL_PLATFORM_VERSION = C.CL_PLATFORM_VERSION
CL_PLATFORM_NAME = C.CL_PLATFORM_NAME
CL_PLATFORM_VENDOR = C.CL_PLATFORM_VENDOR
CL_PLATFORM_EXTENSIONS = C.CL_PLATFORM_EXTENSIONS
)
type PlatformInfo C.cl_platform_info
const (
CL_DEVICE_TYPE_DEFAULT = C.CL_DEVICE_TYPE_DEFAULT
CL_DEVICE_TYPE_CPU = C.CL_DEVICE_TYPE_CPU
CL_DEVICE_TYPE_GPU = C.CL_DEVICE_TYPE_GPU
CL_DEVICE_TYPE_ACCELERATOR = C.CL_DEVICE_TYPE_ACCELERATOR
CL_DEVICE_TYPE_ALL = C.CL_DEVICE_TYPE_ALL
)
type DeviceType C.cl_device_type
func PlatformsNumber() uint {
var numPlatforms C.cl_uint
C.clGetPlatformIDs(0, nil, &numPlatforms)
var res uint = uint(numPlatforms)
return res
}
type aPlatform C.cl_platform_id
type Platform struct {
item aPlatform
}
type aDevice C.cl_device_id
type Device struct {
item aDevice
}
type aCommandQueue C.cl_command_queue
type CommandQueue struct {
item aCommandQueue
}
type Event C.cl_event
//type ContextProperty C.cl_context_property
func Platforms(num uint) []Platform {
if num == 0 { num = PlatformsNumber() }
platforms := make([]aPlatform, num)
var realNum C.cl_uint = 0
C.clGetPlatformIDs(C.cl_uint(num), (*C.cl_platform_id)(&platforms[0]), &realNum)
res := make([]Platform, realNum)
var i C.cl_uint
for i = 0; i < realNum; i++ {
res[i].item = platforms[i]
}
return res
}
func (pl *Platform) Info(pinfo PlatformInfo) string {
const bufSize = 4096
var bufReal C.size_t = 0
var cStr unsafe.Pointer = unsafe.Pointer(C.malloc(bufSize))
defer C.free(cStr)
C.clGetPlatformInfo(pl.item, C.cl_platform_info(pinfo), bufSize, cStr, &bufReal)
res := C.GoString((*C.char)(cStr))
return res
}
func (pl* Platform) Devices(devType DeviceType, num uint) []Device {
devices := make([]aDevice, num)
var realNum C.cl_uint = 0
error := C.clGetDeviceIDs(pl.item, C.cl_device_type(devType), C.cl_uint(num), (*C.cl_device_id)(&devices[0]), &realNum)
res := make([]Device, realNum)
fmt.Printf("xxx %d\n", int(error))
var i C.cl_uint
for i = 0; i < realNum; i++ {
res[i].item = devices[i]
}
return res
}
func (cq *CommandQueue) Finish() {
fmt.Printf("Supress.\n")
C.clFinish(cq.item);
}
type Context struct {
item C.cl_context
}
func CreateContext(platform *Platform, devType DeviceType) *Context {
var ctx Context
var error C.cl_int
properties := []C.cl_context_properties{CL_CONTEXT_PLATFORM, C.platform2property(platform.item), C.cl_context_properties(0)}
ctx.item = C.clCreateContextFromType(&properties[0], C.cl_device_type(devType), nil, nil, &error)
fmt.Printf("Context error:%d\n", error)
return &ctx
}
type Program struct {
item C.cl_program
}
func CreateProgram(context *Context, sources []string) *Program {
runtime.LockOSThread()
var program Program
var error C.cl_int
csources := C.makeCharArray(C.int(len(sources)))
clenghts := make([]C.size_t, len(sources))
defer C.freeCharArray(csources, C.int(len(sources)))
for i := 0; i < len(sources); i++ {
C.setArrayString(csources, C.CString(sources[i]), C.int(i))
clenghts[i] = C.size_t(len(sources[i]))
fmt.Printf("Program log:%d %s\n",clenghts[i], sources[i])
}
program.item = C.clCreateProgramWithSource(
context.item,
C.cl_uint(len(sources)),
csources,
&clenghts[0],
&error)
fmt.Printf("Program error:%d\n", error)
return &program
}
type Kernel struct {
item C.cl_kernel
}
func CreateKernel(program *Program, name string) *Kernel {
var kernel Kernel
var error C.cl_int
kernel.item = C.clCreateKernel(program.item, C.CString(name), &error)
fmt.Printf("Kernel error:%d\n", error)
return &kernel
}
func (*Kernel) Foo() {
}
type Buffer struct {
item C.cl_mem
}