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

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

SAS_OPENCL updates properly.

  • Property mode set to 100644
File size: 5.1 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    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        pass
96
97    def helpButtonClicked(self):
98        """
99        Open the help menu when the help button is clicked
100        """
101        pass
102
103    def reject(self):
104        """
105        Close the window without modifying SAS_OPENCL
106        """
107        self.close()
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.parent.gpu_options_widget = reload(GPUOptions(self))
133        self.reject()
134
135
136def _get_clinfo():
137    """
138    Reading in information about available OpenCL infrastructure
139    :return:
140    """
141    clinfo = []
142    platforms = []
143    try:
144        import pyopencl as cl
145        platforms = cl.get_platforms()
146    except ImportError:
147        print("pyopencl import failed. Using only CPU computations")
148
149    p_index = 0
150    for platform in platforms:
151        d_index = 0
152        devices = platform.get_devices()
153        for device in devices:
154            if len(devices) > 1 and len(platforms) > 1:
155                combined_index = ":".join([str(p_index), str(d_index)])
156            elif len(platforms) > 1:
157                combined_index = str(p_index)
158            else:
159                combined_index = str(d_index)
160            clinfo.append((combined_index, ":".join([platform.name, device.name])))
161            d_index += 1
162        p_index += 1
163
164    clinfo.append(("None", "No OpenCL"))
165    return clinfo
Note: See TracBrowser for help on using the repository browser.