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

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

GUI hooks added, but not all working.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1# global
2import os
3import sys
4import sasmodels
5
6from PyQt4 import QtGui, QtCore
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    checkBoxes = None
31    sas_open_cl = None
32
33    def __init__(self, parent=None):
34        super(GPUOptions, self).__init__(parent)
35        self.parent = parent
36        self.setupUi(self)
37        self.addOpenCLOptions()
38        self.createLinks()
39
40    def addOpenCLOptions(self):
41        """
42        Populate the window with a list of OpenCL options
43        """
44        # Get list of openCL options and add to GUI
45        cl_tuple = _get_clinfo()
46        i = 0
47        self.sas_open_cl = os.environ.get("SAS_OPENCL", "")
48        self.checkBoxes = []
49        for title, descr in cl_tuple:
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
57            self.checkBoxes.append(checkBox)
58            # Expand group and shift items down as more are added
59            self.openCLCheckBoxGroup.resize(391, 60 + i)
60            self.okButton.setGeometry(QtCore.QRect(20, 90 + i, 93, 28))
61            self.resetButton.setGeometry(QtCore.QRect(120, 90 + i, 93, 28))
62            self.testButton.setGeometry(QtCore.QRect(220, 90 + i, 93, 28))
63            self.helpButton.setGeometry(QtCore.QRect(320, 90 + i, 93, 28))
64            self.resize(440, 130 + i)
65            i += 30
66
67    def createLinks(self):
68        """
69        Link actions to function calls
70        """
71        self.testButton.clicked.connect(lambda: self.testButtonClicked())
72        self.helpButton.clicked.connect(lambda: self.helpButtonClicked())
73        self.openCLCheckBoxGroup.clicked.connect(lambda: self.checked())
74
75    def checked(self):
76        """
77        Action triggered when box is selected
78        """
79        selected_box = None
80        for box in self.checkBoxes:
81            if box.isChecked() and box.getText() != self.sas_open_cl:
82                selected_box = box
83            else:
84                box.setChecked(False)
85        if selected_box.getText():
86            self.sas_open_cl = self.option_button[selected_box.title]
87        else:
88            self.sas_open_cl = None
89
90    def testButtonClicked(self):
91        """
92        Run the model tests when the test button is clicked
93        """
94        pass
95
96    def helpButtonClicked(self):
97        """
98        Open the help menu when the help button is clicked
99        """
100        pass
101
102    def reject(self):
103        """
104        Close the window without modifying SAS_OPENCL
105        """
106        self.close()
107        self.parent.gpu_options_widget = GPUOptions(self)
108        self.open()
109
110    def accept(self):
111        """
112        Close the window after modifying the SAS_OPENCL value
113        """
114        # If statement added to handle Reset
115        if self.sas_open_cl:
116            os.environ["SAS_OPENCL"] = self.sas_open_cl
117        else:
118            if "SAS_OPENCL" in os.environ:
119                del os.environ["SAS_OPENCL"]
120
121        # Sasmodels kernelcl doesn't exist when initiated with None
122        if 'sasmodels.kernelcl' in sys.modules:
123            sasmodels.kernelcl.ENV = None
124
125        reload(sasmodels.core)
126        super(GPUOptions, self).accept()
127
128    def closeEvent(self, event):
129        """
130        Overwrite QDialog close method to allow for custom widget close
131        """
132        self.reject()
133
134
135def _get_clinfo():
136    """
137    Reading in information about available OpenCL infrastructure
138    :return:
139    """
140    clinfo = []
141    platforms = []
142    try:
143        import pyopencl as cl
144        platforms = cl.get_platforms()
145    except ImportError:
146        print("pyopencl import failed. Using only CPU computations")
147
148    p_index = 0
149    for platform in platforms:
150        d_index = 0
151        devices = platform.get_devices()
152        for device in devices:
153            if len(devices) > 1 and len(platforms) > 1:
154                combined_index = ":".join([str(p_index), str(d_index)])
155            elif len(platforms) > 1:
156                combined_index = str(p_index)
157            else:
158                combined_index = str(d_index)
159            clinfo.append((combined_index, ":".join([platform.name, device.name])))
160            d_index += 1
161        p_index += 1
162
163    clinfo.append(("None", "No OpenCL"))
164    return clinfo
Note: See TracBrowser for help on using the repository browser.