首页 > 解决方案 > 如何使用 DPC++ 和 CMake 构建 SYCL 程序?

问题描述

背景

我正在尝试使用 CUDA 后端学习 SYCL(我使用这些指令编译了 dpc++ 编译器,并且矢量添加有效)。然而,第二天我无法使用 CMake 让书中的第一个示例工作,但是使用命令行和调用编译器直接解决了这个问题。

#include <CL/sycl.hpp>
#include <iostream>
#include <string>

using namespace sycl;

const std::string secret =
    "Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01";

const auto sz = secret.size();

int main() {
    queue Q;

    char* result = malloc_shared<char>(sz, Q);
    std::memcpy(result, secret.data(), sz);

    Q.parallel_for(sz, [=](auto& i) {
        result[i] -= 1;
    }).wait();

    std::cout << result << '\n';
}

我使用这个脚本来设置环境变量:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/shino/software/sources/llvm/build/lib/
export DPCPP_HOME=/home/shino/software/sources
export CUDA_LIB_PATH=/usr/local/cuda/lib64/stubs/
export CXX=/home/shino/software/sources/llvm/build/bin/clang++
export CC=/home/shino/software/sources/llvm/build/bin/clang
export CUDA_PATH=/usr/local/cuda

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(sycl-convolution)

add_executable(convolution main.cpp)
target_include_directories(convolution PRIVATE /home/shino/software/sources/llvm/sycl/include)
target_compile_features(convolution PRIVATE cxx_std_17)
target_compile_options(convolution PRIVATE -fsycl -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=$ENV{CUDA_PATH})
target_link_directories(convolution PRIVATE /home/shino/software/sources/llvm/build/lib/)

以下是链接器错误的前几行:

/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `main':
main-4cf7ed.cpp:(.text+0x122): undefined reference to `cl::sycl::event::wait()'
/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `main::{lambda(auto:1&)#1} cl::sycl::queue::submit<cl::sycl::queue::parallel_for_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1}, cl::sycl::detail::code_location const&)::{lambda(cl::sycl::handler&)#1}>(cl::sycl::queue::parallel_for_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1}, cl::sycl::detail::code_location const&)::{lambda(cl::sycl::handler&)#1}, cl::sycl::detail::code_location const)':
main-4cf7ed.cpp:(.text+0x3c0): undefined reference to `cl::sycl::event::event()'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x3cc): undefined reference to `cl::sycl::queue::is_host() const'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x47b): undefined reference to `cl::sycl::queue::submit_impl_and_postprocess(std::function<void (cl::sycl::handler&)>, cl::sycl::detail::code_location const&, std::function<void (bool, bool, cl::sycl::event&)> const&)'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x543): undefined reference to `cl::sycl::queue::submit_impl(std::function<void (cl::sycl::handler&)>, cl::sycl::detail::code_location const&)'
/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `void cl::sycl::handler::parallel_for_lambda_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1})':
main-4cf7ed.cpp:(.text+0xa23): undefined reference to `cl::sycl::handler::GetRangeRoundingSettings(unsigned long&, unsigned long&, unsigned long&)'

我该如何解决?我的直觉告诉我,编译器在直接调用时以不同的方式调用链接器,并且我没有指定链接过程的一些非常重要的部分。

使用详细模式调用编译器会给出以下链接命令:

 "/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/home/shino/software/sources/llvm/build/bin/../lib -L/lib -L/usr/lib /tmp/main-7d74d3.o /tmp/a-cacdd3.o -lstdc++ -lm -lgcc_s -lgcc -lsycl -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o /lib/x86_64-linux-gnu/crtn.o

除了链接器错误之外,还有更多警告,这表明编译器未处于正确的“模式”。我不知道接下来从哪里开始挖掘。

标签: c++cmakelinker-errorssycldpc++

解决方案


您缺少要链接的实际库,请使用target_link_libraries指令指定它们。在CMake 的文档中查找更多信息。

请注意,这target_link_directories还不够 - 它仅指定链接器应搜索的库的路径。

更具体地说,通过阅读您发布的链接命令,您应该添加:

target_link_libraries(convolution PRIVATE sycl)

OP 的添加:添加SYSTEMtarget_include_directories指令将使警告静音:

target_include_directories(convolution SYSTEM PRIVATE /home/shino/software/sources/llvm/sycl/include) 

推荐阅读