1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #define _CRT_SECURE_NO_WARNINGS
23
24 #include "OpenCLSupportPlugin.h"
25
26 #include <QLibrary>
27
28 #include <U2Core/AppContext.h>
29 #include <U2Core/AppResources.h>
30 #include <U2Core/GAutoDeleteList.h>
31 #include <U2Core/Log.h>
32 #include <U2Core/Settings.h>
33
34 #include "OpenCLSupportSettingsController.h"
35
36 namespace U2 {
37
U2_PLUGIN_INIT_FUNC()38 extern "C" Q_DECL_EXPORT Plugin *U2_PLUGIN_INIT_FUNC() {
39 OpenCLSupportPlugin *plug = new OpenCLSupportPlugin();
40 return plug;
41 }
42
U2_PLUGIN_VERIFY_FUNC()43 extern "C" Q_DECL_EXPORT bool U2_PLUGIN_VERIFY_FUNC() {
44 {
45 volatile QScopedPointer<OpenCLSupportPlugin> plug(new OpenCLSupportPlugin());
46 Q_UNUSED(plug);
47 }
48 return true;
49 }
50
U2_PLUGIN_FAIL_MASSAGE_FUNC()51 extern "C" Q_DECL_EXPORT QString *U2_PLUGIN_FAIL_MASSAGE_FUNC() {
52 return new QString(OpenCLSupportPlugin::tr("Problem occurred loading the OpenCL driver. Please try to update drivers if \
53 you're going to make calculations on your video card. For details see this page: \
54 <a href=\"%1\">%1</a>")
55 .arg("http://ugene.net/using-video-cards.html"));
56 }
57
58 const char *OpenCLSupportPlugin::RESOURCE_OPENCL_GPU_NAME = "OpenCLGpu";
59
OpenCLSupportPlugin()60 OpenCLSupportPlugin::OpenCLSupportPlugin()
61 : Plugin(tr("OpenCL Support"),
62 tr("Plugin provides support for OpenCL-enabled GPUs.")) {
63 // Temporary disable OpenCL plugin
64 return;
65
66 QString err_str;
67
68 OpenCLGpuRegistry *registry = AppContext::getOpenCLGpuRegistry();
69 registry->setOpenCLHelper(&openCLHelper);
70
71 err = obtainGpusInfo(err_str);
72 if (err_str.isEmpty() && gpus.empty()) {
73 err_str = "No OpenCL-enabled GPUs found.";
74 }
75 if (Error_NoError == err) {
76 loadGpusSettings();
77 registerAvailableGpus();
78 } else {
79 coreLog.details(err_str);
80 }
81
82 // adding settings page
83 if (AppContext::getMainWindow()) {
84 QString settingsPageMsg = getSettingsErrorString(err);
85 AppContext::getAppSettingsGUI()->registerPage(new OpenCLSupportSettingsPageController(settingsPageMsg));
86 }
87
88 // registering gpu resource
89 if (!gpus.empty()) {
90 AppResource *gpuResource = new AppResourceSemaphore(RESOURCE_OPENCL_GPU, gpus.size(), RESOURCE_OPENCL_GPU_NAME);
91 AppResourcePool::instance()->registerResource(gpuResource);
92 }
93 }
~OpenCLSupportPlugin()94 OpenCLSupportPlugin::~OpenCLSupportPlugin() {
95 OpenCLGpuRegistry *registry = AppContext::getOpenCLGpuRegistry();
96 CHECK(nullptr != registry, );
97 registry->saveGpusSettings();
98 unregisterAvailableGpus();
99 AppResourcePool::instance()->unregisterResource(RESOURCE_OPENCL_GPU);
100 registry->setOpenCLHelper(nullptr);
101 }
102
getError() const103 OpenCLSupportPlugin::OpenCLSupportError OpenCLSupportPlugin::getError() const {
104 return err;
105 }
106
getSettingsErrorString(OpenCLSupportError err)107 QString OpenCLSupportPlugin::getSettingsErrorString(OpenCLSupportError err) {
108 switch (err) {
109 case Error_NoError:
110 return QString("");
111
112 case Error_BadDriverLib:
113 return tr("Cannot load OpenCL driver dynamic library.<p> \
114 Install the latest video GPU driver.");
115
116 case Error_OpenCLError:
117 return tr("An error has occurred while obtaining information \
118 about installed OpenCL GPUs.<br>\
119 See OpenCL Support plugin log for details.");
120
121 default:
122 assert(false);
123 return QString();
124 }
125 }
126
obtainGpusInfo(QString & errStr)127 OpenCLSupportPlugin::OpenCLSupportError OpenCLSupportPlugin::obtainGpusInfo(QString &errStr) {
128 // load driver library
129 if (!openCLHelper.isLoaded()) {
130 errStr = openCLHelper.getErrorString();
131 return Error_BadDriverLib;
132 }
133
134 cl_int errCode = 0;
135
136 coreLog.details(tr("Initializing OpenCL"));
137
138 // numEntries is the number of cl_platform_id entries that can be added to platforms
139 cl_uint numPlatformEntries = 15;
140 gauto_array<cl_platform_id> platformIDs(new cl_platform_id[numPlatformEntries]);
141 cl_uint numPlatforms = 0;
142
143 errCode = openCLHelper.clGetPlatformIDs_p(numPlatformEntries, platformIDs.get(), &numPlatforms);
144 if (hasOPENCLError(errCode, errStr)) {
145 return Error_OpenCLError;
146 }
147 coreLog.details(tr("Number of OpenCL platforms: %1").arg(numPlatforms));
148
149 // Get each platform info
150 for (unsigned int i = 0; i < numPlatforms; i++) {
151 // numEntries is the number of cl_platform_id entries that can be added to platforms
152 cl_uint numDeviceEntries = 15;
153 gauto_array<cl_device_id> deviceIDs(new cl_device_id[numDeviceEntries]);
154
155 cl_uint numDevices = 0;
156
157 errCode = openCLHelper.clGetDeviceIDs_p(platformIDs.get()[i], CL_DEVICE_TYPE_GPU, numDeviceEntries, deviceIDs.get(), &numDevices);
158 if (hasOPENCLError(errCode, errStr)) {
159 return Error_OpenCLError;
160 }
161 coreLog.details(tr("Number of OpenCL devices: %1").arg(numDevices));
162
163 for (unsigned int k = 0; k < numDevices; k++) {
164 cl_device_id deviceId = deviceIDs.get()[k];
165
166 int maximumParamLength = 200;
167 gauto_array<char> paramValue(new char[maximumParamLength]);
168 size_t actualParamSize;
169 int actualParamLength = 0;
170
171 //******************************
172 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_VENDOR, sizeof(char) * maximumParamLength, paramValue.get(), &actualParamSize);
173 if (hasOPENCLError(errCode, errStr)) {
174 return Error_OpenCLError;
175 }
176
177 actualParamLength = (int)(actualParamSize / sizeof(char));
178 gauto_array<char> vendorNameValue(new char[actualParamLength + 1]);
179 strncpy(vendorNameValue.get(), paramValue.get(), actualParamLength);
180
181 QString vendorName = vendorNameValue.get();
182 //******************************
183 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_NAME, sizeof(char) * maximumParamLength, paramValue.get(), &actualParamSize);
184 if (hasOPENCLError(errCode, errStr)) {
185 return Error_OpenCLError;
186 }
187
188 actualParamLength = int(actualParamSize / sizeof(char));
189 gauto_array<char> deviceNameValue(new char[actualParamLength + 1]);
190 strncpy(deviceNameValue.get(), paramValue.get(), actualParamLength);
191
192 QString deviceName = deviceNameValue.get();
193 //******************************
194 cl_ulong globalMemSize = 0;
195
196 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong), &globalMemSize, &actualParamSize);
197 if (hasOPENCLError(errCode, errStr)) {
198 return Error_OpenCLError;
199 }
200
201 cl_ulong maxAllocateMemorySize = 0;
202
203 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong), &maxAllocateMemorySize, &actualParamSize);
204 if (hasOPENCLError(errCode, errStr)) {
205 return Error_OpenCLError;
206 }
207
208 //******************************
209 cl_ulong localMemSize = 0;
210
211 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_LOCAL_MEM_SIZE, sizeof(cl_ulong), &localMemSize, &actualParamSize);
212 if (hasOPENCLError(errCode, errStr)) {
213 return Error_OpenCLError;
214 }
215 //******************************
216 cl_uint maxClockFrequency = 0;
217
218 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(cl_uint), &maxClockFrequency, &actualParamSize);
219 if (hasOPENCLError(errCode, errStr)) {
220 return Error_OpenCLError;
221 }
222 //******************************
223 cl_uint maxComputeUnits = 10;
224
225 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &maxComputeUnits, &actualParamSize);
226 if (hasOPENCLError(errCode, errStr)) {
227 return Error_OpenCLError;
228 }
229 //******************************
230 cl_uint maxWorkItemDimensions = 0;
231
232 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &maxWorkItemDimensions, &actualParamSize);
233 if (hasOPENCLError(errCode, errStr)) {
234 return Error_OpenCLError;
235 }
236 //******************************
237 size_t maxWorkGroupSize = 0;
238
239 errCode = openCLHelper.clGetDeviceInfo_p(deviceId, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), &maxWorkGroupSize, &actualParamSize);
240 if (hasOPENCLError(errCode, errStr)) {
241 return Error_OpenCLError;
242 }
243
244 cl_context deviceContext = openCLHelper.clCreateContext_p(0, 1, &deviceId, nullptr, nullptr, &errCode);
245 if (hasOPENCLError(errCode, errStr)) {
246 return Error_OpenCLError;
247 }
248
249 // create OpenCL model
250 OpenCLGpuModel *openCLGpuModel = new OpenCLGpuModel(vendorName + " " + deviceName,
251 cl_context(deviceContext),
252 cl_device_id(deviceId),
253 (qint64)platformIDs.get()[i],
254 globalMemSize,
255 maxAllocateMemorySize,
256 localMemSize,
257 maxComputeUnits,
258 maxWorkGroupSize,
259 maxClockFrequency);
260 gpus.push_back(openCLGpuModel);
261 coreLog.info(tr("Registering OpenCL-enabled GPU: %1, global mem: %2 Mb, \
262 local mem: %3 Kb, max compute units: %4, \
263 max work group size: %5, max frequency: %6 Hz")
264 .arg(openCLGpuModel->getName())
265 .arg(openCLGpuModel->getGlobalMemorySizeBytes() / (1024 * 1024))
266 .arg(openCLGpuModel->getLocalMemorySizeBytes() / 1024)
267 .arg(openCLGpuModel->getMaxComputeUnits())
268 .arg(openCLGpuModel->getMaxWorkGroupSize())
269 .arg(openCLGpuModel->getMaxClockFrequency()));
270 }
271 }
272
273 return Error_NoError;
274 }
275
hasOPENCLError(cl_int errCode,QString & errMessage)276 bool OpenCLSupportPlugin::hasOPENCLError(cl_int errCode, QString &errMessage) {
277 // TODO: print details error message
278 if (errCode != CL_SUCCESS) {
279 errMessage = tr("OpenCL error code (%1)").arg(errCode);
280 return true;
281 } else {
282 return false;
283 }
284 }
285
registerAvailableGpus()286 void OpenCLSupportPlugin::registerAvailableGpus() {
287 foreach (OpenCLGpuModel *m, gpus) {
288 AppContext::getOpenCLGpuRegistry()->registerOpenCLGpu(m);
289 }
290 }
291
unregisterAvailableGpus()292 void OpenCLSupportPlugin::unregisterAvailableGpus() {
293 foreach (OpenCLGpuModel *m, gpus) {
294 AppContext::getOpenCLGpuRegistry()->unregisterOpenCLGpu(m);
295 }
296 }
297
loadGpusSettings()298 void OpenCLSupportPlugin::loadGpusSettings() {
299 CHECK(!gpus.isEmpty(), );
300
301 Settings *s = AppContext::getSettings();
302 QString enabledGpuName = s->getValue(OPENCL_GPU_REGISTRY_SETTINGS_GPU_ENABLED, QVariant()).toString();
303 CHECK_EXT(!enabledGpuName.isEmpty(), gpus.first()->setEnabled(true), );
304
305 OpenCLGpuModel *enabledGpu = AppContext::getOpenCLGpuRegistry()->getGpuByName(enabledGpuName);
306 if (nullptr != enabledGpu) {
307 SAFE_POINT(gpus.contains(enabledGpu), "The GPU is absent", );
308
309 enabledGpu->setEnabled(true);
310 } else {
311 gpus.first()->setEnabled(true);
312 }
313 }
314
315 } // namespace U2
316