Commit a248256f by Philipp Adolf

Enable creating and releasing contexts

parent 73372c6d
...@@ -14,7 +14,7 @@ It is mainly developed and tested on Linux and with Intel and Nvidia GPUs. It sh ...@@ -14,7 +14,7 @@ It is mainly developed and tested on Linux and with Intel and Nvidia GPUs. It sh
- [x] querying info - [x] querying info
- [ ] subdevices (not planned for now) - [ ] subdevices (not planned for now)
- [ ] context - [ ] context
- [ ] creating and releasing contexts - [x] creating and releasing contexts
- [ ] querying info - [ ] querying info
- [ ] command queues - [ ] command queues
- [ ] creating and releasing command queues - [ ] creating and releasing command queues
......
package opencl
/*
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
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{}
}
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}
ids := make([]C.cl_device_id, len(devices))
for i, d := range devices {
ids[i] = d.id
}
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 {
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)
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment