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 | |
---|
14 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
15 | |
---|
16 | class GpuOptions(wx.Dialog): |
---|
17 | """ |
---|
18 | "OpenCL options" Dialog Box |
---|
19 | |
---|
20 | Provides dialog for setting SAS_OPENCL variable, which defines |
---|
21 | device choice for OpenCL calculation |
---|
22 | |
---|
23 | """ |
---|
24 | |
---|
25 | def __init__(self, *args, **kwds): |
---|
26 | |
---|
27 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
---|
28 | wx.Dialog.__init__(self, *args, **kwds) |
---|
29 | |
---|
30 | clinfo = self._get_clinfo() |
---|
31 | |
---|
32 | self.panel1 = wx.Panel(self, -1) |
---|
33 | static_box1 = wx.StaticBox(self.panel1, -1, "Available OpenCL Options:") |
---|
34 | |
---|
35 | boxsizer = wx.BoxSizer(orient=wx.VERTICAL) |
---|
36 | self.option_button = {} |
---|
37 | self.buttons = [] |
---|
38 | #Check if SAS_OPENCL is already set |
---|
39 | self.sas_opencl = os.environ.get("SAS_OPENCL","") |
---|
40 | for clopt in clinfo: |
---|
41 | button = wx.CheckBox(self.panel1, -1, label=clopt[1], name=clopt[1]) |
---|
42 | |
---|
43 | if clopt != "No OpenCL": |
---|
44 | self.option_button[clopt[1]] = clopt[0] |
---|
45 | if self.sas_opencl == clopt[0]: |
---|
46 | button.SetValue(1) |
---|
47 | else: |
---|
48 | self.option_button[clopt] = "None" |
---|
49 | if self.sas_opencl.lower() == "none" : |
---|
50 | button.SetValue(1) |
---|
51 | |
---|
52 | self.Bind(wx.EVT_CHECKBOX, self.on_check, id=button.GetId()) |
---|
53 | self.buttons.append(button) |
---|
54 | boxsizer.Add(button, 0, 0) |
---|
55 | |
---|
56 | fit_hsizer = wx.StaticBoxSizer(static_box1, orient=wx.VERTICAL) |
---|
57 | fit_hsizer.Add(boxsizer, 0, wx.ALL, 5) |
---|
58 | |
---|
59 | self.panel1.SetSizer(fit_hsizer) |
---|
60 | |
---|
61 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
62 | self.vbox.Add(self.panel1, 0, wx.ALL, 10) |
---|
63 | |
---|
64 | accept_btn = wx.Button(self, wx.ID_OK) |
---|
65 | accept_btn.SetToolTipString("Accept new OpenCL settings. This will" |
---|
66 | " overwrite SAS_OPENCL variable if set") |
---|
67 | |
---|
68 | help_id = wx.NewId() |
---|
69 | help_btn = wx.Button(self, help_id, 'Help') |
---|
70 | help_btn.SetToolTipString("Help on the GPU options") |
---|
71 | |
---|
72 | reset_id = wx.NewId() |
---|
73 | reset_btn = wx.Button(self, reset_id, 'Reset') |
---|
74 | reset_btn.SetToolTipString("Restore initial settings") |
---|
75 | |
---|
76 | self.Bind(wx.EVT_BUTTON, self.on_OK, accept_btn) |
---|
77 | self.Bind(wx.EVT_BUTTON, self.on_reset, reset_btn) |
---|
78 | self.Bind(wx.EVT_BUTTON, self.on_help, help_btn) |
---|
79 | |
---|
80 | btn_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
81 | btn_sizer.Add((10, 20), 1) # stretchable whitespace |
---|
82 | btn_sizer.Add(accept_btn, 0) |
---|
83 | btn_sizer.Add((10, 20), 0) # non-stretchable whitespace |
---|
84 | btn_sizer.Add(reset_btn, 0) |
---|
85 | btn_sizer.Add((10, 20), 0) # non-stretchable whitespace |
---|
86 | btn_sizer.Add(help_btn, 0) |
---|
87 | |
---|
88 | # Add the button sizer to the main sizer. |
---|
89 | self.vbox.Add(btn_sizer, 0, wx.EXPAND|wx.ALL, 10) |
---|
90 | |
---|
91 | self.SetSizer(self.vbox) |
---|
92 | self.vbox.Fit(self) |
---|
93 | self.SetTitle("OpenCL options") |
---|
94 | self.Centre() |
---|
95 | |
---|
96 | def _get_clinfo(self): |
---|
97 | """ |
---|
98 | Reading in information about available OpenCL infrastructure |
---|
99 | :return: |
---|
100 | """ |
---|
101 | clinfo = [] |
---|
102 | try: |
---|
103 | import pyopencl as cl |
---|
104 | platforms = cl.get_platforms() |
---|
105 | p_index = 0 |
---|
106 | for platform in platforms: |
---|
107 | d_index = 0 |
---|
108 | devices = platform.get_devices() |
---|
109 | for device in devices: |
---|
110 | if len(devices) > 1 and len(platforms) > 1: |
---|
111 | combined_index = ":".join([str(p_index),str(d_index)]) |
---|
112 | elif len(platforms) > 1: |
---|
113 | combined_index = str(p_index) |
---|
114 | else: |
---|
115 | combined_index = str(d_index) |
---|
116 | #combined_index = ":".join([str(p_index),str(d_index)]) \ |
---|
117 | # if len(platforms) > 1 else str(d_index) |
---|
118 | clinfo.append((combined_index, ":".join([platform.name,device.name]))) |
---|
119 | d_index += 1 |
---|
120 | p_index += 1 |
---|
121 | except ImportError: |
---|
122 | warnings.warn("pyopencl import failed. Using only CPU computations") |
---|
123 | |
---|
124 | clinfo.append(("None","No OpenCL")) |
---|
125 | return clinfo |
---|
126 | |
---|
127 | def on_check(self, event): |
---|
128 | """ |
---|
129 | Action triggered when button is selected |
---|
130 | :param event: |
---|
131 | :return: |
---|
132 | """ |
---|
133 | selected_button = event.GetEventObject() |
---|
134 | for btn in self.buttons: |
---|
135 | if btn != selected_button: |
---|
136 | btn.SetValue(0) |
---|
137 | if selected_button.GetValue(): |
---|
138 | self.sas_opencl = self.option_button[selected_button.Name] |
---|
139 | else: |
---|
140 | self.sas_opencl = None |
---|
141 | |
---|
142 | def on_OK(self, event): |
---|
143 | """ |
---|
144 | Close window on accpetance |
---|
145 | """ |
---|
146 | import sasmodels |
---|
147 | #If statement added to handle Reset |
---|
148 | if self.sas_opencl: |
---|
149 | os.environ["SAS_OPENCL"] = self.sas_opencl |
---|
150 | else: |
---|
151 | if "SAS_OPENCL" in os.environ: |
---|
152 | del(os.environ["SAS_OPENCL"]) |
---|
153 | sasmodels.kernelcl.ENV = None |
---|
154 | #Need to reload sasmodels.core module to account SAS_OPENCL = "None" |
---|
155 | reload(sasmodels.core) |
---|
156 | event.Skip() |
---|
157 | |
---|
158 | def on_reset(self, event): |
---|
159 | """ |
---|
160 | Close window on accpetance |
---|
161 | """ |
---|
162 | for btn in self.buttons: |
---|
163 | btn.SetValue(0) |
---|
164 | self.sas_opencl=None |
---|
165 | |
---|
166 | def on_help(self, event): |
---|
167 | """ |
---|
168 | Provide help on opencl options. |
---|
169 | """ |
---|
170 | TreeLocation = "user/gpu_computations.html" |
---|
171 | anchor = "#device-selection" |
---|
172 | DocumentationWindow(self, -1, |
---|
173 | TreeLocation, anchor, "OpenCL Options Help") |
---|