forked from microo8/blackcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.go
73 lines (64 loc) · 1.53 KB
/
bytes.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
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 (
"unsafe"
)
//Bytes is a memory buffer on the device that holds []byte
type Bytes struct {
buf *buffer
}
//Size the size of the bytes buffer
func (b *Bytes) Size() int {
return b.buf.size
}
//Release releases the buffer on the device
func (b *Bytes) Release() error {
return b.buf.Release()
}
//NewBytes allocates new memory buffer with specified size on device
func (d *Device) NewBytes(size int) (*Bytes, error) {
buf, err := newBuffer(d, size)
if err != nil {
return nil, err
}
return &Bytes{&buffer{memobj: buf, device: d, size: size}}, nil
}
//Copy copies the data from host data to device buffer
//it's a non-blocking call, channel will return an error or nil if the data transfer is complete
func (b *Bytes) Copy(data []byte) <-chan error {
return b.buf.copy(len(data), unsafe.Pointer(&data[0]))
}
//Data gets data from device, it's a blocking call
func (b *Bytes) Data() ([]byte, error) {
data := make([]byte, b.buf.size)
err := toErr(C.clEnqueueReadBuffer(
b.buf.device.queue,
b.buf.memobj,
C.CL_TRUE,
0,
C.size_t(b.buf.size),
unsafe.Pointer(&data[0]),
0,
nil,
nil,
))
if err != nil {
return nil, err
}
return data, nil
}
//Map applies an map kernel on all elements of the buffer
func (b *Bytes) Map(k *Kernel) <-chan error {
return k.Global(b.buf.size).Local(1).Run(b)
}