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

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

Changing clinfo setup

  • 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, 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            #TODO: Is PYOPENCL_CTX setup up in this order?
98            import pyopencl as cl
99            clinfo = []
100            for platform in cl.get_platforms():
101                for device in platform.get_devices():
102                    clinfo.append(device.name)
103            clinfo.append("No OpenCL")
104        except:
105            warnings.warn(str(exc))
106            warnings.warn("pyopencl import failed")
107            clinfo = None
108        return clinfo
109
110    def OnRadio(self, event):
111        import sasmodels
112        button = event.GetEventObject()
113        os.environ["SAS_OPENCL"] = self.option_button[button.Name]
114        sasmodels.kernelcl.ENV = None
115        reload(sasmodels.core)
116
117    def OnAccept(self, event):
118        """
119        Close window on accpetance
120        """
121        event.Skip()
122
123    def OnHelp(self, event):
124        """
125        Provide help on opencl options.
126        """
127        _TreeLocation = "user/gpu_computations.html"
128        _anchor = "#device-selection"
129        DocumentationWindow(self, -1,
130                            _TreeLocation, _anchor, "OpenCL Options Help")
131
132    def OnClose(self, event):
133        """
134        Close window
135        """
136        event.Skip()
137
138##### testing code ############################################################
139class MyApp(wx.App):
140    """
141    Class for running module as stand alone for testing
142    """
143    def OnInit(self):
144        """
145        Defines an init when running as standalone
146        """
147        wx.InitAllImageHandlers()
148        dialog = GpuOptions(None, -1, "")
149        self.SetTopWindow(dialog)
150        dialog.ShowModal()
151        dialog.Destroy()
152        return 1
153
154# end of class MyApp
155
156if __name__ == "__main__":
157    app = MyApp(0)
158    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.