Changeset dd7fc12 in sasmodels for sasmodels/core.py
- Timestamp:
- Apr 15, 2016 11:11:43 AM (8 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/core.py
rfa5fd8d rdd7fc12 28 28 try: 29 29 from typing import List, Union, Optional, Any 30 DType = Union[None, str, np.dtype]31 30 from .kernel import KernelModel 31 from .modelinfo import ModelInfo 32 32 except ImportError: 33 33 pass … … 56 56 return available_models 57 57 58 def isstr(s):59 # type: (Any) -> bool60 """61 Return True if *s* is a string-like object.62 """63 try: s + ''64 except Exception: return False65 return True66 67 58 def load_model(model_name, dtype=None, platform='ocl'): 68 # type: (str, DType, str) -> KernelModel59 # type: (str, str, str) -> KernelModel 69 60 """ 70 61 Load model info and build model. … … 102 93 103 94 def build_model(model_info, dtype=None, platform="ocl"): 104 # type: (modelinfo.ModelInfo, np.dtype, str) -> KernelModel95 # type: (modelinfo.ModelInfo, str, str) -> KernelModel 105 96 """ 106 97 Prepare the model for the default execution platform. … … 113 104 114 105 *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. 119 109 120 110 *platform* should be "dll" to force the dll to be used for C models, … … 147 137 # source = open(model_info.name+'.cl','r').read() 148 138 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) 151 140 if (platform == "dll" 141 or dtype.endswith('!') 152 142 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) 155 145 else: 156 return kernelcl.GpuModel(source, model_info, dtype)146 return kernelcl.GpuModel(source, model_info, numpy_dtype, fast=fast) 157 147 158 148 def precompile_dll(model_name, dtype="double"): 159 # type: (str, DType) -> Optional[str]149 # type: (str, str) -> Optional[str] 160 150 """ 161 151 Precompile the dll for a model. … … 172 162 """ 173 163 model_info = load_model_info(model_name) 164 numpy_dtype, fast = parse_dtype(model_info, dtype) 174 165 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 168 def 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.