Proper way of compiling OpenCL applications and using available compiler options

一曲冷凌霜 提交于 2019-12-22 12:30:37

问题


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.


回答1:


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.




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



来源:https://stackoverflow.com/questions/43238587/proper-way-of-compiling-opencl-applications-and-using-available-compiler-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!