Execute OpenCL

Dear all,

After a huge amount of time trying to install LLVM and Clang i could finally do it, so now im trying to use this tools for generating a bytecode, then apply it modular optimizations and then generate an executable to test the result.

First, I only want to compile a project and execute it to see how it works, specifically this one: https://github.com/rsnemmen/OpenCL-examples/tree/master/add_numbers

Which commands would you use?

I want to do that the .c file uses the .cl that i have compiled before with clang and i do not know how to do it.

Thanks in advance.

Hi Enrique,

First, I only want to compile a project and execute it to see how it works, specifically this one: https://github.com/rsnemmen/OpenCL-examples/tree/master/add_numbers

As I can see, it has a Makefile which you could use. Anyway, on Linux you need to do something like:

clang -std=c99 add_numbers.c -lOpenCL -I/path/to/folder/with/CL/cl.h

Usually, libOpenCL.so and CL/cl.h are provided by OpenCL SDKs from HW/SW vendor, for example: [1], [2], [3], [4] and others [5]

But, you can also get them from KhronosGroup repos: OpenCL-ICD-Loader [6], OpenCL-Headers [7]

I want to do that the .c file uses the .cl that i have compiled before with clang and i do not know how to do it.

This is an interesting question. Basically, in OpenCL you can build your device program in a separate ways:

Hi Alexey,

Your reply has been a great help to me,your way of explain the different types of compilation is very detailed and easy to understand.

Even so, I have a couple of questions.

  1. What do yourefer by OpenCL RT?

  2. Could you give me some examples of an open-source OpenCL implementation and update optimization pipeline?

Thank you in advance. Regards

Hi Enrique,

  1. What do yourefer by OpenCL RT?

I mean OpenCL Runtime – some library which implements OpenCL API for you.

Basically, OpenCL consist of two parts: host code and device code.

Host code can be written at any language and actually it is a regular C/C++/Python/whatever application which uses OpenCL API.

OpenCL API allows you to select platform, device, create context and buffers for data, create and build program for your device, create command queue and kernel, set kernel arguments and then enqueue your kernel for execution on the selected device.

You can always read the spec 1 to understand all these concepts. I would recommend section 3 “The OpenCL Architecture”: in particular, section 3.1 “Platform Model” and 3.2 “Execution Model”.

Also you can find some videos 2, slides 3 or articles 4 about this topic.

So, the next part is device code: it can be written on OpenCL C, C++ for OpenCL in clang, or any other language which can be represented in SPIR-V.

As I said in previous emails, device code can be compiled online or offline and then it will be launched on selected device using API.

I encourage you to read the spec or search for videos/slides/articles on this topic to get familiar with main OpenCL concepts/architecture/ideas.

  1. Could you give me some examples of an open-source OpenCL implementation and update optimization pipeline?

Sure, here you go (there might be more implementations, I’m just not aware of them):

  • POCL 5
  • Intel graphics compute runtime and compiler 6 and 7
  • ROCm 8. I guess compiler sources are located in corresponding forks of llvm 9 and clang 10
  • VC4CL 11

Please note that each implementation targets specific device or set of devices from one or more particular vendors.

The most generic/portable one is POCL, I guess.

Hello everyone,

I have been trying to use clang with the spir64 target (soir64-unknown-unknown) and the tool does not detect this target, i do not know why.

I have tried to install SPIR-V Tool project[1] and it still does no detect the target I

I also tried to build also SPIRV-LLVM-Translator project[2] and it fails when making the project. It does not encounter the Polly library and fails.

Could someone help me with this problem?

Thanks in advance.

References:
[1] https://github.com/KhronosGroup/SPIRV-Tools
[2] https://github.com/KhronosGroup/SPIRV-LLVM-Translator

Hi Enrique,

I have been trying to use clang with the spir64 target (soir64-unknown-unknown) and the tool does not detect this target, i do not know why.

It is hard to say anything without additional information. Please provide command you are trying to launch

I also tried to build also SPIRV-LLVM-Translator project[2] and it fails when making the project. It does not encounter the Polly library and fails.

Again, it is hard to help without any additional information or logs

Hi Alexey,

I have finally build successfully the SPIRV-LLVM-Translator project inside the llvm-project, but i still cannot compile a OpenCL code file into a spirv format because of the lack of the spirv64 target.

I attach the error when trying to use spirv64 target:

enrique@enrique-VirtualBox:~/Escritorio/OpenCL_Projects/opencl-example/helloworld$ clang++ -Xclang -finclude-default-header --target=spir64-unknown-unknown HelloWorld_Kernel.cl
error: unable to create target: ‘No available targets are compatible with triple “spir64-unknown-unknown”’
1 error generated.

Thanks in advance.