8 Replies Latest reply on Aug 22, 2013 3:48 PM by thesmith

    Save binary using C++ wrapper

    thesmith

      Hi everyone,

       

      I have a small question which should be simple enough for you. I am trying to save a binary to disk in order to load if later on. I am able to do it using C, but using the C++ wrapper, I can't seem to get it to work. Here is the relevant code, originally taken from here http://www.mss.cbi.uni-erlangen.de/KkuDatabase/files/files/316/main.pdf :

       

      // Allocate some memory for all the kernel binary data
      const std::vector<unsigned long> binSizes = program.getInfo<CL_PROGRAM_BINARY_SIZES>();
      std::vector<char> binData (std::accumulate(binSizes.begin(),binSizes.end(),0));
      char* binChunk = &binData[0] ;
      
      
      //A list of pointers to the binary data
      std::vector<char*> binaries;
      for(unsigned int i = 0; i<binSizes.size(); ++i) {
           binaries.push_back(binChunk) ;
           binChunk += binSizes[i] ;
      }
      
      program.getInfo(CL_PROGRAM_BINARIES , &binaries[0] ) ;
      std::ofstream binaryfile("kernel.bin", std::ios::binary);
      for (unsigned int i = 0; i < binaries.size(); ++i)
           binaryfile << binaries[i];
      
      

       

      The only thing I get in my "kernel.bin" file is the word "ELF", surrounded by some other bytes that aren't caracters. There are 7 bytes in all. So what's wrong here? It seems to fail at the last step, getting the actual binary, because I do get a binary size (28496 for my particular kernel).

       

      Edit: I forgot to say that I'm using AMDAPP 2.8.1, and the OpenCL device is a CPU, an Intel i7-2600.

       

      Thanks for the help,

       

      Carl

        • Re: Save binary using C++ wrapper
          realhet

          Hi!

           

          Try the '-save-temps' opencl compiler directive! Perharps getInfo(CL_PROGRAM_BINARIES is broken.

           

          For example -save-temps=c:\aaa will dump temporary files into the C root with filenames starting with aaa.

            • Re: Save binary using C++ wrapper
              thesmith

              Hi,

               

              This is interesting. Doing so, it dumps a few files, two of which I can load using "Program::Binaries" and then run "Program(context, devices, binaries)". They are the "kern_0_Corei7_AVX.so" and "kern_0_Corei7_AVX.o". Are these the right files? Any other file returns error -42, CL_INVALID_BINARY.

               

              However, it then segfaults during the clBuildProgram, called by "program.build(devices, options)".

               

              By the way, by now you might have guessed I'm on Ubuntu, not Windows.

               

              Thanks,

               

              Carl

            • Re: Save binary using C++ wrapper
              kd2

              the operator << is assuming null-terminated string since you're not supplying the length to it. Use write().

                • Re: Re: Save binary using C++ wrapper
                  thesmith

                  Oh yes that was it. So, for future references, the code becomes:

                   

                  // Allocate some memory for all the kernel binary data 
                  const std::vector<unsigned long> binSizes = program.getInfo<CL_PROGRAM_BINARY_SIZES>();  
                  std::vector<char> binData (std::accumulate(binSizes.begin(),binSizes.end(),0));  
                  char* binChunk = &binData[0] ;  
                    
                    
                  //A list of pointers to the binary data  
                  std::vector<char*> binaries;  
                  for(unsigned int i = 0; i<binSizes.size(); ++i) {  
                       binaries.push_back(binChunk) ;  
                       binChunk += binSizes[i] ;  
                  }  
                    
                  program.getInfo(CL_PROGRAM_BINARIES , &binaries[0] ) ;  
                  std::ofstream binaryfile("kernel.bin", std::ios::binary);  
                  for (unsigned int i = 0; i < binaries.size(); ++i)  
                       binaryfile.write(binaries[i], binSizes[i]);
                  
                  

                   

                  Thanks a bunch for your help.