コード例 #1
0
ファイル: platform.go プロジェクト: salviati/go-opencl
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
ファイル: context.go プロジェクト: rainliu/gocl
func CLCreateContextFromType(properties []CL_context_properties,
	device_type CL_device_type,
	pfn_notify CL_ctx_notify,
	user_data unsafe.Pointer,
	errcode_ret *CL_int) CL_context {

	var c_errcode_ret C.cl_int
	var c_context C.cl_context

	if pfn_notify == nil && user_data != nil {
		c_errcode_ret = CL_INVALID_VALUE
		c_context = nil
	} else {
		var c_properties []C.cl_context_properties
		var c_properties_ptr *C.cl_context_properties

		if properties != nil {
			c_properties = make([]C.cl_context_properties, len(properties))
			for i := 0; i < len(properties); i++ {
				c_properties[i] = C.cl_context_properties(properties[i])
			}
			c_properties_ptr = &c_properties[0]
		} else {
			c_properties_ptr = nil
		}

		if pfn_notify != nil {
			//var c_user_data []unsafe.Pointer
			//c_user_data = make([]unsafe.Pointer, 2)
			arr := C.allocArray(2)
			c_user_data := (*[2]C.pVoid)(unsafe.Pointer(arr))[:]
			c_user_data[0] = (C.pVoid)(user_data)
			c_user_data[1] = (C.pVoid)(unsafe.Pointer(&pfn_notify))

			ctx_notify[unsafe.Pointer(&pfn_notify)] = pfn_notify

			c_context = C.CLCreateContextFromType(c_properties_ptr,
				C.cl_device_type(device_type),
				unsafe.Pointer(&c_user_data),
				&c_errcode_ret)

			C.freeArray(arr)
		} else {
			c_context = C.clCreateContextFromType(c_properties_ptr,
				C.cl_device_type(device_type),
				nil,
				nil,
				&c_errcode_ret)
		}
	}

	if errcode_ret != nil {
		*errcode_ret = CL_int(c_errcode_ret)
	}

	return CL_context{c_context}
}
コード例 #3
0
ファイル: context.go プロジェクト: xfong/gocl
func CLCreateContextFromType(properties []CL_context_properties,
	device_type CL_device_type,
	pfn_notify CL_ctx_notify,
	user_data unsafe.Pointer,
	errcode_ret *CL_int) CL_context {

	var c_errcode_ret C.cl_int
	var c_context C.cl_context

	if pfn_notify == nil && user_data != nil {
		c_errcode_ret = CL_INVALID_VALUE
		c_context = nil
	} else {
		var c_properties []C.cl_context_properties
		var c_properties_ptr *C.cl_context_properties

		if properties != nil {
			c_properties = make([]C.cl_context_properties, len(properties))
			for i := 0; i < len(properties); i++ {
				c_properties[i] = C.cl_context_properties(properties[i])
			}
			c_properties_ptr = &c_properties[0]
		} else {
			c_properties_ptr = nil
		}

		if pfn_notify != nil {
			var c_user_data []unsafe.Pointer
			c_user_data = make([]unsafe.Pointer, 2)
			c_user_data[0] = user_data
			c_user_data[1] = unsafe.Pointer(&pfn_notify)

			ctx_notify[c_user_data[1]] = pfn_notify

			c_context = C.CLCreateContextFromType(c_properties_ptr,
				C.cl_device_type(device_type),
				unsafe.Pointer(&c_user_data),
				&c_errcode_ret)

		} else {
			c_context = C.clCreateContextFromType(c_properties_ptr,
				C.cl_device_type(device_type),
				nil,
				nil,
				&c_errcode_ret)
		}
	}

	if errcode_ret != nil {
		*errcode_ret = CL_int(c_errcode_ret)
	}

	return CL_context{c_context}
}
コード例 #4
0
ファイル: ocl.go プロジェクト: aa1214808834/go-cl
func CreateContext(platform *Platform, devType DeviceType) *Context {
	var ctx Context
	var error C.cl_int
	properties := []C.cl_context_properties{CL_CONTEXT_PLATFORM, C.platform2property(platform.item), C.cl_context_properties(0)}
	ctx.item = C.clCreateContextFromType(&properties[0], C.cl_device_type(devType), nil, nil, &error)
	fmt.Printf("Context error:%d\n", error)
	return &ctx
}
コード例 #5
0
ファイル: device.go プロジェクト: Dirbaio/gominer
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)
	}
}
コード例 #6
0
ファイル: ocl.go プロジェクト: aa1214808834/go-cl
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
}
コード例 #7
0
ファイル: context.go プロジェクト: salviati/go-opencl
func NewContextOfType(params map[ContextParameter]interface{}, t DeviceType) (*Context, error) {
	var c_params []C.cl_context_properties
	var propErr error
	if c_params, propErr = createParameters(params); propErr != nil {
		return nil, propErr
	}

	var c_context C.cl_context
	var err C.cl_int
	if c_context = C.clCreateContextFromType(&c_params[0], C.cl_device_type(t), nil, nil, &err); err != C.CL_SUCCESS {
		return nil, Cl_error(err)
	}
	context := &Context{id: c_context}
	runtime.SetFinalizer(context, (*Context).release)

	return context, nil
}
コード例 #8
0
ファイル: device.go プロジェクト: jackscan/go-opencl
// 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
}
コード例 #9
0
ファイル: package.go プロジェクト: mantyr/cl
// see https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html
func CreateContextFromType(properties *ContextProperties, deviceType DeviceType, notify func(string, unsafe.Pointer, uint64, interface{}), userdata interface{}, errcode *ErrorCode) Context {
	ctx := gocontext{nil, notify, userdata}
	var f *[0]byte
	var u unsafe.Pointer
	if notify != nil {
		f = (*[0]byte)(C.contextErrorCallback)
		u = unsafe.Pointer(&ctx)
	}
	ctx.clContext = C.clCreateContextFromType((*C.cl_context_properties)(unsafe.Pointer(properties)), C.cl_device_type(deviceType), f, u, (*C.cl_int)(unsafe.Pointer(errcode)))
	return Context(&ctx)
}
コード例 #10
0
ファイル: package.go プロジェクト: mantyr/cl
// 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)))
}
コード例 #11
0
ファイル: context.go プロジェクト: xfong/tmpCL
func (devType *DeviceType) toCl() C.cl_device_type {
	return C.cl_device_type(*devType)
}