I am a newbie in OpenCL stuffs.
Whats is the best way to compiler an OpenCL project ?
Using a supported compiler (
GCC
orClang
):When we use a compiler like
gcc
orclang
, how do we control these options? Are they have to be set inside the source code, or, likewise the normal compilation flow we can pass them on the command line. Looking at the Khornos-Manual-1.2, there are a fewoptions
provided forcl_int clBuildProgram
for optimizations. :gcc|clang -O3 -I<INCLUDES> OpenCL_app.c -framework OpenCL OPTION -lm
Actually, I Tried this and received an error :
gcc: error: unrecognized command line option '<OPTION>'
Alternatively, using
openclc
:I have seen people using
openclc
to compiler using a Makefile.
I would like to know which is the best way (if there are actually two separate ways), and how do we control the usage of different compile time options.
You might be aware but it is important to reiterate. OpenCL standard contains two things:
- OpenCL C language and programming model (I think recent standard include some C++)
- OpenCL host library to manage device
gcc
and clang
are compilers for the host side of your OpenCL project. So there will be no way to provide compiler options for OpenCL device code compilations using a host compiler since they are not even aware of any OpenCL.
Except with clang
there is a flag that accept OpenCL device code, .cl file which contains the kernels. That way you can use clang
and provide also the flags and options if I remember correctly, but now you would have either llvm IR or SPIR
output not an device executable object. You can then load SPIR
object to a device using device's run-time environment(opencl drivers).
You can checkout these links:
Using Clang to compile kernels
Other alternative is to use the tools provided by your target platform. Each vendor that claims to support opencl, should have a run-time environment. Usually, they have separate CLI tools to compile OpenCL device code. In you case(I guess) you have drivers from Apple, therefore you have openclc
.
Now to your main question (best way to compile opencl). It depends what you want to do. You didn't specify what kind of requirements you have so I had to speculate.
If you want to have off-line compilation without a host program, the considerations above will help you. Otherwise, you have to use OpenCL library and have on-line compilation for you kernels, this is generally preferred for products that needs portability. Since if you compile all your kernels at the start of your program, you directly use the provided environment and you don't need to provide libraries for each target platform.
Therefore, if you have an OpenCL project, you have to decide how to compile. If you really want to use the generic flags and do not rely on third party tools. I suggest you to have a class that builds your kernels and provides the flags you want.
...how do we control these options? Are they have to be set inside the source code, or, likewise the normal compilation flow we can pass them on the command line.
Options can be set inside the source code. For example:
const char options[] = "-cl-finite-math-only -cl-no-signed-zeros"; /* Build program */ err = clBuildProgram(program, 1, &device, options, NULL, NULL);
I have never seen opencl options being specified at the command line and I'm unaware whether this is possible or not.
来源:https://stackoverflow.com/questions/43238587/proper-way-of-compiling-opencl-applications-and-using-available-compiler-options