std::vector<cl_device_id> get_device_ids(cl_platform_id platformID) { cl_uint numDeviceIDs; ::clGetDeviceIDs(platformID, CL_DEVICE_TYPE_ALL, 0, NULL, &numDeviceIDs); std::vector<cl_device_id> deviceIDs(numDeviceIDs); ::clGetDeviceIDs(platformID, CL_DEVICE_TYPE_ALL, numDeviceIDs, &deviceIDs[0], NULL); return deviceIDs; }
void PlantMgr::addPluginFactory(const QString& libName, PlantMgr::PGIN_TYPE theType) { QObject* thePlugInRef=0; QString theID=""; QPluginLoader loader(libName); thePlugInRef=loader.instance(); qDebug()<< thePlugInRef->metaObject()->className(); if(thePlugInRef != 0) { switch(theType) { case PLANT: { PlantFactory* theFact = qobject_cast<PlantFactory*>(thePlugInRef); if(theFact != 0) { theID = theFact->getFactoryID(); if(m_plantFactList.contains(theID)>0) { qWarning()<<"Error a plugin with the same ID has been already loaded: "<<libName; } else { m_plantFactList.insert(theID, theFact); qDebug()<<"Loaded the plugin library: "<<libName; } } else { qWarning()<<"Error loading factory for plugin: "; } break; } case DEVICE: { DeviceFactory* theFact = qobject_cast<DeviceFactory*>(thePlugInRef); if(theFact != 0) { QString deviceIDs(theFact->getFactoryID()); QStringList devicesLstIDs = deviceIDs.split('|', QString::SkipEmptyParts); foreach (QString deviceID, devicesLstIDs) { QString theID(deviceID); if(m_devFactList.contains(theID)>0) { qWarning()<<"Error a plugin with the same ID has been already loaded: "<<libName; } else { m_devFactList.insert(theID, theFact); qDebug()<<"Loaded the plugin library: "<<libName; } } } else { qWarning()<<"Error loading factory for plugin: "<<libName; } break; }
void init() { /* * En premier lieu, il faut obtenir les identifiants des * plateformes disponibles. Avant, il faut allouer la mémoire * requise pour contenir ces identifiants, donc il faut savoir * le nombre de plateformes disponibles. */ cl_uint nrPlatforms = 0; clGetPlatformIDs(0, nullptr, &nrPlatforms); if (nrPlatforms == 0) throw std::runtime_error("No OpenCL platform"); /* * Maintenant que le vecteur est de la bonne taille, on obtient * les identifiants eux-mêmes. */ m_platforms.resize(nrPlatforms); clGetPlatformIDs(nrPlatforms, m_platforms.data(), nullptr); /* * Inspection des périphériques (device) pour chaque plateforme. * Ceci se fait aussi en deux étapes: on obtient le nombre de device * pour la plateforme, on alloue le vecteur et finalement on obtient * les identifiants de périphérique. */ for(const auto id: m_platforms) { cl_uint nDev = 0; clGetDeviceIDs(id, CL_DEVICE_TYPE_ALL, 0, nullptr, &nDev); QVector<cl_device_id> deviceIDs(nDev); clGetDeviceIDs(id, CL_DEVICE_TYPE_ALL, nDev, deviceIDs.data(), nullptr); for (const auto dev: deviceIDs) { m_devices.insert(dev, id); } } if (m_devices.size() == 0) throw std::runtime_error("No OpenCL device"); qDebug() << "platforms" << m_platforms; qDebug() << "devices" << m_devices; }
//------------------------------------------------------------------------------ // returns context associated with single device only, // to make it support multiple devices, a list of // <device type, device num> pairs is required cl_context create_cl_context(const std::string& platformName, const std::string& deviceTypeName, int deviceNum) { cl_int status = 0; //1) get platfors and search for platform matching platformName cl_uint numPlatforms = 0; status = clGetPlatformIDs(0, 0, &numPlatforms); check_cl_error(status, "clGetPlatformIDs"); if(numPlatforms < 1) { std::cout << "No OpenCL platforms found" << std::endl; exit(EXIT_SUCCESS); } typedef std::vector< cl_platform_id > PlatformIDs; PlatformIDs platformIDs(numPlatforms); status = clGetPlatformIDs(numPlatforms, &platformIDs[0], 0); check_cl_error(status, "clGetPlatformIDs"); std::vector< char > buf(0x10000, char(0)); cl_platform_id platformID; PlatformIDs::const_iterator pi = platformIDs.begin(); for(; pi != platformIDs.end(); ++pi) { status = clGetPlatformInfo(*pi, CL_PLATFORM_NAME, buf.size(), &buf[0], 0); check_cl_error(status, "clGetPlatformInfo"); if(platformName == &buf[0]) { platformID = *pi; break; } } if(pi == platformIDs.end()) { std::cerr << "ERROR - Couldn't find platform " << platformName << std::endl; exit(EXIT_FAILURE); } //2) get devices of deviceTypeName type and store their ids into // an array then select device id at position deviceNum cl_device_type deviceType; if(deviceTypeName == "default") deviceType = CL_DEVICE_TYPE_DEFAULT; else if(deviceTypeName == "cpu") deviceType = CL_DEVICE_TYPE_CPU; else if(deviceTypeName == "gpu") deviceType = CL_DEVICE_TYPE_GPU; else if(deviceTypeName == "acc") deviceType = CL_DEVICE_TYPE_ACCELERATOR; else if(deviceTypeName == "all") deviceType = CL_DEVICE_TYPE_CPU; else { std::cerr << "ERROR - device type " << deviceTypeName << " unknown" << std::endl; exit(EXIT_FAILURE); } cl_uint numDevices = 0; status = clGetDeviceIDs(platformID, deviceType, 0, 0, &numDevices); check_cl_error(status, "clGetDeviceIDs"); if(numDevices < 1) { std::cerr << "ERROR - Cannot find device of type " << deviceTypeName << std::endl; exit(EXIT_FAILURE); } typedef std::vector< cl_device_id > DeviceIDs; DeviceIDs deviceIDs(numDevices); status = clGetDeviceIDs(platformID, deviceType, numDevices, &deviceIDs[0], 0); check_cl_error(status, "clGetDeviceIDs"); if(deviceNum < 0 || deviceNum >= numDevices) { std::cerr << "ERROR - device number out of range: [0," << (numDevices - 1) << ']' << std::endl; exit(EXIT_FAILURE); } cl_device_id deviceID = deviceIDs[deviceNum]; //3) create and return context cl_context_properties ctxProps[] = { CL_CONTEXT_PLATFORM, cl_context_properties(platformID), 0 }; //only a single device supported cl_context ctx = clCreateContext(ctxProps, 1, &deviceID, &context_callback, 0, &status); check_cl_error(status, "clCreateContext"); return ctx; }