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

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

Corrected warning

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