I realise after spending over 16 hours getting OpenCL to work on Windows and Linux, that OpenCL On ubuntu 16.04, Intel Sandy Bridge CPU is the closest to get it working.
What didn't work for me was from the installation of drivers, and configuration, after which I tried from some more places. Yeah, the versions were different. I took that into consideration wherever possible. But, then, it was my first time installing libraries and linking. So, I hope this helps any newbies like me. I have only covered the part on Intel GPUs
Getting OpenCL to work
sudo apt install clinfo ocl-icd-opencl-dev opencl-headers
Check the output of clinfo
clinfo | grep "GPU"
to check if GPU is included. If there's no line like
Device Type GPU
in the output, then, GPU does not yet support OpenCL. In that case, try installing beignet-opencl-icd, mesa-opencl-icd, etc - may be, one at a time. Keep checking the output of
clinfo after each. If it includes the above line, you are done.
Installing Intel OpenCL Drivers
If that doesn't work, install the opencl drivers of intel from https://software.intel.com/en-us/articles/opencl-drivers#latest_linux_driver. The automatic install requires a ton of time and space. There is an alternate method though.
Unzip the archive. Install alien, if you don't already have:
sudo apt install alien
cd into the unzipped folder, and run
sudo alien -i *.rpm
Check the output of “clinfo” again. It should have a GPU.
clinfo | grep “GPU”
At this point, you'd have a working implementation of OpenCL.
However, while trying out examples (from here), I found that most examples work with Intel's libraries (see below), some don't work with the libraries installed with the distribution (installed above). (I guess, this is because of the different openCL versions involved.)
Install Intel's OpenCL SDK
Install the Intel OpenCL SDK for Linux (after registering for free) with them, from here Intel OpenCL SDK for Linux. Just run the installer script - no need to work on the parts of making it work with Eclipse and all. You may need to
sudo apt install dkms
At this point, you should have all the things ready.
If you are a newbie (like me)
Know the process of compilation of C programs: firstly, object files are generated, then these are linked. See https://stackoverflow.com/questions/31179452/what-exactly-is-in-a-o-a-so-file.
In an IDE (I don't know about the terminal), there are two parts to dealing with libraries. First is including the path to the header files. Second is including the library files in the project.
I am using CodeBlocks. So, the reason Linux is loved is because, the very first command of this answer sent the files to their intended places. The header files are in the /usr/include directory, under 'CL' folder. The library (in my case) is under /usr/lib/x86_64-linux-gnu, named 'libOpenCL.so'. You don't need to do anything further to use them. You can just start coding -
In case of the Intel OpenCL SDK, the header files are under /opt/intel/opencl-sdk/include; while the library files are under
After installing, check the contents of /opt/intel/opencl for the contents. These too have the libraries. However, one of the library-files gave me a syntax error (libclang.so). The Intel OpenCL SDK is to be downloaded mainly for the header files in: /opt/intel/opencl-sdk/lib64 - only the .so ones. (I found the libclang.so file to be giving a syntax error. Check while running the program.)
In case of CodeBlocks, to use these Intel's files - the non-default ones -
Create a new project. Right click the project in the manager-subwindow. Go to “Build options”. Select the Project name in the left pane – thus, the settings will be applied for both Release and Debug versions.
In Linker settings -> link libraries, click “Add”. Here, add the libraries (not the header files; all the .so ones): at /opt/intel/opencl-sdk/lib64.
In Search directories tab, under ‘compiler’, add /opt/intel/opencl/include (or wherever your header files are).
In case of Eclipse, first, install the CDT. Then, after creating the project, under Project -> Properties -> C/C++ Build -> Settings -> Tool Settings, (1) under GCC C++ Compiler, includes, add the header files directory. (2) under GCC C++ Linker, in the Libraries section, add OpenCL and in the lower part, in the Library Path section, add the path. In my case, to get it working, I required, /opt/intel/opencl/include in the first part and /opt/intel/opencl/SDK/lib64 in the second part. The default as in the case of Codeblocks didn't work. Perhaps, its due to the versions, or I haven't tried properly - Eclipse is new to me.
The following should run and compile: https://github.com/bgaster/opencl-book-samples/blob/master/src/Chapter_3/OpenCLInfo/OpenCLInfo.cpp