source: sasview/src/sas/qtgui/Perspectives/Fitting/GPUOptions.py @ 133812c7

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 133812c7 was 133812c7, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 5 years ago

Merged ESS_GUI

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