[aa4946b] | 1 | """ |
---|
| 2 | Core model handling routines. |
---|
| 3 | """ |
---|
[6d6508e] | 4 | from __future__ import print_function |
---|
| 5 | |
---|
[98f60fc] | 6 | __all__ = [ |
---|
[6d6508e] | 7 | "list_models", "load_model", "load_model_info", |
---|
| 8 | "build_model", "precompile_dll", |
---|
[98f60fc] | 9 | ] |
---|
[f734e7d] | 10 | |
---|
[6d6508e] | 11 | from os.path import basename, dirname, join as joinpath |
---|
[f734e7d] | 12 | from glob import glob |
---|
| 13 | |
---|
[7ae2b7f] | 14 | import numpy as np # type: ignore |
---|
[f734e7d] | 15 | |
---|
[aa4946b] | 16 | from . import generate |
---|
[6d6508e] | 17 | from . import modelinfo |
---|
| 18 | from . import product |
---|
[72a081d] | 19 | from . import mixture |
---|
[aa4946b] | 20 | from . import kernelpy |
---|
| 21 | from . import kerneldll |
---|
[f734e7d] | 22 | try: |
---|
[aa4946b] | 23 | from . import kernelcl |
---|
| 24 | HAVE_OPENCL = True |
---|
[ee8f734] | 25 | except Exception: |
---|
[aa4946b] | 26 | HAVE_OPENCL = False |
---|
| 27 | |
---|
[f619de7] | 28 | try: |
---|
| 29 | from typing import List, Union, Optional, Any |
---|
| 30 | from .kernel import KernelModel |
---|
[dd7fc12] | 31 | from .modelinfo import ModelInfo |
---|
[f619de7] | 32 | except ImportError: |
---|
| 33 | pass |
---|
| 34 | |
---|
| 35 | |
---|
[4d76711] | 36 | # TODO: refactor composite model support |
---|
| 37 | # The current load_model_info/build_model does not reuse existing model |
---|
| 38 | # definitions when loading a composite model, instead reloading and |
---|
| 39 | # rebuilding the kernel for each component model in the expression. This |
---|
| 40 | # is fine in a scripting environment where the model is built when the script |
---|
| 41 | # starts and is thrown away when the script ends, but may not be the best |
---|
| 42 | # solution in a long-lived application. This affects the following functions: |
---|
| 43 | # |
---|
| 44 | # load_model |
---|
| 45 | # load_model_info |
---|
| 46 | # build_model |
---|
[f734e7d] | 47 | |
---|
| 48 | def list_models(): |
---|
[f619de7] | 49 | # type: () -> List[str] |
---|
[aa4946b] | 50 | """ |
---|
| 51 | Return the list of available models on the model path. |
---|
| 52 | """ |
---|
[f734e7d] | 53 | root = dirname(__file__) |
---|
| 54 | files = sorted(glob(joinpath(root, 'models', "[a-zA-Z]*.py"))) |
---|
| 55 | available_models = [basename(f)[:-3] for f in files] |
---|
| 56 | return available_models |
---|
| 57 | |
---|
[f619de7] | 58 | def load_model(model_name, dtype=None, platform='ocl'): |
---|
[dd7fc12] | 59 | # type: (str, str, str) -> KernelModel |
---|
[b8e5e21] | 60 | """ |
---|
| 61 | Load model info and build model. |
---|
[f619de7] | 62 | |
---|
| 63 | *model_name* is the name of the model as used by :func:`load_model_info`. |
---|
| 64 | Additional keyword arguments are passed directly to :func:`build_model`. |
---|
[b8e5e21] | 65 | """ |
---|
[f619de7] | 66 | return build_model(load_model_info(model_name), |
---|
| 67 | dtype=dtype, platform=platform) |
---|
[aa4946b] | 68 | |
---|
| 69 | |
---|
[17bbadd] | 70 | def load_model_info(model_name): |
---|
[f619de7] | 71 | # type: (str) -> modelinfo.ModelInfo |
---|
[aa4946b] | 72 | """ |
---|
| 73 | Load a model definition given the model name. |
---|
[1d4017a] | 74 | |
---|
| 75 | This returns a handle to the module defining the model. This can be |
---|
| 76 | used with functions in generate to build the docs or extract model info. |
---|
[aa4946b] | 77 | """ |
---|
[72a081d] | 78 | parts = model_name.split('+') |
---|
| 79 | if len(parts) > 1: |
---|
| 80 | model_info_list = [load_model_info(p) for p in parts] |
---|
| 81 | return mixture.make_mixture_info(model_info_list) |
---|
| 82 | |
---|
| 83 | parts = model_name.split('*') |
---|
| 84 | if len(parts) > 1: |
---|
| 85 | if len(parts) > 2: |
---|
| 86 | raise ValueError("use P*S to apply structure factor S to model P") |
---|
| 87 | P_info, Q_info = [load_model_info(p) for p in parts] |
---|
| 88 | return product.make_product_info(P_info, Q_info) |
---|
| 89 | |
---|
[68e7f9d] | 90 | kernel_module = generate.load_kernel_module(model_name) |
---|
[6d6508e] | 91 | return modelinfo.make_model_info(kernel_module) |
---|
[d19962c] | 92 | |
---|
| 93 | |
---|
[17bbadd] | 94 | def build_model(model_info, dtype=None, platform="ocl"): |
---|
[dd7fc12] | 95 | # type: (modelinfo.ModelInfo, str, str) -> KernelModel |
---|
[aa4946b] | 96 | """ |
---|
| 97 | Prepare the model for the default execution platform. |
---|
| 98 | |
---|
| 99 | This will return an OpenCL model, a DLL model or a python model depending |
---|
| 100 | on the model and the computing platform. |
---|
| 101 | |
---|
[17bbadd] | 102 | *model_info* is the model definition structure returned from |
---|
| 103 | :func:`load_model_info`. |
---|
[bcd3aa3] | 104 | |
---|
[aa4946b] | 105 | *dtype* indicates whether the model should use single or double precision |
---|
[dd7fc12] | 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. |
---|
[aa4946b] | 109 | |
---|
| 110 | *platform* should be "dll" to force the dll to be used for C models, |
---|
| 111 | otherwise it uses the default "ocl". |
---|
| 112 | """ |
---|
[6d6508e] | 113 | composition = model_info.composition |
---|
[72a081d] | 114 | if composition is not None: |
---|
| 115 | composition_type, parts = composition |
---|
| 116 | models = [build_model(p, dtype=dtype, platform=platform) for p in parts] |
---|
| 117 | if composition_type == 'mixture': |
---|
| 118 | return mixture.MixtureModel(model_info, models) |
---|
| 119 | elif composition_type == 'product': |
---|
| 120 | from . import product |
---|
[e79f0a5] | 121 | P, S = models |
---|
[72a081d] | 122 | return product.ProductModel(model_info, P, S) |
---|
| 123 | else: |
---|
| 124 | raise ValueError('unknown mixture type %s'%composition_type) |
---|
[aa4946b] | 125 | |
---|
[fa5fd8d] | 126 | # If it is a python model, return it immediately |
---|
| 127 | if callable(model_info.Iq): |
---|
| 128 | return kernelpy.PyModel(model_info) |
---|
| 129 | |
---|
[aa4946b] | 130 | ## for debugging: |
---|
| 131 | ## 1. uncomment open().write so that the source will be saved next time |
---|
| 132 | ## 2. run "python -m sasmodels.direct_model $MODELNAME" to save the source |
---|
| 133 | ## 3. recomment the open.write() and uncomment open().read() |
---|
| 134 | ## 4. rerun "python -m sasmodels.direct_model $MODELNAME" |
---|
| 135 | ## 5. uncomment open().read() so that source will be regenerated from model |
---|
[6d6508e] | 136 | # open(model_info.name+'.c','w').write(source) |
---|
| 137 | # source = open(model_info.name+'.cl','r').read() |
---|
[72a081d] | 138 | source = generate.make_source(model_info) |
---|
[dd7fc12] | 139 | numpy_dtype, fast = parse_dtype(model_info, dtype) |
---|
[d15a908] | 140 | if (platform == "dll" |
---|
[dd7fc12] | 141 | or dtype.endswith('!') |
---|
[aa4946b] | 142 | or not HAVE_OPENCL |
---|
[dd7fc12] | 143 | or not kernelcl.environment().has_type(numpy_dtype)): |
---|
| 144 | return kerneldll.load_dll(source, model_info, numpy_dtype) |
---|
[aa4946b] | 145 | else: |
---|
[dd7fc12] | 146 | return kernelcl.GpuModel(source, model_info, numpy_dtype, fast=fast) |
---|
[aa4946b] | 147 | |
---|
[b8e5e21] | 148 | def precompile_dll(model_name, dtype="double"): |
---|
[dd7fc12] | 149 | # type: (str, str) -> Optional[str] |
---|
[b8e5e21] | 150 | """ |
---|
| 151 | Precompile the dll for a model. |
---|
| 152 | |
---|
| 153 | Returns the path to the compiled model, or None if the model is a pure |
---|
| 154 | python model. |
---|
| 155 | |
---|
| 156 | This can be used when build the windows distribution of sasmodels |
---|
| 157 | (which may be missing the OpenCL driver and the dll compiler), or |
---|
| 158 | otherwise sharing models with windows users who do not have a compiler. |
---|
| 159 | |
---|
| 160 | See :func:`sasmodels.kerneldll.make_dll` for details on controlling the |
---|
| 161 | dll path and the allowed floating point precision. |
---|
| 162 | """ |
---|
| 163 | model_info = load_model_info(model_name) |
---|
[dd7fc12] | 164 | numpy_dtype, fast = parse_dtype(model_info, dtype) |
---|
[b8e5e21] | 165 | source = generate.make_source(model_info) |
---|
[dd7fc12] | 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 | |
---|