コード例 #1
0
ファイル: program.go プロジェクト: aa1214808834/go-opencl-1
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
}
コード例 #2
0
ファイル: program.go プロジェクト: jackscan/go-opencl
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
}
コード例 #3
0
ファイル: package.go プロジェクト: 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))
}
コード例 #4
0
ファイル: program.go プロジェクト: snipsnipsnip/go-opencl
func (p *Program) Build(devices []Device, options string) error {
	cs := C.CString(options)
	defer C.free(unsafe.Pointer(cs))

	if len(devices) < 1 {
		if ret := C.clBuildProgram(p.id, 0, nil, cs, nil, nil); ret != C.CL_SUCCESS {
			return Cl_error(ret)
		}
		return nil
	}

	c_devices := make([]C.cl_device_id, len(devices))
	for i, device := range devices {
		c_devices[i] = device.id
	}
	if ret := C.clBuildProgram(p.id, C.cl_uint(len(c_devices)), &c_devices[0], cs, nil, nil); ret != C.CL_SUCCESS {
		return Cl_error(ret)
	}
	return nil
}
コード例 #5
0
ファイル: program.go プロジェクト: xfong/tmpCL
func (p *Program) BuildProgram(devices []*Device, options string) error {
	var optBuffer bytes.Buffer
	optBuffer.WriteString("-cl-std=CL1.2 -cl-kernel-arg-info ")
	var cOptions *C.char
	if options != "" {
		optBuffer.WriteString(options)
	}
	cOptions = C.CString(optBuffer.String())
	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]
	}
	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 _, 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
}
コード例 #6
0
ファイル: program.go プロジェクト: Dirbaio/gominer
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)
}
コード例 #7
0
ファイル: context.go プロジェクト: salviati/go-opencl
func (c *Context) NewProgramFromSource(prog string) (*Program, error) {
	var c_program C.cl_program
	var err C.cl_int

	cs := C.CString(prog)
	defer C.free(unsafe.Pointer(cs))

	if c_program = C.clCreateProgramWithSource(c.id, 1, &cs, (*C.size_t)(nil), &err); err != C.CL_SUCCESS {
		return nil, Cl_error(err)
	} else if err = C.clBuildProgram(c_program, 0, nil, nil, nil, nil); err != C.CL_SUCCESS {
		C.clReleaseProgram(c_program)
		return nil, Cl_error(err)
	}

	program := &Program{id: c_program}
	runtime.SetFinalizer(program, (*Program).release)

	return program, nil
}