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

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

Support for more than one platform and one device

  • Property mode set to 100644
File size: 5.6 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 clopt in clinfo:
41            print("clopt",clopt)
42            button = wx.CheckBox(self.panel1, -1, label=clopt[1], name=clopt[1])
43
44            if clopt != "No OpenCL":
45                self.option_button[clopt[1]] = clopt[0]
46                if self.sas_opencl == clopt[0]:
47                    button.SetValue(1)
48            else:
49                self.option_button[clopt] = "None"
50                if self.sas_opencl.lower() == "none" :
51                    button.SetValue(1)
52
53            self.Bind(wx.EVT_CHECKBOX, self.on_check, id=button.GetId())
54            self.buttons.append(button)
55            boxsizer.Add(button, 0, 0)
56
57        fit_hsizer = wx.StaticBoxSizer(static_box1, orient=wx.VERTICAL)
58        fit_hsizer.Add(boxsizer, 0, wx.ALL, 5)
59
60        self.panel1.SetSizer(fit_hsizer)
61
62        self.vbox = wx.BoxSizer(wx.VERTICAL)
63        self.vbox.Add(self.panel1, 0, wx.ALL, 10)
64
65        accept_btn = wx.Button(self, wx.ID_OK)
66        accept_btn.SetToolTipString("Accept new OpenCL settings. This will"
67                                    " overwrite SAS_OPENCL variable if set")
68
69        help_id = wx.NewId()
70        help_btn = wx.Button(self, help_id, 'Help')
71        help_btn.SetToolTipString("Help on the GPU options")
72
73        reset_id = wx.NewId()
74        reset_btn = wx.Button(self, reset_id, 'Reset')
75        reset_btn.SetToolTipString("Restore initial settings")
76
77        self.Bind(wx.EVT_BUTTON, self.on_OK, accept_btn)
78        self.Bind(wx.EVT_BUTTON, self.on_reset, reset_btn)
79        self.Bind(wx.EVT_BUTTON, self.on_help, help_btn)
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(reset_btn, 0)
86        btn_sizer.Add((10, 20), 0) # non-stretchable whitespace
87        btn_sizer.Add(help_btn, 0)
88
89        # Add the button sizer to the main sizer.
90        self.vbox.Add(btn_sizer, 0, wx.EXPAND|wx.ALL, 10)
91
92        self.SetSizer(self.vbox)
93        self.vbox.Fit(self)
94        self.SetTitle("OpenCL options")
95        self.Centre()
96
97    def _get_clinfo(self):
98        """
99        Reading in information about available OpenCL infrastructure
100        :return:
101        """
102        clinfo = []
103        try:
104            import pyopencl as cl
105            platforms = cl.get_platforms()
106            p_index = 0
107            for platform in platforms:
108                d_index = 0
109                devices = platform.get_devices()
110                for device in devices:
111                    if len(devices) > 1 and len(platforms) > 1:
112                        combined_index = ":".join([str(p_index),str(d_index)])
113                    elif len(platforms) > 1:
114                        combined_index = str(p_index)
115                    else:
116                        combined_index = str(d_index)
117                    #combined_index = ":".join([str(p_index),str(d_index)]) \
118                    #    if len(platforms) > 1 else str(d_index)
119                    clinfo.append((combined_index, ":".join([platform.name,device.name])))
120                    d_index += 1
121                p_index += 1
122        except ImportError:
123            warnings.warn("pyopencl import failed. Using only CPU computations")
124
125        clinfo.append(("None","No OpenCL"))
126        return clinfo
127
128    def on_check(self, event):
129        """
130        Action triggered when button is selected
131        :param event:
132        :return:
133        """
134        selected_button = event.GetEventObject()
135        for btn in self.buttons:
136            if btn != selected_button:
137                btn.SetValue(0)
138        if selected_button.GetValue():
139            self.sas_opencl = self.option_button[selected_button.Name]
140        else:
141            self.sas_opencl = None
142
143    def on_OK(self, event):
144        """
145        Close window on accpetance
146        """
147        import sasmodels
148        #If statement added to handle Reset
149        if self.sas_opencl:
150            os.environ["SAS_OPENCL"] = self.sas_opencl
151        else:
152            if "SAS_OPENCL" in os.environ:
153                del(os.environ["SAS_OPENCL"])
154        sasmodels.kernelcl.ENV = None
155        #Need to reload sasmodels.core module to account SAS_OPENCL = "None"
156        reload(sasmodels.core)
157        event.Skip()
158
159    def on_reset(self, event):
160        """
161        Close window on accpetance
162        """
163        for btn in self.buttons:
164            btn.SetValue(0)
165        self.sas_opencl=None
166
167    def on_help(self, event):
168        """
169        Provide help on opencl options.
170        """
171        TreeLocation = "user/gpu_computations.html"
172        anchor = "#device-selection"
173        DocumentationWindow(self, -1,
174                            TreeLocation, anchor, "OpenCL Options Help")
Note: See TracBrowser for help on using the repository browser.