source: sasmodels/sasmodels/core.py @ 7624dd3

Last change on this file since 7624dd3 was 7624dd3, checked in by Adam Washington <adam.washington@…>, 6 years ago

Remove old, commented out code

  • Property mode set to 100644
File size: 12.6 KB
Line 
1"""
2Core model handling routines.
3"""
4from __future__ import print_function
5
6__all__ = [
7    "list_models", "load_model", "load_model_info",
8    "build_model", "precompile_dlls",
9    ]
10
11import os
12from os.path import basename, join as joinpath
13from glob import glob
14import re
15
16import numpy as np # type: ignore
17
18from . import generate
19from . import modelinfo
20from . import product
21from . import mixture
22from . import kernelpy
23from . import kerneldll
24from . import custom
25
26if os.environ.get("SAS_OPENCL", "").lower() == "none":
27    HAVE_OPENCL = False
28else:
29    try:
30        from . import kernelcl
31        HAVE_OPENCL = True
32    except Exception:
33        HAVE_OPENCL = False
34
35CUSTOM_MODEL_PATH = os.environ.get('SAS_MODELPATH', "")
36if CUSTOM_MODEL_PATH == "":
37    CUSTOM_MODEL_PATH = joinpath(os.path.expanduser("~"), ".sasmodels", "custom_models")
38    if not os.path.isdir(CUSTOM_MODEL_PATH):
39        os.makedirs(CUSTOM_MODEL_PATH)
40
41# pylint: disable=unused-import
42try:
43    from typing import List, Union, Optional, Any
44    from .kernel import KernelModel
45    from .modelinfo import ModelInfo
46except ImportError:
47    pass
48# pylint: enable=unused-import
49
50# TODO: refactor composite model support
51# The current load_model_info/build_model does not reuse existing model
52# definitions when loading a composite model, instead reloading and
53# rebuilding the kernel for each component model in the expression.  This
54# is fine in a scripting environment where the model is built when the script
55# starts and is thrown away when the script ends, but may not be the best
56# solution in a long-lived application.  This affects the following functions:
57#
58#    load_model
59#    load_model_info
60#    build_model
61
62KINDS = ("all", "py", "c", "double", "single", "opencl", "1d", "2d",
63         "nonmagnetic", "magnetic")
64def list_models(kind=None):
65    # type: (str) -> List[str]
66    """
67    Return the list of available models on the model path.
68
69    *kind* can be one of the following:
70
71        * all: all models
72        * py: python models only
73        * c: compiled models only
74        * single: models which support single precision
75        * double: models which require double precision
76        * opencl: controls if OpenCL is supperessed
77        * 1d: models which are 1D only, or 2D using abs(q)
78        * 2d: models which can be 2D
79        * magnetic: models with an sld
80        * nommagnetic: models without an sld
81
82    For multiple conditions, combine with plus.  For example, *c+single+2d*
83    would return all oriented models implemented in C which can be computed
84    accurately with single precision arithmetic.
85    """
86    if kind and any(k not in KINDS for k in kind.split('+')):
87        raise ValueError("kind not in " + ", ".join(KINDS))
88    files = sorted(glob(joinpath(generate.MODEL_PATH, "[a-zA-Z]*.py")))
89    available_models = [basename(f)[:-3] for f in files]
90    if kind and '+' in kind:
91        all_kinds = kind.split('+')
92        condition = lambda name: all(_matches(name, k) for k in all_kinds)
93    else:
94        condition = lambda name: _matches(name, kind)
95    selected = [name for name in available_models if condition(name)]
96
97    return selected
98
99def _matches(name, kind):
100    if kind is None or kind == "all":
101        return True
102    info = load_model_info(name)
103    pars = info.parameters.kernel_parameters
104    if kind == "py" and callable(info.Iq):
105        return True
106    elif kind == "c" and not callable(info.Iq):
107        return True
108    elif kind == "double" and not info.single:
109        return True
110    elif kind == "single" and info.single:
111        return True
112    elif kind == "opencl" and info.opencl:
113        return True
114    elif kind == "2d" and any(p.type == 'orientation' for p in pars):
115        return True
116    elif kind == "1d" and all(p.type != 'orientation' for p in pars):
117        return True
118    elif kind == "magnetic" and any(p.type == 'sld' for p in pars):
119        return True
120    elif kind == "nonmagnetic" and any(p.type != 'sld' for p in pars):
121        return True
122    return False
123
124def load_model(model_name, dtype=None, platform='ocl'):
125    # type: (str, str, str) -> KernelModel
126    """
127    Load model info and build model.
128
129    *model_name* is the name of the model, or perhaps a model expression
130    such as sphere*hardsphere or sphere+cylinder.
131
132    *dtype* and *platform* are given by :func:`build_model`.
133    """
134    return build_model(load_model_info(model_name),
135                       dtype=dtype, platform=platform)
136
137def load_model_info(model_string):
138    # type: (str) -> modelinfo.ModelInfo
139    """
140    Load a model definition given the model name.
141
142    *model_string* is the name of the model, or perhaps a model expression
143    such as sphere*cylinder or sphere+cylinder. Use '@' for a structure
144    factor product, e.g. sphere@hardsphere. Custom models can be specified by
145    prefixing the model name with 'custom.', e.g. 'custom.MyModel+sphere'.
146
147    This returns a handle to the module defining the model.  This can be
148    used with functions in generate to build the docs or extract model info.
149    """
150    if '@' in model_string:
151        terms = model_string.split('+')
152        results = []
153        for term in terms:
154            if '@' in term:
155                p_info, q_info = [load_model_info(part)
156                                  for part in term.split("@")]
157                results.append(product.make_product_info(p_info, q_info))
158            else:
159                results.append(load_model_info(term))
160        return mixture.make_mixture_info(results, operation='+')
161
162    product_parts = []
163    addition_parts = []
164
165    addition_parts_names = model_string.split('+')
166    if len(addition_parts_names) >= 2:
167        addition_parts = [load_model_info(part) for part in addition_parts_names]
168    elif len(addition_parts_names) == 1:
169        product_parts_names = model_string.split('*')
170        if len(product_parts_names) >= 2:
171            product_parts = [load_model_info(part) for part in product_parts_names]
172        elif len(product_parts_names) == 1:
173            if "custom." in product_parts_names[0]:
174                # Extract ModelName from "custom.ModelName"
175                pattern = "custom.([A-Za-z0-9_-]+)"
176                result = re.match(pattern, product_parts_names[0])
177                if result is None:
178                    raise ValueError("Model name in invalid format: " + product_parts_names[0])
179                model_name = result.group(1)
180                # Use ModelName to find the path to the custom model file
181                model_path = joinpath(CUSTOM_MODEL_PATH, model_name + ".py")
182                if not os.path.isfile(model_path):
183                    raise ValueError("The model file {} doesn't exist".format(model_path))
184                kernel_module = custom.load_custom_kernel_module(model_path)
185                return modelinfo.make_model_info(kernel_module)
186            # Model is a core model
187            kernel_module = generate.load_kernel_module(product_parts_names[0])
188            return modelinfo.make_model_info(kernel_module)
189
190    model = None
191    if len(product_parts) > 1:
192        model = mixture.make_mixture_info(product_parts, operation='*')
193    if len(addition_parts) > 1:
194        if model is not None:
195            addition_parts.append(model)
196        model = mixture.make_mixture_info(addition_parts, operation='+')
197    return model
198
199
200def build_model(model_info, dtype=None, platform="ocl"):
201    # type: (modelinfo.ModelInfo, str, str) -> KernelModel
202    """
203    Prepare the model for the default execution platform.
204
205    This will return an OpenCL model, a DLL model or a python model depending
206    on the model and the computing platform.
207
208    *model_info* is the model definition structure returned from
209    :func:`load_model_info`.
210
211    *dtype* indicates whether the model should use single or double precision
212    for the calculation.  Choices are 'single', 'double', 'quad', 'half',
213    or 'fast'.  If *dtype* ends with '!', then force the use of the DLL rather
214    than OpenCL for the calculation.
215
216    *platform* should be "dll" to force the dll to be used for C models,
217    otherwise it uses the default "ocl".
218    """
219    composition = model_info.composition
220    if composition is not None:
221        composition_type, parts = composition
222        models = [build_model(p, dtype=dtype, platform=platform) for p in parts]
223        if composition_type == 'mixture':
224            return mixture.MixtureModel(model_info, models)
225        elif composition_type == 'product':
226            P, S = models
227            return product.ProductModel(model_info, P, S)
228        else:
229            raise ValueError('unknown mixture type %s'%composition_type)
230
231    # If it is a python model, return it immediately
232    if callable(model_info.Iq):
233        return kernelpy.PyModel(model_info)
234
235    numpy_dtype, fast, platform = parse_dtype(model_info, dtype, platform)
236
237    source = generate.make_source(model_info)
238    if platform == "dll":
239        #print("building dll", numpy_dtype)
240        return kerneldll.load_dll(source['dll'], model_info, numpy_dtype)
241    else:
242        #print("building ocl", numpy_dtype)
243        return kernelcl.GpuModel(source, model_info, numpy_dtype, fast=fast)
244
245def precompile_dlls(path, dtype="double"):
246    # type: (str, str) -> List[str]
247    """
248    Precompile the dlls for all builtin models, returning a list of dll paths.
249
250    *path* is the directory in which to save the dlls.  It will be created if
251    it does not already exist.
252
253    This can be used when build the windows distribution of sasmodels
254    which may be missing the OpenCL driver and the dll compiler.
255    """
256    numpy_dtype = np.dtype(dtype)
257    if not os.path.exists(path):
258        os.makedirs(path)
259    compiled_dlls = []
260    for model_name in list_models():
261        model_info = load_model_info(model_name)
262        if not callable(model_info.Iq):
263            source = generate.make_source(model_info)['dll']
264            old_path = kerneldll.DLL_PATH
265            try:
266                kerneldll.DLL_PATH = path
267                dll = kerneldll.make_dll(source, model_info, dtype=numpy_dtype)
268            finally:
269                kerneldll.DLL_PATH = old_path
270            compiled_dlls.append(dll)
271    return compiled_dlls
272
273def parse_dtype(model_info, dtype=None, platform=None):
274    # type: (ModelInfo, str, str) -> (np.dtype, bool, str)
275    """
276    Interpret dtype string, returning np.dtype and fast flag.
277
278    Possible types include 'half', 'single', 'double' and 'quad'.  If the
279    type is 'fast', then this is equivalent to dtype 'single' but using
280    fast native functions rather than those with the precision level
281    guaranteed by the OpenCL standard.  'default' will choose the appropriate
282    default for the model and platform.
283
284    Platform preference can be specfied ("ocl" vs "dll"), with the default
285    being OpenCL if it is availabe.  If the dtype name ends with '!' then
286    platform is forced to be DLL rather than OpenCL.
287
288    This routine ignores the preferences within the model definition.  This
289    is by design.  It allows us to test models in single precision even when
290    we have flagged them as requiring double precision so we can easily check
291    the performance on different platforms without having to change the model
292    definition.
293    """
294    # Assign default platform, overriding ocl with dll if OpenCL is unavailable
295    # If opencl=False OpenCL is switched off
296
297    if platform is None:
298        platform = "ocl"
299    if platform == "ocl" and not HAVE_OPENCL or not model_info.opencl:
300        platform = "dll"
301
302    # Check if type indicates dll regardless of which platform is given
303    if dtype is not None and dtype.endswith('!'):
304        platform = "dll"
305        dtype = dtype[:-1]
306
307    # Convert special type names "half", "fast", and "quad"
308    fast = (dtype == "fast")
309    if fast:
310        dtype = "single"
311    elif dtype == "quad":
312        dtype = "longdouble"
313    elif dtype == "half":
314        dtype = "float16"
315
316    # Convert dtype string to numpy dtype.
317    if dtype is None or dtype == "default":
318        numpy_dtype = (generate.F32 if platform == "ocl" and model_info.single
319                       else generate.F64)
320    else:
321        numpy_dtype = np.dtype(dtype)
322
323    # Make sure that the type is supported by opencl, otherwise use dll
324    if platform == "ocl":
325        env = kernelcl.environment()
326        if not env.has_type(numpy_dtype):
327            platform = "dll"
328            if dtype is None:
329                numpy_dtype = generate.F64
330
331    return numpy_dtype, fast, platform
332
333def list_models_main():
334    # type: () -> None
335    """
336    Run list_models as a main program.  See :func:`list_models` for the
337    kinds of models that can be requested on the command line.
338    """
339    import sys
340    kind = sys.argv[1] if len(sys.argv) > 1 else "all"
341    print("\n".join(list_models(kind)))
342
343if __name__ == "__main__":
344    list_models_main()
Note: See TracBrowser for help on using the repository browser.