Commit 7d91c6e7 authored by Christopher Bruns's avatar Christopher Bruns
Browse files

Add gpu sniffing program to OpenCL platform cmake configuration process,

to avoid running OpenCL tests on build machines with OpenCL installed, but no GPU.
parent ba23b627
......@@ -19,7 +19,52 @@ IF (APPLE)
SET (CMAKE_C_FLAGS "-arch i386")
ENDIF (APPLE)
SUBDIRS (tests)
# Only run tests if this machine has a OpenCL-capable GPU
# So run a little test program at configuration time to sniff for GPUs
# But first, we need to set DYLD_LIBRARY_PATH here.
get_filename_component(OPENCL_LIB_DIR "${OPENCL_LIBRARY}" PATH)
file(TO_NATIVE_PATH "${OPENCL_LIB_DIR}" OPENCL_LIB_NATIVE_DIR)
if(APPLE)
set(ENV{DYLD_LIBRARY_PATH} "$ENV{DYLD_LIBRARY_PATH}:${OPENCL_LIB_NATIVE_DIR}")
elseif(UNIX)
set(ENV{LD_LIBRARY_PATH} "$ENV{LD_LIBRARY_PATH}:${OPENCL_LIB_NATIVE_DIR}")
elseif(MSVC)
set(ENV{PATH} "$ENV{PATH};${OPENCL_LIB_NATIVE_DIR}")
endif(APPLE)
try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/tests/has_opencl_gpu.c
CMAKE_FLAGS
-DINCLUDE_DIRECTORIES:STRING=${OPENCL_INCLUDE_DIR}
-DLINK_LIBRARIES:STRING=${OPENCL_LIBRARY}
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT_VAR
RUN_OUTPUT_VARIABLE RUN_OUTPUT_VAR
)
# message("${RUN_OUTPUT_VAR}") # Display number of GPUs found
# COMPILE_RESULT_VAR is TRUE when compile succeeds
# RUN_RESULT_VAR is zero when a GPU is found
set(OPENCL_HAVE_GPU_MAYBE TRUE)
if(NOT COMPILE_RESULT_VAR)
set(OPENCL_HAVE_GPU_MAYBE FALSE)
endif(NOT COMPILE_RESULT_VAR)
if(RUN_RESULT_VAR)
set(OPENCL_HAVE_GPU_MAYBE FALSE)
endif(RUN_RESULT_VAR)
if(OPENCL_HAVE_GPU_MAYBE)
set(OPENCL_HAVE_GPU TRUE CACHE BOOL "Whether OpenCL-capable GPU is present")
# Remain silent in case where GPU is found.
# message("GPU check succeeded")
# message("${COMPILE_OUTPUT_VAR}")
# message("${RUN_OUTPUT_VAR}")
else(OPENCL_HAVE_GPU_MAYBE)
set(OPENCL_HAVE_GPU FALSE CACHE BOOL "Whether OpenCL-capable GPU is present")
message("OpenCL GPU check failed")
message("${COMPILE_OUTPUT_VAR}")
message("${RUN_OUTPUT_VAR}")
endif(OPENCL_HAVE_GPU_MAYBE)
if(OPENCL_HAVE_GPU)
SUBDIRS (tests)
endif(OPENCL_HAVE_GPU)
# The source is organized into subdirectories, but we handle them all from
# this CMakeLists file rather than letting CMake visit them as SUBDIRS.
......
/*
* file has_opencl_gpu.c
* created May 6, 2010
* by Christopher Bruns
* Returns zero if an OpenCL-capable GPU is found.
* Returns one otherwise.
*/
#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include <stdio.h>
/*
* check_devices() looks for a GPU among all OpenCL devices
* in a particular OpenCL platform.
* Returns zero if a GPU is found. Returns one otherwise.
*/
int check_devices(cl_platform_id platform_id)
{
cl_int err;
cl_device_id devices[10];
cl_uint num_devices;
size_t d;
char dname[500];
size_t namesize;
err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 10, devices, &num_devices);
if (err != CL_SUCCESS) {
printf("clGetDeviceIDs error: %d", err);
switch(err) {
case CL_INVALID_PLATFORM :
printf(" INVALID_PLATFORM; platform is not a valid platform.\n");
break;
case CL_INVALID_DEVICE_TYPE :
printf(" CL_INVALID_DEVICE_TYPE; device_type is not a valid value.\n");
break;
case CL_INVALID_VALUE :
printf(" CL_INVALID_VALUE; num_entries is equal to zero and device_type is not NULL or if both num_devices and device_type are NULL.\n");
break;
case CL_DEVICE_NOT_FOUND :
printf(" CL_DEVICE_NOT_FOUND; no OpenCL devices that matched device_type were found.\n");
break;
default :
printf(" unrecognized error code '%d'\n", err);
break;
}
return 1;
}
printf("%d devices found\n", num_devices);
if (num_devices < 1)
return 1;
for (d = 0; d < num_devices; ++d) {
clGetDeviceInfo(devices[d], CL_DEVICE_NAME, 500, dname, &namesize);
printf("Device #%d name = %s\n", d, dname);
}
return 0;
}
int main(int argc, char** argv)
{
size_t p;
cl_int err;
cl_platform_id platforms[10];
cl_uint num_platforms;
int status;
/* Investigate all valid platform IDs */
err = clGetPlatformIDs(10, platforms, &num_platforms);
if (num_platforms < 1) {
printf("No OpenCL platforms found.\n");
return 1;
}
for (p = 0; p < num_platforms; ++p) {
printf("Checking platform ID %d\n", p);
status = check_devices(platforms[p]);
if (status == 0)
return status; // found GPU
}
return 1; // did NOT find GPU
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment