Here is a piece of code that I'm trying to run and understand. but it has a awkward error in the setDefault function.
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)
Code:
#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;
}
ERROR:
/home/BLA/CLionProjects/OpenCL_HPP/cmake-build-debug/OpenCL_HPP terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
The error comes int the call_once function, as far as I understand this should be part of the pThread libs but all of that interferes with the stdlib. Correct me if I'm wrong.
The machine where I run this is Ubuntu 16.04, the Opencl comes from Intel, and I do not have any other OpenCL driver installed (for the GPU for example). This code is the main bind example in the OpenCL-HPP doxygen.
I'm wondering, is there a way to rectify this. Is OpenCL-HPP using Pthread lib or STD lib, for linking purposes?
After some debuging and reading about OpenCL-HPP I found the problem.
The main issue is that the OpenCL-HPP uses pthreads and if they are not included / linked one gets problems like described above.
Articles that helped:
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
The main issue is that the Call_once method crashes without any really understandable cause. The project will build though.
One thing that derails everything is the CMake it is not really helping with understanding the linking procedure.
Output from the CMake setup:
-- 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
The main point here is that pthreads are not really found, and that was not clear to me.
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} ${OpenCL_LIBRARIES} Threads::Threads)
The code is not really doing anything since the pthreads are not linked correctly. Or it is not recognized that the pthreads should be linked.
Once I added the following code to my CMake, the magic happens and the crashes go magically away.
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()
Here is where the next problem for me arises. Why does all of this does not give some more verbose warnings or error when linking?
The OpenCL-HPP documentation does not really express the need for linking to pthreads. As a consequence then one has a quite painful experience in searching for the problem.
Why does CMake have to have such a setup for the CMAKE_CXX_FLAGS and is not able to link int the target_link_libraries command?