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

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

Open gpu options help when button is clicked.

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