6 Replies Latest reply on Jan 12, 2010 8:39 AM by maelteir

    Unable to implement generic data types

    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.

        • Unable to implement generic data types
          mfried

           

          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.



           



           



           



           

            • Unable to implement generic data types
              maelteir

              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.

            • Unable to implement generic data types
              MicahVillmow
              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;
              }