source: sasview/src/sas/sasgui/perspectives/fitting/gpu_options.py @ e29c798

Last change on this file since e29c798 was e29c798, checked in by wojciech, 8 years ago

Bug fixed

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