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

Last change on this file since a9279cc was a9279cc, checked in by wojciech, 7 years ago

Code cleanup

  • Property mode set to 100644
File size: 4.5 KB
Line 
1'''
2Created on Nov 29, 2016
3
4@author: wpotrzebowski
5'''
6
7import wx
8import wx.richtext
9import wx.lib.hyperlink
10import os
11
12from sas.sasgui.guiframe.documentation_window import DocumentationWindow
13
14try:
15    # Try to find a local config
16    import imp
17    path = os.getcwd()
18    if(os.path.isfile("%s/%s.py" % (path, 'local_config'))) or \
19      (os.path.isfile("%s/%s.pyc" % (path, 'local_config'))):
20        fObj, path, descr = imp.find_module('local_config', [path])
21        config = imp.load_module('local_config', fObj, path, descr)
22    else:
23        # Try simply importing local_config
24        import local_config as config
25except:
26    # Didn't find local config, load the default
27    import config
28
29
30class GpuOptions(wx.Dialog):
31    """
32    "Acknowledgement" Dialog Box
33
34    Shows the current method for acknowledging SasView in
35    scholarly publications.
36
37    """
38
39    def __init__(self, *args, **kwds):
40
41        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
42        wx.Dialog.__init__(self, *args, **kwds)
43
44        clinfo = self._get_clinfo()
45
46        self.panel1 = wx.Panel(self, -1)
47        static_box1 = wx.StaticBox(self.panel1, -1, "Available OpenCL Options:")
48
49        boxsizer = wx.BoxSizer(orient=wx.VERTICAL)
50        self.option_button = {}
51        for index, clopt in enumerate(clinfo):
52            button = wx.RadioButton(self.panel1, -1,
53                    label=clopt, name=clopt)
54            if clopt != "No OpenCL":
55                self.option_button[clopt] = str(index)
56            else:
57                self.option_button[clopt] = "None"
58            self.Bind(wx.EVT_RADIOBUTTON, self.OnRadio, id=button.GetId())
59            boxsizer.Add(button, 0, 0)
60
61        fit_hsizer = wx.StaticBoxSizer(static_box1, orient=wx.VERTICAL)
62        fit_hsizer.Add(boxsizer, 0, wx.ALL, 5)
63
64        self.panel1.SetSizer(fit_hsizer)
65
66        self.vbox = wx.BoxSizer(wx.VERTICAL)
67        self.vbox.Add(self.panel1, 0, wx.ALL, 10)
68
69        accept_btn = wx.Button(self, wx.ID_OK)
70        accept_btn.SetToolTipString("Accept OpenCL settings")
71
72        help_btn = wx.Button(self, wx.ID_HELP, 'Help')
73        help_btn.SetToolTipString("Help on the GPU options")
74
75        self.Bind(wx.EVT_BUTTON, self.OnAccept, accept_btn)
76        self.Bind(wx.EVT_BUTTON, self.OnHelp, help_btn)
77        self.Bind(wx.EVT_CLOSE, self.OnClose)
78
79        btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
80        btn_sizer.Add((10,20), 1)  # stretchable whitespace
81        btn_sizer.Add(accept_btn, 0)
82        btn_sizer.Add((10,20), 0)  # non-stretchable whitespace
83        btn_sizer.Add(help_btn, 0)
84
85        # Add the button sizer to the main sizer.
86        self.vbox.Add(btn_sizer, 0, wx.EXPAND|wx.ALL, 10)
87
88        self.SetSizer(self.vbox)
89        self.vbox.Fit(self)
90
91        self.Centre()
92
93    def _get_clinfo(self):
94        try:
95            #TODO: Is PYOPENCL_CTX setup up in this order?
96            import pyopencl as cl
97            clinfo = []
98            for platform in cl.get_platforms():
99                for device in platform.get_devices():
100                    clinfo.append(device.name)
101            clinfo.append("No OpenCL")
102        except:
103            warnings.warn(str(exc))
104            warnings.warn("pyopencl import failed")
105            clinfo = None
106        return clinfo
107
108    def OnRadio(self, event):
109        import sasmodels
110        button = event.GetEventObject()
111        os.environ["SAS_OPENCL"] = self.option_button[button.Name]
112        sasmodels.kernelcl.ENV = None
113        #Need to reload sasmodels.core module to account SAS_OPENCL = "None"
114        reload(sasmodels.core)
115
116    def OnAccept(self, event):
117        """
118        Close window on accpetance
119        """
120        event.Skip()
121
122    def OnHelp(self, event):
123        """
124        Provide help on opencl options.
125        """
126        _TreeLocation = "user/gpu_computations.html"
127        _anchor = "#device-selection"
128        DocumentationWindow(self, -1,
129                            _TreeLocation, _anchor, "OpenCL Options Help")
130
131    def OnClose(self, event):
132        """
133        Close window
134        """
135        event.Skip()
136
137##### testing code ############################################################
138class MyApp(wx.App):
139    """
140    Class for running module as stand alone for testing
141    """
142    def OnInit(self):
143        """
144        Defines an init when running as standalone
145        """
146        wx.InitAllImageHandlers()
147        dialog = GpuOptions(None, -1, "")
148        self.SetTopWindow(dialog)
149        dialog.ShowModal()
150        dialog.Destroy()
151        return 1
152
153# end of class MyApp
154
155if __name__ == "__main__":
156    app = MyApp(0)
157    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.