|
|
|
#include "OpenCLManager.h"
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
OpenCLManager::OpenCLManager()
|
|
|
|
{
|
|
|
|
connectToDBusSignals();
|
|
|
|
initOpenCLResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenCLManager::connectToDBusSignals() {
|
|
|
|
QDBusConnection bus = QDBusConnection::systemBus();
|
|
|
|
bus.connect(QString(), QString(), "org.freedesktop.login1.Manager", "PrepareForSleep",
|
|
|
|
this, SLOT(handleSuspendResume(bool)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenCLManager::handleSuspendResume(bool isSuspending) {
|
|
|
|
if(isSuspending)
|
|
|
|
{
|
|
|
|
std::cout << "System is going to suspend. Releasing OpenCL resources..." << std::endl;
|
|
|
|
releaseOpenCLResources();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << "System has resumed. Reinitializing OpenCL resources..." << std::endl;
|
|
|
|
initOpenCLResources();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenCLManager::initOpenCLResources() {
|
|
|
|
cl_int err;
|
|
|
|
cl_platform_id platform;
|
|
|
|
cl_device_id device;
|
|
|
|
|
|
|
|
err = clGetPlatformIDs(1, &platform, nullptr);
|
|
|
|
if(err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to get OpenCL platform" << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, nullptr);
|
|
|
|
if(err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to get OpenCL device" << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
context = clCreateContext(nullptr, 1, &device, nullptr, nullptr, &err);
|
|
|
|
if(err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to create OpenCL context" << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
commandQueue = clCreateCommandQueue(context, device, 0, &err);
|
|
|
|
if(err != CL_SUCCESS)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to create OpenCL command queue" << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "OpenCL resources initialized successfully." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenCLManager::releaseOpenCLResources() {
|
|
|
|
if(commandQueue)
|
|
|
|
{
|
|
|
|
clReleaseCommandQueue(commandQueue);
|
|
|
|
commandQueue = nullptr;
|
|
|
|
}
|
|
|
|
if(context)
|
|
|
|
{
|
|
|
|
clReleaseContext(context);
|
|
|
|
context = nullptr;
|
|
|
|
}
|
|
|
|
std::cout << "OpenCL resources released." << std::endl;
|
|
|
|
}
|