forked from microo8/blackcl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram.go
More file actions
60 lines (51 loc) · 1.52 KB
/
program.go
File metadata and controls
60 lines (51 loc) · 1.52 KB
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
// program
package blackcl
/*
#cgo CFLAGS: -I CL
#cgo !darwin LDFLAGS: -lOpenCL
#cgo darwin LDFLAGS: -framework OpenCL
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
*/
import "C"
import (
"unsafe"
)
type Program struct {
program C.cl_program
}
// Return the program binaries associated with program.
func (p *Program) GetBinaries() ([][]byte, error) {
var devices C.cl_uint
err := toErr(C.clGetProgramInfo(p.program, C.CL_PROGRAM_NUM_DEVICES, C.size_t(C.sizeof_cl_uint), unsafe.Pointer(&devices), nil))
if err != nil {
return nil, err
}
deviceIDs := make([]C.cl_device_id, devices)
err = toErr(C.clGetProgramInfo(p.program, C.CL_PROGRAM_DEVICES, C.size_t(len(deviceIDs)*C.sizeof_cl_device_id), unsafe.Pointer(&deviceIDs[0]), nil))
if err != nil {
return nil, err
}
binarySizes := make([]C.size_t, devices)
err = toErr(C.clGetProgramInfo(p.program, C.CL_PROGRAM_BINARY_SIZES, C.size_t(len(deviceIDs)*C.sizeof_size_t), unsafe.Pointer(&binarySizes[0]), nil))
if err != nil {
return nil, err
}
binaries := make([][]byte, devices)
cBinaries := make([]unsafe.Pointer, devices)
for i, size := range binarySizes {
cBinaries[i] = C.malloc(C.size_t(size))
defer C.free(cBinaries[i])
}
err = toErr(C.clGetProgramInfo(p.program, C.CL_PROGRAM_BINARIES, C.size_t(len(cBinaries)*C.sizeof_size_t), unsafe.Pointer(&cBinaries[0]), nil))
if err != nil {
return nil, err
}
for i, size := range binarySizes {
binaries[i] = C.GoBytes(unsafe.Pointer(cBinaries[i]), C.int(size))
}
return binaries, nil
}