6 Replies Latest reply on Apr 2, 2012 3:21 AM by rick.weber

    Compiler crashes with large number of -D arguments

    arsenm

      I've run into a compiler crash when using long strings of defines for the compiler. It happens if I have a large number of -D arguments that do not have a space between the -D and the symbol. I thought to try this since a long time ago there was a bug in the Apple compiler where -DD_I_BEGIN_WITH_D would lose the leading D so only _I_BEGIN_WITH_D would be defined.

       

      If I have at least 40 -D arguments, it crashes. If I start removing the space from some of them, it does not. I'm not quite sure what the relation is between the missing space and the number, but the problem start happening around 40 arguments and changes depending on whether the space is there or not for some of the arguments. The length of the definitions seems to not matter, only the number.

       

      The code that ran into this worked a few months ago with previous releases. I have tested this on Linux x86_64 and Windows 7 64. This is with Catalyst 12.3 on both.

       

       

       

      const char* sampleSrc = "#warning I compile\n";
      
          // crashes if I have 40 or more
      const char* sampleFlags =
              "-D D1=1 -D D2=1 -D D3=1 -D D4=1 -D D5=1 -D D6=1 -D D7=0 -D D8=0 -D D9=0 -D D10=0 "
              "-D D11=0 -D D12=0 -D D13=0 -D D14=0 -D D15=0 -D D16=0 -D D17=0 -D D18=0 -D D19=0 -D D20=0 "
              "-D D21=0 -D D22=0 -D D23=0 -D D24=0 -D D25=0 -D D26=0 -D D27=0 -D D28=0 -D D29=0 -D D30=0 "
              "-D D31=0 -D D32=0 -D D33=0 -D D34=0 -D D35=0 -D D36=0 -D D37=0 -D D38=0 -D D39=0 -D D40=1";
      
      // if I remove the space between the -D and the value, it works.
      // It also seems to work if you remove it from a small number of them also
      const char* sampleFlagsNoSpace = 
              "-DD1=1 -DD2=1 -DD3=1 -DD4=1 -DD5=1 -DD6=1 -DD7=0 -DD8=0 -DD9=0 -DD10=0 "
              "-DD11=0 -DD12=0 -DD13=0 -DD14=0 -DD15=0 -DD16=0 -DD17=0 -DD18=0 -DD19=0 -DD20=0 "
              "-DD21=0 -DD22=0 -DD23=0 -DD24=0 -DD25=0 -DD26=0 -DD27=0 -DD28=0 -DD29=0 -DD30=0 "
              "-DD31=0 -DD32=0 -DD33=0 -DD34=0 -DD35=0 -DD36=0 -DD37=0 -DD38=0 -DD39=0 -DD40=1";
      
      
      clCreateProgramWithSource(context, 1, &sampleSrc, NULL, sampleFlags);
      // crashes
      
      
      
        • Re: Compiler crashes with large number of -D arguments
          Wenju

          Hi arsenm, I don't very clear about your question,But I think you use a wrong parameter for clCreateProgramWithSource.

           

          The function

           

            cl_program  clCreateProgramWithSource (cl_context context,

                                       cl_uint count, 

                                       const char **strings, 

                                       const size_t *lengths,

                   cl_int *errcode_ret)

           

          creates a program object for a context, and loads the source code specified by the text strings in

          the strings array into the program object.  The devices associated with the program object are the

          devices associated with context.

           

          context must be a valid OpenCL context.

           

          strings is an array of count pointers to optionally null-terminated character strings that make up

          the source code.  

          The lengths argument is an array with the number of chars in each string (the string length).  If

          an element in lengths is zero, its accompanying string is null-terminated.  If lengths is NULL, all

          strings in the strings argument are considered null-terminated. Any length value passed in that is

          greater than zero excludes the null terminator in its count.

           

          errcode_ret will return an appropriate error code.  If errcode_ret is NULL, no error code is

          returned. 

           

          clCreateProgramWithSource returns a valid non-zero program object and errcode_ret is set to

          CL_SUCCESS if the program object is created successfully.  Otherwise, it returns a NULL value

          with one of the following error values returned in errcode_ret:

           

            CL_INVALID_CONTEXT if context is not a valid context.

           

            CL_INVALID_VALUE if count is zero or if strings or any entry in strings is NULL.

           

            CL_OUT_OF_RESOURCES if there is a failure to allocate resources required by the

          OpenCL implementation on the device.

           

            CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required by the

          OpenCL implementation on the host.

            • Re: Compiler crashes with large number of -D arguments
              arsenm

              That isn't the actual call. It's an example.

                • Re: Compiler crashes with large number of -D arguments
                  Wenju

                  Can you give more details about your example. so we can confirm it.

                  • Re: Compiler crashes with large number of -D arguments
                    Wenju

                    Maybe , you want use the  "clBuildProgram" or "clCompileProgram" , not the clCreateProgramWithSource", 

                    "clCreateProgramWithSource" have no any option..


                      • Re: Compiler crashes with large number of -D arguments
                        arsenm

                        Here is a complete test case. The first set of flags crashes the compiler.

                         

                        #include <CL/opencl.h>
                        #include <stdlib.h>
                        #include <stdio.h>
                        
                        
                        int main (int argc, const char* argv[])
                        {
                            cl_platform_id platform;
                                  cl_device_id device;
                                  cl_context context;
                                  cl_program program;
                        
                        
                                  const char* sampleSrc = "#warning I compile\n";
                        
                        #if 1
                            const char* flags =  
                                "-D D1=1 -D D2=1 -D D3=1 -D D4=1 -D D5=1 -D D6=1 -D D7=0 -D D8=0 -D D9=0 -D D10=0 "  
                                "-D D11=0 -D D12=0 -D D13=0 -D D14=0 -D D15=0 -D D16=0 -D D17=0 -D D18=0 -D D19=0 -D D20=0 "  
                                "-D D21=0 -D D22=0 -D D23=0 -D D24=0 -D D25=0 -D D26=0 -D D27=0 -D D28=0 -D D29=0 -D D30=0 "  
                                "-D D31=0 -D D32=0 -D D33=0 -D D34=0 -D D35=0 -D D36=0 -D D37=0 -D D38=0 -D D39=0 -D D40=1"; 
                        #else
                            const char* flags =   
                                "-DD1=1 -DD2=1 -DD3=1 -DD4=1 -DD5=1 -DD6=1 -DD7=0 -DD8=0 -DD9=0 -DD10=0 "  
                                "-DD11=0 -DD12=0 -DD13=0 -DD14=0 -DD15=0 -DD16=0 -DD17=0 -DD18=0 -DD19=0 -DD20=0 "  
                                "-DD21=0 -DD22=0 -DD23=0 -DD24=0 -DD25=0 -DD26=0 -DD27=0 -DD28=0 -DD29=0 -DD30=0 "  
                                "-DD31=0 -DD32=0 -DD33=0 -DD34=0 -DD35=0 -DD36=0 -DD37=0 -DD38=0 -DD39=0 -DD40=1";  
                        #endif
                        
                        
                            clGetPlatformIDs(1, &platform, NULL);
                        
                        
                                  clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT, 1, &device, NULL);
                                  context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
                        
                        
                                  program = clCreateProgramWithSource(context, 1, &sampleSrc, NULL, NULL);
                                  clBuildProgram(program, 0, NULL, flags, NULL, NULL);
                        
                        
                            return 0;
                        }
                        
                        
                  • Re: Compiler crashes with large number of -D arguments
                    rick.weber

                    As a workaround, when you run the program, emit a file called defines.cl that just contains

                     

                    #define D1 1

                    #define D2 1

                    ...

                    etc

                     

                    Then in your kernel source code, #include<defines.cl> and pass -I. to clBuildProgram.

                     

                    I use this technique all the time for dynamic code generation and is a little easier than concatenating string with all the parameters you want.