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

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

Boxsizer instead of FlexGridSizer? introduced

  • Property mode set to 100644
File size: 4.3 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, fitter in enumerate(clinfo):
52            button = wx.RadioButton(self.panel1, -1,
53                    label=fitter, name=fitter)
54            if fitter != "No OpenCL":
55                self.option_button[fitter] = str(index)
56            else:
57                self.option_button[fitter] = "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 new options for GPU")
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        if help is not None:
77            self.Bind(wx.EVT_BUTTON, self.OnHelp, help_btn)
78
79        self.Bind(wx.EVT_CLOSE, self.OnClose)
80
81        btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
82        btn_sizer.Add((10,20), 1)  # stretchable whitespace
83        btn_sizer.Add(accept_btn, 0)
84        btn_sizer.Add((10,20), 0)  # non-stretchable whitespace
85        btn_sizer.Add(help_btn, 0)
86
87        # Add the button sizer to the main sizer.
88        self.vbox.Add(btn_sizer, 0, wx.EXPAND|wx.ALL, 10)
89
90        self.SetSizer(self.vbox)
91        self.vbox.Fit(self)
92
93        self.Centre()
94
95    def _get_clinfo(self):
96        try:
97            import pyopencl as cl
98            devices = cl.get_platforms()[0].get_devices()
99            clinfo = [dev.name for dev in devices]
100            clinfo.append("No OpenCL")
101        except:
102            warnings.warn(str(exc))
103            warnings.warn("pyopencl import failed")
104            clinfo = None
105        return clinfo
106
107    def OnRadio(self, event):
108        import sasmodels
109        button = event.GetEventObject()
110        os.environ["SAS_OPENCL"] = self.option_button[button.Name]
111        sasmodels.kernelcl.ENV = None
112        reload(sasmodels.core)
113
114    def OnAccept(self, event):
115        """
116        Close window on accpetance
117        """
118        event.Skip()
119
120    def OnHelp(self, event):
121        """
122        Provide help on opencl options.
123        """
124        _TreeLocation = "user/gpu_computations.html"
125        _anchor = "#device-selection"
126        DocumentationWindow(self, -1,
127                            _TreeLocation, _anchor, "OpenCL Options Help")
128
129    def OnClose(self, event):
130        """
131        Close window
132        """
133        event.Skip()
134
135##### testing code ############################################################
136class MyApp(wx.App):
137    """
138    Class for running module as stand alone for testing
139    """
140    def OnInit(self):
141        """
142        Defines an init when running as standalone
143        """
144        wx.InitAllImageHandlers()
145        dialog = GpuOptions(None, -1, "")
146        self.SetTopWindow(dialog)
147        dialog.ShowModal()
148        dialog.Destroy()
149        return 1
150
151# end of class MyApp
152
153if __name__ == "__main__":
154    app = MyApp(0)
155    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.