Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
smistad committed Jan 22, 2014
0 parents commit 3276b76
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 0 deletions.
14 changes: 14 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 2.8)
project(DetectScreen)

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})

find_package(OpenCL REQUIRED)
include_directories(${OPENCL_INCLUDE_DIR})

find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})

add_executable(detect main.cpp)

target_link_libraries(detect ${OPENGL_LIBRARIES} ${OPENCL_LIBRARIES})
84 changes: 84 additions & 0 deletions FindOpenCL.cmake
@@ -0,0 +1,84 @@
## Thanks to git://gitorious.org/findopencl/findopencl.git

# - Try to find OpenCL
# This module tries to find an OpenCL implementation on your system. It supports
# AMD / ATI, Apple and NVIDIA implementations, but should work, too.
#
# To set manually the paths, define these environment variables:
# OpenCL_INCPATH - Include path (e.g. OpenCL_INCPATH=/opt/cuda/4.0/cuda/include)
# OpenCL_LIBPATH - Library path (e.h. OpenCL_LIBPATH=/usr/lib64/nvidia)
#
# Once done this will define
# OPENCL_FOUND - system has OpenCL
# OPENCL_INCLUDE_DIRS - the OpenCL include directory
# OPENCL_LIBRARIES - link these to use OpenCL
#
# WIN32 should work, but is untested

FIND_PACKAGE(PackageHandleStandardArgs)

SET (OPENCL_VERSION_STRING "0.1.0")
SET (OPENCL_VERSION_MAJOR 0)
SET (OPENCL_VERSION_MINOR 1)
SET (OPENCL_VERSION_PATCH 0)

IF (APPLE)

FIND_LIBRARY(OPENCL_LIBRARIES OpenCL DOC "OpenCL lib for OSX")
FIND_PATH(OPENCL_INCLUDE_DIRS OpenCL/cl.h DOC "Include for OpenCL on OSX")
FIND_PATH(_OPENCL_CPP_INCLUDE_DIRS OpenCL/cl.hpp DOC "Include for OpenCL CPP bindings on OSX")

ELSE (APPLE)

IF (WIN32)

FIND_PATH(OPENCL_INCLUDE_DIRS CL/cl.h)
FIND_PATH(_OPENCL_CPP_INCLUDE_DIRS CL/cl.hpp)

# The AMD SDK currently installs both x86 and x86_64 libraries
# This is only a hack to find out architecture
IF( ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64" )
SET(OPENCL_LIB_DIR "$ENV{ATISTREAMSDKROOT}/lib/x86_64")
ELSE (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
SET(OPENCL_LIB_DIR "$ENV{ATISTREAMSDKROOT}/lib/x86")
ENDIF( ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64" )
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL.lib PATHS ${OPENCL_LIB_DIR} ENV OpenCL_LIBPATH)

GET_FILENAME_COMPONENT(_OPENCL_INC_CAND ${OPENCL_LIB_DIR}/../../include ABSOLUTE)

# On Win32 search relative to the library
FIND_PATH(OPENCL_INCLUDE_DIRS CL/cl.h PATHS "${_OPENCL_INC_CAND}" ENV OpenCL_INCPATH)
FIND_PATH(_OPENCL_CPP_INCLUDE_DIRS CL/cl.hpp PATHS "${_OPENCL_INC_CAND}" ENV OpenCL_INCPATH)

ELSE (WIN32)

# Unix style platforms
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL
PATHS ENV LD_LIBRARY_PATH ENV OpenCL_LIBPATH
)

GET_FILENAME_COMPONENT(OPENCL_LIB_DIR ${OPENCL_LIBRARIES} PATH)
GET_FILENAME_COMPONENT(_OPENCL_INC_CAND ${OPENCL_LIB_DIR}/../../include ABSOLUTE)

# The AMD SDK currently does not place its headers
# in /usr/include, therefore also search relative
# to the library
FIND_PATH(OPENCL_INCLUDE_DIRS CL/cl.h PATHS ${_OPENCL_INC_CAND} "/usr/local/cuda/include" "/opt/AMDAPP/include" ENV OpenCL_INCPATH)
FIND_PATH(_OPENCL_CPP_INCLUDE_DIRS CL/cl.hpp PATHS ${_OPENCL_INC_CAND} "/usr/local/cuda/include" "/opt/AMDAPP/include" ENV OpenCL_INCPATH)

ENDIF (WIN32)

ENDIF (APPLE)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenCL DEFAULT_MSG OPENCL_LIBRARIES OPENCL_INCLUDE_DIRS)

IF(_OPENCL_CPP_INCLUDE_DIRS)
SET( OPENCL_HAS_CPP_BINDINGS TRUE )
LIST( APPEND OPENCL_INCLUDE_DIRS ${_OPENCL_CPP_INCLUDE_DIRS} )
# This is often the same, so clean up
LIST( REMOVE_DUPLICATES OPENCL_INCLUDE_DIRS )
ENDIF(_OPENCL_CPP_INCLUDE_DIRS)

MARK_AS_ADVANCED(
OPENCL_INCLUDE_DIRS
)
164 changes: 164 additions & 0 deletions main.cpp
@@ -0,0 +1,164 @@
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#include <iostream>
#include <CL/cl.hpp>
#include <GL/glut.h>

