1 | ''' |
---|
2 | Module provides dialog for setting SAS_OPENCL variable, which defines |
---|
3 | device choice for OpenCL calculation |
---|
4 | |
---|
5 | Created on Nov 29, 2016 |
---|
6 | |
---|
7 | @author: wpotrzebowski |
---|
8 | ''' |
---|
9 | |
---|
10 | import os |
---|
11 | import warnings |
---|
12 | import wx |
---|
13 | import logging |
---|
14 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
15 | import imp |
---|
16 | |
---|
17 | class GpuOptions(wx.Dialog): |
---|
18 | """ |
---|
19 | "OpenCL options" Dialog Box |
---|
20 | |
---|
21 | Provides dialog for setting SAS_OPENCL variable, which defines |
---|
22 | device choice for OpenCL calculation |
---|
23 | |
---|
24 | """ |
---|
25 | |
---|
26 | def __init__(self, *args, **kwds): |
---|
27 | |
---|
28 | # from sas.sasgui.guiframe.customdir import SetupCustom |
---|
29 | # from sas.sasgui.guiframe.gui_manager import get_app_dir, _find_local_config |
---|
30 | # |
---|
31 | # PATH_APP = get_app_dir() |
---|
32 | # c_conf_dir = SetupCustom().setup_dir(PATH_APP) |
---|
33 | # self.custom_config = _find_local_config('custom_config', c_conf_dir) |
---|
34 | # if self.custom_config is None: |
---|
35 | # self.custom_config = _find_local_config('custom_config', os.getcwd()) |
---|
36 | # if self.custom_config is None: |
---|
37 | # msgConfig = "Custom_config file was not imported" |
---|
38 | # logging.info(msgConfig) |
---|
39 | # else: |
---|
40 | # logging.info("using custom_config in %s" % os.getcwd()) |
---|
41 | # else: |
---|
42 | # logging.info("using custom_config from %s" % c_conf_dir) |
---|
43 | |
---|
44 | # SAS_OPENCL_CUSTOM = None |
---|
45 | # if self.custom_config.SAS_OPENCL: |
---|
46 | # SAS_OPENCL_CUSTOM = self.custom_config.SAS_OPENCL |
---|
47 | |
---|
48 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
---|
49 | wx.Dialog.__init__(self, *args, **kwds) |
---|
50 | |
---|
51 | clinfo = self._get_clinfo() |
---|
52 | |
---|
53 | self.panel1 = wx.Panel(self, -1) |
---|
54 | static_box1 = wx.StaticBox(self.panel1, -1, "Available OpenCL Options:") |
---|
55 | |
---|
56 | boxsizer = wx.BoxSizer(orient=wx.VERTICAL) |
---|
57 | self.option_button = {} |
---|
58 | self.buttons = [] |
---|
59 | #Check if SAS_OPENCL is already set as enviromental variable |
---|
60 | self.sas_opencl = os.environ.get("SAS_OPENCL","") |
---|
61 | #self.sas_opencl = os.environ["SAS_OPENCL"] \ |
---|
62 | # if "SAS_OPENCL" in os.environ else SAS_OPENCL_CUSTOM |
---|
63 | |
---|
64 | for clopt in clinfo: |
---|
65 | button = wx.CheckBox(self.panel1, -1, label=clopt[1], name=clopt[1]) |
---|
66 | |
---|
67 | if clopt != "No OpenCL": |
---|
68 | self.option_button[clopt[1]] = clopt[0] |
---|
69 | if self.sas_opencl == clopt[0]: |
---|
70 | button.SetValue(1) |
---|
71 | else: |
---|
72 | self.option_button[clopt] = "None" |
---|
73 | if self.sas_opencl.lower() == "none": |
---|
74 | button.SetValue(1) |
---|
75 | |
---|
76 | self.Bind(wx.EVT_CHECKBOX, self.on_check, id=button.GetId()) |
---|
77 | self.buttons.append(button) |
---|
78 | boxsizer.Add(button, 0, 0) |
---|
79 | |
---|
80 | fit_hsizer = wx.StaticBoxSizer(static_box1, orient=wx.VERTICAL) |
---|
81 | fit_hsizer.Add(boxsizer, 0, wx.ALL, 5) |
---|
82 | |
---|
83 | self.panel1.SetSizer(fit_hsizer) |
---|
84 | |
---|
85 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
86 | self.vbox.Add(self.panel1, 0, wx.ALL, 10) |
---|
87 | |
---|
88 | accept_btn = wx.Button(self, wx.ID_OK) |
---|
89 | accept_btn.SetToolTipString("Accept new OpenCL settings. This will" |
---|
90 | " overwrite SAS_OPENCL variable if set") |
---|
91 | |
---|
92 | help_id = wx.NewId() |
---|
93 | help_btn = wx.Button(self, help_id, 'Help') |
---|
94 | help_btn.SetToolTipString("Help on the GPU options") |
---|
95 | |
---|
96 | reset_id = wx.NewId() |
---|
97 | reset_btn = wx.Button(self, reset_id, 'Reset') |
---|
98 | reset_btn.SetToolTipString("Restore initial settings") |
---|
99 | |
---|
100 | test_id = wx.NewId() |
---|
101 | test_btn = wx.Button(self, test_id, 'Test') |
---|
102 | test_btn.SetToolTipString("Test if models compile on the given infrastructure") |
---|
103 | |
---|
104 | self.Bind(wx.EVT_BUTTON, self.on_OK, accept_btn) |
---|
105 | self.Bind(wx.EVT_BUTTON, self.on_test, test_btn) |
---|
106 | self.Bind(wx.EVT_BUTTON, self.on_reset, reset_btn) |
---|
107 | self.Bind(wx.EVT_BUTTON, self.on_help, help_btn) |
---|
108 | |
---|
109 | |
---|
110 | btn_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
111 | btn_sizer.Add((10, 20), 1) # stretchable whitespace |
---|
112 | btn_sizer.Add(accept_btn, 0) |
---|
113 | btn_sizer.Add((10, 20), 0) # non-stretchable whitespace |
---|
114 | btn_sizer.Add(test_btn, 0) |
---|
115 | btn_sizer.Add((10, 20), 0) # non-stretchable whitespace |
---|
116 | btn_sizer.Add(reset_btn, 0) |
---|
117 | btn_sizer.Add((10, 20), 0) # non-stretchable whitespace |
---|
118 | btn_sizer.Add(help_btn, 0) |
---|
119 | |
---|
120 | # Add the button sizer to the main sizer. |
---|
121 | self.vbox.Add(btn_sizer, 0, wx.EXPAND|wx.ALL, 10) |
---|
122 | |
---|
123 | self.SetSizer(self.vbox) |
---|
124 | self.vbox.Fit(self) |
---|
125 | self.SetTitle("OpenCL options") |
---|
126 | self.Centre() |
---|
127 | |
---|
128 | def _get_clinfo(self): |
---|
129 | """ |
---|
130 | Reading in information about available OpenCL infrastructure |
---|
131 | :return: |
---|
132 | """ |
---|
133 | clinfo = [] |
---|
134 | try: |
---|
135 | import pyopencl as cl |
---|
136 | platforms = cl.get_platforms() |
---|
137 | p_index = 0 |
---|
138 | for platform in platforms: |
---|
139 | d_index = 0 |
---|
140 | devices = platform.get_devices() |
---|
141 | for device in devices: |
---|
142 | if len(devices) > 1 and len(platforms) > 1: |
---|
143 | combined_index = ":".join([str(p_index),str(d_index)]) |
---|
144 | elif len(platforms) > 1: |
---|
145 | combined_index = str(p_index) |
---|
146 | else: |
---|
147 | combined_index = str(d_index) |
---|
148 | #combined_index = ":".join([str(p_index),str(d_index)]) \ |
---|
149 | # if len(platforms) > 1 else str(d_index) |
---|
150 | clinfo.append((combined_index, ":".join([platform.name,device.name]))) |
---|
151 | d_index += 1 |
---|
152 | p_index += 1 |
---|
153 | except ImportError: |
---|
154 | warnings.warn("pyopencl import failed. Using only CPU computations") |
---|
155 | |
---|
156 | clinfo.append(("None","No OpenCL")) |
---|
157 | return clinfo |
---|
158 | |
---|
159 | def on_check(self, event): |
---|
160 | """ |
---|
161 | Action triggered when button is selected |
---|
162 | :param event: |
---|
163 | :return: |
---|
164 | """ |
---|
165 | selected_button = event.GetEventObject() |
---|
166 | for btn in self.buttons: |
---|
167 | if btn != selected_button: |
---|
168 | btn.SetValue(0) |
---|
169 | if selected_button.GetValue(): |
---|
170 | self.sas_opencl = self.option_button[selected_button.Name] |
---|
171 | else: |
---|
172 | self.sas_opencl = None |
---|
173 | |
---|
174 | def on_OK(self, event): |
---|
175 | """ |
---|
176 | Close window on accpetance |
---|
177 | """ |
---|
178 | import sasmodels |
---|
179 | #If statement added to handle Reset |
---|
180 | if self.sas_opencl: |
---|
181 | os.environ["SAS_OPENCL"] = self.sas_opencl |
---|
182 | else: |
---|
183 | if "SAS_OPENCL" in os.environ: |
---|
184 | del(os.environ["SAS_OPENCL"]) |
---|
185 | |
---|
186 | try: |
---|
187 | imp.find_module('sasmodels.kernelcl') |
---|
188 | kernelcl_exist = True |
---|
189 | except: |
---|
190 | kernelcl_exist = False |
---|
191 | if kernelcl_exist: |
---|
192 | sasmodels.kernelcl.ENV = None |
---|
193 | #self.custom_config.SAS_OPENCL = self.sas_opencl |
---|
194 | |
---|
195 | #Need to reload sasmodels.core module to account SAS_OPENCL = "None" |
---|
196 | reload(sasmodels.core) |
---|
197 | event.Skip() |
---|
198 | |
---|
199 | def on_reset(self, event): |
---|
200 | """ |
---|
201 | Close window on accpetance |
---|
202 | """ |
---|
203 | for btn in self.buttons: |
---|
204 | btn.SetValue(0) |
---|
205 | self.sas_opencl=None |
---|
206 | |
---|
207 | def on_test(self, event): |
---|
208 | """ |
---|
209 | Run sasmodels check from here and report results from |
---|
210 | """ |
---|
211 | import json |
---|
212 | import platform |
---|
213 | |
---|
214 | import sasmodels |
---|
215 | from sasmodels.model_test import model_tests |
---|
216 | try: |
---|
217 | from sasmodels.kernelcl import environment |
---|
218 | env = environment() |
---|
219 | clinfo = [(ctx.devices[0].platform.vendor, |
---|
220 | ctx.devices[0].platform.version, |
---|
221 | ctx.devices[0].vendor, |
---|
222 | ctx.devices[0].name, |
---|
223 | ctx.devices[0].version) |
---|
224 | for ctx in env.context] |
---|
225 | except ImportError: |
---|
226 | clinfo = None |
---|
227 | |
---|
228 | failures = [] |
---|
229 | for test in model_tests(): |
---|
230 | try: |
---|
231 | test() |
---|
232 | except Exception: |
---|
233 | failures.append(test.description) |
---|
234 | |
---|
235 | info = { |
---|
236 | 'version': sasmodels.__version__, |
---|
237 | 'platform': platform.uname(), |
---|
238 | 'opencl': clinfo, |
---|
239 | 'failing tests': failures, |
---|
240 | } |
---|
241 | print(json.dumps(info['failing tests'])) |
---|
242 | |
---|
243 | def on_help(self, event): |
---|
244 | """ |
---|
245 | Provide help on opencl options. |
---|
246 | """ |
---|
247 | TreeLocation = "user/gpu_computations.html" |
---|
248 | anchor = "#device-selection" |
---|
249 | DocumentationWindow(self, -1, |
---|
250 | TreeLocation, anchor, "OpenCL Options Help") |
---|