9 Replies Latest reply on Apr 21, 2011 12:35 PM by spectral

    Align union ?

    spectral

      Hi,

      I'm facing a problem, I have the following structure :
      typedef struct __attribute__((packed))
      {
      float Ks;
      clSpectrum Cs;
      float exponent;
      } ShaderParam_plastic;
      But when I add 2 variables like this :
      typedef struct __attribute__((packed))
      {
      float Kd;
      clSpectrum Cd;
      float Ks;
      clSpectrum Cs;
      float exponent;
      } ShaderParam_plastic;
      I got a crash ! I have checked for the data I send and they are aligned correctly !
      In fact I use this structure :
      typedef struct __attribute__((packed))
      {
      unsigned int shaderId;
      union __attribute__((packed))
      {
      ShaderParam_emitter emitter;
      ShaderParam_glass glass;
      ShaderParam_matte matte;
      ShaderParam_mirror mirror;
      ShaderParam_plastic plastic;
      ShaderParam_Textured_emitter Textured_emitter;
      ShaderParam_Textured_glass Textured_glass;
      ShaderParam_Textured_matte Textured_matte;
      ShaderParam_Textured_mirror Textured_mirror;
      ShaderParam_transmissive transmissive;
      } params;
      } ShaderParameters;
      Even my union is aligned !!! If the syntax is correct ?
      If someone has an idea ?
      Thanks


        • Align union ?
          himanshu.gautam

          Can you please post your system configuration: CPU,GPU,SDK,DRIVER,OS.

          Also posting a nice testcase which reproduces the crash would be helpful.

          Please refer to section 6.10.1 of OpenCL Spec for confirming any syntax issues you might be having.

            • Align union ?
              spectral

              Thanks Himanshu,

              Yes, I have refer to section 6.10.1 already :-P and finally what I understand is that I have to also add "__attribute__((packed))" to my union.

              Does the syntax is correct ? I want a structure that is fully packed as a normal C++ structure !

              But, unfortunately it does not work ! I have checked the alignment with and it sounds correct :-P

               

               

              NB: Also, have you been able to debug the "software" I've sent you ? The one that crash with SDK 2.4 ?

                • Align union ?
                  spectral

                  I work under Win7 64 bits, on a Intel CPU !

                    • Align union ?
                      himanshu.gautam

                      Please post some code to show the crash or data corruption.

                       

                        • Align union ?
                          Jawed

                          Suggest you try "aligned", not "packed".

                            • Align union ?
                              moritz14

                              Maybe your computer is running on a 64-bit CPU. Mine is 32-bit 

                                • Align union ?
                                  spectral

                                  Yes, for now I use a 64 bits computer.

                                  But alignment should be the same ?

                                    • Align union ?
                                      spectral

                                      In the OpenCL spec. page 199, they wrote this :

                                      "You may only specify this attribute on the definition of a enum, struct or

                                       

                                      union, not on a typedef which does not also define the enumerated type,

                                      structure or union."



                                       

                                      Does it mean that I cannot pack a "typedef" ? or that I can write :

                                          typedef struct __attribute__((packed)) MyStruct {...};

                                      But not ?

                                          typedef __attribute__((packed)) struct MyStruct {...};

                                      NB: I have replace my typedef with a struct, but it still crash !!!

                                      NB2 :  I cannot use "align" because my OpenCL code is generated at run-time. So, it is difficult to do and error prone :-P (Also, my buffer is generated from a malloc, then filled at run-time with memcpy !!)

                                        • Align union ?
                                          spectral

                                          Even with this version... I still got a crash !!!

                                          struct __attribute__((packed)) ShaderParam_transmissive { float Ks __attribute__((packed)); clSpectrum Cs __attribute__((packed)); float eta __attribute__((packed)); }; struct __attribute__((packed)) ShaderParametersStruct { uint shaderId __attribute__((packed)); union __attribute__((packed)) { struct ShaderParam_emitter emitter; struct ShaderParam_glass glass; struct ShaderParam_matte matte; struct ShaderParam_matte_rough matte_rough; struct ShaderParam_mirror mirror; struct ShaderParam_plastic plastic; struct ShaderParam_Textured_emitter Textured_emitter; struct ShaderParam_Textured_glass Textured_glass; struct ShaderParam_Textured_matte Textured_matte; struct ShaderParam_Textured_mirror Textured_mirror; struct ShaderParam_transmissive transmissive; } __attribute__((packed)) params __attribute__((packed)); };