Commit c02b04cb by Philipp Adolf

Enable enumeration of devices

parent 689c905d
...@@ -41,5 +41,14 @@ func main() { ...@@ -41,5 +41,14 @@ func main() {
log.Printf(" Profile: %s", profile) log.Printf(" Profile: %s", profile)
log.Printf(" Version: %s", version) log.Printf(" Version: %s", version)
log.Printf(" Extensions: %s", strings.Join(extensions, ", ")) log.Printf(" Extensions: %s", strings.Join(extensions, ", "))
devices, err := p.Devices(opencl.DeviceTypeAll)
if err != nil {
log.Fatal(err)
}
for i := range devices {
log.Printf(" Device %d", i)
}
} }
} }
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
}
...@@ -8,7 +8,9 @@ package opencl ...@@ -8,7 +8,9 @@ package opencl
#endif #endif
*/ */
import "C" import "C"
import "fmt" import (
"fmt"
)
type Platform struct { type Platform struct {
id C.cl_platform_id id C.cl_platform_id
...@@ -32,3 +34,7 @@ func GetPlatforms() ([]Platform, error) { ...@@ -32,3 +34,7 @@ func GetPlatforms() ([]Platform, error) {
return platforms, nil return platforms, nil
} }
func (p Platform) Devices(deviceType DeviceType) ([]Device, error) {
return getDevices(p, deviceType)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment