Changeset dd7fc12 in sasmodels for sasmodels/core.py


Ignore:
Timestamp:
Apr 15, 2016 11:11:43 AM (8 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:
3599d36
Parents:
b151003
Message:

fix kerneldll dtype problem; more type hinting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/core.py

    rfa5fd8d rdd7fc12  
    2828try: 
    2929    from typing import List, Union, Optional, Any 
    30     DType = Union[None, str, np.dtype] 
    3130    from .kernel import KernelModel 
     31    from .modelinfo import ModelInfo 
    3232except ImportError: 
    3333    pass 
     
    5656    return available_models 
    5757 
    58 def isstr(s): 
    59     # type: (Any) -> bool 
    60     """ 
    61     Return True if *s* is a string-like object. 
    62     """ 
    63     try: s + '' 
    64     except Exception: return False 
    65     return True 
    66  
    6758def load_model(model_name, dtype=None, platform='ocl'): 
    68     # type: (str, DType, str) -> KernelModel 
     59    # type: (str, str, str) -> KernelModel 
    6960    """ 
    7061    Load model info and build model. 
     
    10293 
    10394def build_model(model_info, dtype=None, platform="ocl"): 
    104     # type: (modelinfo.ModelInfo, np.dtype, str) -> KernelModel 
     95    # type: (modelinfo.ModelInfo, str, str) -> KernelModel 
    10596    """ 
    10697    Prepare the model for the default execution platform. 
     
    113104 
    114105    *dtype* indicates whether the model should use single or double precision 
    115     for the calculation. Any valid numpy single or double precision identifier 
    116     is valid, such as 'single', 'f', 'f32', or np.float32 for single, or 
    117     'double', 'd', 'f64'  and np.float64 for double.  If *None*, then use 
    118     'single' unless the model defines single=False. 
     106    for the calculation.  Choices are 'single', 'double', 'quad', 'half', 
     107    or 'fast'.  If *dtype* ends with '!', then force the use of the DLL rather 
     108    than OpenCL for the calculation. 
    119109 
    120110    *platform* should be "dll" to force the dll to be used for C models, 
     
    147137    # source = open(model_info.name+'.cl','r').read() 
    148138    source = generate.make_source(model_info) 
    149     if dtype is None: 
    150         dtype = generate.F32 if model_info.single else generate.F64 
     139    numpy_dtype, fast = parse_dtype(model_info, dtype) 
    151140    if (platform == "dll" 
     141            or dtype.endswith('!') 
    152142            or not HAVE_OPENCL 
    153             or not kernelcl.environment().has_type(dtype)): 
    154         return kerneldll.load_dll(source, model_info, dtype) 
     143            or not kernelcl.environment().has_type(numpy_dtype)): 
     144        return kerneldll.load_dll(source, model_info, numpy_dtype) 
    155145    else: 
    156         return kernelcl.GpuModel(source, model_info, dtype) 
     146        return kernelcl.GpuModel(source, model_info, numpy_dtype, fast=fast) 
    157147 
    158148def precompile_dll(model_name, dtype="double"): 
    159     # type: (str, DType) -> Optional[str] 
     149    # type: (str, str) -> Optional[str] 
    160150    """ 
    161151    Precompile the dll for a model. 
     
    172162    """ 
    173163    model_info = load_model_info(model_name) 
     164    numpy_dtype, fast = parse_dtype(model_info, dtype) 
    174165    source = generate.make_source(model_info) 
    175     return kerneldll.make_dll(source, model_info, dtype=dtype) if source else None 
     166    return kerneldll.make_dll(source, model_info, dtype=numpy_dtype) if source else None 
     167 
     168def parse_dtype(model_info, dtype): 
     169    # type: (ModelInfo, str) -> Tuple[np.dtype, bool] 
     170    """ 
     171    Interpret dtype string, returning np.dtype and fast flag. 
     172 
     173    Possible types include 'half', 'single', 'double' and 'quad'.  If the 
     174    type is 'fast', then this is equivalent to dtype 'single' with the 
     175    fast flag set to True. 
     176    """ 
     177    # Fill in default type based on required precision in the model 
     178    if dtype is None: 
     179        dtype = 'single' if model_info.single else 'double' 
     180 
     181    # Ignore platform indicator 
     182    if dtype.endswith('!'): 
     183        dtype = dtype[:-1] 
     184 
     185    # Convert type string to type 
     186    if dtype == 'quad': 
     187        return generate.F128, False 
     188    elif dtype == 'half': 
     189        return generate.F16, False 
     190    elif dtype == 'fast': 
     191        return generate.F32, True 
     192    else: 
     193        return np.dtype(dtype), False 
     194 
Note: See TracChangeset for help on using the changeset viewer.