Added a quick printout of the hardware info. Running into a problem choosing between platforms, going to abstract CL out into it's own class and hide all that logic

master
MitchellHansen 8 years ago
parent 3c9b39f682
commit edd8075afb

@ -1,3 +1,10 @@
#pragma once
#include <cstdio>
#include <cstring>
#include <CL/cl_ext.h>
#include <iostream>
#include <vector>
#ifdef linux
#elif defined _WIN32
@ -9,6 +16,8 @@
#endif
#ifdef TARGET_OS_MAC
int IsExtensionSupported(
const char* support_str,
const char* ext_string,
@ -34,21 +43,23 @@ int IsExtensionSupported(
space_pos = space_substr ? space_substr - ext_string : 0;
}
printf("Warning: Extension not supported %s!\n", support_str);
std::cout << "Warning: Extension not supported " << support_str << std::endl;
return 0;
}
#endif
int test_for_gl_cl_sharing() {
inline int test_for_gl_cl_sharing() {
int err = 0;
#if defined (__APPLE__) || defined(MACOSX)
static const char *CL_GL_SHARING_EXT = "cl_APPLE_gl_sharing";
#else
static const char* CL_GL_SHARING_EXT = "cl_khr_gl_sharing";
#endif
cl_uint num_devices, i;
cl_uint num_devices;
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
cl_device_id *devices = (cl_device_id *) calloc(sizeof(cl_device_id), num_devices);
@ -62,7 +73,8 @@ int test_for_gl_cl_sharing() {
free(devices);
// Search for GL support in extension string (space delimited)
int supported = IsExtensionSupported(CL_GL_SHARING_EXT, ext_string, ext_size);
//int supported = IsExtensionSupported(CL_GL_SHARING_EXT, ext_string, ext_size);
int supported = 0;
if (supported) {
// Device supports context sharing with OpenGL
printf("Found GL Sharing Support!\n");
@ -72,13 +84,26 @@ int test_for_gl_cl_sharing() {
}
int query_platform_devices() {
inline int query_platform_devices() {
int error = 0;
// Get the number of platforms
cl_uint platform_count = 0;
clGetPlatformIDs(0, nullptr, &platform_count);
// Fetch the platforms
std::vector<cl_platform_id> platformIds(platform_count);
clGetPlatformIDs(platform_count, platformIds.data(), nullptr);
for (unsigned int q = 0; q < platform_count; q++) {
// From stackoverflow, gets and lists the compute devices
cl_uint num_devices, i;
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
error = clGetDeviceIDs(platformIds[q], CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
cl_device_id *devices = (cl_device_id *) calloc(sizeof(cl_device_id), num_devices);
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
cl_device_id *devices = (cl_device_id *)calloc(sizeof(cl_device_id), num_devices);
error = clGetDeviceIDs(platformIds[q], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
char buf[128];
for (i = 0; i < num_devices; i++) {
@ -90,6 +115,7 @@ int query_platform_devices() {
}
free(devices);
}
return 1;
}

@ -10,6 +10,7 @@
#include <CL/opencl.h>
#elif defined _WIN32
#include <CL/cl_gl.h>
#include <CL/cl.h>
#include <CL/opencl.h>
#include <windows.h>
@ -19,14 +20,13 @@
# include <OpenCL/opencl.h>
#endif
#include "TestPlatform.cpp"
#include "Map.h"
#include "Curses.h"
#include "util.hpp"
#include "RayCaster.h"
const int WINDOW_X = 150;
const int WINDOW_Y = 150;
@ -45,43 +45,82 @@ std::string read_file(std::string file_name){
int main(){
char buffer[256];
char *val = getcwd(buffer, sizeof(buffer));
if (val) {
std::cout << buffer << std::endl;
}
// ===================================================================== //
// ==== Opencl setup
int error = 0;
// ===================================================================== //
// ==== Opencl setup
// Get the number of platforms
cl_uint platformIdCount = 0;
clGetPlatformIDs(0, nullptr, &platformIdCount);
cl_uint platform_count = 0;
clGetPlatformIDs(0, nullptr, &platform_count);
// Fetch the platforms
std::vector<cl_platform_id> platformIds (platformIdCount);
clGetPlatformIDs(platformIdCount, platformIds.data(), nullptr);
std::vector<cl_platform_id> platformIds(platform_count);
clGetPlatformIDs(platform_count, platformIds.data(), nullptr);
// Print out this machines info
std::cout << "============ Hardware info =================" << std::endl;
for (unsigned int i = 0; i < platform_count; i++) {
std::cout << "--Platform: " << i << std::endl;
char platform[128];
char version[128];
clGetPlatformInfo(platformIds[i], CL_PLATFORM_NAME, 128, platform, NULL);
clGetPlatformInfo(platformIds[i], CL_PLATFORM_VERSION, 128, version, NULL);
std::cout << platform << "\n";
std::cout << version << "\n\n";
// get the number of devices, fetch them, choose the first one
cl_uint deviceIdCount = 0;
error = clGetDeviceIDs(platformIds[i], CL_DEVICE_TYPE_ALL, 0, nullptr, &deviceIdCount);
std::vector<cl_device_id> deviceIds(deviceIdCount);
for (int q = 0; q < deviceIdCount; q++) {
std::cout << "++++Device " << q << std::endl;
error = clGetDeviceIDs(platformIds[i], CL_DEVICE_TYPE_ALL, deviceIdCount, deviceIds.data(), NULL);
clGetDeviceInfo(deviceIds[q], CL_DEVICE_NAME, 128, platform, NULL);
clGetDeviceInfo(deviceIds[q], CL_DEVICE_VERSION, 128, version, NULL);
std::cout << platform << "\n";
std::cout << version << "\n\n";
}
}
std::cout << "============================================" << std::endl;
cl_uint deviceIdCount = 0;
std::vector<cl_device_id> deviceIds;
// Try to get a GPU first
error = clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_GPU, 0, nullptr,
error = clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_GPU, 0, nullptr,
&deviceIdCount);
if (deviceIdCount == 0) {
std::cout << "couldn't aquire a GPU, falling back to CPU" << std::endl;
error = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_CPU, 0, nullptr, &deviceIdCount);
std::cout << "couldn't acquire a GPU, falling back to CPU" << std::endl;
error = clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_CPU, 0, nullptr, &deviceIdCount);
deviceIds.resize(deviceIdCount);
error = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_CPU, deviceIdCount, deviceIds.data(), NULL);
error = clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_CPU, deviceIdCount, deviceIds.data(), NULL);
} else {
std::cout << "aquired GPU cl target" << std::endl;
std::cout << "acquired GPU cl target" << std::endl;
deviceIds.resize(deviceIdCount);
clGetDeviceIDs (platformIds[0], CL_DEVICE_TYPE_GPU, deviceIdCount, deviceIds.data (), nullptr);
clGetDeviceIDs (platformIds[1], CL_DEVICE_TYPE_GPU, deviceIdCount, deviceIds.data (), nullptr);
}
if (error != 0){
std::cout << "Err: clGetDeviceIDs returned: " << error << std::endl;
return error;
@ -98,12 +137,16 @@ int main(){
};
#elif defined _WIN32
cl_context_properties context_properties[] = {
CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(),
CL_CONTEXT_PLATFORM, (cl_context_properties) platform,
0
};
//cl_context_properties context_properties[] = {
// CL_CONTEXT_PLATFORM, (cl_context_properties) platformIds[0],
// CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
// CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(),
// 0
//};
HGLRC hGLRC = wglGetCurrentContext();
HDC hDC = wglGetCurrentDC();
cl_context_properties context_properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platformIds[1], CL_GL_CONTEXT_KHR, (cl_context_properties)hGLRC, CL_WGL_HDC_KHR, (cl_context_properties)hDC, 0 };
#elif defined TARGET_OS_MAC
CGLContextObj glContext = CGLGetCurrentContext();
@ -119,12 +162,18 @@ int main(){
// Create our shared context
auto context = clCreateContext(
context_properties,
deviceIdCount,
deviceIds.data(),
1,
&deviceIds[0],
nullptr, nullptr,
&error
);
//cl_device_id devices[32];
//size_t size;
//clGetGLContextInfoKHR(context_properties, CL_DEVICES_FOR_GL_CONTEXT_KHR,
// 32 * sizeof(cl_device_id), devices, &size);
if (error != 0){
std::cout << "Err: clCreateContext returned: " << error << std::endl;
return error;

Loading…
Cancel
Save