Swift 2 OpenCL "cannot invoke" type problem

Hi Everyone,


I just tried to post this, but the cancel button is right below the tags control, and doesn't have a confirmation action, so I think I lost it. If this is a double post due to some kind of moderation delay apologies in advance...


Anyway, on to the issue at hand. I have some Swift 1.2 code for OpenCL that I'm working on porting to Swift 2, and I'm having some trouble with the type system. The way that OpenCL is bridged into swift relies heavily on typedefs, and there are a TON!! of them. I had to perform some gymnastics in 1.2 to convince the type system that my argument is just the right kind of UInt32 (or is it cl_device_info?), but now those aren't enough in 2.


A short sample of (some of!) the suspect code follows. There are about 50 instances of this kind of error in the project:


    private class func deviceInfoString(deviceID: cl_device_id, param paramIn: Int32) throws -> String {
        let param = cl_device_info(paramIn)
        var returnBufferSize: Int = 0
        var retval: cl_int = CL_SUCCESS

        retval = clGetDeviceInfo(deviceID, paramIn, 0, nil, &returnBufferSize)
        if retval != CL_SUCCESS {
            throw OSUOpenCLError.deviceStringQueryError(optional: SwiftOpenCL.printCLError(retval))
        }

        var buffer: [CChar] = Array<CChar>(count: Int(returnBufferSize),
            repeatedValue: CChar(0x00))
        retval = clGetDeviceInfo(deviceID, paramIn, returnBufferSize, &buffer, nil)

        if retval != CL_SUCCESS {
            throw OSUOpenCLError.deviceStringQueryError(optional: SwiftOpenCL.printCLError(retval))
        }

        if let str = String(UTF8String: buffer) {
            return str
        } else {
            throw OSUOpenCLError.deviceStringQueryError(optional: "Unable to convert byte buffer into string")
        }
    }


The input argument param/paramIn is because all the OpenCL CL_DEVICE_NAME constants are defined as Int32's, and rather than cast them everywhere into cl_device_info, I do it once, here. The error I'm getting is:


/Users/wdillon/Documents/Source Code/Thesis/OSUOpenCL/OSUOpenCL/OSUOpenCLDevice.swift:79:18: error: cannot invoke 'clGetDeviceInfo' with an argument list of type '(cl_device_id, Int32, Int, nil, inout Int)'
        retval = clGetDeviceInfo(deviceID, paramIn, 0, nil, &returnBufferSize)
                 ^
/Users/wdillon/Documents/Source Code/Thesis/OSUOpenCL/OSUOpenCL/OSUOpenCLDevice.swift:79:33: note: expected an argument list of type '(cl_device_id, cl_device_info, Int, UnsafeMutablePointer<Void>, UnsafeMutablePointer<Int>)'
        retval = clGetDeviceInfo(deviceID, paramIn, 0, nil, &returnBufferSize)
                                ^


To condense that to just the type signatures:


clGetDeviceInfo(cl_device_id, Int32, Int, nil, inout Int)
clGetDeviceInfo(cl_device_id, cl_device_info, Int, UnsafeMutablePointer<Void>, UnsafeMutablePointer<Int>)


I don't believe that problem is related to the pointers, because I've massaged them a fair amount to no avail.


Does anyone have any ideas??

Thanks in advance!

Answered by Monicker in 9961022

Interesting, the rest of your question showed up, I think I see your problem.


You are doing the cast as in my first suggestion, but you are passing the non-casted parameter (paramIn) to the function instead of the new casted version (param)

I'm not sure what sort of answer you are looking for, it seems your question was cut short.


There are a couple of things you can do:


Cast input params to the correct type in your preamble


    let devInfo = cl_device_info(paramIn)

    retval = clGetDeviceInfo(deviceID, devInfo, 0, nil, &returnBufferSize)


or override the function proto and cast there


func clGetDeviceInfo(devid: cl_device_id, _ devinfo: Int32, _ bufSize: Int, _ buff: UnsafeMutablePointer<Void>, _ otherThing: UnsafeMutablePointer<Int>) -> cl_int {
    return clGetDeviceInfo(devid, cl_device_info(devinfo), bufSize, buff, otherThing)
}
Accepted Answer

Interesting, the rest of your question showed up, I think I see your problem.


You are doing the cast as in my first suggestion, but you are passing the non-casted parameter (paramIn) to the function instead of the new casted version (param)

Yah, it was cut out. For some reason the forum software decided I was finished and posted on its own.


Thanks for looking at it anyway, though! I'll try it out.

Good eyes.


I'll crawl back into my hole now. 😉

Swift 2 OpenCL "cannot invoke" type problem
 
 
Q