-
-
Save jirihnidek/3f23b7451c1744d71dbf to your computer and use it in GitHub Desktop.
# Minimal version of CMake | |
cmake_minimum_required (VERSION 2.6) | |
# Build type | |
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) | |
message(STATUS "Setting build type to 'Debug' as none was specified.") | |
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE) | |
# Set the possible values of build type for cmake-gui | |
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release") | |
endif () | |
# Define project name | |
project (OpenCL_Example) | |
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/") | |
find_package( OpenCL REQUIRED ) | |
include_directories( ${OPENCL_INCLUDE_DIR} ) | |
# Source code of application | |
set (opencl_example_src opencl_example.cc) | |
# Compiler flags | |
if (CMAKE_COMPILER_IS_GNUCC) | |
set (CMAKE_CXX_FLAGS "-D_REETRANT -Wall -Wextra -pedantic -Wno-long-long") | |
if (CMAKE_BUILD_TYPE STREQUAL "Debug") | |
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ggdb -O0") | |
elseif( CMAKE_BUILD_TYPE STREQUAL "Release" ) | |
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 -fno-strict-aliasing") | |
endif () | |
endif (CMAKE_COMPILER_IS_GNUCC) | |
# Set up executable | |
add_executable (opencl_example ${opencl_example_src}) | |
target_link_libraries(opencl_example ${OPENCL_LIBRARIES}) |
# - Try to find OpenCL | |
# Once done this will define | |
# | |
# OPENCL_FOUND - system has OpenCL | |
# OPENCL_INCLUDE_DIR - the OpenCL include directory | |
# OPENCL_LIBRARIES - link these to use OpenCL | |
# | |
# WIN32 should work, but is untested | |
IF (WIN32) | |
FIND_PATH(OPENCL_INCLUDE_DIR CL/cl.h ) | |
# TODO this is only a hack assuming the 64 bit library will | |
# not be found on 32 bit system | |
FIND_LIBRARY(OPENCL_LIBRARIES opencl64 ) | |
IF( OPENCL_LIBRARIES ) | |
FIND_LIBRARY(OPENCL_LIBRARIES opencl32 ) | |
ENDIF( OPENCL_LIBRARIES ) | |
ELSE (WIN32) | |
# Unix style platforms | |
# We also search for OpenCL in the NVIDIA SDK default location | |
FIND_PATH(OPENCL_INCLUDE_DIR CL/cl.h /opt/AMDAPPSDK-2.9-1/include/ ) | |
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL | |
ENV LD_LIBRARY_PATH | |
) | |
ENDIF (WIN32) | |
SET( OPENCL_FOUND "NO" ) | |
IF(OPENCL_LIBRARIES ) | |
SET( OPENCL_FOUND "YES" ) | |
ENDIF(OPENCL_LIBRARIES) | |
MARK_AS_ADVANCED( | |
OPENCL_INCLUDE_DIR | |
) |
#define __CL_ENABLE_EXCEPTIONS | |
#if defined(__APPLE__) || defined(__MACOSX) | |
#include <OpenCL/cl.hpp> | |
#else | |
#include <CL/cl.hpp> | |
#endif | |
#include <cstdio> | |
#include <cstdlib> | |
#include <iostream> | |
#include <vector> | |
const char * helloStr = "__kernel void " | |
"hello(void) " | |
"{ " | |
" " | |
"} "; | |
int main(void) | |
{ | |
cl_int err = CL_SUCCESS; | |
try { | |
std::vector<cl::Platform> platforms; | |
cl::Platform::get(&platforms); | |
if (platforms.size() == 0) { | |
std::cout << "Platform size 0\n"; | |
return -1; | |
} | |
// Print number of platforms and list of platforms | |
std::cout << "Platform number is: " << platforms.size() << std::endl; | |
std::string platformVendor; | |
for (unsigned int i = 0; i < platforms.size(); ++i) { | |
platforms[i].getInfo((cl_platform_info)CL_PLATFORM_VENDOR, &platformVendor); | |
std::cout << "Platform is by: " << platformVendor << std::endl; | |
} | |
cl_context_properties properties[] = | |
{ | |
CL_CONTEXT_PLATFORM, | |
(cl_context_properties)(platforms[0])(), | |
0 | |
}; | |
cl::Context context(CL_DEVICE_TYPE_ALL, properties); | |
std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>(); | |
// Print number of devices and list of devices | |
std::cout << "Device number is: " << devices.size() << std::endl; | |
for (unsigned int i = 0; i < devices.size(); ++i) { | |
std::cout << "Device #" << i << ": " << devices[i].getInfo<CL_DEVICE_NAME>() << std::endl; | |
} | |
cl::Program::Sources source(1, | |
std::make_pair(helloStr, strlen(helloStr))); | |
cl::Program program_ = cl::Program(context, source); | |
program_.build(devices); | |
cl::Kernel kernel(program_, "hello", &err); | |
cl::Event event; | |
cl::CommandQueue queue(context, devices[0], 0, &err); | |
queue.enqueueNDRangeKernel( | |
kernel, | |
cl::NullRange, | |
cl::NDRange(4,4), | |
cl::NullRange, | |
NULL, | |
&event); | |
event.wait(); | |
} | |
catch (cl::Error err) { | |
std::cerr | |
<< "ERROR: " | |
<< err.what() | |
<< "(" | |
<< err.err() | |
<< ")" | |
<< std::endl; | |
} | |
return EXIT_SUCCESS; | |
} |
works with arch installing everything
Output :
Platform number is: 3 Platform is by: Intel(R) Corporation Platform is by: NVIDIA Corporation Platform is by: Mesa Device number is: 1 Device #0: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Works also on Nvidia GTX 1080 on Ubuntu 18.04. (OpenCL libs distributed with the Cuda 8.0 toolkit)
Output:
Platform number is: 1
Platform is by: NVIDIA Corporation
Device number is: 1
Device #0: GeForce GTX 1080
Works on PopOS 20.04 with Nvidia GTX 2070
The key to make my OpenCL program work was to add set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/")
to my CMakeList.txt
file. Then my program would work with the custom FindOpenCL.cmake
file.
works on Ubuntu14.04, nvidia GTX 1060. minor changes by setting cuda/include
as OPENCL_INCLUDE_DIR
and cuda/lib64/libOpenCL.so
as OPENCL_LIBRARIES
. output:
Platform number is: 1
Platform is by: NVIDIA Corporation
Device number is: 1
Device #0: GeForce GTX 1060 6GB
Works with Intel GT2 on Ubuntu 17.04 installing everything here: https://askubuntu.com/a/850594/698904
Output: