generalizing the CL class

master
MitchellHansen 8 years ago
parent 278c5a37e3
commit f5c3f63b68

@ -26,8 +26,26 @@
#include <OpenCL/opencl.h> #include <OpenCL/opencl.h>
#endif #endif
struct device {
class device {
public:
struct packed_data {
cl_device_type type;
cl_uint clock_frequency;
char version[64];
cl_uint comp_units;
char extensions[1024];
char name[128];
};
device(cl_device_id device_id, cl_platform_id platform_id);
private:
cl_device_id id; cl_device_id id;
cl_device_type type; cl_device_type type;
cl_uint clock_frequency; cl_uint clock_frequency;
@ -38,10 +56,16 @@ struct device {
char name[256]; char name[256];
cl_bool is_little_endian = false; cl_bool is_little_endian = false;
bool cl_gl_sharing = false; bool cl_gl_sharing = false;
char platform_name[128];
}; };
const struct saved_device {
};
class OpenCL { class OpenCL {
public: public:
@ -49,14 +73,32 @@ public:
OpenCL(sf::Vector2i resolution); OpenCL(sf::Vector2i resolution);
~OpenCL(); ~OpenCL();
// command queues are associated with a device and context, so for multi-gpu applications you would need
// multiple command queues
// CONTEXTS
// - An OpenCL context is created with one or more devices. Contexts are used by the OpenCL runtime
// for managing objects such as command - queues, memory, program and kernel objects and for executing
// kernels on one or more devices specified in the context.
// - Contexts cannot be created using more than one platform!
bool load_config();
bool init(sf::Vector4f *range); bool init(sf::Vector4f *range);
void run_kernel(std::string kernel_name); void run_kernel(std::string kernel_name);
void draw(sf::RenderWindow *window); void draw(sf::RenderWindow *window);
private: private:
std::vector<std::pair<cl_platform_id, std::vector<cl_device_id>>> platforms_and_devices;
int error = 0; int error = 0;
// Sprite and texture that is shared between CL and GL // Sprite and texture that is shared between CL and GL

@ -36,25 +36,30 @@ void OpenCL::draw(sf::RenderWindow *window) {
} }
void OpenCL::aquire_hardware() { void OpenCL::aquire_hardware() {
// Get the number of platforms // Get the number of platforms
cl_uint plt_cnt = 0; cl_uint plt_cnt = 0;
clGetPlatformIDs(0, nullptr, &plt_cnt); clGetPlatformIDs(0, nullptr, &plt_cnt);
// Fetch the platforms // Get the ID's for those platforms
std::map<cl_platform_id, std::vector<device>> plt_ids;
// buffer before map init
std::vector<cl_platform_id> plt_buf(plt_cnt); std::vector<cl_platform_id> plt_buf(plt_cnt);
clGetPlatformIDs(plt_cnt, plt_buf.data(), nullptr); clGetPlatformIDs(plt_cnt, plt_buf.data(), nullptr);
// Map init // Populate the storage vector with the platform id's
for (auto id : plt_buf) { for (auto id : plt_buf) {
plt_ids.emplace(std::make_pair(id, std::vector<device>())); platforms_and_devices.push_back(std::make_pair(id, std::vector<cl_device_id>()));
} }
for (std::pair<cl_platform_id, std::vector<cl_device_id>)
// For each platform, populate its devices // For each platform, populate its devices
for (unsigned int i = 0; i < plt_cnt; i++) { for (unsigned int i = 0; i < plt_cnt; i++) {
char plt_name[128];
clGetPlatformInfo(plt_buf[i], CL_PLATFORM_NAME, 128, (void*)&plt_name, nullptr);
std::cout << "Platform name " << plt_name << std::endl;
cl_uint deviceIdCount = 0; cl_uint deviceIdCount = 0;
error = clGetDeviceIDs(plt_buf[i], CL_DEVICE_TYPE_ALL, 0, nullptr, &deviceIdCount); error = clGetDeviceIDs(plt_buf[i], CL_DEVICE_TYPE_ALL, 0, nullptr, &deviceIdCount);
@ -86,9 +91,14 @@ void OpenCL::aquire_hardware() {
clGetDeviceInfo(d.id, CL_DEVICE_NAME, 256, &d.name, NULL); clGetDeviceInfo(d.id, CL_DEVICE_NAME, 256, &d.name, NULL);
clGetDeviceInfo(d.id, CL_DEVICE_ENDIAN_LITTLE, sizeof(cl_bool), &d.is_little_endian, NULL); clGetDeviceInfo(d.id, CL_DEVICE_ENDIAN_LITTLE, sizeof(cl_bool), &d.is_little_endian, NULL);
std::cout << "Device: " << q << std::endl; std::cout << "Device: " << q << std::endl;
std::cout << "Device ID : " << d.id << std::endl;
std::cout << "Device Name : " << d.name << std::endl; std::cout << "Device Name : " << d.name << std::endl;
memcpy(d.platform_name, plt_name, 128);
std::cout << "Platform name : " << d.platform_name << std::endl;
std::cout << "Platform ID : " << d.platform << std::endl; std::cout << "Platform ID : " << d.platform << std::endl;
std::cout << "Device Version : " << d.version << std::endl; std::cout << "Device Version : " << d.version << std::endl;
@ -226,16 +236,28 @@ void OpenCL::create_shared_context() {
}; };
#endif #endif
context = clCreateContextFromType(context_properties,
// Create our shared context CL_DEVICE_TYPE_CPU,
context = clCreateContext( nullptr,
context_properties, nullptr,
1,
&device_id,
nullptr, nullptr,
&error &error
); );
cl_device_id devices[10]{0};
size_t num_devices = 0;
clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(cl_device_id) * 10, (void*)&devices, &num_devices);
std::cout << num_devices;
//// Create our shared context
//context = clCreateContext(
// context_properties,
// 1,
// &device_id,
// nullptr, nullptr,
// &error
//);
if (vr_assert(error, "clCreateContext")) if (vr_assert(error, "clCreateContext"))
return; return;
@ -442,6 +464,26 @@ OpenCL::~OpenCL() {
} }
bool OpenCL::load_config() {
std::ifstream input_file("config.bin", std::ios::binary | std::ios::in);
if (!input_file.is_open()) {
std::cout << "No config file..." << std::endl;
return false;
}
saved_device prefered_device;
input_file.read(reinterpret_cast<char*>(&prefered_device), sizeof(prefered_device));
std::cout << "config loaded, looking for device..." << std::endl;
input_file.close();
return true;
}
bool OpenCL::init(sf::Vector4f *range) bool OpenCL::init(sf::Vector4f *range)
{ {

Loading…
Cancel
Save