2

I have two related questions here and so I am asking as one question:

1- We compile the opencl-program at run time using

clCreateProgramWithSource(context, 1,  (const char**)&source, NULL, NULL);
clBuildProgram(program, 1, &device, NULL, NULL,
NULL);

My question is every time my opencl application runs it will do this compilation, and it might take considerable time. Is there a way so that the compilation will happen for the first time, and in subsequent application runs, it uses the binary from the previous compilation?

2- What are the different ways to speed up the compilation using clBuildProgram()? may be using compiler flags or something else?

user25108
  • 383
  • 5
  • 15
  • You could split your source file into several files and build them separately. – Michael Dorner Aug 19 '14 at 17:13
  • Do you have any nested loops in your kernel? OpenCL compilers tend to go crazy with trying to unroll deeply nested loops. You may try to restrict unrolling (some info at http://stackoverflow.com/a/23206339/3182664 , but probably also at other places). Until now, this is just a guess, though... – Marco13 Aug 19 '14 at 21:42
  • There will likely be no other techniques besides pre-compiling things: http://stackoverflow.com/questions/23601058/how-to-create-do-offline-compilation-in-opencl-and-create-its-binary – Ciro Santilli OurBigBook.com Apr 09 '17 at 08:05

1 Answers1

4

At the expense of portability, you can use clCreateProgramWithBinary.

To save your compiled OpenCL code to run on the same device, you need to do the following:

  • Compile the code using clCreateProgramWithSource
  • Use clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, //...) to obtain the size of the binary
  • Use clGetProgramInfo(program, CL_PROGRAM_BINARIES, //...) to write the binary to a char buffer.
  • Write the buffer to disk.

Then in future, you can use clCreateProgramWithBinary rather than compile from source.

There's an example of how to do all of this in this code. You can trim it down to suit your needs.

As mentioned in the comments (thanks @Dithermaster) and to reiterate my first point, the compiled binary is very specific to the system upon which it was compiled. If there are any changes to the system a new binary must be compiled.

Community
  • 1
  • 1
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • I want to automate this. so first time I do clCreateProgramWithSource stuffs, and when the program is run again, it will automatically use clCreateProgramWithBinary. so the end customer do not have to look into the code. – user25108 Aug 20 '14 at 04:13
  • You could achieve this in a variety of ways. For example, a simple idea would be to check for the presence of a file and if it is present, then use it. Alternatively, you could use a command line switch to specify the binary input/output. – Tom Fenech Aug 20 '14 at 06:56
  • 1
    You should only use the binary if the platform name, platform version, device name, and device version are all the same. In other words, if the user has updated their driver, you'll want to recompile from source (and then save the new binary for subsequent runs). – Dithermaster Aug 21 '14 at 23:51
  • **Minimal runnable example**: http://stackoverflow.com/questions/7338718/how-to-use-clcreateprogramwithbinary-in-opencl/43292725#43292725 – Ciro Santilli OurBigBook.com Apr 09 '17 at 08:04