Friday, September 18, 2009

OpenCL Compiler (OCLTools)

Unlike C for CUDA OpenCL does not come with a compiler. More precisely to be compliant with the specification a vendor does not need to supply a compiler. The OpenCL driver has a few functions that are related to compiling kernel source to prepare it for execution on the device. Developers building OpenCL applications must put code into their application to find the kernel source, read it in, and compile it. When the source is compiled it is compiled down to either an intermediate representation (ptx assembly in Nvidia's case) or a device specific executable.

Why would a vendor compile to an intermediate representation instead of a device specific binary? This allows OpenCL vendors a greater degree of flexibility in forward and backward application compatibility as the underlying hardware implementations change.

So what does this lack of compiler mean to you the developer? Well... it depends. Lets say I built a suite of option models on OpenCL for internal use at my firm that offload to Nvidia's GPUs. I definitely do not want to be paying the penalty to read in and compile the source each time I run my model. Since I know I'm going to be running on Nvidia GPUs I'll just compile my kernel down to ptx and release the ptx with my models. Later if we decide to run on Larrabee cards I can just recompile the kernel source for Larrabee and re-release the kernel binary. For this type of work flow it would be nice to have a stand alone OpenCL compiler.

What if my product is video encoding / decoding software? Do I want to have to ship multiple versions of my kernel binary? Do I want my users to have to download a new binary just because they have installed a new OpenCL capable device? Probably not. In this case I would just ship the source for my kernel and let the OpenCL driver take care of compiling it for the right platform. If this is my work flow then I don't really need a compiler.

If you are interested in an offline OpenCL compiler go to ClusterChimps.org. They have a suite of Open Source OpenCL compiler tools called OCLTools that not only support offline compilation but also embedded kernel source and encryption.