forked from microo8/blackcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
74 lines (68 loc) · 1.3 KB
/
buffer.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
package blackcl
/*
#cgo CFLAGS: -I CL
#cgo !darwin LDFLAGS: -lOpenCL
#cgo darwin LDFLAGS: -framework OpenCL
#define CL_SILENCE_DEPRECATION
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
*/
import "C"
import (
"errors"
"unsafe"
)
//buffer memory buffer on the device
type buffer struct {
memobj C.cl_mem
size int
device *Device
}
//newBuffer creates new buffer with specified size
func newBuffer(d *Device, size int) (C.cl_mem, error) {
var ret C.cl_int
clBuffer := C.clCreateBuffer(d.ctx, C.CL_MEM_READ_WRITE, C.size_t(size), nil, &ret)
err := toErr(ret)
if err != nil {
return nil, err
}
if clBuffer == nil {
return nil, ErrUnknown
}
return clBuffer, nil
}
//Release releases the buffer on the device
func (b *buffer) Release() error {
return toErr(C.clReleaseMemObject(b.memobj))
}
func (b *buffer) copy(size int, ptr unsafe.Pointer) <-chan error {
ch := make(chan error, 1)
if b.size != size {
ch <- errors.New("buffer size not equal to data len")
return ch
}
var event C.cl_event
err := toErr(C.clEnqueueWriteBuffer(
b.device.queue,
b.memobj,
C.CL_FALSE,
0,
C.size_t(size),
ptr,
0,
nil,
&event,
))
if err != nil {
ch <- err
return ch
}
go func() {
defer C.clReleaseEvent(event)
ch <- toErr(C.clWaitForEvents(1, &event))
}()
return ch
}