cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

maelteir
Journeyman III

Unable to implement generic data types

Hi,

I'd like to implement a generic function that can work with any data type; integer, float...

When I passed the input as (__global void*) to the kernel function, I wasn't able to cast it to any other data type. In the runtime, I received "Invalid type conversion".

Any help would be appreciated.

0 Likes
6 Replies
mfried
Adept II

Originally posted by: maelteir Hi,

I'd like to implement a generic function that can work with any data type; integer, float...

When I passed the input as (__global void*) to the kernel function, I wasn't able to cast it to any other data type. In the runtime, I received "Invalid type conversion".

Any help would be appreciated.

Can you give us any more specific information about what you are trying to do and what you want it to look like? For example, maybe what you really want to do is dynamically generate OpenCL kernel source based on the data types you need by concatenating different strings together. So for example, say you had an array of strings...

const char*[] types = { "int", "float", ... };

Then you might make a Program with a list of strings that you concatenate together when you pass them into the clBuildProgramFromSource call, and perhaps use the indices into the types array in the name of your kernel...

That's just a thought.



 



 



 



 

0 Likes

Hi mfried,

Thank you for your suggested solution. I'm not sure about the feasibility of dynamically generating the code for every data type, especially if the main program is complicated.

Here is a specific example for what I want to do. Assume we want to implement a generic sorting program that can sort a list of any data type i.e., int, float, *char... Then the straight forward approach is to define the input as *void and cast to a specific data type only within the compare function as shown below:

int compare(__global void *d_a, __global void *d_b)

{

int key1 = *(int*)d_a;

int key2 = *(int*)d_b;

if (key1 > key2) return 1;

if (key1 < key2) return -1;

return 0;

}

The problem is that I got "Invalid type conversion" error in the first two lines of the function. I consult the Khronos OpenCL specification, they doesn't support type casting from void * to any other data type.

0 Likes

well you can set mem object containt float data as int argument. it will get values without conversion. i think that you can compare float in binary form as int.

0 Likes

Thank you nou,

If this solves the problem for int and float datatypes, then there is still a problem with *char or structure data types?

0 Likes

maelteir, the problem is in your cast, you are attemping to cast from the global address space to the private address space, the following code works.
int compare(__global void *d_a, __global void *d_b)
{
int key1 = *(global int*)d_a;
int key2 = *(global int*)d_b;
if (key1 > key2) return 1;
if (key1 < key2) return -1;
return 0;
}

0 Likes

Thank you MicahVillmow! this solves my problem.

0 Likes