1 | # global |
---|
2 | import os |
---|
3 | import sys |
---|
4 | import sasmodels |
---|
5 | |
---|
6 | from PyQt4 import QtGui, QtCore, QtWebKit |
---|
7 | from sas.qtgui.Perspectives.Fitting.UI.GPUOptionsUI import Ui_GPUOptions |
---|
8 | |
---|
9 | try: |
---|
10 | _fromUtf8 = QtCore.QString.fromUtf8 |
---|
11 | except AttributeError: |
---|
12 | def _fromUtf8(s): |
---|
13 | return s |
---|
14 | |
---|
15 | try: |
---|
16 | _encoding = QtGui.QApplication.UnicodeUTF8 |
---|
17 | def _translate(context, text, disambig): |
---|
18 | return QtGui.QApplication.translate(context, text, disambig, _encoding) |
---|
19 | except AttributeError: |
---|
20 | def _translate(context, text, disambig): |
---|
21 | return QtGui.QApplication.translate(context, text, disambig) |
---|
22 | |
---|
23 | |
---|
24 | class 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 | self.setWindowFlags(self.windowFlags() | QtCore.Qt.CustomizeWindowHint) |
---|
48 | self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint) |
---|
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 | # Expand group and shift items down as more are added |
---|
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)) |
---|
63 | self.resize(440, 130 + i) |
---|
64 | i += 30 |
---|
65 | |
---|
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()) |
---|
72 | for item in self.openCLCheckBoxGroup.findChildren(QtGui.QCheckBox): |
---|
73 | item.clicked.connect(lambda: self.checked()) |
---|
74 | |
---|
75 | def checked(self): |
---|
76 | """ |
---|
77 | Action triggered when box is selected |
---|
78 | """ |
---|
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 == "")): |
---|
83 | box.setChecked(False) |
---|
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()) |
---|
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 | """ |
---|
97 | # TODO: Do something |
---|
98 | pass |
---|
99 | |
---|
100 | def helpButtonClicked(self): |
---|
101 | """ |
---|
102 | Open the help menu when the help button is clicked |
---|
103 | """ |
---|
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() |
---|
110 | |
---|
111 | def reject(self): |
---|
112 | """ |
---|
113 | Close the window without modifying SAS_OPENCL |
---|
114 | """ |
---|
115 | self.closeEvent(None) |
---|
116 | self.parent.gpu_options_widget.open() |
---|
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) |
---|
134 | super(GPUOptions, self).reject() |
---|
135 | |
---|
136 | def closeEvent(self, event): |
---|
137 | """ |
---|
138 | Overwrite QDialog close method to allow for custom widget close |
---|
139 | """ |
---|
140 | self.close() |
---|
141 | self.parent.gpu_options_widget = GPUOptions(self) |
---|
142 | |
---|
143 | |
---|
144 | def _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 |
---|