-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext.go
98 lines (83 loc) · 2.62 KB
/
context.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
package opencl
/*
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
#include <stdlib.h>
extern void contextCallback(char *, void *, size_t, void *);
*/
import "C"
import (
"fmt"
"unsafe"
)
type Context struct {
context *C.cl_context
callback func(string, unsafe.Pointer, uintptr, interface{})
userdata interface{}
devices []*Device
}
type ContextProperties struct {
}
//export contextCallback
func contextCallback(errinfo *C.char, private_info unsafe.Pointer, cb C.size_t, user_data unsafe.Pointer) {
context := *(*Context)(user_data)
context.callback(C.GoString(errinfo), private_info, uintptr(cb), context.userdata)
}
func CreateContext(properties *ContextProperties, devices []*Device, notify func(string, unsafe.Pointer, uintptr, interface{}), userdata interface{}) (*Context, error) {
context := Context{nil, notify, userdata, devices}
context.context = (*C.cl_context)(C.malloc(C.sizeof_cl_context))
ids := asCLDeviceIDs(devices)
var callback *[0]byte
var user unsafe.Pointer
if notify != nil {
callback = (*[0]byte)(unsafe.Pointer(C.contextCallback))
user = unsafe.Pointer(&context)
}
var err C.cl_int
*context.context = C.clCreateContext(nil, C.cl_uint(len(ids)), &ids[0], callback, user, &err)
if err != C.CL_SUCCESS {
C.free(unsafe.Pointer(context.context))
return nil, fmt.Errorf("Error creating context: %d", err)
}
return &context, nil
}
func (c Context) Release() error {
err := C.clReleaseContext(*c.context)
if err != C.CL_SUCCESS {
return fmt.Errorf("Error releasing context: %d", err)
}
C.free(unsafe.Pointer(c.context))
return nil
}
func (c Context) Devices() []*Device {
return c.devices
}
func (c Context) CreateCommandQueue(device Device, properties *CommandQueueProperties) (*CommandQueue, error) {
return createCommandQueue(c, device, properties)
}
func (c Context) CreateBuffer(flags MemoryFlags, size uintptr, hostPtr unsafe.Pointer) (*Memory, error) {
return createBuffer(c, flags, size, hostPtr)
}
func (c Context) CreateProgramWithSource(source []string) (*Program, error) {
clSource := make([]*C.char, len(source))
for i, s := range source {
clSource[i] = C.CString(s)
defer C.free(unsafe.Pointer(clSource[i]))
}
var clSourcePtr **C.char
if len(source) > 0 {
clSourcePtr = &clSource[0]
}
var err C.cl_int
var program Program
program.program = (*C.cl_program)(C.malloc(C.sizeof_cl_program))
*program.program = C.clCreateProgramWithSource(*c.context, C.cl_uint(len(source)), clSourcePtr, nil, &err)
if err != C.CL_SUCCESS {
C.free(unsafe.Pointer(program.program))
return nil, fmt.Errorf("failed to create program: %d", err)
}
return &program, nil
}