Ubuntu Linux上的OpenCL入门

Getting started with OpenCL on Ubuntu Linux

Ubuntu Linux上的OpenCL入门

OpenCL is included in all the main distributions of Linux. In this guide we choose Ubuntu as one of the most widespread variants, but all of the distributions have some sort of centralized system of package maintaining and OpenCL realizations can be installed from there.

OpenCL包含在Linux的所有主要发行版中。在本指南中,我们选择Ubuntu作为最广泛的变体之一,但所有的发行版都有某种集中的包维护系统,并且可以从那里安装OpenCL实现。

In this guide we're minimally going to use the following tools:

在本指南中,我们将最低限度地使用以下工具:

  • The command-line
  • 命令行
  • C/C++ compiler
  • C/C++编译器

And depending on which ways one extends the bare minimum:

根据扩展最低限度的方式:

  • CMake (Cross-platform Make)
  • CMake(跨平台Make)
  • Git
  • Vcpkg (Cross-platform pacakge management)
  • Vcpkg(跨平台包装管理)
  • Visual Studio Code

Steps will be provided to obtain a minimal and productive environment.

将采取步骤,以获得最低限度的生产环境。

Installation

安装

In this guide we'll be using latest (at the time of writing) Ubuntu 20.04 LTS. Installation for the most part will happen via apt (Advanced Packaging Tool), the definitive command-line tool for installing software on Debian-base Linux distribution. It is installed with the system automatically.

​在本指南中,我们将使用最新的(在撰写本文时)Ubuntu 20.04 LTS。大部分安装将通过apt(高级打包工具)进行,apt是在Debian Linux发行版上安装软件的权威命令行工具。它是随系统自动安装的。

(NOTE: installation commands should be issued with root access, so that use of privilege-escalation by sudo is mandatory.)

(注意:安装命令应具有root访问权限,因此sudo必须使用权限提升。)

First update your system. Open your favorite shell in your favorite terminal and run:

首先更新您的系统。在您喜爱的终端中打开您喜爱的shell,然后运行:

sudo apt update
sudo apt upgrade

C/C++ compiler

C/C++编译器

Open your favorite shell in your favorite terminal and run:

在您喜爱的终端中打开您喜爱的shell,然后运行:

sudo apt install build-essential -y

This will install canonical GNU compiler set g++/GNU 9.3, debugger gdb, as well as some tools (e.g. makebinutils) and libraries for compilation and linking of programs.

这将安装规范的GNU编译器集g++GNU9.3、调试器gdb,以及一些用于编译和链接程序的工具(例如make、binutils)和库。

Git

You most likely already have Git installed. If not, we'll install it, because this is what is needed to keep your repositories up-to-date. Open your favorite shell in your favorite terminal and run:

你很可能已经安装了Git。如果没有,我们将安装它,因为这是保持存储库最新所必需的。在您喜爱的终端中打开您喜爱的shell,然后运行:

sudo apt install git -y

CMake

If you do not have CMake installed yet, we'll install it, as it's the primary supported build system, and it will make our builds much simpler. Open your favorite shell in your favorite terminal and run:

如果您还没有安装CMake,我们将安装它,因为它是主要支持的构建系统,它将使我们的构建更加简单。在您喜爱的终端中打开您喜爱的shell,然后运行:

sudo apt install cmake -y

Visual Studio Code

While IDEs are highly opinionated, for an IDE with widespread adoption and small footprint, VS Code has everything (and more) to get off the ground. It can be installed in two ways. The first way is to use the Ubuntu Software GUI, where you can search for code and install it in one click. If you are a fan of command-line tools, use the second method: open your favorite shell in your favorite terminal and run

虽然IDE非常固执己见,但对于一个广泛采用且占地面积小的IDE来说,VS Code有一切(甚至更多)需要起步。它可以通过两种方式安装。第一种方法是使用Ubuntu软件GUI,在那里你可以搜索代码并一键安装。如果您是命令行工具的爱好者,请使用第二种方法:在您喜欢的终端中打开您喜欢的shell并运行

sudo snap install --classic code

OpenCL-SDK

To build native OpenCL applications, one will minimally need:

要构建本机OpenCL应用程序,最低限度地需要:

  • C or C++ compiler
  • C或C++编译器
  • The OpenCL headers
  • OpenCL头
    • The C and optionally the C++ headers
    • C和可选的C++头
  • An Installable Client Driver (ICD) Loader
  • 可安装客户端驱动程序(ICD)加载器
    • Shared objects library (libOpenCL.so)
    • 共享对象库(libOpenCL.so)

The easiest way to obtain these files is using the system package manager, albeit may not be the latest on non-rolling distributions. Cutting-edge versions may be obtained using C/C++ package managers (like Vcpkg) or can be built on-demand from the canonical GitHub-hosted repositories. Git submodules of the SDK include the headers, both C and C++, and Khronos canonical ICD loader, that are built and installed as parts of SDK.

获取这些文件的最简单方法是使用系统包管理器,尽管它可能不是最新的非滚动发行版。尖端版本可以使用C/C++包管理器(如Vcpkg)获得,也可以从规范的GitHub托管存储库中按需构建。SDK的Git子模块包括头文件,包括C和C++,以及Khronos规范ICD加载程序,它们是作为SDK的一部分构建和安装的。

