4

In online compilation of OpenCl, we have to do...

program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &ret);

But, for offline creation of program for opencl..

program = clCreateProgramWithBinary(context, 1, &device_id, (const size_t *)&binary_size, (const unsigned char **)&binary_buf, &binary_status, &ret);

where binary_buf is...

fread(binary_buf, 1, MAX_BINARY_SIZE, fp);

Hence in offline compilation, we can skip the clBuildProgram step, which makes this step faster. (Is this approach correct, that we can re-use again and again that binary for running the program?)

So, my question is how to create opencl binary file so i can skip the step of building cl program?

Vishwadeep Singh
  • 1,043
  • 1
  • 13
  • 38
  • you still have to call `clBuildProgram()`, even with programs creates with `clCreateProgramWithBinary()`. – Kyle Lutz May 12 '14 at 05:59
  • @KyleLutz my clBuildProgram is taking lot time because my cl file is big.. to skip this compilation i thought of offline compilation?? is there any other way to remove this reinvoking of clBuildProgram everytime i invoke the program?? – Vishwadeep Singh May 12 '14 at 07:05

1 Answers1

6

Once the program has been created you can use clGetProgramInfo with CL_PROGRAM_BINARY_SIZES and then CL_PROGRAM_BINARIES, storing the resulting binary programs (one for each device of the context) into a buffer you supply. You can then save this binary data to disk for use in later runs.

Not all devices might support binaries, so you will need to check the CL_PROGRAM_BINARY_SIZES result (it returns a zero size for that device if binaries are not supported).

To save time in the future (say in future runs of your application), you can use clCreateProgramWithBinary with the returned binaries. However, you will only ever want to do this with exactly the same hardware. Even when the graphics driver changes for the same hardware, you might want to throw the binary away and rebuild since there is potential the OpenCL compiler in the new driver has bugfixes and/or performance improvements.

prunge
  • 22,460
  • 3
  • 73
  • 80
  • 1
    Thanks @prunge But is it true that instead of creating binaries with clCreateProgramWithBinary, still i need to call clBuildProgram...?? Because my program is of very big size (~10-12MB) which is threaded across 32 Workgroups. Everytime clBuildProgram is taking lot of time for compilation.. if after using clCreateProgramWithBinary, still i need to do clBuildProgram then how can i skip that? – Vishwadeep Singh May 12 '14 at 07:04
  • @Vishwadeep if you call `clBuildProgram` with a `cl_program` built from binary, it should decrease the amount of build time taken because the binary does not need to be recompiled. What is the difference in time between using clBuildProgram with source versus clBuildProgram with ready-made binaries? – prunge May 12 '14 at 07:12
  • Thanks for the help @prunge i will get back to you with stats. – Vishwadeep Singh May 12 '14 at 07:25
  • **Minimal runnable example**: http://stackoverflow.com/questions/7338718/how-to-use-clcreateprogramwithbinary-in-opencl/43292725#43292725 – Ciro Santilli OurBigBook.com Apr 09 '17 at 07:59