3

I am a newbie in OpenCL stuffs.

Whats is the best way to compiler an OpenCL project ?

  1. Using a supported compiler (GCC or Clang):

    When we use a compiler like gcc or clang, 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 few options provided for cl_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>'
    
  2. 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.

Amir
  • 1,348
  • 3
  • 21
  • 44
  • Do you mean binary output of another kernel compilation done before? It shortens compiling time, especially for fpgas. What do you ask in 1st question? Do you need to add header files from another C program? – huseyin tugrul buyukisik Apr 05 '17 at 21:13
  • @huseyintugrulbuyukisik. My question is general. what is the propoer wayr of compiling opencl applications (host+kernels). Is is using gcc/llvm and opencl library or using openclc. – Amir Apr 05 '17 at 21:16
  • you need to include opencl lib-dll files from operating system for the C++ bindings to work and compile. The opencl kernel compiling can be done just giivng a string to cl program and cl kernel objects and compiling on runtime, not compiletime. If you have binaries, then you can have the opencl ready from the beginning. If gpu drivers are installed, you can find them in some folders but I don't remember. – huseyin tugrul buyukisik Apr 05 '17 at 21:18
  • so what is the usage of `openclc` then? – Amir Apr 05 '17 at 21:20
  • its compiling outside of your program so you can use its result file to run kernels in your program without opencl-compiling. For example, I'm not using that so compiling for 3 devices in my computer takes 10-20 seconds (in run-time) because I'm giving kernel program as string and opencl.dll with necessary function it compiles when I want from that string but a fpga takes hours to compile so they compile beforehand once and publish it so users dont wait hours for compute – huseyin tugrul buyukisik Apr 05 '17 at 21:22
  • You are using another OS than windows I suppose, each OS support can be different and have bugs, maybe people using openclc to workaround that bugs – huseyin tugrul buyukisik Apr 05 '17 at 21:24
  • @huseyintugrulbuyukisik. I have got another question for you man :) Thanks :p http://stackoverflow.com/questions/43417757/generic-opencl-stencil-kernel – Amir Apr 14 '17 at 19:14

2 Answers2

4

You might be aware but it is important to reiterate. OpenCL standard contains two things:

  1. OpenCL C language and programming model (I think recent standard include some C++)
  2. 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

Llvm IR generation

SPIR

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.

Intel CLI as an example

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.

Community
  • 1
  • 1
Vemulo
  • 404
  • 5
  • 14
  • Thanks for your complete answer @Vermulo. Could you provide a hint for my other question as well : http://stackoverflow.com/questions/43417757/generic-opencl-stencil-kernel – Amir Apr 16 '17 at 15:56
2

...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.

builder-7000
  • 7,131
  • 3
  • 19
  • 43
  • 1
    Thanks @Sergio. It would be nice if OpenCL provides some env that can be controlled at compile time, otherwise you should modify that `options[]` at source-code everytime – Amir Apr 06 '17 at 17:08