From f5c3f63b68db63c17f7f0ca02d5cdb34f631be11 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Mon, 3 Apr 2017 23:35:55 -0700 Subject: [PATCH] generalizing the CL class --- include/OpenCL.h | 44 ++++++++++++++++++++++++++++++- src/OpenCL.cpp | 68 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/include/OpenCL.h b/include/OpenCL.h index f2aee09..6cc740e 100644 --- a/include/OpenCL.h +++ b/include/OpenCL.h @@ -26,8 +26,26 @@ #include #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_type type; cl_uint clock_frequency; @@ -38,10 +56,16 @@ struct device { char name[256]; cl_bool is_little_endian = false; bool cl_gl_sharing = false; + char platform_name[128]; }; +const struct saved_device { + + +}; + class OpenCL { public: @@ -49,14 +73,32 @@ public: OpenCL(sf::Vector2i resolution); ~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); void run_kernel(std::string kernel_name); void draw(sf::RenderWindow *window); + + private: + std::vector>> platforms_and_devices; + + int error = 0; // Sprite and texture that is shared between CL and GL diff --git a/src/OpenCL.cpp b/src/OpenCL.cpp index a4146f9..42164f8 100644 --- a/src/OpenCL.cpp +++ b/src/OpenCL.cpp @@ -36,25 +36,30 @@ void OpenCL::draw(sf::RenderWindow *window) { } void OpenCL::aquire_hardware() { + // Get the number of platforms cl_uint plt_cnt = 0; clGetPlatformIDs(0, nullptr, &plt_cnt); - // Fetch the platforms - std::map> plt_ids; - - // buffer before map init + // Get the ID's for those platforms std::vector plt_buf(plt_cnt); clGetPlatformIDs(plt_cnt, plt_buf.data(), nullptr); - // Map init + // Populate the storage vector with the platform id's for (auto id : plt_buf) { - plt_ids.emplace(std::make_pair(id, std::vector())); + platforms_and_devices.push_back(std::make_pair(id, std::vector())); } + for (std::pair) // For each platform, populate its devices 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; 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_ENDIAN_LITTLE, sizeof(cl_bool), &d.is_little_endian, NULL); + std::cout << "Device: " << q << std::endl; + std::cout << "Device ID : " << d.id << 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 << "Device Version : " << d.version << std::endl; @@ -226,16 +236,28 @@ void OpenCL::create_shared_context() { }; #endif - - // Create our shared context - context = clCreateContext( - context_properties, - 1, - &device_id, - nullptr, nullptr, + context = clCreateContextFromType(context_properties, + CL_DEVICE_TYPE_CPU, + nullptr, + nullptr, &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")) 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(&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) {