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

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

Calling CL devices from get_devices rather than create some context

  • Property mode set to 100644
File size: 5.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        self.panel1 = wx.Panel(self, -1)
46        static_box1 = wx.StaticBox(self.panel1, -1, "Available OpenCL Options:")
47
48        rows = len(clinfo)
49
50        flexsizer = wx.FlexGridSizer(rows, 1, hgap=20, vgap=10)
51        self.option_button = {}
52        for index, fitter in enumerate(clinfo):
53            button = wx.RadioButton(self.panel1, -1,
54                    label=fitter, name=fitter)
55            if fitter != "No OpenCL":
56                self.option_button[fitter] = str(index)
57            else:
58                self.option_button[fitter] = "None"
59            self.Bind(wx.EVT_RADIOBUTTON, self.OnRadio, id=button.GetId())
60            flexsizer.Add(button, 0, 0)
61
62        fit_hsizer = wx.StaticBoxSizer(static_box1, orient=wx.VERTICAL)
63        fit_hsizer.Add(flexsizer, 0, wx.ALL, 5)
64
65        self.panel1.SetSizer(fit_hsizer)
66
67        self.vbox = wx.BoxSizer(wx.VERTICAL)
68        self.vbox.Add(self.panel1, 0, wx.ALL, 10)
69
70        accept_btn = wx.Button(self, wx.ID_OK)
71        accept_btn.SetToolTipString("Accept new options for GPU")
72
73        help_btn = wx.Button(self, wx.ID_HELP, 'Help')
74        #help_btn = wx.Button(self, wx.ID_ANY, 'Help')
75        help_btn.SetToolTipString("Help on the GPU options")
76
77        self.Bind(wx.EVT_BUTTON, self.OnAccept, accept_btn)
78        #self.Bind(wx.EVT_BUTTON, self.OnCancel, cancel_btn)
79        if help is not None:
80            self.Bind(wx.EVT_BUTTON, self.OnHelp, help_btn)
81
82        self.Bind(wx.EVT_CLOSE, self.OnClose)
83
84        # Create the button sizer that will put the buttons in a row, right
85        # justified, and with a fixed amount of space between them.  This
86        # emulates the Windows convention for placing a set of buttons at the
87        # bottom right of the window.
88        btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
89        btn_sizer.Add((10,20), 1)  # stretchable whitespace
90        btn_sizer.Add(accept_btn, 0)
91        #btn_sizer.Add((10,20), 0)  # non-stretchable whitespace
92        #btn_sizer.Add(cancel_btn, 0)
93        #btn_sizer.Add((10,20), 0)  # non-stretchable whitespace
94        btn_sizer.Add((10,20), 0)  # non-stretchable whitespace
95        btn_sizer.Add(help_btn, 0)
96
97        # Add the button sizer to the main sizer.
98        self.vbox.Add(btn_sizer, 0, wx.EXPAND|wx.ALL, 10)
99
100        # Finalize the sizer and establish the dimensions of the dialog box.
101        # The minimum width is explicitly set because the sizer is not able to
102        # take into consideration the width of the enclosing frame's title.
103        self.SetSizer(self.vbox)
104        #self.vbox.SetMinSize((size[0], -1))
105        self.vbox.Fit(self)
106
107        self.Centre()
108
109    def _get_clinfo(self):
110        try:
111            import pyopencl as cl
112            devices = cl.get_platforms()[0].get_devices()
113            clinfo = [dev.name for dev in devices]
114            clinfo.append("No OpenCL")
115        except:
116            warnings.warn(str(exc))
117            warnings.warn("pyopencl import failed")
118            clinfo = None
119        return clinfo
120
121    def OnRadio(self, event):
122        import sasmodels
123        button = event.GetEventObject()
124        os.environ["SAS_OPENCL"] = self.option_button[button.Name]
125        sasmodels.kernelcl.ENV = None
126        reload(sasmodels.core)
127
128    def OnAccept(self, event):
129        """
130        Save the current fitter and options to the fit config.
131        """
132        event.Skip()
133
134    def OnHelp(self, event):
135        """
136        Provide help on the selected fitter.
137        """
138        _TreeLocation = "user/gpu_computations.html"
139        _anchor = "#device-selection"
140        DocumentationWindow(self, -1,
141                            _TreeLocation, _anchor, "OpenCL Options Help")
142
143    def OnClose(self, event):
144        """
145        Don't close the window, just hide it.
146        """
147        event.Skip()
148
149##### testing code ############################################################
150class MyApp(wx.App):
151    """
152    Class for running module as stand alone for testing
153    """
154    def OnInit(self):
155        """
156        Defines an init when running as standalone
157        """
158        wx.InitAllImageHandlers()
159        dialog = GpuOptions(None, -1, "")
160        self.SetTopWindow(dialog)
161        dialog.ShowModal()
162        dialog.Destroy()
163        return 1
164
165# end of class MyApp
166
167if __name__ == "__main__":
168    app = MyApp(0)
169    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.