#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenCL/cl_gl.h>
#include <OpenGL/OpenGL.h>
#else
#if _WIN32
#else
#include <GL/glx.h>
#include <CL/cl_gl.h>
#endif
#endif


int main(int argc, char ** argv) {
// Get platforms
VECTOR_CLASS<cl::Platform> platforms;
cl::Platform::get(&platforms);

std::cout << platforms.size() << " OpenCL platforms found." << std::endl;

for(int j = 0; j < platforms.size(); j++) {
std::cout << std::endl << "=========================================================================" << std::endl;
std::cout << "Platform " << j << " with name " << platforms[j].getInfo<CL_PLATFORM_VENDOR>() << std::endl;

// Get devices for platform
VECTOR_CLASS<cl::Device> devices;
platforms[j].getDevices(CL_DEVICE_TYPE_ALL, &devices);
std::cout << devices.size() << " devices found." << std::endl;
if(devices.size() == 0) {
std::cout << "Aborting. No devices found." << std::endl;
continue;
}

// Create properties for CL-GL context
#if defined(__APPLE__) || defined(__MACOSX)
// Apple (untested)
// TODO: create GL context for Apple
cl_context_properties cps[] = {
CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
(cl_context_properties)CGLGetShareGroup(CGLGetCurrentContext()),
0};

#else
#ifdef _WIN32
// Windows
// TODO: create GL context for Windows
cl_context_properties cps[] = {
CL_GL_CONTEXT_KHR,
(cl_context_properties)wglGetCurrentContext(),
CL_WGL_HDC_KHR,
(cl_context_properties)wglGetCurrentDC(),
CL_CONTEXT_PLATFORM,
(cl_context_properties)(platforms[j])(),
0
};
#else
// Linux
// Create a GL context using glX
int sngBuf[] = {
GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 12,
None };

// TODO: should probably free this stuff
Display * display = XOpenDisplay(0);
XVisualInfo* vi = glXChooseVisual(display, DefaultScreen(display), sngBuf);
GLXContext gl2Context = glXCreateContext(display, vi, 0, GL_TRUE);

if(gl2Context == NULL) {
std::cout << "Could not create a GL 2.1 context, please check your graphics drivers" << std::endl;
continue;
}

cl_context_properties cps[] = {
CL_GL_CONTEXT_KHR,
(cl_context_properties)gl2Context,
CL_GLX_DISPLAY_KHR,
(cl_context_properties)display,
CL_CONTEXT_PLATFORM,
(cl_context_properties)(platforms[j])(),
0
};
std::cout << "Current glX context is: " << cps[1] << std::endl;
#endif
#endif

// Query which device is associated with GL context
cl_device_id cl_gl_device_ids[32];
size_t returnSize = 0;
clGetGLContextInfoKHR_fn glGetGLContextInfo_func = (clGetGLContextInfoKHR_fn)clGetExtensionFunctionAddress("clGetGLContextInfoKHR");
glGetGLContextInfo_func(
cps,
CL_DEVICES_FOR_GL_CONTEXT_KHR,
32*sizeof(cl_device_id),
&cl_gl_device_ids,
&returnSize
);

if(returnSize == 0) {
std::cout << "No valid GPU for GL context found" << std::endl;
continue;
}

std::cout << "There are " << returnSize / sizeof(cl_device_id) << " devices that can be associated with the GL context" << std::endl;

// This code assumes that there is only one GPU that is connected to the screen
cl_device_id interopDeviceID;
bool found = false;
for(int i = 0; i < returnSize/sizeof(cl_device_id); i++) {
cl::Device device(cl_gl_device_ids[i]);
if(device.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_GPU) {
interopDeviceID = device();
found = true;
break;
}
}

if(!found) {
std::cout << "Could not find a GPU that was associated with the GL context" << std::endl;
exit(0);
}

cl::Device interopDevice(interopDeviceID);
std::cout << "----------------------------------------------------------------------------------" << std::endl;
std::cout << "Device with name " << interopDevice.getInfo<CL_DEVICE_NAME>() << std::endl;
std::cout << "This device is associated with the GL context. And thus most likely connected to the screen." << std::endl;
std::cout << "It has: " << interopDevice.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>() / (1024*1024) << " MB of memory" << std::endl;
std::cout << "----------------------------------------------------------------------------------" << std::endl;

// Get all other devices
if(devices.size() > 1) {
for(int i = 0; i < devices.size(); i++) {
cl_device_id currentDeviceID = devices[i]();
if(currentDeviceID != interopDeviceID) {
cl::Device device(currentDeviceID);
std::cout << "----------------------------------------------------------------------------------" << std::endl;
std::cout << "Device with name " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
std::cout << "This device is either NOT GPU or NOT associated with the GL context. Thus most likely NOT connected to screen." << std::endl;
std::cout << "It has: " << device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>() / (1024*1024) << " MB of memory" << std::endl;
std::cout << "----------------------------------------------------------------------------------" << std::endl;
}
}
}

// Cleanup
#if defined(__APPLE__) || defined(__MACOSX)
#ifdef _WIN32
#else
// Linux
glXDestroyContext(display, gl2Context);
#endif
#endif

}

return 0;
}

0 comments on commit 3276b76

Please sign in to comment.