Changeset 20317b3 in sasmodels


Ignore:
Timestamp:
Jul 28, 2016 8:16:31 AM (3 years ago)
Author:
Paul Kienzle <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:
2547694
Parents:
d2d6100
Message:

lint

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/kernelcl.py

    ra4280bd r20317b3  
    6565    raise RuntimeError("OpenCL not available") 
    6666 
    67 from pyopencl import mem_flags as mf  # type: ignore 
    68 from pyopencl.characterize import get_fast_inaccurate_build_options  # type: ignore 
    69 # CRUFT: pyopencl < 2017.1  (as of June 2016 needs quotes around include path) 
    70 def _quote_path(v): 
    71     """ 
    72     Quote the path if it is not already quoted. 
    73  
    74     If v starts with '-', then assume that it is a -I option or similar 
    75     and do not quote it.  This is fragile:  -Ipath with space needs to 
    76     be quoted. 
    77     """ 
    78     return '"'+v+'"' if v and ' ' in v and not v[0] in "\"'-" else v 
    79  
    80 if hasattr(cl, '_DEFAULT_INCLUDE_OPTIONS'): 
    81     cl._DEFAULT_INCLUDE_OPTIONS = [_quote_path(v) for v in cl._DEFAULT_INCLUDE_OPTIONS] 
    82  
    8367from pyopencl import mem_flags as mf 
    8468from pyopencl.characterize import get_fast_inaccurate_build_options 
     
    9377except ImportError: 
    9478    pass 
     79 
     80# CRUFT: pyopencl < 2017.1  (as of June 2016 needs quotes around include path) 
     81def quote_path(v): 
     82    """ 
     83    Quote the path if it is not already quoted. 
     84 
     85    If v starts with '-', then assume that it is a -I option or similar 
     86    and do not quote it.  This is fragile:  -Ipath with space needs to 
     87    be quoted. 
     88    """ 
     89    return '"'+v+'"' if v and ' ' in v and not v[0] in "\"'-" else v 
     90 
     91def fix_pyopencl_include(): 
     92    import pyopencl as cl 
     93    if hasattr(cl, '_DEFAULT_INCLUDE_OPTIONS'): 
     94        cl._DEFAULT_INCLUDE_OPTIONS = [quote_path(v) for v in cl._DEFAULT_INCLUDE_OPTIONS] 
     95 
     96fix_pyopencl_include() 
     97 
    9598 
    9699# The max loops number is limited by the amount of local memory available 
     
    256259        Return a OpenCL context for the kernels of type dtype. 
    257260        """ 
    258         for context, queue in zip(self.context, self.queues): 
     261        for context in self.context: 
    259262            if all(has_type(d, dtype) for d in context.devices): 
    260263                return context 
     
    282285        if key not in self.compiled: 
    283286            context = self.get_context(dtype) 
    284             logging.info("building %s for OpenCL %s" 
    285                          % (key, context.devices[0].name.strip())) 
     287            logging.info("building %s for OpenCL %s", key, 
     288                         context.devices[0].name.strip()) 
    286289            program = compile_model(self.get_context(dtype), 
    287290                                    str(source), dtype, fast) 
     
    299302 
    300303def _get_default_context(): 
    301     # type: () -> cl.Context 
     304    # type: () -> List[cl.Context] 
    302305    """ 
    303306    Get an OpenCL context, preferring GPU over CPU, and preferring Intel 
     
    318321    for platform in cl.get_platforms(): 
    319322        # AMD provides a much weaker CPU driver than Intel/Apple, so avoid it. 
    320         # If someone has bothered to install the AMD/NVIDIA drivers, prefer them over the integrated 
    321         # graphics driver that may have been supplied with the CPU chipset. 
    322         preferred_cpu = platform.vendor.startswith('Intel') or platform.vendor.startswith('Apple') 
    323         preferred_gpu = platform.vendor.startswith('Advanced') or platform.vendor.startswith('NVIDIA') 
     323        # If someone has bothered to install the AMD/NVIDIA drivers, prefer 
     324        # them over the integrated graphics driver that may have been supplied 
     325        # with the CPU chipset. 
     326        preferred_cpu = (platform.vendor.startswith('Intel') 
     327                         or platform.vendor.startswith('Apple')) 
     328        preferred_gpu = (platform.vendor.startswith('Advanced') 
     329                         or platform.vendor.startswith('NVIDIA')) 
    324330        for device in platform.get_devices(): 
    325331            if device.type == cl.device_type.GPU: 
    326                 # If the existing type is not GPU then it will be CUSTOM or ACCELERATOR, 
    327                 # so don't override it. 
     332                # If the existing type is not GPU then it will be CUSTOM 
     333                # or ACCELERATOR so don't override it. 
    328334                if gpu is None or (preferred_gpu and gpu.type == cl.device_type.GPU): 
    329335                    gpu = device 
     
    334340                # System has cl.device_type.ACCELERATOR or cl.device_type.CUSTOM 
    335341                # Intel Phi for example registers as an accelerator 
    336                 # Since the user installed a custom device on their system and went through the 
    337                 # pain of sorting out OpenCL drivers for it, lets assume they really do want to 
    338                 # use it as their primary compute device. 
     342                # Since the user installed a custom device on their system 
     343                # and went through the pain of sorting out OpenCL drivers for 
     344                # it, lets assume they really do want to use it as their 
     345                # primary compute device. 
    339346                gpu = device 
    340347 
    341     # order the devices by gpu then by cpu; when searching for an available device by data type they 
    342     # will be checked in this order, which means that if the gpu supports double then the cpu will never 
    343     # be used (though we may make it possible to explicitly request the cpu at some point). 
     348    # order the devices by gpu then by cpu; when searching for an available 
     349    # device by data type they will be checked in this order, which means 
     350    # that if the gpu supports double then the cpu will never be used (though 
     351    # we may make it possible to explicitly request the cpu at some point). 
    344352    devices = [] 
    345353    if gpu is not None: 
     
    372380        self.fast = fast 
    373381        self.program = None # delay program creation 
     382        self._kernels = None 
    374383 
    375384    def __getstate__(self): 
     
    394403            names = [generate.kernel_name(self.info, k) for k in variants] 
    395404            kernels = [getattr(self.program, k) for k in names] 
    396             self._kernels = dict((k,v) for k,v in zip(variants, kernels)) 
     405            self._kernels = dict((k, v) for k, v in zip(variants, kernels)) 
    397406        is_2d = len(q_vectors) == 2 
    398407        if is_2d: 
     
    500509    def __init__(self, kernel, dtype, model_info, q_vectors): 
    501510        # type: (cl.Kernel, np.dtype, ModelInfo, List[np.ndarray]) -> None 
    502         max_pd = model_info.parameters.max_pd 
    503         npars = len(model_info.parameters.kernel_parameters)-2 
    504511        q_input = GpuInput(q_vectors, dtype) 
    505512        self.kernel = kernel 
     
    516523 
    517524        self.result_b = cl.Buffer(self.queue.context, mf.READ_WRITE, 
    518                                q_input.global_size[0] * dtype.itemsize) 
     525                                  q_input.global_size[0] * dtype.itemsize) 
    519526        self.q_input = q_input # allocated by GpuInput above 
    520527 
    521         self._need_release = [ self.result_b, self.q_input ] 
     528        self._need_release = [self.result_b, self.q_input] 
    522529        self.real = (np.float32 if dtype == generate.F32 
    523530                     else np.float64 if dtype == generate.F64 
Note: See TracChangeset for help on using the changeset viewer.