..

搭建基于Boost.compute的OpenCL开发环境

准备

  • Ubuntu 18.04
  • Boost >= 1.61 (compute是在1.61版本加入的)
  • AMD OpenCL SDK
  • CMake
  • KhronosGroup/OpenCL-SDK

步骤

  • 编译 Boost 1.65.0 版本1
  • 安装CMake
  • 下载 AMD - OpenCL - SDK2
./AMD-APP-SDK-v3.0.130.135-GA-linux64.sh

等待自动安装完成,新启一个 terminal 进行操作。

  • 编译 KhronosGroup/OpenCL-SDK 3
mkdir build && cd build 
cmake ..
  • 编写CMakeLists.txt
#Author : beautifularea
#Date   : 06/04/2021
#Desc   : OpenCL Hello world 

SET(PROJECTNAME example)
PROJECT(${PROJECTNAME})
CMAKE_MINIMUM_REQUIRED(VERSION 3.10.2)

set(boost_root "/root/debug_boost/boost_1_65_0")
INCLUDE_DIRECTORIES("${boost_root}/include")
LINK_DIRECTORIES("${boost_root}/stage/lib")

AUX_SOURCE_DIRECTORY(./ DIR_MAIN_SRCS)
ADD_EXECUTABLE(${PROJECTNAME} ${DIR_MAIN_SRCS})

FIND_PACKAGE(Boost 1.65.0 COMPONENTS thread date_time filesystem system log log_setup REQUIRED)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

TARGET_LINK_LIBRARIES(${PROJECTNAME} libboost_log_setup.a)
TARGET_LINK_LIBRARIES(${PROJECTNAME} libboost_log.a)
TARGET_LINK_LIBRARIES(${PROJECTNAME} libboost_system.a)
TARGET_LINK_LIBRARIES(${PROJECTNAME} libboost_date_time.a)
TARGET_LINK_LIBRARIES(${PROJECTNAME} libboost_filesystem.a)
TARGET_LINK_LIBRARIES(${PROJECTNAME} libboost_thread.a)
TARGET_LINK_LIBRARIES(${PROJECTNAME} -lpthread)
TARGET_LINK_LIBRARIES(${PROJECTNAME} /root/debug_boost/opencl/OpenCL-SDK/build/libOpenCL.so)
  • 编写 OpenCL 版的 Hello world
#include <iostream>
  
#include <boost/compute.hpp>

namespace compute = boost::compute;

int main()
{
    // get the default device
    compute::device device = compute::system::default_device();

    // print the device's name
    std::cout << "hello from " << device.name() << std::endl;

    return 0;
}

【输出】

hello from Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz

到此为止, 一个 OpenCL 版本的helloworld 就算完成了。
hello world都完成了,lower_bound 还远吗?!