Changeset 250fa25 in sasmodels


Ignore:
Timestamp:
Feb 16, 2015 7:01:09 AM (9 years ago)
Author:
pkienzle
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
10576d1
Parents:
f786ff3
Message:

autoselect gpu for opencl if available

Location:
sasmodels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/kernelcl.py

    rf786ff3 r250fa25  
    2323factors and have instrumental resolution effects applied. 
    2424""" 
     25import os 
     26import warnings 
     27 
    2528import numpy as np 
    2629 
    27 import pyopencl as cl 
     30try: 
     31    import pyopencl as cl 
     32except ImportError,exc: 
     33    warnings.warn(str(exc)) 
     34    raise RuntimeError("OpenCL not available") 
     35 
    2836from pyopencl import mem_flags as mf 
    2937 
     
    140148    """ 
    141149    def __init__(self): 
    142         self.context = cl.create_some_context() 
    143         self.queues = [cl.CommandQueue(self.context, d) 
    144                        for d in self.context.devices] 
     150        # find gpu context 
     151        #self.context = cl.create_some_context() 
     152 
     153        self.context = None 
     154        if 'PYOPENCL_CTX' in os.environ: 
     155            self._create_some_context() 
     156 
     157        if not self.context: 
     158            self.context = self._find_context() 
     159 
    145160        # Byte boundary for data alignment 
    146161        #self.data_boundary = max(d.min_data_type_align_size 
    147162        #                         for d in self.context.devices) 
     163        self.queues = [cl.CommandQueue(self.context, d) 
     164                       for d in self.context.devices] 
    148165        self.has_double = all(has_double(d) for d in self.context.devices) 
    149166        self.compiled = {} 
     167 
     168    def _create_some_context(self): 
     169        try: 
     170            self.context = cl.create_some_context(interactive=False) 
     171        except Exception,exc: 
     172            warnings.warn(str(exc)) 
     173            warnings.warn("pyopencl.create_some_context() failed") 
     174            warnings.warn("the environment variable 'PYOPENCL_CTX' might not be set correctly") 
     175 
     176    def _find_context(self): 
     177        default = None 
     178        for platform in cl.get_platforms(): 
     179            for device in platform.get_devices(): 
     180                if device.type == cl.device_type.GPU: 
     181                    return cl.Context([device]) 
     182                if default is None: 
     183                    default = device 
     184 
     185        if not default: 
     186            raise RuntimeError("OpenCL device not found") 
     187 
     188        return cl.Context([default]) 
    150189 
    151190    def compile_program(self, name, source, dtype): 
     
    159198            self.compiled[name].release() 
    160199            del self.compiled[name] 
     200 
    161201 
    162202class GpuModel(object): 
  • sasmodels/sasview_model.py

    rf786ff3 r250fa25  
    2828except ImportError,exc: 
    2929    warnings.warn(str(exc)) 
    30     warnings.warn("OpenCL not available --- using ctypes instead") 
     30    warnings.warn("using ctypes instead") 
    3131    from .kerneldll import load_model 
    3232 
Note: See TracChangeset for help on using the changeset viewer.