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 #ifdef OPENCL_SUPPORT
23 
24 #    include <algorithm>
25 #    include <functional>
26 
27 #    include <U2Core/AppContext.h>
28 #    include <U2Core/Settings.h>
29 #    include <U2Core/U2SafePoints.h>
30 
31 #    include "OpenCLGpuRegistry.h"
32 
33 namespace U2 {
34 
OpenCLGpuRegistry()35 OpenCLGpuRegistry::OpenCLGpuRegistry()
36     : openCLHelper(nullptr) {
37 }
38 
~OpenCLGpuRegistry()39 OpenCLGpuRegistry::~OpenCLGpuRegistry() {
40     qDeleteAll(gpus.values());
41 }
42 
registerOpenCLGpu(OpenCLGpuModel * gpu)43 void OpenCLGpuRegistry::registerOpenCLGpu(OpenCLGpuModel *gpu) {
44     assert(!gpus.contains(gpu->getId()));
45     gpus.insert(gpu->getId(), gpu);
46 }
47 
unregisterOpenCLGpu(OpenCLGpuModel * gpu)48 void OpenCLGpuRegistry::unregisterOpenCLGpu(OpenCLGpuModel *gpu) {
49     CHECK(gpus.contains(gpu->getId()), );
50     delete gpus.take(gpu->getId());
51 }
52 
getGpuById(cl_device_id id) const53 OpenCLGpuModel *OpenCLGpuRegistry::getGpuById(cl_device_id id) const {
54     return gpus.value(id, 0);
55 }
56 
getGpuByName(const QString & name) const57 OpenCLGpuModel *OpenCLGpuRegistry::getGpuByName(const QString &name) const {
58     OpenCLGpuModel *gpu = nullptr;
59     foreach (OpenCLGpuModel *m, gpus.values()) {
60         CHECK_CONTINUE(m->getName() == name);
61 
62         gpu = m;
63         break;
64     }
65 
66     return gpu;
67 }
68 
getRegisteredGpus() const69 QList<OpenCLGpuModel *> OpenCLGpuRegistry::getRegisteredGpus() const {
70     return gpus.values();
71 }
72 
getEnabledGpu() const73 OpenCLGpuModel *OpenCLGpuRegistry::getEnabledGpu() const {
74     QList<OpenCLGpuModel *> registeredGpus = getRegisteredGpus();
75 
76     OpenCLGpuModel *enabledGpu = nullptr;
77     foreach (OpenCLGpuModel *m, registeredGpus) {
78         if (m && m->isEnabled()) {
79             enabledGpu = m;
80             break;
81         }
82     }
83 
84     return enabledGpu;
85 }
86 
getEnabledGpuName() const87 QString OpenCLGpuRegistry::getEnabledGpuName() const {
88     OpenCLGpuModel *enabledGpu = getEnabledGpu();
89     CHECK(nullptr != enabledGpu, QString());
90 
91     return enabledGpu->getName();
92 }
93 
acquireEnabledGpuIfReady()94 OpenCLGpuModel *OpenCLGpuRegistry::acquireEnabledGpuIfReady() {
95     OpenCLGpuModel *model = nullptr;
96     foreach (OpenCLGpuModel *gpuModel, gpus.values()) {
97         CHECK_CONTINUE(gpuModel->isEnabled());
98         CHECK_BREAK(gpuModel->isReady());
99 
100         gpuModel->setAcquired(true);
101         model = gpuModel;
102     }
103 
104     return model;
105 }
106 
saveGpusSettings() const107 void OpenCLGpuRegistry::saveGpusSettings() const {
108     Settings *s = AppContext::getSettings();
109     foreach (OpenCLGpuModel *m, gpus) {
110         CHECK_CONTINUE(m->isEnabled());
111 
112         s->setValue(OPENCL_GPU_REGISTRY_SETTINGS_GPU_ENABLED, QVariant(m->getName()));
113         break;
114     }
115 }
116 
117 }  // namespace U2
118 
119 #endif /*OPENCL_SUPPORT*/
120