Пример #1
0
func GetPlatforms() []Platform {
	var count C.cl_uint
	if ret := C.clGetPlatformIDs(0, (*C.cl_platform_id)(nil), &count); ret != C.CL_SUCCESS || count == 0 {
		return nil
	}

	c_platforms := make([]C.cl_platform_id, count)
	if ret := C.clGetPlatformIDs(count, &c_platforms[0], &count); ret != C.CL_SUCCESS || count == 0 {
		return nil
	}
	platforms := make([]Platform, 0, count)

	for _, pid := range c_platforms {
		if ret := C.clGetDeviceIDs(pid, C.cl_device_type(DEVICE_TYPE_ALL), 0, (*C.cl_device_id)(nil), &count); ret != C.CL_SUCCESS || count == 0 {
			continue
		}

		c_devices := make([]C.cl_device_id, count)
		if ret := C.clGetDeviceIDs(pid, C.cl_device_type(DEVICE_TYPE_ALL), count, &c_devices[0], &count); ret != C.CL_SUCCESS || count == 0 {
			continue
		}

		platform := Platform{
			id:         pid,
			Devices:    make([]Device, count),
			properties: make(map[PlatformProperty]string),
		}
		for i, did := range c_devices {
			platform.Devices[i].id = did
			platform.Devices[i].properties = make(map[DeviceProperty]interface{})
		}
		platforms = append(platforms, platform)
	}
	return platforms
}
Пример #2
0
func CLGetDeviceIDs(platform CL_platform_id,
	device_type CL_device_type,
	num_entries CL_uint,
	devices []CL_device_id,
	num_devices *CL_uint) CL_int {

	if (num_entries == 0 && devices != nil) || (num_devices == nil && devices == nil) {
		return CL_INVALID_VALUE
	} else {
		var c_num_devices C.cl_uint
		var c_errcode_ret C.cl_int

		if devices == nil {
			c_errcode_ret = C.clGetDeviceIDs(platform.cl_platform_id,
				C.cl_device_type(device_type),
				C.cl_uint(num_entries),
				nil,
				&c_num_devices)
		} else {
			devices_id := make([]C.cl_device_id, len(devices))
			c_errcode_ret = C.clGetDeviceIDs(platform.cl_platform_id,
				C.cl_device_type(device_type),
				C.cl_uint(num_entries),
				&devices_id[0],
				&c_num_devices)
			if c_errcode_ret == C.CL_SUCCESS {
				for i := 0; i < len(devices); i++ {
					devices[i].cl_device_id = devices_id[i]
				}
			}
		}

		if num_devices != nil {
			*num_devices = CL_uint(c_num_devices)
		}

		return CL_int(c_errcode_ret)
	}
}
Пример #3
0
func (pl *Platform) Devices(devType DeviceType, num uint) []Device {
	devices := make([]aDevice, num)
	var realNum C.cl_uint = 0

	error := C.clGetDeviceIDs(pl.item, C.cl_device_type(devType), C.cl_uint(num), (*C.cl_device_id)(&devices[0]), &realNum)
	res := make([]Device, realNum)
	fmt.Printf("xxx %d\n", int(error))
	var i C.cl_uint
	for i = 0; i < realNum; i++ {
		res[i].item = devices[i]
	}
	return res
}
Пример #4
0
func getPlatformByID(id C.cl_platform_id) (p *Platform, err error) {
	var numDevices C.cl_uint
	deviceIDs := [maxPlatformDevices]C.cl_device_id{}
	deviceIDsPtr := (*C.cl_device_id)(unsafe.Pointer(&deviceIDs))

	ret := C.clGetDeviceIDs(id, C.CL_DEVICE_TYPE_ALL, C.cl_uint(maxPlatformDevices), deviceIDsPtr, &numDevices)

	if ret != 0 {
		err = newError(ret)
		return
	}

	p = &Platform{
		id:      id,
		devices: make([]*Device, int(numDevices)),
	}

	var str [4096]byte
	getMap := []struct {
		info C.cl_platform_info
		dst  interface{}
		hold interface{}
	}{
		{C.CL_PLATFORM_NAME, &p.name, &str},
		{C.CL_PLATFORM_PROFILE, &p.profile, &str},
		{C.CL_PLATFORM_VENDOR, &p.vendor, &str},
		{C.CL_PLATFORM_VERSION, &p.version, &str},
		{C.CL_PLATFORM_EXTENSIONS, &p.extensions, &str},
	}

	for _, v := range getMap {
		if _, err = getPlatformInfo(id, v.info, v.hold, v.dst); err != nil {
			return
		}
	}

	for i := 0; i < int(numDevices); i++ {
		if p.devices[i], err = getDeviceByID(deviceIDs[i]); err != nil {
			break
		}
	}

	return
}
Пример #5
0
// Obtain the list of devices available on a platform. 'platform' refers
// to the platform returned by GetPlatforms or can be nil. If platform
// is nil, the behavior is implementation-defined.
func GetDevices(platform *Platform, deviceType DeviceType) ([]*Device, error) {
	var deviceIds [maxDeviceCount]C.cl_device_id
	var numDevices C.cl_uint
	var platformId C.cl_platform_id
	if platform != nil {
		platformId = platform.id
	}
	if err := C.clGetDeviceIDs(platformId, C.cl_device_type(deviceType), C.cl_uint(maxDeviceCount), &deviceIds[0], &numDevices); err != C.CL_SUCCESS {
		return nil, toError(err)
	}
	if numDevices > maxDeviceCount {
		numDevices = maxDeviceCount
	}
	devices := make([]*Device, numDevices)
	for i := 0; i < int(numDevices); i++ {
		devices[i] = &Device{id: deviceIds[i]}
	}
	return devices, nil
}
Пример #6
0
// see https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceIDs.html
func GetDeviceIDs(pid PlatformID, deviceType DeviceType, numentries uint32, devices *DeviceId, numdevices *uint32) ErrorCode {
	return ErrorCode(C.clGetDeviceIDs(pid, C.cl_device_type(deviceType), C.cl_uint(numentries), (*C.cl_device_id)(unsafe.Pointer(devices)), (*C.cl_uint)(numdevices)))
}