OpenCL calls not correctly transformed from Java to Kotlin
I have a piece of Java Code based on the OpenCL library with a plan to leverage the general computation capacity of the graphic processor (GPU). It works (as demonstrated by the successful console printout of the device name demonstrates). After transformation to Kotlin it does not work any more. The device list is empty. It has something to do with the way the parameter to the devices array is given to function clGetContextInfo. Does anybody have an idea why it does not work (or alternatively a piece of working Kotlin code for that)?
Find subsequently first the Java Code and than the Kotlin code (aquired by simply copy pasting from the Java code to a Kotlin file using the automatic conversion). I am using IntelliJ Idea with the Kotlin plugin of course.
Please sign in to leave a comment.
Is it possible to share sample project and steps to reproduce?
Hi Annikov. I made it working by offering just a pointer to one cl_device_id object. However, that is not the idea. It should be an array of of cl_device_id to allow the function to return more than one device.
I have a zip of the program available. How can I bring it to you? It appears I can attach here only images.
You can use JetBrains file share:
https://intellij-support.jetbrains.com/hc/en-us/articles/206869619-Uploading-Large-Files-for-JetBrains-Support-Team
I have uploaded the zip as "TestOpenCL 2020-06-17.zip".
Kotlin always makes a copy of `vararg` arguments for safety, so `devices` array will not have its entries initialized after the call to `clGetContextInfo` - they will still be `null`.
As a workaround, please contain all such client code to Java files.
Filed https://youtrack.jetbrains.com/issue/KT-39723 for reference.
OK. If you happen to know that it is the first device you are looking for, my work around helps too, but it is of course not a "sound" solution to the problem.
I thought of that containment work around too, of course. Could it be that there are other functions in the JOCL library that exhibit the same issue? It would be - in the long run - a bit unsatisfacory for the KOTLIN fans, if there is a type of interface that Kotlin doesn't support without a Java wrapper to translate. Does the fact that you registered it as a Issue KT-39723 that there might be an answer to that challenge in the future?
The fact that Java allows you to mutate a `varargs` array is arguably a bug in the language design and abuse of `varargs` feature. I doubt that Kotlin will change its behavior here, because it will be a breaking change.
I think the right way to go is to modify JOCL to expose a different static factory method on `Pointer` that takes an array of devices, not `varargs` devices. In that case the array argument will not be copied and everything will work.
Thanks for the clarification and your investigation efforts. I think we can consider this post here as closed now.