cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

thesmith
Adept I

Save binary using C++ wrapper

Jump to solution

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 ;


}



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;



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

Tags (4)
0 Likes
1 Solution

Accepted Solutions
kd2
Adept II

Re: Save binary using C++ wrapper

Jump to solution

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

View solution in original post

0 Likes
5 Replies
realhet
Miniboss

Re: Save binary using C++ wrapper

Jump to solution

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.

0 Likes
thesmith
Adept I

Re: Save binary using C++ wrapper

Jump to solution

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

0 Likes
realhet
Miniboss

Re: Save binary using C++ wrapper

Jump to solution

Oups, I thought you're using the GPU. On GPU it produces an .isa file which runs well if you load it as binary. But unfortunately I don't know how it works on CPU.

0 Likes
kd2
Adept II

Re: Save binary using C++ wrapper

Jump to solution

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

View solution in original post

0 Likes
thesmith
Adept I

Re: Re: Save binary using C++ wrapper

Jump to solution

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


  1. // Allocate some memory for all the kernel binary data

  2. const std::vector<unsigned long> binSizes = program.getInfo<CL_PROGRAM_BINARY_SIZES>(); 

  3. std::vector<char> binData (std::accumulate(binSizes.begin(),binSizes.end(),0)); 

  4. char* binChunk = &binData[0] ; 

  5.  

  6.  

  7. //A list of pointers to the binary data 

  8. std::vector<char*> binaries; 

  9. for(unsigned int i = 0; i<binSizes.size(); ++i) { 

  10.      binaries.push_back(binChunk) ; 

  11.      binChunk += binSizes


  12.  

  13. program.getInfo(CL_PROGRAM_BINARIES , &binaries[0] ) ; 

  14. std::ofstream binaryfile("kernel.bin", std::ios::binary); 

  15. for (unsigned int i = 0; i < binaries.size(); ++i) 

  16.      binaryfile.write(binaries, binSizes);

Thanks a bunch for your help.

0 Likes