forked from Zyko0/go-opencl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.go
88 lines (81 loc) · 2.38 KB
/
platform.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package middleCL
import (
constants "github.com/opencl-pure/constantsCL"
pure "github.com/opencl-pure/pureCL"
"strings"
)
type Platform struct {
P pure.Platform
}
func GetPlatforms() ([]Platform, error) {
numPlatforms := uint32(0)
if pure.GetPlatformIDs == nil {
return nil, pure.Uninitialized("GetPlatformIDs")
}
st := pure.GetPlatformIDs(0, nil, &numPlatforms)
if st != constants.CL_SUCCESS {
return nil, pure.StatusToErr(st)
}
platformIDs := make([]pure.Platform, numPlatforms)
st = pure.GetPlatformIDs(numPlatforms, platformIDs, nil)
if st != constants.CL_SUCCESS {
return nil, pure.StatusToErr(st)
}
res := make([]Platform, numPlatforms)
for i := uint32(0); i < numPlatforms; i++ {
res[i] = Platform{P: platformIDs[i]}
}
return res, nil
}
func (p *Platform) getInfo(name pure.PlatformInfo) (string, error) {
size := pure.Size(0)
st := pure.GetPlatformInfo(p.P, name, pure.Size(0), nil, &size)
if st != constants.CL_SUCCESS {
return "", pure.StatusToErr(st)
}
info := make([]byte, size)
st = pure.GetPlatformInfo(p.P, name, size, info, nil)
if st != constants.CL_SUCCESS {
return "", pure.StatusToErr(st)
}
return string(info), nil
}
func (p *Platform) GetProfile() (string, error) {
return p.getInfo(constants.CL_PLATFORM_PROFILE)
}
func (p *Platform) GetVersion() (string, error) {
return p.getInfo(constants.CL_PLATFORM_VERSION)
}
func (p *Platform) GetName() (string, error) {
return p.getInfo(constants.CL_PLATFORM_NAME)
}
func (p *Platform) GetVendor() (string, error) {
return p.getInfo(constants.CL_PLATFORM_VENDOR)
}
func (p *Platform) GetExtensions() ([]pure.Extension, error) {
extensions, err := p.getInfo(constants.CL_PLATFORM_EXTENSIONS)
if err != nil {
return nil, err
}
return strings.Split(extensions, " "), nil
}
func (p *Platform) GetDevices(deviceType pure.DeviceType) ([]Device, error) {
numDevices := uint32(0)
if pure.GetDeviceIDs == nil {
return nil, pure.Uninitialized("GetDeviceIDs")
}
st := pure.GetDeviceIDs(p.P, deviceType, 0, nil, &numDevices)
if st != constants.CL_SUCCESS {
return nil, pure.StatusToErr(st)
}
deviceIDs := make([]pure.Device, numDevices)
st = pure.GetDeviceIDs(p.P, deviceType, numDevices, deviceIDs, nil)
if st != constants.CL_SUCCESS {
return nil, pure.StatusToErr(st)
}
res := make([]Device, numDevices)
for i := uint32(0); i < numDevices; i++ {
res[i] = Device{D: deviceIDs[i]}
}
return res, nil
}