Large amount done. OpenCL is almost completely abstracted out to the class
from init to kernel compilation. Next will be getting buffer handling, executing, and reading done. Changed the kernel to a more minimal example. Added a jump table for error codes and an assert function for error checking. Added a routine to compare all the available cl devices and platforms, chooses the high clock of either the GPU or CPU.
This commit is contained in:
@@ -1,194 +0,0 @@
|
||||
#include "Clapper.h"
|
||||
|
||||
|
||||
|
||||
Clapper::Clapper() {
|
||||
}
|
||||
|
||||
|
||||
Clapper::~Clapper() {
|
||||
}
|
||||
|
||||
void Clapper::init() {
|
||||
|
||||
int error = 0;
|
||||
|
||||
// ===================================================================== //
|
||||
// ==== Opencl setup
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
|
||||
// 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[1], CL_DEVICE_TYPE_GPU, 0, nullptr,
|
||||
&deviceIdCount);
|
||||
|
||||
|
||||
if (deviceIdCount == 0) {
|
||||
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[1], CL_DEVICE_TYPE_CPU, deviceIdCount, deviceIds.data(), NULL);
|
||||
}
|
||||
else {
|
||||
std::cout << "acquired GPU cl target" << std::endl;
|
||||
deviceIds.resize(deviceIdCount);
|
||||
clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_GPU, deviceIdCount, deviceIds.data(), nullptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (error != 0) {
|
||||
std::cout << "Err: clGetDeviceIDs returned: " << error << std::endl;
|
||||
return error;
|
||||
}
|
||||
|
||||
// Hurray for standards!
|
||||
// Setup the context properties to grab the current GL context
|
||||
#ifdef linux
|
||||
cl_context_properties context_properties[] = {
|
||||
CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
|
||||
CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
|
||||
CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
|
||||
0
|
||||
};
|
||||
|
||||
#elif defined _WIN32
|
||||
//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();
|
||||
CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);
|
||||
cl_context_properties context_properties[] = {
|
||||
CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
|
||||
(cl_context_properties)shareGroup,
|
||||
0
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// Create our shared context
|
||||
auto context = clCreateContext(
|
||||
context_properties,
|
||||
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;
|
||||
}
|
||||
|
||||
// And the cl command queue
|
||||
auto commandQueue = clCreateCommandQueue(context, deviceIds[0], 0, &error);
|
||||
|
||||
if (error != 0) {
|
||||
std::cout << "Err: clCreateCommandQueue returned: " << error << std::endl;
|
||||
return error;
|
||||
}
|
||||
|
||||
// At this point the shared GL/CL context is up and running
|
||||
}
|
||||
|
||||
void Clapper::print_sys_info() {
|
||||
|
||||
int error = 0;
|
||||
|
||||
// Get the number of platforms
|
||||
cl_uint plt_cnt = 0;
|
||||
clGetPlatformIDs(0, nullptr, &plt_cnt);
|
||||
|
||||
// Fetch the platforms
|
||||
std::vector<cl_platform_id> plt_ids(plt_cnt);
|
||||
clGetPlatformIDs(plt_cnt, plt_ids.data(), nullptr);
|
||||
|
||||
|
||||
// Print out this machines info
|
||||
|
||||
std::cout << "============ Hardware info =================" << std::endl;
|
||||
|
||||
for (unsigned int i = 0; i < plt_cnt; i++) {
|
||||
|
||||
std::cout << "--Platform: " << i << std::endl;
|
||||
|
||||
char platform[128];
|
||||
char version[128];
|
||||
|
||||
clGetPlatformInfo(plt_ids[i], CL_PLATFORM_NAME, 128, platform, NULL);
|
||||
clGetPlatformInfo(plt_ids[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(plt_ids[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(plt_ids[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;
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
#pragma once
|
||||
#include <GL/glew.h>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef linux
|
||||
#include <CL/cl.h>
|
||||
#include <CL/opencl.h>
|
||||
|
||||
#elif defined _WIN32
|
||||
#include <CL/cl_gl.h>
|
||||
#include <CL/cl.h>
|
||||
#include <CL/opencl.h>
|
||||
#include <windows.h>
|
||||
|
||||
#elif defined TARGET_OS_MAC
|
||||
# include <OpenGL/OpenGL.h>
|
||||
# include <OpenCL/opencl.h>
|
||||
|
||||
#endif
|
||||
|
||||
struct platform_tracker {
|
||||
|
||||
};
|
||||
|
||||
class Clapper {
|
||||
public:
|
||||
|
||||
enum PLATFORM_TYPE { GPU, CPU, ALL };
|
||||
|
||||
Clapper();
|
||||
~Clapper();
|
||||
|
||||
void init();
|
||||
void print_sys_info();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool initialized = false;
|
||||
|
||||
cl_uint platform_count;
|
||||
std::vector<cl_platform_id> platformIds;
|
||||
|
||||
cl_uint deviceIdCount = 0;
|
||||
std::vector<cl_device_id> deviceIds;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user