APT

sudo apt install opencl-headers ocl-icd-opencl-dev -y

GitHub

Vcpkg

Compiling on the command-line

在命令行上编译

Invoking the compiler manually

手动调用编译器

Compilers native to *nix OS flavors work just out of the box. Then navigate to the folder where you wish to build your application. Our application will have a single Main.c source file:

*nix OS风格的本地编译器可以开箱即用。然后导航到要构建应用程序的文件夹。我们的应用程序将有一个Main.c源文件:

// C standard includes
#include <stdio.h>

// OpenCL includes
#include <CL/cl.h>

int main()
{
    cl_int CL_err = CL_SUCCESS;
    cl_uint numPlatforms = 0;

    CL_err = clGetPlatformIDs( 0, NULL, &numPlatforms );

    if (CL_err == CL_SUCCESS)
        printf("%u platform(s) found\n", numPlatforms);
    else
        printf("clGetPlatformIDs(%i)\n", CL_err);

    return 0;
}

Then invoke the compiler to build our source file as such:

然后调用编译器来构建我们的源文件,如下所示:

APT

gcc -Wall -Wextra -D CL_TARGET_OPENCL_VERSION=100 Main.c -o HelloOpenCL -lOpenCL

What do the command-line arguments mean?

命令行参数是什么意思?

  • -Wall -Wextra turns on all warnings (highest sensible level)
  • -Wall-Wextra打开所有警告(最高可感知级别)
  • -D instructs the preprocessor to create a define with NAME:VALUE
  • -D指示预处理器使用NAME:VALUE创建一个定义
    • CL_TARGET_OPENCL_VERSION enables/disables API functions corresponding to the defined version. Setting it to 100 will disable all API functions in the header that are newer than OpenCL 1.0
    • CL_TARGET_OPENCL_VERSION启用/禁用对应于定义版本的API函数。将其设置为100将禁用标头中比OpenCL 1.0更新的所有API函数
  • -I sets additional paths to the include directory search paths
  • -I 将其他路径设置为包含目录搜索路径
  • Main.c is the name of the input source file
  • Main.c是输入源文件的名称
  • -o sets the name of the output executable (default would be a.out)
  • -o设置输出可执行文件的名称(默认为a.out)
  • -l instructs linker to link library OpenCL
  • -l指示链接器链接库OpenCL

GitHub

Vcpkg

Running our executable by ./HelloOpenCL either prints the number of platforms found or an error code which is often the result of corrupted or absent runtime installations.

由运行我们的可执行文件/HelloOpenCL要么打印找到的平台数量,要么打印错误代码,这通常是运行时安装损坏或缺失的结果。

Automating the build using CMake

使用CMake实现构建自动化

The CMake build script for this application that builds it as an ISO C11 app with most sensible compiler warnings turned on looks like:

该应用程序的CMake构建脚本将其构建为ISO C11应用程序,并打开了最合理的编译器警告,其外观如下:

cmake_minimum_required(VERSION 3.1) # 3.1 << C_STANDARD 11

project(HelloOpenCL LANGUAGES C)

find_package(OpenCL REQUIRED)

add_executable(${PROJECT_NAME} Main.c)

target_link_libraries(${PROJECT_NAME} PRIVATE OpenCL::OpenCL)

set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 11
                                                 C_STANDARD_REQUIRED ON
                                                 C_EXTENSIONS OFF)

target_compile_definitions(${PROJECT_NAME} PRIVATE CL_TARGET_OPENCL_VERSION=100)

What does the script do?

这个脚本是做什么的?

  • Give a name to the project and tell CMake to only look for a C compiler (default is to search for a C and a C++ compiler)
  • 为项目命名,并告诉CMake只查找C编译器(默认情况下搜索C和C++编译器)
  • Look for an OpenCL SDK and fail if not found
  • 查找OpenCL SDK,如果找不到则失败
    • If detection fails, refer to the CMake Build-System Support chapter.
    • ​如果检测失败,请参阅CMake Build System Support一章。
  • Specify our source files and name the executable
  • 指定我们的源文件并命名可执行文件
  • Specify dependency to the SDK (not just linkage)
  • 指定对SDK的依赖关系(不仅仅是链接)
  • Set language properties to all source files of our application
  • 为应用程序的所有源文件设置语言属性
  • Set the OpenCL version target to control API coverage in header
  • 设置OpenCL版本目标以控制标头中的API覆盖范围

To invoke this script, place it next to our Main.c file in a file called CMakeLists.txt. Once that's done, CMake may be invoked the following way to generate makefiles in the advised out-of-source fashion into a subfolder named build:

要调用此脚本,请将其放在名为CMakeLists.txt的文件中的Main.c文件旁边。完成后,可以通过以下方式调用CMake,以建议的源代码外方式将makefile生成到名为build的子文件夹中:

APT

cmake -S . -B ./build

GitHub

Vcpkg

Which will output something like

它将输出以下内容

-- The C compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ivan/OCL/build

To kick off the build, one may use CMakes build driver:

要启动构建,可以使用CMakes构建驱动程序:

cmake --build ./build --config Release

Once build is complete, we can run the program by typing:

构建完成后,我们可以通过键入以下内容来运行程序:

./build/HelloOpenCL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值