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

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

Handling for PYOPENCL_CTX for multiple platforms added

  • Property mode set to 100644
File size: 5.3 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                for device in platform.get_devices():
110                    combined_index = ":".join(str(p_index),str(d_index)) \
111                        if len(platforms) > 1 else str(d_index)
112                    clinfo.append((combined_index, ":".join([platform.name,device.name])))
113                    d_index+=1
114                p_index+=1
115        except ImportError:
116            warnings.warn("pyopencl import failed. Using only CPU computations")
117
118        clinfo.append(("None","No OpenCL"))
119        return clinfo
120
121    def on_check(self, event):
122        """
123        Action triggered when button is selected
124        :param event:
125        :return:
126        """
127        selected_button = event.GetEventObject()
128        for btn in self.buttons:
129            if btn != selected_button:
130                btn.SetValue(0)
131        if selected_button.GetValue():
132            self.sas_opencl = self.option_button[selected_button.Name]
133        else:
134            self.sas_opencl = None
135
136    def on_OK(self, event):
137        """
138        Close window on accpetance
139        """
140        import sasmodels
141        #If statement added to handle Reset
142        if self.sas_opencl:
143            os.environ["SAS_OPENCL"] = self.sas_opencl
144        else:
145            if "SAS_OPENCL" in os.environ:
146                del(os.environ["SAS_OPENCL"])
147        sasmodels.kernelcl.ENV = None
148        #Need to reload sasmodels.core module to account SAS_OPENCL = "None"
149        reload(sasmodels.core)
150        event.Skip()
151
152    def on_reset(self, event):
153        """
154        Close window on accpetance
155        """
156        for btn in self.buttons:
157            btn.SetValue(0)
158        self.sas_opencl=None
159
160    def on_help(self, event):
161        """
162        Provide help on opencl options.
163        """
164        TreeLocation = "user/gpu_computations.html"
165        anchor = "#device-selection"
166        DocumentationWindow(self, -1,
167                            TreeLocation, anchor, "OpenCL Options Help")
Note: See TracBrowser for help on using the repository browser.