source: sasview/src/sas/qtgui/Perspectives/Fitting/GPUOptions.py @ 1543f0c

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 1543f0c was 1543f0c, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Updated GPUOptions for py3/qt5

  • Property mode set to 100644
File size: 7.7 KB
Line 
1# global
2import os
3import sys
4import sasmodels
5import json
6import platform
7
8import sas.qtgui.Utilities.GuiUtils as GuiUtils
9from PyQt5 import QtGui, QtCore, QtWebKit, QtWidgets
10from sas.qtgui.Perspectives.Fitting.UI.GPUOptionsUI import Ui_GPUOptions
11from sas.qtgui.Perspectives.Fitting.UI.GPUTestResultsUI import Ui_GPUTestResults
12
13try:
14    _fromUtf8 = QtCore.QString.fromUtf8
15except AttributeError:
16    def _fromUtf8(s):
17        return s
18
19try:
20    _encoding = QtWidgets.QApplication.UnicodeUTF8
21    def _translate(context, text, disambig):
22        return QtWidgets.QApplication.translate(context, text, disambig, _encoding)
23except AttributeError:
24    def _translate(context, text, disambig):
25        return QtWidgets.QApplication.translate(context, text, disambig)
26
27
28class GPUOptions(QtWidgets.QDialog, Ui_GPUOptions):
29    """
30    OpenCL Dialog to select the desired OpenCL driver
31    """
32
33    clicked = False
34    sas_open_cl = None
35
36    def __init__(self, parent=None):
37        super(GPUOptions, self).__init__(parent)
38        self.parent = parent
39        self.setupUi(self)
40        self.addOpenCLOptions()
41        self.createLinks()
42
43    def addOpenCLOptions(self):
44        """
45        Populate the window with a list of OpenCL options
46        """
47        # Get list of openCL options and add to GUI
48        cl_tuple = _get_clinfo()
49        self.sas_open_cl = os.environ.get("SAS_OPENCL", "")
50        for title, descr in cl_tuple:
51            # Create an item for each openCL option
52            check_box = QtWidgets.QCheckBox()
53            check_box.setObjectName(_fromUtf8(descr))
54            check_box.setText(_translate("GPUOptions", descr, None))
55            self.optionsLayout.addWidget(check_box)
56            if (descr == self.sas_open_cl) or (
57                            title == "None" and not self.clicked):
58                check_box.click()
59                self.clicked = True
60        self.openCLCheckBoxGroup.setMinimumWidth(self.optionsLayout.sizeHint().width()+10)
61
62    def createLinks(self):
63        """
64        Link user interactions to function calls
65        """
66        self.testButton.clicked.connect(self.testButtonClicked)
67        self.helpButton.clicked.connect(self.helpButtonClicked)
68        for item in self.openCLCheckBoxGroup.findChildren(QtWidgets.QCheckBox):
69            item.clicked.connect(self.checked)
70
71    def checked(self):
72        """
73        Only allow a single check box to be selected. Uncheck others.
74        """
75        checked = None
76        for box in self.openCLCheckBoxGroup.findChildren(QtWidgets.QCheckBox):
77            if box.isChecked() and (str(box.text()) == self.sas_open_cl or (
78                    str(box.text()) == "No OpenCL" and self.sas_open_cl == "")):
79                box.setChecked(False)
80            elif box.isChecked():
81                checked = box
82        if hasattr(checked, "text"):
83            self.sas_open_cl = str(checked.text())
84        else:
85            self.sas_open_cl = None
86
87    def set_sas_open_cl(self):
88        """
89        Set SAS_OPENCL value when tests run or OK button clicked
90        """
91        no_opencl_msg = False
92        if self.sas_open_cl:
93            os.environ["SAS_OPENCL"] = self.sas_open_cl
94            if self.sas_open_cl.lower() == "none":
95                no_opencl_msg = True
96        else:
97            if "SAS_OPENCL" in os.environ:
98                del os.environ["SAS_OPENCL"]
99        # Sasmodels kernelcl doesn't exist when initiated with None
100        if 'sasmodels.kernelcl' in sys.modules:
101            sasmodels.kernelcl.ENV = None
102        from importlib import reload # assumed Python > 3.3
103        reload(sasmodels.core)
104        return no_opencl_msg
105
106    def testButtonClicked(self):
107        """
108        Run sasmodels check from here and report results from
109        """
110
111        no_opencl_msg = self.set_sas_open_cl()
112
113        # Only import when tests are run
114        from sasmodels.model_test import model_tests
115
116        try:
117            from sasmodels.kernelcl import environment
118            env = environment()
119            clinfo = [(ctx.devices[0].platform.vendor,
120                       ctx.devices[0].platform.version,
121                       ctx.devices[0].vendor,
122                       ctx.devices[0].name,
123                       ctx.devices[0].version)
124                      for ctx in env.context]
125        except ImportError:
126            clinfo = None
127
128        failures = []
129        tests_completed = 0
130        for test in model_tests():
131            try:
132                test()
133            except Exception:
134                failures.append(test.description)
135
136            tests_completed += 1
137
138        info = {
139            'version': sasmodels.__version__,
140            'platform': platform.uname(),
141            'opencl': clinfo,
142            'failing tests': failures,
143        }
144
145        msg = str(tests_completed) + ' tests completed.\n'
146        if len(failures) > 0:
147            msg += str(len(failures)) + ' tests failed.\n'
148            msg += 'Failing tests: '
149            msg += json.dumps(info['failing tests'])
150            msg += "\n"
151        else:
152            msg += "All tests passed!\n"
153
154        msg += "\nPlatform Details:\n\n"
155        msg += "Sasmodels version: "
156        msg += info['version'] + "\n"
157        msg += "\nPlatform used: "
158        msg += json.dumps(info['platform']) + "\n"
159        if no_opencl_msg:
160            msg += "\nOpenCL driver: None"
161        else:
162            msg += "\nOpenCL driver: "
163            msg += json.dumps(info['opencl']) + "\n"
164        GPUTestResults(self, msg)
165
166    def helpButtonClicked(self):
167        """
168        Open the help menu when the help button is clicked
169        """
170        help_location = GuiUtils.HELP_DIRECTORY_LOCATION
171        help_location += "/user/sasgui/perspectives/fitting/gpu_setup.html"
172        help_location += "#device-selection"
173        self.helpView = QtWebKit.QWebView()
174        self.helpView.load(QtCore.QUrl(help_location))
175        self.helpView.show()
176
177    def reject(self):
178        """
179        Close the window without modifying SAS_OPENCL
180        """
181        self.closeEvent(None)
182        self.parent.gpu_options_widget.open()
183
184    def accept(self):
185        """
186        Close the window after modifying the SAS_OPENCL value
187        """
188        self.set_sas_open_cl()
189        self.closeEvent(None)
190
191    def closeEvent(self, event):
192        """
193        Overwrite QDialog close method to allow for custom widget close
194        """
195        self.close()
196        self.parent.gpu_options_widget = GPUOptions(self.parent)
197
198
199class GPUTestResults(QtWidgets.QDialog, Ui_GPUTestResults):
200    """
201    OpenCL Dialog to modify the OpenCL options
202    """
203    def __init__(self, parent, msg):
204        super(GPUTestResults, self).__init__(parent)
205        self.setupUi(self)
206        self.resultsText.setText(_translate("GPUTestResults", msg, None))
207        #self.setFixedSize(self.size())
208        self.open()
209
210
211def _get_clinfo():
212    """
213    Read in information about available OpenCL infrastructure
214    """
215    clinfo = []
216    cl_platforms = []
217    try:
218        import pyopencl as cl
219        cl_platforms = cl.get_platforms()
220    except ImportError:
221        print("pyopencl import failed. Using only CPU computations")
222
223    p_index = 0
224    for cl_platform in cl_platforms:
225        d_index = 0
226        cl_platforms = cl_platform.get_devices()
227        for cl_platform in cl_platforms:
228            if len(cl_platforms) > 1 and len(cl_platforms) > 1:
229                combined_index = ":".join([str(p_index), str(d_index)])
230            elif len(cl_platforms) > 1:
231                combined_index = str(p_index)
232            else:
233                combined_index = str(d_index)
234            clinfo.append((combined_index, ":".join([cl_platform.name,
235                                                     cl_platform.name])))
236            d_index += 1
237        p_index += 1
238
239    clinfo.append(("None", "No OpenCL"))
240    return clinfo
Note: See TracBrowser for help on using the repository browser.