Results 1 to 5 of 5

Thread: user type struct

  1. #1

    user type struct

    I have defined a new type as struct in a separate file and the file is included for further reference. Then i am sending the use-defined type as kernel arguments , but i am getting the error:

    Code :
    --- build log ---
    :6:42: error: unknown type name 'SimParams'
                                  __constant SimParams* params,

    Any hint to get around this issue ?


    Thanks

  2. #2
    Senior Member
    Join Date
    Dec 2011
    Posts
    170
    The OpenCL C compiler has no access to your struct definition. While it does have some #include capabilities, I'd start by just repeating your struct definition inside the kernel source. Also, you have to be super careful about element alignment when using structs, because they have to line up between your host and your compute device. If possible, stick with basic parameter types and vector parameter types if you can before moving to using a struct.

  3. #3
    Hi

    I am doing it as follows:

    Code :
     
    typedef struct SimParams
    {
       //compute::float4_  gravity;
       cl_float4 gravity;
     
       float globalDamping;
       float noiseFreq;
       float noiseAmp;
     
       //compute::float4_ cursorPos;
       cl_float4 cursorPos;
     
       float time;
     
       //compute::float4_ noiseSpeed;
       cl_float4 noiseSpeed;
    } SimParams;

    The above is declared in a separate file. I declare an object of the above type , initialize some values, create a device buffer of the above type and then send the struct type as kernel arguments. Then again i am defining the following inside the kernel source:


    Code :
       const char source[] =
          "typedef struct SimParams                                                       \n"
          "{                                                                              \n"
          "  float4  gravity;                                                             \n"
          "  float globalDamping;                                                         \n"
          "  float noiseFreq;                                                             \n"
          "  float noiseAmp;                                                              \n"
     
          "  float4 cursorPos;                                                            \n"
     
          "  float time;                                                                  \n"
          "  float4 noiseSpeed;                                                           \n"
          "} SimParams; 
    ....................
    ...................


    Is that what you meant? Now it not complaining.

    Is this the only way to get it done. A bit complex , dont you think ?

    Thanks

  4. #4
    Senior Member
    Join Date
    Dec 2011
    Posts
    170
    Yes, that's what I meant. Perhaps more complex that you'd like, but using #include files in OpenCL can be equally tricky.

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    Vancouver, Canada
    Posts
    66
    Quote Originally Posted by Dithermaster View Post
    Yes, that's what I meant. Perhaps more complex that you'd like, but using #include files in OpenCL can be equally tricky.
    One approach that I have used is to create a #include file that has just my types in it and is included from the host. Then this header and the .cl source file(s) are passed to clCreateProgramFromSource as multiple strings (or concatenated into one string). That way you don't need to mess with #include in CL source, but you get the same effect. The downside is that you then need to package the header and .cl files with your application, but each OS usually has a way to do this sort of resource packaging.

    The common source approach does suffer from the problem that the types in the host API and the kernel language differ, so you may need to introduce some typedef'ing to get things to work. Also, exercise caution around struct alignment and packing -- the OpenCL spec doesn't contain explicit guarantees about how structs are laid out.

    One last note, don't try to pass structs to your kernels by value. Put them in buffer objects and pass those to the kernels as typed pointers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •