[f78c7d2] | 1 | ''' |
---|
[8f02f7f] | 2 | Module provides dialog for setting SAS_OPENCL variable, which defines |
---|
| 3 | device choice for OpenCL calculation |
---|
| 4 | |
---|
[16c1297] | 5 | Created on Nov 29, 2016 |
---|
[f78c7d2] | 6 | |
---|
[16c1297] | 7 | @author: wpotrzebowski |
---|
[f78c7d2] | 8 | ''' |
---|
| 9 | |
---|
[6a569b3] | 10 | import os |
---|
| 11 | import warnings |
---|
[f78c7d2] | 12 | import wx |
---|
[16c1297] | 13 | |
---|
| 14 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
| 15 | |
---|
[f78c7d2] | 16 | class GpuOptions(wx.Dialog): |
---|
| 17 | """ |
---|
[8f02f7f] | 18 | "OpenCL options" Dialog Box |
---|
[f78c7d2] | 19 | |
---|
[8f02f7f] | 20 | Provides dialog for setting SAS_OPENCL variable, which defines |
---|
| 21 | device choice for OpenCL calculation |
---|
[f78c7d2] | 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 | |
---|
[16c1297] | 30 | clinfo = self._get_clinfo() |
---|
[9fdf302] | 31 | |
---|
[f78c7d2] | 32 | self.panel1 = wx.Panel(self, -1) |
---|
[16c1297] | 33 | static_box1 = wx.StaticBox(self.panel1, -1, "Available OpenCL Options:") |
---|
[f78c7d2] | 34 | |
---|
[9fdf302] | 35 | boxsizer = wx.BoxSizer(orient=wx.VERTICAL) |
---|
[c2cb772] | 36 | self.option_button = {} |
---|
[bacc04b] | 37 | self.buttons = [] |
---|
[71ac835] | 38 | #Check if SAS_OPENCL is already set |
---|
| 39 | self.sas_opencl = os.environ.get("SAS_OPENCL","") |
---|
[a9279cc] | 40 | for index, clopt in enumerate(clinfo): |
---|
[bacc04b] | 41 | button = wx.CheckBox(self.panel1, -1, label=clopt, name=clopt) |
---|
[71ac835] | 42 | |
---|
[a9279cc] | 43 | if clopt != "No OpenCL": |
---|
| 44 | self.option_button[clopt] = str(index) |
---|
[71ac835] | 45 | if self.sas_opencl == str(index): |
---|
| 46 | button.SetValue(1) |
---|
[c2cb772] | 47 | else: |
---|
[a9279cc] | 48 | self.option_button[clopt] = "None" |
---|
[71ac835] | 49 | if self.sas_opencl.lower() == "none" : |
---|
| 50 | button.SetValue(1) |
---|
| 51 | |
---|
[8f02f7f] | 52 | self.Bind(wx.EVT_CHECKBOX, self.on_check, id=button.GetId()) |
---|
[bacc04b] | 53 | self.buttons.append(button) |
---|
[9fdf302] | 54 | boxsizer.Add(button, 0, 0) |
---|
[f78c7d2] | 55 | |
---|
| 56 | fit_hsizer = wx.StaticBoxSizer(static_box1, orient=wx.VERTICAL) |
---|
[9fdf302] | 57 | fit_hsizer.Add(boxsizer, 0, wx.ALL, 5) |
---|
[f78c7d2] | 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) |
---|
[c2cb772] | 63 | |
---|
| 64 | accept_btn = wx.Button(self, wx.ID_OK) |
---|
[71ac835] | 65 | accept_btn.SetToolTipString("Accept new OpenCL settings. This will" |
---|
[7feb69d] | 66 | " overwrite SAS_OPENCL variable if set") |
---|
[c2cb772] | 67 | |
---|
[075c460] | 68 | help_id = wx.NewId() |
---|
| 69 | help_btn = wx.Button(self, help_id, 'Help') |
---|
[c2cb772] | 70 | help_btn.SetToolTipString("Help on the GPU options") |
---|
| 71 | |
---|
[71ac835] | 72 | reset_id = wx.NewId() |
---|
| 73 | reset_btn = wx.Button(self, reset_id, 'Reset') |
---|
| 74 | reset_btn.SetToolTipString("Restore initial settings") |
---|
| 75 | |
---|
[8f02f7f] | 76 | self.Bind(wx.EVT_BUTTON, self.on_OK, accept_btn) |
---|
[71ac835] | 77 | self.Bind(wx.EVT_BUTTON, self.on_reset, reset_btn) |
---|
[6a569b3] | 78 | self.Bind(wx.EVT_BUTTON, self.on_help, help_btn) |
---|
[c2cb772] | 79 | |
---|
| 80 | btn_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[6a569b3] | 81 | btn_sizer.Add((10, 20), 1) # stretchable whitespace |
---|
[c2cb772] | 82 | btn_sizer.Add(accept_btn, 0) |
---|
[6a569b3] | 83 | btn_sizer.Add((10, 20), 0) # non-stretchable whitespace |
---|
[71ac835] | 84 | btn_sizer.Add(reset_btn, 0) |
---|
| 85 | btn_sizer.Add((10, 20), 0) # non-stretchable whitespace |
---|
[c2cb772] | 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) |
---|
[7feb69d] | 93 | self.SetTitle("OpenCL options") |
---|
[c2cb772] | 94 | self.Centre() |
---|
[f78c7d2] | 95 | |
---|
[16c1297] | 96 | def _get_clinfo(self): |
---|
[7feb69d] | 97 | """ |
---|
| 98 | Reading in information about available OpenCL infrastructure |
---|
| 99 | :return: |
---|
| 100 | """ |
---|
[95f0cbb] | 101 | clinfo = [] |
---|
[16c1297] | 102 | try: |
---|
| 103 | import pyopencl as cl |
---|
[2e63860] | 104 | for platform in cl.get_platforms(): |
---|
[4638f1d] | 105 | for device in platform.get_devices(): |
---|
[7feb69d] | 106 | clinfo.append(":".join([platform.name,device.name])) |
---|
[6a569b3] | 107 | except ImportError: |
---|
[4139147] | 108 | warnings.warn("pyopencl import failed. Using only CPU computations") |
---|
[95f0cbb] | 109 | |
---|
| 110 | clinfo.append("No OpenCL") |
---|
[16c1297] | 111 | return clinfo |
---|
[f78c7d2] | 112 | |
---|
[8f02f7f] | 113 | def on_check(self, event): |
---|
[6a569b3] | 114 | """ |
---|
| 115 | Action triggered when button is selected |
---|
| 116 | :param event: |
---|
| 117 | :return: |
---|
| 118 | """ |
---|
[bacc04b] | 119 | selected_button = event.GetEventObject() |
---|
| 120 | for btn in self.buttons: |
---|
| 121 | if btn != selected_button: |
---|
| 122 | btn.SetValue(0) |
---|
[7feb69d] | 123 | if selected_button.GetValue(): |
---|
| 124 | self.sas_opencl = self.option_button[selected_button.Name] |
---|
| 125 | else: |
---|
| 126 | self.sas_opencl = None |
---|
[c2cb772] | 127 | |
---|
[8f02f7f] | 128 | def on_OK(self, event): |
---|
[c2cb772] | 129 | """ |
---|
[9fdf302] | 130 | Close window on accpetance |
---|
[c2cb772] | 131 | """ |
---|
[8f02f7f] | 132 | import sasmodels |
---|
[71ac835] | 133 | #If statement added to handle Reset |
---|
| 134 | if self.sas_opencl: |
---|
| 135 | os.environ["SAS_OPENCL"] = self.sas_opencl |
---|
| 136 | else: |
---|
| 137 | if "SAS_OPENCL" in os.environ: |
---|
| 138 | del(os.environ["SAS_OPENCL"]) |
---|
[8f02f7f] | 139 | sasmodels.kernelcl.ENV = None |
---|
| 140 | #Need to reload sasmodels.core module to account SAS_OPENCL = "None" |
---|
| 141 | reload(sasmodels.core) |
---|
[c2cb772] | 142 | event.Skip() |
---|
| 143 | |
---|
[71ac835] | 144 | def on_reset(self, event): |
---|
| 145 | """ |
---|
| 146 | Close window on accpetance |
---|
| 147 | """ |
---|
| 148 | for btn in self.buttons: |
---|
| 149 | btn.SetValue(0) |
---|
| 150 | self.sas_opencl=None |
---|
| 151 | |
---|
[c027106e] | 152 | def on_help(self, event): |
---|
[c2cb772] | 153 | """ |
---|
[9fdf302] | 154 | Provide help on opencl options. |
---|
[c2cb772] | 155 | """ |
---|
[6a569b3] | 156 | TreeLocation = "user/gpu_computations.html" |
---|
| 157 | anchor = "#device-selection" |
---|
[16c1297] | 158 | DocumentationWindow(self, -1, |
---|
[6a569b3] | 159 | TreeLocation, anchor, "OpenCL Options Help") |
---|