Last active
November 2, 2021 17:15
-
-
Save jirihnidek/3f23b7451c1744d71dbf to your computer and use it in GitHub Desktop.
C++ OpenCL Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# - 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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works on Ubuntu14.04, nvidia GTX 1060. minor changes by setting
cuda/include
asOPENCL_INCLUDE_DIR
andcuda/lib64/libOpenCL.so
asOPENCL_LIBRARIES
. output: