Esempio n. 1
0
func CreateContextUnsafe(properties *C.cl_context_properties, devices []*Device, pfn_notify CL_ctx_notify, user_data unsafe.Pointer) (*Context, error) {
	deviceIds := buildDeviceIdList(devices)
	var err C.cl_int
	var clContext C.cl_context
	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

		clContext = C.CLCreateContext(properties, C.cl_uint(len(devices)), &deviceIds[0], unsafe.Pointer(&c_user_data), &err)
	} else {
		clContext = C.clCreateContext(properties, C.cl_uint(len(devices)), &deviceIds[0], nil, nil, &err)
	}
	if err != C.CL_SUCCESS {
		return nil, toError(err)
	}
	if clContext == nil {
		return nil, ErrUnknown
	}
	context := &Context{clContext: clContext, devices: devices}
	runtime.SetFinalizer(context, releaseContext)
	return context, nil
}
Esempio n. 2
0
func CLGetPlatformIDs(num_entries CL_uint,
	platforms []CL_platform_id,
	num_platforms *CL_uint) CL_int {

	if (num_entries == 0 && platforms != nil) || (num_platforms == nil && platforms == nil) {
		return CL_INVALID_VALUE
	} else {
		var c_errcode_ret C.cl_int
		var c_num_platforms C.cl_uint

		if platforms == nil {
			c_errcode_ret = C.clGetPlatformIDs(C.cl_uint(num_entries),
				nil,
				&c_num_platforms)
		} else {
			c_platforms := make([]C.cl_platform_id, len(platforms))
			c_errcode_ret = C.clGetPlatformIDs(C.cl_uint(num_entries),
				&c_platforms[0],
				&c_num_platforms)
			if c_errcode_ret == C.CL_SUCCESS {
				for i := 0; i < len(platforms); i++ {
					platforms[i].cl_platform_id = c_platforms[i]
				}
			}
		}

		if num_platforms != nil {
			*num_platforms = CL_uint(c_num_platforms)
		}
		return CL_int(c_errcode_ret)
	}
}
Esempio n. 3
0
func CLCreateKernelsInProgram(program CL_program,
	num_kernels CL_uint,
	kernels []CL_kernel,
	num_kernels_ret *CL_uint) CL_int {

	if (num_kernels == 0 && kernels != nil) || (num_kernels_ret == nil && kernels == nil) {
		return CL_INVALID_VALUE
	} else {
		var c_num_kernels_ret C.cl_uint
		var c_errcode_ret C.cl_int

		if kernels == nil {
			c_errcode_ret = C.clCreateKernelsInProgram(program.cl_program,
				C.cl_uint(num_kernels),
				nil,
				&c_num_kernels_ret)
		} else {
			c_kernels := make([]C.cl_kernel, len(kernels))
			c_errcode_ret = C.clCreateKernelsInProgram(program.cl_program,
				C.cl_uint(num_kernels),
				&c_kernels[0],
				&c_num_kernels_ret)
			if c_errcode_ret == C.CL_SUCCESS {
				for i := 0; i < len(kernels); i++ {
					kernels[i].cl_kernel = c_kernels[i]
				}
			}
		}
		if num_kernels_ret != nil {
			*num_kernels_ret = CL_uint(c_num_kernels_ret)
		}

		return CL_int(c_errcode_ret)
	}
}
Esempio n. 4
0
func (k *Kernel) SetArg(index uint, arg interface{}) error {
	var ret C.cl_int

	switch t := arg.(type) {
	case *Buffer: // can't use *Image here, compiler bug?
		ret = C.clSetKernelArg(k.id, C.cl_uint(index), C.size_t(unsafe.Sizeof(t.id)), unsafe.Pointer(&t.id))
	case *Image:
		ret = C.clSetKernelArg(k.id, C.cl_uint(index), C.size_t(unsafe.Sizeof(t.id)), unsafe.Pointer(&t.id))
	case float32:
		ret = C.clSetKernelArg(k.id, C.cl_uint(index), C.size_t(4), unsafe.Pointer(&t))
	case []float32:
		ret = C.clSetKernelArg(k.id, C.cl_uint(index), C.size_t(len(t)*4), unsafe.Pointer(&t[0]))
	case *float32:
		var p *C.float
		ret = C.clSetKernelArg(k.id, C.cl_uint(index), C.size_t(unsafe.Sizeof(p)), unsafe.Pointer(&t))

	default:
		return Cl_error(C.CL_INVALID_VALUE)
	}

	if ret != C.CL_SUCCESS {
		return Cl_error(ret)
	}
	return nil
}
Esempio n. 5
0
func (p *Program) CompileProgramWithCallback(devices []*Device, options string, program_headers []*ProgramHeaders, user_data unsafe.Pointer) error {
	var cOptions *C.char
	if options != "" {
		cOptions = C.CString(options)
		defer C.free(unsafe.Pointer(cOptions))
	}
	var deviceList []C.cl_device_id
	var deviceListPtr *C.cl_device_id
	numDevices := C.cl_uint(len(devices))
	if devices != nil && len(devices) > 0 {
		deviceList = buildDeviceIdList(devices)
		deviceListPtr = &deviceList[0]
	}
	num_headers := len(program_headers)
	cHeaders := make([]C.cl_program, num_headers)
	cHeader_names := make([]*C.char, num_headers)
	for idx, ph := range program_headers {
		chs := ph.codes
		chn := C.CString(ph.names)
		cHeaders[idx] = chs.clProgram
		cHeader_names[idx] = chn
		defer C.free(chs)
		defer C.free(chn)
	}
	err := C.CLCompileProgram(p.clProgram, numDevices, deviceListPtr, cOptions, C.cl_uint(num_headers), &cHeaders[0], &cHeader_names[0], user_data)
	if err != C.CL_SUCCESS {
		buffer := make([]byte, 4096)
		var bLen C.size_t
		var err C.cl_int

		for _, dev := range p.devices {
			for i := 2; i >= 0; i-- {
				err = C.clGetProgramBuildInfo(p.clProgram, dev.id, C.CL_PROGRAM_BUILD_LOG, C.size_t(len(buffer)), unsafe.Pointer(&buffer[0]), &bLen)
				if err == C.CL_INVALID_VALUE && i > 0 && bLen < 1024*1024 {
					// INVALID_VALUE probably means our buffer isn't large enough
					buffer = make([]byte, bLen)
				} else {
					break
				}
			}
			if err != C.CL_SUCCESS {
				return toError(err)
			}

			if bLen > 1 {
				return BuildError{
					Device:  dev,
					Message: string(buffer[:bLen-1]),
				}
			}
		}

		return BuildError{
			Device:  nil,
			Message: "build failed and produced no log entries",
		}
	}
	return nil
}
Esempio n. 6
0
func CLEnqueueMigrateMemObjects(command_queue CL_command_queue,
	num_mem_objects CL_uint,
	mem_objects []CL_mem,
	flags CL_mem_migration_flags,
	num_events_in_wait_list CL_uint,
	event_wait_list []CL_event,
	event *CL_event) CL_int {

	if num_mem_objects == 0 || mem_objects == nil || int(num_mem_objects) != len(mem_objects) {
		return CL_INVALID_VALUE
	}

	if (num_events_in_wait_list == 0 && event_wait_list != nil) ||
		(num_events_in_wait_list != 0 && event_wait_list == nil) ||
		int(num_events_in_wait_list) != len(event_wait_list) {
		return CL_INVALID_EVENT_WAIT_LIST
	}

	var c_mem_objects []C.cl_mem
	var c_event C.cl_event
	var c_errcode_ret C.cl_int

	c_mem_objects = make([]C.cl_mem, len(mem_objects))
	for i := 0; i < len(mem_objects); i++ {
		c_mem_objects[i] = mem_objects[i].cl_mem
	}

	if num_events_in_wait_list != 0 {
		var c_event_wait_list []C.cl_event
		c_event_wait_list = make([]C.cl_event, num_events_in_wait_list)
		for i := 0; i < int(num_events_in_wait_list); i++ {
			c_event_wait_list[i] = event_wait_list[i].cl_event
		}

		c_errcode_ret = C.clEnqueueMigrateMemObjects(command_queue.cl_command_queue,
			C.cl_uint(num_mem_objects),
			&c_mem_objects[0],
			C.cl_mem_migration_flags(flags),
			C.cl_uint(num_events_in_wait_list),
			&c_event_wait_list[0],
			&c_event)
	} else {
		c_errcode_ret = C.clEnqueueMigrateMemObjects(command_queue.cl_command_queue,
			C.cl_uint(num_mem_objects),
			&c_mem_objects[0],
			C.cl_mem_migration_flags(flags),
			0,
			nil,
			&c_event)
	}

	if event != nil {
		event.cl_event = c_event
	}

	return CL_int(c_errcode_ret)
}
Esempio n. 7
0
func (ctx *Context) LinkProgramWithCallback(programs []*Program, devices []*Device, options string, user_data unsafe.Pointer) (*Program, error) {
	var cOptions *C.char
	if options != "" {
		cOptions = C.CString(options)
		defer C.free(unsafe.Pointer(cOptions))
	}
	var deviceList []C.cl_device_id
	var deviceListPtr *C.cl_device_id
	numDevices := C.cl_uint(len(devices))
	if devices != nil && len(devices) > 0 {
		deviceList = buildDeviceIdList(devices)
		deviceListPtr = &deviceList[0]
	}
	programList := make([]C.cl_program, len(programs))
	for idx, progId := range programs {
		programList[idx] = progId.clProgram
	}
	var err C.cl_int
	programExe := C.CLLinkProgram(ctx.clContext, numDevices, deviceListPtr, cOptions, C.cl_uint(len(programs)), &programList[0], user_data, &err)
	p := &Program{clProgram: programExe, devices: devices}
	if err != C.CL_SUCCESS {
		buffer := make([]byte, 4096)
		var bLen C.size_t
		var err C.cl_int

		for _, dev := range p.devices {
			for i := 2; i >= 0; i-- {
				err = C.clGetProgramBuildInfo(p.clProgram, dev.id, C.CL_PROGRAM_BUILD_LOG, C.size_t(len(buffer)), unsafe.Pointer(&buffer[0]), &bLen)
				if err == C.CL_INVALID_VALUE && i > 0 && bLen < 1024*1024 {
					// INVALID_VALUE probably means our buffer isn't large enough
					buffer = make([]byte, bLen)
				} else {
					break
				}
			}
			if err != C.CL_SUCCESS {
				return nil, toError(err)
			}

			if bLen > 1 {
				return nil, BuildError{
					Device:  dev,
					Message: string(buffer[:bLen-1]),
				}
			}
		}

		return nil, BuildError{
			Device:  nil,
			Message: "build failed and produced no log entries",
		}
	}
	return p, nil
}
Esempio n. 8
0
func (k *Kernel) ArgName(index int) (string, error) {
	var strC [1024]byte
	var strN C.size_t
	// get the size of the string
	if err := C.clGetKernelArgInfo(k.clKernel, C.cl_uint(index), C.CL_KERNEL_ARG_NAME, 8, nil, &strN); err != C.CL_SUCCESS {
		return "", toError(err)
	}
	if err := C.clGetKernelArgInfo(k.clKernel, C.cl_uint(index), C.CL_KERNEL_ARG_NAME, strN, unsafe.Pointer(&strC[0]), &strN); err != C.CL_SUCCESS {
		return "", toError(err)
	}
	return string(strC[:strN]), nil
}
Esempio n. 9
0
File: package.go Progetto: mantyr/cl
// see (https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/climde.html
func (imde ImageDesc) toC() *C.cl_image_desc {
	return &C.cl_image_desc{
		image_type:        C.cl_mem_object_type(imde.imageType),
		image_width:       C.size_t(imde.imageWidth),
		image_height:      C.size_t(imde.imageHeight),
		image_depth:       C.size_t(imde.imageDepth),
		image_array_size:  C.size_t(imde.imageArraySize),
		image_row_pitch:   C.size_t(imde.imageRowPitch),
		image_slice_pitch: C.size_t(imde.imageSlicePitch),
		num_mip_levels:    C.cl_uint(imde.numMipLevels),
		num_samples:       C.cl_uint(imde.numSamples),
		buffer:            C.cl_mem(imde.buffer)}
}
Esempio n. 10
0
///////////////////////////////////////////////
//OpenCL 1.2
///////////////////////////////////////////////
func CLCreateSubDevices(in_device CL_device_id,
	properties []CL_device_partition_property,
	num_devices CL_uint,
	out_devices []CL_device_id,
	num_devices_ret *CL_uint) CL_int {

	if (num_devices == 0 && out_devices != nil) || (out_devices == nil && num_devices_ret == nil) {
		return CL_INVALID_VALUE
	} else {
		var c_properties []C.cl_device_partition_property
		var c_properties_ptr *C.cl_device_partition_property

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

		var c_errcode_ret C.cl_int
		var c_num_devices_ret C.cl_uint

		if out_devices == nil {
			c_errcode_ret = C.clCreateSubDevices(in_device.cl_device_id,
				c_properties_ptr,
				C.cl_uint(num_devices),
				nil,
				&c_num_devices_ret)
		} else {
			c_out_devices := make([]C.cl_device_id, len(out_devices))
			c_errcode_ret = C.clCreateSubDevices(in_device.cl_device_id,
				c_properties_ptr,
				C.cl_uint(num_devices),
				&c_out_devices[0],
				&c_num_devices_ret)
			if c_errcode_ret == C.CL_SUCCESS {
				for i := 0; i < len(out_devices); i++ {
					out_devices[i].cl_device_id = c_out_devices[i]
				}
			}
		}

		if num_devices_ret != nil {
			*num_devices_ret = CL_uint(c_num_devices_ret)
		}
		return CL_int(c_errcode_ret)
	}
}
Esempio n. 11
0
func (ctx *Context) CreateProgramWithBuiltInKernels(devices []*Device, kernel_names []string) (*Program, error) {
	cSources := make([]*C.char, 1)
	merge_string := strings.Join(kernel_names, ";")
	cs := C.CString(merge_string)
	cSources[0] = cs
	defer C.free(unsafe.Pointer(cs))
	var deviceList []C.cl_device_id
	var deviceListPtr *C.cl_device_id
	numDevices := C.cl_uint(len(devices))
	if devices != nil && len(devices) > 0 {
		deviceList = buildDeviceIdList(devices)
		deviceListPtr = &deviceList[0]
	}
	var err C.cl_int
	clProgram := C.clCreateProgramWithBuiltInKernels(ctx.clContext, numDevices, deviceListPtr, cSources[0], &err)
	if err != C.CL_SUCCESS {
		return nil, toError(err)
	}
	if clProgram == nil {
		return nil, ErrUnknown
	}
	program := &Program{clProgram: clProgram, devices: ctx.devices}
	runtime.SetFinalizer(program, releaseProgram)
	return program, nil
}
Esempio n. 12
0
func (p *Program) BuildProgram(devices []*Device, options string) error {
	var cOptions *C.char
	if options != "" {
		cOptions = C.CString(options)
		defer C.free(unsafe.Pointer(cOptions))
	}
	var deviceList []C.cl_device_id
	var deviceListPtr *C.cl_device_id
	if devices != nil && len(devices) > 0 {
		deviceList = buildDeviceIdList(devices)
		deviceListPtr = &deviceList[0]
	}
	numDevices := C.cl_uint(len(deviceList))

	if err := C.clBuildProgram(p.clProgram, numDevices, deviceListPtr, cOptions, nil, nil); err != C.CL_SUCCESS {
		if err != C.CL_BUILD_PROGRAM_FAILURE {
			return toError(err)
		}
		var bLen C.size_t
		var err C.cl_int
		err = C.clGetProgramBuildInfo(p.clProgram, p.devices[0].id, C.CL_PROGRAM_BUILD_LOG, 0, nil, &bLen)
		if err != C.CL_SUCCESS {
			return toError(err)
		}
		buffer := make([]byte, bLen)
		err = C.clGetProgramBuildInfo(p.clProgram, p.devices[0].id, C.CL_PROGRAM_BUILD_LOG, C.size_t(len(buffer)), unsafe.Pointer(&buffer[0]), &bLen)
		if err != C.CL_SUCCESS {
			return toError(err)
		}
		return BuildError(string(buffer[:bLen-1]))
	}
	return nil
}
Esempio n. 13
0
// Enqueues a command to unmap a previously mapped region of a memory object.
func (q *CommandQueue) EnqueueUnmapMemObject(buffer *MemObject, mappedObj *MappedMemObject, eventWaitList []*Event) (*Event, error) {
	var event C.cl_event
	if err := C.clEnqueueUnmapMemObject(q.clQueue, buffer.clMem, mappedObj.ptr, C.cl_uint(len(eventWaitList)), eventListPtr(eventWaitList), &event); err != C.CL_SUCCESS {
		return nil, toError(err)
	}
	return newEvent(event), nil
}
Esempio n. 14
0
// Enqueues a command to execute a kernel on a device.
func (q *CommandQueue) EnqueueNDRangeKernel(kernel *Kernel, globalWorkOffset, globalWorkSize, localWorkSize []int, eventWaitList []*Event) (*Event, error) {
	workDim := len(globalWorkSize)
	var globalWorkOffsetList []C.size_t
	var globalWorkOffsetPtr *C.size_t
	if globalWorkOffset != nil {
		globalWorkOffsetList = make([]C.size_t, len(globalWorkOffset))
		for i, off := range globalWorkOffset {
			globalWorkOffsetList[i] = C.size_t(off)
		}
		globalWorkOffsetPtr = &globalWorkOffsetList[0]
	}
	var globalWorkSizeList []C.size_t
	var globalWorkSizePtr *C.size_t
	if globalWorkSize != nil {
		globalWorkSizeList = make([]C.size_t, len(globalWorkSize))
		for i, off := range globalWorkSize {
			globalWorkSizeList[i] = C.size_t(off)
		}
		globalWorkSizePtr = &globalWorkSizeList[0]
	}
	var localWorkSizeList []C.size_t
	var localWorkSizePtr *C.size_t
	if localWorkSize != nil {
		localWorkSizeList = make([]C.size_t, len(localWorkSize))
		for i, off := range localWorkSize {
			localWorkSizeList[i] = C.size_t(off)
		}
		localWorkSizePtr = &localWorkSizeList[0]
	}
	var event C.cl_event
	err := toError(C.clEnqueueNDRangeKernel(q.clQueue, kernel.clKernel, C.cl_uint(workDim), globalWorkOffsetPtr, globalWorkSizePtr, localWorkSizePtr, C.cl_uint(len(eventWaitList)), eventListPtr(eventWaitList), &event))
	return newEvent(event), err
}
Esempio n. 15
0
func CLSetKernelArgSVMPointer(kernel CL_kernel,
	arg_index CL_uint,
	arg_value unsafe.Pointer) CL_int {
	return CL_int(C.clSetKernelArgSVMPointer(kernel.cl_kernel,
		C.cl_uint(arg_index),
		arg_value))
}
Esempio n. 16
0
func CLEnqueueWaitForEvents(command_queue CL_command_queue,
	num_events_in_wait_list CL_uint,
	event_wait_list []CL_event) CL_int {

	if (num_events_in_wait_list == 0 && event_wait_list != nil) ||
		(num_events_in_wait_list != 0 && event_wait_list == nil) ||
		int(num_events_in_wait_list) != len(event_wait_list) {
		return CL_INVALID_EVENT_WAIT_LIST
	}

	var c_errcode_ret C.cl_int

	if num_events_in_wait_list != 0 {
		var c_event_wait_list []C.cl_event
		c_event_wait_list = make([]C.cl_event, num_events_in_wait_list)
		for i := 0; i < int(num_events_in_wait_list); i++ {
			c_event_wait_list[i] = event_wait_list[i].cl_event
		}

		c_errcode_ret = C.clEnqueueWaitForEvents(command_queue.cl_command_queue,
			C.cl_uint(num_events_in_wait_list),
			&c_event_wait_list[0])
	} else {
		c_errcode_ret = C.clEnqueueWaitForEvents(command_queue.cl_command_queue,
			0,
			nil)
	}

	return CL_int(c_errcode_ret)
}
Esempio n. 17
0
func (p *Program) BuildProgram(devices []*Device, options string) error {
	var cOptions *C.char
	if options != "" {
		cOptions = C.CString(options)
		defer C.free(unsafe.Pointer(cOptions))
	}
	var deviceList []C.cl_device_id
	var deviceListPtr *C.cl_device_id
	numDevices := C.cl_uint(0)
	if devices != nil && len(devices) > 0 {
		deviceList = buildDeviceIdList(devices)
		deviceListPtr = &deviceList[0]
	}
	if err := C.clBuildProgram(p.clProgram, numDevices, deviceListPtr, cOptions, nil, nil); err != C.CL_SUCCESS {
		buffer := make([]byte, 4096)
		var bLen C.size_t
		var err C.cl_int
		for i := 2; i >= 0; i-- {
			err = C.clGetProgramBuildInfo(p.clProgram, p.devices[0].id, C.CL_PROGRAM_BUILD_LOG, C.size_t(len(buffer)), unsafe.Pointer(&buffer[0]), &bLen)
			if err == C.CL_INVALID_VALUE && i > 0 && bLen < 1024*1024 {
				// INVALID_VALUE probably means our buffer isn't large enough
				buffer = make([]byte, bLen)
			} else {
				break
			}
		}
		if err != C.CL_SUCCESS {
			return toError(err)
		}
		return BuildError(string(buffer[:bLen]))
	}
	return nil
}
Esempio n. 18
0
func CreateProgram(context *Context, sources []string) *Program {
	runtime.LockOSThread()
	var program Program
	var error C.cl_int

	csources := C.makeCharArray(C.int(len(sources)))
	clenghts := make([]C.size_t, len(sources))

	defer C.freeCharArray(csources, C.int(len(sources)))

	for i := 0; i < len(sources); i++ {
		C.setArrayString(csources, C.CString(sources[i]), C.int(i))
		clenghts[i] = C.size_t(len(sources[i]))
		fmt.Printf("Program log:%d %s\n", clenghts[i], sources[i])
	}

	program.item = C.clCreateProgramWithSource(
		context.item,
		C.cl_uint(len(sources)),
		csources,
		&clenghts[0],
		&error)

	fmt.Printf("Program error:%d\n", error)

	return &program
}
Esempio n. 19
0
func CLBuildProgram(program CL_program,
	num_devices CL_uint,
	devices []CL_device_id,
	options []byte,
	pfn_notify CL_prg_notify,
	user_data unsafe.Pointer) CL_int {

	if (num_devices == 0 && devices != nil) ||
		(num_devices != 0 && devices == nil) ||
		(pfn_notify == nil && user_data != nil) {
		return CL_INVALID_VALUE
	}

	var c_devices []C.cl_device_id
	var c_options *C.char
	var c_errcode_ret C.cl_int

	c_devices = make([]C.cl_device_id, len(devices))
	for i := 0; i < len(devices); i++ {
		c_devices[i] = C.cl_device_id(devices[i].cl_device_id)
	}
	if options != nil {
		c_options = C.CString(string(options))
		defer C.free(unsafe.Pointer(c_options))
	} else {
		c_options = nil
	}

	if pfn_notify != nil {
		prg_notify[program.cl_program] = pfn_notify

		c_errcode_ret = C.CLBuildProgram(program.cl_program,
			C.cl_uint(num_devices),
			&c_devices[0],
			c_options,
			user_data)
	} else {
		c_errcode_ret = C.clBuildProgram(program.cl_program,
			C.cl_uint(num_devices),
			&c_devices[0],
			c_options,
			nil,
			nil)
	}

	return CL_int(c_errcode_ret)
}
Esempio n. 20
0
File: image.go Progetto: xfong/gocl
func CLEnqueueCopyImage(command_queue CL_command_queue,
	src_image CL_mem,
	dst_image CL_mem,
	src_origin [3]CL_size_t,
	dst_origin [3]CL_size_t,
	region [3]CL_size_t,
	num_events_in_wait_list CL_uint,
	event_wait_list []CL_event,
	event *CL_event) CL_int {

	if (num_events_in_wait_list == 0 && event_wait_list != nil) ||
		(num_events_in_wait_list != 0 && event_wait_list == nil) ||
		int(num_events_in_wait_list) != len(event_wait_list) {
		return CL_INVALID_EVENT_WAIT_LIST
	}

	var c_src_origin, c_dst_origin, c_region [3]C.size_t
	var c_event C.cl_event
	var c_errcode_ret C.cl_int

	for i := 0; i < 3; i++ {
		c_src_origin[i] = C.size_t(src_origin[i])
		c_dst_origin[i] = C.size_t(dst_origin[i])
		c_region[i] = C.size_t(region[i])
	}
	if num_events_in_wait_list != 0 {
		var c_event_wait_list []C.cl_event
		c_event_wait_list = make([]C.cl_event, num_events_in_wait_list)
		for i := 0; i < int(num_events_in_wait_list); i++ {
			c_event_wait_list[i] = event_wait_list[i].cl_event
		}

		c_errcode_ret = C.clEnqueueCopyImage(command_queue.cl_command_queue,
			src_image.cl_mem,
			dst_image.cl_mem,
			&c_src_origin[0],
			&c_dst_origin[0],
			&c_region[0],
			C.cl_uint(num_events_in_wait_list),
			&c_event_wait_list[0],
			&c_event)
	} else {
		c_errcode_ret = C.clEnqueueCopyImage(command_queue.cl_command_queue,
			src_image.cl_mem,
			dst_image.cl_mem,
			&c_src_origin[0],
			&c_dst_origin[0],
			&c_region[0],
			0,
			nil,
			&c_event)
	}

	if event != nil {
		event.cl_event = c_event
	}

	return CL_int(c_errcode_ret)
}
Esempio n. 21
0
func (k *Kernel) ArgTypeName(index int) (string, error) {
	var strC [1024]byte
	var strN C.size_t
	if err := C.clGetKernelArgInfo(k.clKernel, C.cl_uint(index), C.CL_KERNEL_ARG_TYPE_NAME, 1024, unsafe.Pointer(&strC[0]), &strN); err != C.CL_SUCCESS {
		return "", toError(err)
	}
	return string(strC[:strN]), nil
}
Esempio n. 22
0
File: image.go Progetto: xfong/tmpCL
func (d ImageDescription) toCl() C.cl_image_desc {
	var desc C.cl_image_desc
	desc.image_type = C.cl_mem_object_type(d.Type)
	desc.image_width = C.size_t(d.Width)
	desc.image_height = C.size_t(d.Height)
	desc.image_depth = C.size_t(d.Depth)
	desc.image_array_size = C.size_t(d.ArraySize)
	desc.image_row_pitch = C.size_t(d.RowPitch)
	desc.image_slice_pitch = C.size_t(d.SlicePitch)
	desc.num_mip_levels = C.cl_uint(d.NumMipLevels)
	desc.num_samples = C.cl_uint(d.NumSamples)
	desc.buffer = nil
	if d.Buffer != nil {
		desc.buffer = d.Buffer.clMem
	}
	return desc
}
Esempio n. 23
0
File: package.go Progetto: mantyr/cl
// see https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetSupportedImageFormats.html
func GetSupportedImageFormats(context Context, flags MemFlags, memObjectType MemObjectType, numEntries uint32, imageformats *ImageFormat, numImageFormat *uint32) ErrorCode {
	return ErrorCode(C.clGetSupportedImageFormats(context.clContext,
		C.cl_mem_flags(flags),
		C.cl_mem_object_type(memObjectType),
		C.cl_uint(numEntries),
		(*C.cl_image_format)(unsafe.Pointer(imageformats)),
		(*C.cl_uint)(numImageFormat)))
}
Esempio n. 24
0
File: svm20.go Progetto: xfong/gocl
func CLSVMAlloc(context CL_context,
	flags CL_mem_flags,
	size CL_size_t,
	alignment CL_uint) unsafe.Pointer {
	return unsafe.Pointer(C.clSVMAlloc(context.cl_context,
		C.cl_svm_mem_flags(flags),
		C.size_t(size),
		C.cl_uint(alignment)))
}
Esempio n. 25
0
func CLSetKernelArg(kernel CL_kernel,
	arg_index CL_uint,
	arg_size CL_size_t,
	arg_value unsafe.Pointer) CL_int {
	return CL_int(C.clSetKernelArg(kernel.cl_kernel,
		C.cl_uint(arg_index),
		C.size_t(arg_size),
		arg_value))
}
Esempio n. 26
0
File: package.go Progetto: mantyr/cl
// see https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateProgramWithBinary.html
func CreateProgramWithBinary(context Context, numDevices uint32, devices *DeviceId, lengths *uint64, binaries **uint8, binaryStatus *int32, errcode *ErrorCode) Program {
	return Program(C.clCreateProgramWithBinary(context.clContext,
		C.cl_uint(numDevices),
		(*C.cl_device_id)(unsafe.Pointer(devices)),
		(*C.size_t)(unsafe.Pointer(lengths)),
		(**C.uchar)(unsafe.Pointer(binaries)),
		(*C.cl_int)(unsafe.Pointer(binaryStatus)),
		(*C.cl_int)(unsafe.Pointer(errcode))))
}
Esempio n. 27
0
File: image.go Progetto: xfong/gocl
func CLGetSupportedImageFormats(context CL_context,
	flags CL_mem_flags,
	image_type CL_mem_object_type,
	num_entries CL_uint,
	image_formats []CL_image_format,
	num_image_formats *CL_uint) CL_int {

	if (num_entries == 0 || image_formats == nil) && num_image_formats == nil {
		return CL_INVALID_VALUE
	} else {
		var c_num_image_formats C.cl_uint
		var c_errcode_ret C.cl_int

		if num_entries == 0 || image_formats == nil {
			c_errcode_ret = C.clGetSupportedImageFormats(context.cl_context,
				C.cl_mem_flags(flags),
				C.cl_mem_object_type(image_type),
				C.cl_uint(num_entries),
				nil,
				&c_num_image_formats)
		} else {
			c_image_formats := make([]C.cl_image_format, len(image_formats))
			c_errcode_ret = C.clGetSupportedImageFormats(context.cl_context,
				C.cl_mem_flags(flags),
				C.cl_mem_object_type(image_type),
				C.cl_uint(num_entries),
				&c_image_formats[0],
				&c_num_image_formats)
			if c_errcode_ret == C.CL_SUCCESS {
				for i := 0; i < len(image_formats); i++ {
					image_formats[i].Image_channel_data_type = CL_channel_type(c_image_formats[i].image_channel_data_type)
					image_formats[i].Image_channel_order = CL_channel_order(c_image_formats[i].image_channel_order)
				}
			}
		}

		if num_image_formats != nil {
			*num_image_formats = CL_uint(c_num_image_formats)
		}

		return CL_int(c_errcode_ret)
	}
}
Esempio n. 28
0
// Enqueue a command to migrate memory objects into a command queue without their content
func (q *CommandQueue) EnqueueMigrateMemObjectsIntoQueue(memObjs []*MemObject, eventWaitList []*Event) (*Event, error) {
	ObjCount := len(memObjs)
	mem_obj_list := make([]C.cl_mem, ObjCount)
	defer C.free(mem_obj_list)
	for idx, obj := range memObjs {
		mem_obj_list[idx] = obj.clMem
	}
	var event C.cl_event
	err := C.clEnqueueMigrateMemObjects(q.clQueue, C.cl_uint(ObjCount), &mem_obj_list[0], C.CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED, C.cl_uint(len(eventWaitList)), eventListPtr(eventWaitList), &event)
	return newEvent(event), toError(err)
}
Esempio n. 29
0
File: package.go Progetto: mantyr/cl
// see https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clBuildProgram.html
func BuildProgram(prog Program, numDevices uint32, devices *DeviceId, options *uint8, notify func(Program, interface{}), userdata interface{}) ErrorCode {
	var f *[0]byte
	var u unsafe.Pointer
	if notify != nil {
		pobch := programCallbackHolder{notify, userdata}
		pochHolder[&pobch] = struct{}{}
		f = (*[0]byte)(C.programCallback)
		u = unsafe.Pointer(&pobch)
	}
	return ErrorCode(C.clBuildProgram(prog, C.cl_uint(numDevices), (*C.cl_device_id)(unsafe.Pointer(devices)), (*C.char)(unsafe.Pointer(options)), f, u))
}
Esempio n. 30
0
func CLEnqueueFillBuffer(command_queue CL_command_queue,
	buffer CL_mem,
	pattern unsafe.Pointer,
	pattern_size CL_size_t,
	offset CL_size_t,
	cb CL_size_t,
	num_events_in_wait_list CL_uint,
	event_wait_list []CL_event,
	event *CL_event) CL_int {

	if (num_events_in_wait_list == 0 && event_wait_list != nil) ||
		(num_events_in_wait_list != 0 && event_wait_list == nil) ||
		int(num_events_in_wait_list) != len(event_wait_list) {
		return CL_INVALID_EVENT_WAIT_LIST
	}

	var c_event C.cl_event
	var c_errcode_ret C.cl_int

	if num_events_in_wait_list != 0 {
		var c_event_wait_list []C.cl_event
		c_event_wait_list = make([]C.cl_event, num_events_in_wait_list)
		for i := 0; i < int(num_events_in_wait_list); i++ {
			c_event_wait_list[i] = event_wait_list[i].cl_event
		}

		c_errcode_ret = C.clEnqueueFillBuffer(command_queue.cl_command_queue,
			buffer.cl_mem,
			pattern,
			C.size_t(pattern_size),
			C.size_t(offset),
			C.size_t(cb),
			C.cl_uint(num_events_in_wait_list),
			&c_event_wait_list[0],
			&c_event)
	} else {
		c_errcode_ret = C.clEnqueueFillBuffer(command_queue.cl_command_queue,
			buffer.cl_mem,
			pattern,
			C.size_t(pattern_size),
			C.size_t(offset),
			C.size_t(cb),
			0,
			nil,
			&c_event)
	}

	if event != nil {
		event.cl_event = c_event
	}

	return CL_int(c_errcode_ret)
}