OpenCL

From LinuxReviews
Jump to navigationJump to search

OpenCL (short for Open Computing Language) is a open framework for writing programs for heterogeneous platforms. OpenCL specifies a programming language based on C++11 and APIs. It is mostly used to accelerate compute operations using graphics cards. OpenCL programs can be executed on or across other devices like CPUs, FPGAs and digital signal processors.

The OpenCL standard has been maintained by the Khronos Group since 2008. It was originally developed by Apple.

Standards

There are two main OpenCL standards, 1.0 from 2009 and 2.0 from 2013. Version 1.0 was followed by 1.1 in 2010 and 1.2 in 2011.

OpenCL 2.0, specified in 2013, added support for shared virtual memory, nested parallelism, pipes and C11 atomics. It was expanded with OpenCL 2.1 in 2015. OpenCL 2.2, specified in 2017, added support for the OpenCL C++ kernel language. It is a subset of the C++14 standard. OpenCL 2.2 will run on any hardware capable of running OpenCL 2.0 code if the OpenCL implementation for that hardware supports OpenCL 2.2.

Practical Use

Researches in computer science, mathematics and related subjects tend to use graphics cards to do accelerate calculations that would take far longer on a CPU. They are mostly doing this using the proprietary CUDA framework from Nvidia to do this. There are a whole lot of code written for CUDA and very little in terms of developer tools and libraries for OpenCL even though OpenCL is a open standard and CUDA is not.

There are, as of 2020, not that many fields or places where OpenCL is used for anything. That does not mean OpenCL could not be used where CUDA is, and it does not mean that CUDA is better. It simply means that one of the two competing standard became dominant and the other became mostly irrelevant.

GNU/Linux alternatives

Most GNU/Linux distribution carry several OpenCL implementations in their repositories, most of which are not at all useful for anything.

Lovelyz Kei ProTip.jpg
TIP: You can use clinfo to see what OpenCL standard you are using and what it supports. Packages with OpenCL implementations will install a .icd file in /etc/OpenCL/vendors/ which specify what library should be used to execute OpenCL. You should ideally only have one file in that folder.

MESA Gallium Compute (clover)

The Mesa graphics library comes with a OpenCL implementation called "Clover". Is is only capable of executing OpenCL 1.1 code regardless of what the hardware supports. All the distributions have this available and most of them install it along with the base system. There is a fair change you have it as a system default for OpenCL.

The Mesa "clover" OpenCL implementation is not very useful for anything due to its OpenCL 1.1 limitation.

POCL

pocl is a portable OpenCL 1.2 implementation for CPUs and CUDA-supported Nvidia graphics cards with many OpenCL 2.0 features implemented. Running OpenCL on a CPU misses the whole point of it and translating OpenCL to CUDA is slower than using CUDA directly. pocl is therefore mostly useless and a complete waste of time.

ROCm (Radeon Open Compute)

ROCm is a "universal platform for gpu-accelerated computing". It is, for practical purposes, a mostly working OpenCL 2.0 implementation for all AMD graphics cards made the last decade. It is not in any major distributions repositories. You will have to follow the instructions at https://github.com/RadeonOpenCompute/ROCm to get this technology. Documentation for it is at rocm-documentation.readthedocs.io/en/latest/.

ROCm is the best option if you want to use OpenCL for anything on a GNU/Linux machine if you use one of the supported operating systems which are:

  • Ubuntu 16.04.6 (Kernel 4.15)
  • Ubuntu 18.04.3(Kernel 5.3)
  • CentOS v7.7
  • RHEL v7.7
  • SLES 15

Getting ROCm on other distributions can be quite problematic. Distributions do not have ROCm available in their repositories.

Getting ROCm to work for your use-case can be a bit problematic even if your distribution is among the supported ones. As an example, the video editor Davinci Resolve does not work with ROCm 2.1 or 3.0 but it happens to work fine with ROCm 2.2. You can't just install the latest ROCm version and expect everything OpenCL to just work.


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.