-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevice.go
74 lines (64 loc) · 1.66 KB
/
device.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 opencl
/*
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
*/
import "C"
import (
"fmt"
"unsafe"
)
type Device struct {
id C.cl_device_id
}
type DeviceType C.cl_device_type
const (
DeviceTypeCPU = DeviceType(C.CL_DEVICE_TYPE_CPU)
DeviceTypeGPU = DeviceType(C.CL_DEVICE_TYPE_GPU)
DeviceTypeAccelerator = DeviceType(C.CL_DEVICE_TYPE_ACCELERATOR)
DeviceTypeCustom = DeviceType(C.CL_DEVICE_TYPE_CUSTOM)
DeviceTypeDefault = DeviceType(C.CL_DEVICE_TYPE_DEFAULT)
DeviceTypeAll = DeviceType(C.CL_DEVICE_TYPE_ALL)
)
func (t DeviceType) String() string {
switch t {
case DeviceTypeCPU:
return "CPU"
case DeviceTypeGPU:
return "GPU"
case DeviceTypeAccelerator:
return "accelereator"
case DeviceTypeCustom:
return "custom"
case DeviceTypeDefault:
return "default"
case DeviceTypeAll:
return "all"
}
return fmt.Sprintf("unknown type %d", t)
}
func getDevices(platform Platform, deviceType DeviceType) ([]Device, error) {
var n C.cl_uint
if err := C.clGetDeviceIDs(platform.id, C.cl_device_type(deviceType), 0, nil, &n); err != C.CL_SUCCESS {
return nil, fmt.Errorf("error getting number of devices: %d", err)
}
ids := make([]C.cl_device_id, n)
if err := C.clGetDeviceIDs(platform.id, C.cl_device_type(deviceType), n, unsafe.Pointer(&ids[0]), nil); err != C.CL_SUCCESS {
return nil, fmt.Errorf("error getting devices: %d", err)
}
devices := make([]Device, n)
for i, id := range ids {
devices[i].id = id
}
return devices, nil
}
func asCLDeviceIDs(devices []*Device) []C.cl_device_id {
ids := make([]C.cl_device_id, len(devices))
for i, d := range devices {
ids[i] = d.id
}
return ids
}