【问题标题】:OpenCL-HPP setDefault crashOpenCL-HPP setDefault 崩溃
【发布时间】:2021-05-24 08:59:09
【问题描述】:

这是我试图运行和理解的一段代码。但它有一个尴尬的错误 设置默认函数。

cmake_minimum_required(VERSION 3.19)
project(OpenCL_HPP)

set(CMAKE_CXX_STANDARD 14)

# find OpenCL
find_package(OpenCL REQUIRED)
find_package(Threads REQUIRED)


include_directories(SYSTEM ${OpenCL_INCLUDE_DIRS})

link_directories(${OpenCL_LIBRARIES})

add_executable(OpenCL_HPP main.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCL_LIBRARIES} Threads::Threads)

代码:

    #define CL_HPP_ENABLE_EXCEPTIONS
    #define CL_HPP_MINIMUM_OPENCL_VERSION 120
    #define CL_HPP_TARGET_OPENCL_VERSION 200
    
    #include <vector>
    #include <memory>
    #include <algorithm>
    #include <iostream>
    #ifdef __APPLE__
    #include <OpenCL/cl.hpp>
    #else
    #include <CL/cl2.hpp>
    #endif
    
    constexpr int numElements = 32;
    
    
    int main(void)
    {
        // Filter for a 2.0 platform and set it as the default
        std::vector<cl::Platform> platforms;
        cl::Platform::get(&platforms);
        cl::Platform plat;
        for (auto &p : platforms) {
            std::string platver = p.getInfo<CL_PLATFORM_VERSION>();
            if (platver.find("OpenCL 2.") != std::string::npos) {
                plat = p;
            }
        }
        if (plat() == 0)  {
            std::cout << "No OpenCL 2.0 platform found.";
            return -1;
        }
    
    
    /*
        The setDefault chrashes in the call_once function, with error code -1
    */
        cl::Platform newP = cl::Platform::setDefault(platforms[0]);
        //cl::Platform newP = plat;
        if (newP != plat) {
            std::cout << "Error setting default platform.";
            return -1;
        }
    
        return 0;
}

错误:

/home/BLA/CLionProjects/OpenCL_HPP/cmake-build-debug/OpenCL_HPP 在抛出“std::system_error”实例后调用终止
what(): 未知错误-1

进程以退出代码 134 结束(被信号 6:SIGABRT 中断)

错误来自 call_once 函数,据我了解,这应该是 pThread 库的一部分,但所有这些都会干扰 stdlib。如果我错了,请纠正我。

我运行它的机器是 Ubuntu 16.04,Opencl 来自 Intel,我没有安装任何其他 OpenCL 驱动程序(例如用于 GPU)。此代码是 OpenCL-HPP doxygen 中的主要绑定示例。

我想知道,有没有办法纠正这个问题。 OpenCL-HPP 是使用 Pthread 库还是 STD 库进行链接?

【问题讨论】:

    标签: c++ pthreads c++14 opencl-c++


    【解决方案1】:

    经过一些调试和阅读有关 OpenCL-HPP 的信息后,我发现了问题。

    主要问题是 OpenCL-HPP 使用 pthreads,如果不包含/链接它们,则会出现上述问题。

    帮助的文章:
    cmake fails to configure with a pthread error
    Cmake error undefined reference to `pthread_create'
    Cmake gives me an error (pthread_create not found) while building BornAgain

    主要问题是 Call_once 方法在没有任何真正可以理解的原因的情况下崩溃。该项目将构建。

    使一切脱轨的一件事是 CMake,它并不能真正帮助理解链接过程。

    CMake 设置的输出:

    -- The C compiler identification is GNU 5.4.0
    -- The CXX compiler identification is GNU 5.4.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Looking for CL_VERSION_2_0
    -- Looking for CL_VERSION_2_0 - found
    -- Found OpenCL: /usr/lib/x86_64-linux-gnu/libOpenCL.so (found version "2.0") 
    -- Looking for pthread.h
    -- Looking for pthread.h - found
    -- Looking for pthread_create
    -- Looking for pthread_create - not found
    -- Looking for pthread_create in pthreads
    -- Looking for pthread_create in pthreads - not found
    -- Looking for pthread_create in pthread
    -- Looking for pthread_create in pthread - found
    -- Found Threads: TRUE  
    -- Configuring done
    -- Generating done
    

    这里的重点是 pthreads 并没有真正找到,这对我来说并不清楚。

    find_package(Threads REQUIRED) 
    target_link_libraries(${PROJECT_NAME} ${OpenCL_LIBRARIES} Threads::Threads)
    

    代码实际上并没有做任何事情,因为 pthread 没有正确链接。或者不承认应该链接 pthread。

    在我将以下代码添加到我的 CMake 后,奇迹发生了,崩溃也神奇地消失了。

    if( CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -Werror=return-type")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
    endif()
    

    这就是我的下一个问题出现的地方。为什么所有这些在链接时都没有给出更详细的警告或错误?
    OpenCL-HPP 文档并没有真正表达链接到 pthread 的需要。因此,人们在寻找问题的过程中会有相当痛苦的经历。
    为什么 CMake 必须对 CMAKE_CXX_FLAGS 进行这样的设置并且无法链接到 target_link_libraries 命令?

    【讨论】:

      猜你喜欢
      • 2019-12-31
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多