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

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 c82fd8f was c82fd8f, checked in by krzywon, 7 years ago

Ensure the selected openCL selection retains its state when opening and closing the window.

  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[06ce180]1# global
[a6cd8d1]2import os
3import sys
4import sasmodels
[06ce180]5
[83710be]6from PyQt4 import QtGui, QtCore, QtWebKit
[06ce180]7from sas.qtgui.Perspectives.Fitting.UI.GPUOptionsUI import Ui_GPUOptions
8
[14fa542]9try:
10    _fromUtf8 = QtCore.QString.fromUtf8
11except AttributeError:
12    def _fromUtf8(s):
13        return s
[06ce180]14
[14fa542]15try:
16    _encoding = QtGui.QApplication.UnicodeUTF8
17    def _translate(context, text, disambig):
18        return QtGui.QApplication.translate(context, text, disambig, _encoding)
19except AttributeError:
20    def _translate(context, text, disambig):
21        return QtGui.QApplication.translate(context, text, disambig)
[06ce180]22
23
24class GPUOptions(QtGui.QDialog, Ui_GPUOptions):
25    """
26    OpenCL Dialog to modify the OpenCL options
27    """
28
[a6cd8d1]29    clicked = False
30    sas_open_cl = None
31
[06ce180]32    def __init__(self, parent=None):
33        super(GPUOptions, self).__init__(parent)
[a6cd8d1]34        self.parent = parent
[06ce180]35        self.setupUi(self)
[a6cd8d1]36        self.addOpenCLOptions()
37        self.createLinks()
[06ce180]38
[a6cd8d1]39    def addOpenCLOptions(self):
40        """
41        Populate the window with a list of OpenCL options
42        """
[14fa542]43        # Get list of openCL options and add to GUI
[a6cd8d1]44        cl_tuple = _get_clinfo()
[14fa542]45        i = 0
[a6cd8d1]46        self.sas_open_cl = os.environ.get("SAS_OPENCL", "")
[c82fd8f]47        self.setWindowFlags(self.windowFlags() | QtCore.Qt.CustomizeWindowHint)
48        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint)
[14fa542]49        for title, descr in cl_tuple:
[a6cd8d1]50            checkBox = QtGui.QCheckBox(self.openCLCheckBoxGroup)
51            checkBox.setGeometry(20, 20 + i, 351, 30)
52            checkBox.setObjectName(_fromUtf8(descr))
53            checkBox.setText(_translate("GPUOptions", descr, None))
54            if (descr == self.sas_open_cl) or (title == "None" and not self.clicked):
55                checkBox.click()
56                self.clicked = True
[14fa542]57            # Expand group and shift items down as more are added
[a6cd8d1]58            self.openCLCheckBoxGroup.resize(391, 60 + i)
59            self.okButton.setGeometry(QtCore.QRect(20, 90 + i, 93, 28))
60            self.resetButton.setGeometry(QtCore.QRect(120, 90 + i, 93, 28))
61            self.testButton.setGeometry(QtCore.QRect(220, 90 + i, 93, 28))
62            self.helpButton.setGeometry(QtCore.QRect(320, 90 + i, 93, 28))
[14fa542]63            self.resize(440, 130 + i)
64            i += 30
[06ce180]65
[a6cd8d1]66    def createLinks(self):
67        """
68        Link actions to function calls
69        """
70        self.testButton.clicked.connect(lambda: self.testButtonClicked())
71        self.helpButton.clicked.connect(lambda: self.helpButtonClicked())
[1df1025]72        for item in self.openCLCheckBoxGroup.findChildren(QtGui.QCheckBox):
73            item.clicked.connect(lambda: self.checked())
[a6cd8d1]74
75    def checked(self):
76        """
77        Action triggered when box is selected
78        """
[1df1025]79        checked = None
80        for box in self.openCLCheckBoxGroup.findChildren(QtGui.QCheckBox):
81            if box.isChecked() and (str(box.text()) == self.sas_open_cl or (
82                            str(box.text()) == "No OpenCL" and self.sas_open_cl == "")):
[a6cd8d1]83                box.setChecked(False)
[1df1025]84            elif not box.isChecked():
85                pass
86            else:
87                checked = box
88        if hasattr(checked, "text"):
89            self.sas_open_cl = str(checked.text())
[a6cd8d1]90        else:
91            self.sas_open_cl = None
92
93    def testButtonClicked(self):
94        """
95        Run the model tests when the test button is clicked
96        """
[83710be]97        # TODO: Do something
[a6cd8d1]98        pass
99
100    def helpButtonClicked(self):
101        """
102        Open the help menu when the help button is clicked
103        """
[83710be]104        tree_location = "user/sasgui/perspectives/fitting/gpu_setup.html"
105        anchor = "#device-selection"
106        self.helpView = QtWebKit.QWebView()
107        help_location = tree_location + anchor
108        self.helpView.load(QtCore.QUrl(help_location))
109        self.helpView.show()
[a6cd8d1]110
111    def reject(self):
112        """
113        Close the window without modifying SAS_OPENCL
114        """
[c82fd8f]115        self.closeEvent(None)
116        self.parent.gpu_options_widget.open()
[a6cd8d1]117
118    def accept(self):
119        """
120        Close the window after modifying the SAS_OPENCL value
121        """
122        # If statement added to handle Reset
123        if self.sas_open_cl:
124            os.environ["SAS_OPENCL"] = self.sas_open_cl
125        else:
126            if "SAS_OPENCL" in os.environ:
127                del os.environ["SAS_OPENCL"]
128
129        # Sasmodels kernelcl doesn't exist when initiated with None
130        if 'sasmodels.kernelcl' in sys.modules:
131            sasmodels.kernelcl.ENV = None
132
133        reload(sasmodels.core)
[c82fd8f]134        super(GPUOptions, self).reject()
[a6cd8d1]135
136    def closeEvent(self, event):
137        """
138        Overwrite QDialog close method to allow for custom widget close
139        """
[c82fd8f]140        self.close()
141        self.parent.gpu_options_widget = GPUOptions(self)
[a6cd8d1]142
143
144def _get_clinfo():
145    """
146    Reading in information about available OpenCL infrastructure
147    :return:
148    """
149    clinfo = []
150    platforms = []
151    try:
152        import pyopencl as cl
153        platforms = cl.get_platforms()
154    except ImportError:
155        print("pyopencl import failed. Using only CPU computations")
156
157    p_index = 0
158    for platform in platforms:
159        d_index = 0
160        devices = platform.get_devices()
161        for device in devices:
162            if len(devices) > 1 and len(platforms) > 1:
163                combined_index = ":".join([str(p_index), str(d_index)])
164            elif len(platforms) > 1:
165                combined_index = str(p_index)
166            else:
167                combined_index = str(d_index)
168            clinfo.append((combined_index, ":".join([platform.name, device.name])))
169            d_index += 1
170        p_index += 1
171
172    clinfo.append(("None", "No OpenCL"))
173    return clinfo
Note: See TracBrowser for help on using the repository browser.