How to compile OpenCL on Ubuntu?

为君一笑 提交于 2019-11-27 08:27:29
virtuallinux

To compile and run OpenCL code under Linux, you'll need four things:

1) An NVIDIA Driver which supports OpenCL. The drivers packaged with Ubuntu are somewhat old, but they should still work just fine. Unless you have explicit need for current drivers, you should stick with the ones packaged with Ubuntu. To be clear, these are the same drivers installed through the restricted drivers manager. OpenCL libaries are shipped with driver, so to just run OpenCL programs driver should be enough.

2) The CUDA toolkit. This includes the headers necessary to compile OpenCL code. Install this to the default location.

3) The GPU Computing SDK (optional). This includes various NVIDIA specific support tools, as well as OpenCL code samples.

All three of these items may be found at http://developer.nvidia.com/cuda-toolkit-40.

4) OpenCL C++ bindings (optional). Strangely, they are not included with CUDA Toolkit, but in case you use C++, they could make your code much more redable. You can download them from http://www.khronos.org/registry/cl/api/1.1/cl.hpp, and just put it in /usr/local/cuda/include/CL an you desktop.

Once these are installed, you'll need to perform a few more steps to be able to compile and run OpenCL outside of the NVIDIA SDK.

1) The CUDA toolkit will have included the OpenCL headers (Listed at http://www.khronos.org/registry/cl/), likely they are in the directory /usr/local/cuda/include/CL. To make these headers available system wide, you should link this directory into /usr/include/, such that they may be accessed as /usr/include/CL/[headerfilename]. Instead of creating a symlink, you could add /usr/local/cuda/include to your C_INCLUDE_PATH and CPLUS_INCLUDE_PATH environment variables, but this would last for only currest session.

2) Make sure that the OpenCL library (libOpenCL.so) is present in /usr/lib. This should have been put in place by the driver, so you shouldn't have to do anything.

You're ready to write code. Make sure to include CL/cl.h (or CL/cl.hpp if you'd like to use C++ version of API) in any C(++) program which makes OpenCL API calls. When you compile, make sure to link against the OpenCL library (pass gcc the -lOpenCL flag).

As far as your netbook, integrated graphics don't generally support OpenCL. In theory, AMD's APP Acceleration supports running OpenCL on the CPU, but it's not clear that it actually works.

Ciro Santilli 新疆改造中心996ICU六四事件

Ubuntu 15.10 with an NVIDIA NVS 5400M, Lenovo T430

sudo apt-get install nvidia-352 nvidia-352-dev nvidia-prime nvidia-modprobe
sudo ln -s /usr/include/nvidia-352/GL /usr/local/include
sudo ln -s /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 /usr/local/lib/libOpenCL.so

Then use the header as:

#include <CL/cl.h>

And compile with:

gcc -o main main.c -lOpenCL

Notes:

I really recommend upgrading to 15.10 to get this to work: I had never managed before.

I've recently used similar process on a clean build on linux, setting up OpenCL with an NVIDIA card.

Steps I took:

1 - install the NVIDIA driver.

2 - install the CUDA tool kit - (follwing the steps in the guide, there are many ways to do it, but I used the .deb installer, guide can be found here: http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/)

3 - using apt-get install the OpenCL headers. Command: sudo apt-get install opencl-headers

Using the : CL/opencl.h header I was able to compile C/C++ code using gcc/g++ and the flag: -lOpenCL

Explaination of steps

1 - Self explanatory

2 - The CUDA toolkit also installs the OpenCL library (libOpencl.so) but not the header (at least not on my system)

3 - hence the header can be installed with apt-get. The header files get stored in the /usr/include/CL directory

Things that worked for me in Ubuntu 16.04

I have installed openCL on:

SandyBridge CPU: cpu only

IvyBridge GPU

Nvidia GTX 950

install packets

Generic ubuntu packages for OpenCL

Basic installation sudo apt install ocl-icd-libopencl1 sudo apt install opencl-headers sudo apt install clinfo

Package that allows to compile OpenCL code (1.2 I think)

Needed to link and compile sudo apt install ocl-icd-opencl-dev

For Intel GT core

Package that enables runnig openCL on Intel GT, IvyBridge and up

sudo apt install beignet

For SandyBridge Intel CPU and possible others

Download this file OpenCL™ Runtime 16.1.1 for Intel® Core™ and Intel® Xeon® Processors for Ubuntu* (64-bit) On https://software.intel.com/en-us/articles/opencl-drivers#latest_linux_SDK_release

Install packages for turning rpm to deb sudo apt-get install -y rpm alien libnuma1

Untar downloaded file tar -xvf opencl_runtime_16.1.1_x64_ubuntu_6.4.0.25.tgz cd opencl_runtime_16.1.1_x64_ubuntu_6.4.0.25/rpm/ Turn rpm files to deb fakeroot alien --to-deb opencl-1.2-base-6.4.0.25-1.x86_64.rpm fakeroot alien --to-deb opencl-1.2-intel-cpu-6.4.0.25-1.x86_64.rpm Install .deb packages sudo dpkg -i opencl-1.2-base_6.4.0.25-2_amd64.deb sudo dpkg -i opencl-1.2-intel-cpu_6.4.0.25-2_amd64.deb Touch local config file sudo touch /etc/ld.so.conf.d/intelOpenCL.conf Open the file sudo vim /etc/ld.so.conf.d/intelOpenCL.conf and add the line

/opt/intel/opencl-1.2-6.4.0.25/lib64/clinfo

Create a vendors dir and add intel.icd sudo mkdir -p /etc/OpenCL/vendors sudo ln /opt/intel/opencl-1.2-6.4.0.25/etc/intel64.icd /etc/OpenCL/vendors/intel64.icd sudo ldconfig

test if this worked

clinfo should list your devices Dowload this file

https://codeload.github.com/hpc12/tools/tar.gz/master

Run this code to make sure everything works tar xzvf tools-master.tar.gz cd tools-master make ./print-devices ./cl-demo 1000 10 This should print out GOOD in the end

For Nvidia

install nvidia drivers (I used 370), this should include all the runtime dirvers

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