File: kerneldll.py

package info (click to toggle)
sasmodels 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 15,888 kB
  • sloc: python: 25,392; ansic: 7,377; makefile: 149; sh: 61
file content (462 lines) | stat: -rw-r--r-- 17,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
r"""
DLL driver for C kernels

If the environment variable *SAS_OPENMP* is set, then sasmodels
will attempt to compile with OpenMP flags so that the model can use all
available kernels.  This may or may not be available on your compiler
toolchain.  Depending on operating system and environment.

Windows does not have provide a compiler with the operating system.
Instead, we assume that TinyCC is installed and available.  This can
be done with a simple pip command if it is not already available::

    pip install tinycc

If Microsoft Visual C++ is available (because VCINSTALLDIR is
defined in the environment), then that will be used instead.
Microsoft Visual C++ for Python is available from Microsoft:

    `<http://www.microsoft.com/en-us/download/details.aspx?id=44266>`_

If neither compiler is available, sasmodels will check for *MinGW*,
the GNU compiler toolchain. This available in packages such as Anaconda
and PythonXY, or available stand alone. This toolchain has had
difficulties on some systems, and may or may not work for you.

You can control which compiler to use by setting SAS_COMPILER in the
environment:

  - tinycc (Windows): use the TinyCC compiler shipped with SasView
  - msvc (Windows): use the Microsoft Visual C++ compiler
  - mingw (Windows): use the MinGW GNU cc compiler
  - unix (Linux): use the system cc compiler.
  - unix (Mac): use the clang compiler. You will need XCode installed, and
    the XCode command line tools. Mac comes with OpenCL drivers, so generally
    this will not be needed.

Both *msvc* and *mingw* require that the compiler is available on your path.
For *msvc*, this can done by running vcvarsall.bat in a windows terminal.
Install locations are system dependent, such as:

    C:\Program Files (x86)\Common Files\Microsoft\Visual
    C++ for Python\9.0\vcvarsall.bat

or maybe

    C:\Users\yourname\AppData\Local\Programs\Common\Microsoft\Visual
    C++ for Python\9.0\vcvarsall.bat

OpenMP for *msvc* requires the Microsoft vcomp90.dll library, which doesn't
seem to be included with the compiler, nor does there appear to be a public
download location.  There may be one on your machine already in a location
such as:

    C:\Windows\winsxs\x86_microsoft.vc90.openmp*\vcomp90.dll

If you copy this to somewhere on your path, such as the python directory or
the install directory for this application, then OpenMP should be supported.

For full control of the compiler, define a function
*compile_command(source,output)* which takes the name of the source file
and the name of the output file and returns a compile command that can be
evaluated in the shell.  For even more control, replace the entire
*compile(source,output)* function.

The global attribute *ALLOW_SINGLE_PRECISION_DLLS* should be set to *False* if
you wish to prevent single precision floating point evaluation for the compiled
models, otherwise set it defaults to *True*.
"""
from __future__ import print_function

import sys
import os
from os.path import join as joinpath, splitext, abspath, dirname
import subprocess
import shlex
import tempfile
import ctypes as ct  # type: ignore
import _ctypes as _ct
import logging

import numpy as np  # type: ignore

try:
    import tinycc
except ImportError:
    tinycc = None

from . import generate
from .kernel import KernelModel, Kernel
from .kernelpy import PyInput
from .exception import annotate_exception
from .generate import F16, F32, F64

# pylint: disable=unused-import
try:
    from typing import Tuple, Callable, Any
    from .modelinfo import ModelInfo
    from .details import CallDetails
except ImportError:
    pass
# pylint: enable=unused-import

# Compiler output is a byte stream that needs to be decode in python 3.
decode = (lambda s: s) if sys.version_info[0] < 3 else (lambda s: s.decode('utf8'))

if "SAS_DLL_PATH" in os.environ:
    SAS_DLL_PATH = os.environ["SAS_DLL_PATH"]
else:
    # Assume the default location of module DLLs is in .sasmodels/compiled_models.
    SAS_DLL_PATH = os.path.join(abspath(dirname(__file__)), 'compiled_models')

if "SAS_COMPILER" in os.environ:
    COMPILER = os.environ["SAS_COMPILER"]
elif os.name == 'nt':
    if tinycc is not None:
        COMPILER = "tinycc"
    elif "VCINSTALLDIR" in os.environ:
        # If vcvarsall.bat has been called, then VCINSTALLDIR is in the
        # environment and we can use the MSVC compiler.  Otherwise, if
        # tinycc is available then use it.  Otherwise, hope that mingw
        # is available.
        COMPILER = "msvc"
    else:
        COMPILER = "mingw"
else:
    COMPILER = "unix"

ARCH = "" if ct.sizeof(ct.c_void_p) > 4 else "x86"  # 4 byte pointers on x86.
if COMPILER == "unix":
    # Generic unix compile.
    # On Mac users will need the X code command line tools installed.
    #COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp"
    CC = os.environ.get("CC", "cc")
    CPPFLAGS = os.environ.get("CPPFLAGS", "")
    CFLAGS = os.environ.get("CFLAGS", "-std=c99 -O2 -Wall")
    LDFLAGS = os.environ.get("LDFLAGS", "")
    SOFLAGS = "-fPIC -shared"
    #LIBS = os.environ.get("LIBS", "-lm")
    compiler_vars = (CC, CPPFLAGS, CFLAGS, LDFLAGS, SOFLAGS)
    compiler = [val for var in compiler_vars for val in shlex.split(var)]
    LIBS = ["-lm" ] + shlex.split(os.environ.get("LIBS", ""))

    # Add OpenMP support if not running on a Mac.
    if sys.platform != "darwin":
        # OpenMP seems to be broken on gcc 5.4.0 (ubuntu 16.04.9).
        # Shut it off for all unix until we can investigate.
        #CC.append("-fopenmp")
        pass
    def compile_command(source, output):
        """unix compiler command"""
        return compiler + [source, "-o", output] + LIBS
elif COMPILER == "msvc":
    # Call vcvarsall.bat before compiling to set path, headers, libs, etc.
    # MSVC compiler is available, so use it.  OpenMP requires a copy of
    # vcomp90.dll on the path.  One may be found here:
    #       C:/Windows/winsxs/x86_microsoft.vc90.openmp*/vcomp90.dll
    # Copy this to the python directory and uncomment the OpenMP COMPILE.
    # TODO: Remove intermediate OBJ file created in the directory.
    # TODO: Maybe don't use randomized name for the c file.
    # TODO: Maybe ask distutils to find MSVC.
    CC = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG".split()
    if "SAS_OPENMP" in os.environ:
        CC.append("/openmp")
    LN = "/link /DLL /INCREMENTAL:NO /MANIFEST".split()
    def compile_command(source, output):
        """MSVC compiler command"""
        return CC + ["/Tp%s"%source] + LN + ["/OUT:%s"%output]
elif COMPILER == "tinycc":
    # TinyCC compiler.
    CC = [tinycc.TCC] + "-shared -rdynamic -Wall".split()
    def compile_command(source, output):
        """tinycc compiler command"""
        return CC + [source, "-o", output]
elif COMPILER == "mingw":
    # MinGW compiler.
    CC = "gcc -shared -std=c99 -O2 -Wall".split()
    if "SAS_OPENMP" in os.environ:
        CC.append("-fopenmp")
    def compile_command(source, output):
        """mingw compiler command"""
        return CC + [source, "-o", output, "-lm"]

ALLOW_SINGLE_PRECISION_DLLS = True


def compile_model(source, output):
    # type: (str, str) -> None
    """
    Compile *source* producing *output*.

    Raises RuntimeError if the compile failed or the output wasn't produced.
    """
    command = compile_command(source=source, output=output)
    command_str = " ".join('"%s"'%p if ' ' in p else p for p in command)
    logging.info(command_str)
    try:
        # Need shell=True on windows to keep console box from popping up.
        shell = (os.name == 'nt')
        subprocess.check_output(command, shell=shell, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as exc:
        output = decode(exc.output)
        raise RuntimeError("compile failed.\n%s\n%s"%(command_str, output))
    if not os.path.exists(output):
        raise RuntimeError("compile failed.  File is in %r"%source)


def dll_name(model_info, dtype):
    # type: (ModelInfo, np.dtype) ->  str
    """
    Name of the dll containing the model.  This is the base file name without
    any path or extension, with a form such as 'sas_sphere32'.
    """
    bits = 8*dtype.itemsize
    basename = "sas%d_%s"%(bits, model_info.id)
    basename += ARCH + ".so"

    # Hack to find precompiled dlls.
    path = joinpath(generate.DATA_PATH, '..', 'compiled_models', basename)
    if os.path.exists(path):
        return path

    return joinpath(SAS_DLL_PATH, basename)


def dll_path(model_info, dtype):
    # type: (ModelInfo, np.dtype) -> str
    """
    Complete path to the dll for the model.  Note that the dll may not
    exist yet if it hasn't been compiled.
    """
    return dll_name(model_info, dtype)


def make_dll(source, model_info, dtype=F64, system=False):
    # type: (str, ModelInfo, np.dtype, bool) -> str
    """
    Returns the path to the compiled model defined by *kernel_module*.

    If the model has not been compiled, or if the source file(s) are newer
    than the dll, then *make_dll* will compile the model before returning.
    This routine does not load the resulting dll.

    *dtype* is a numpy floating point precision specifier indicating whether
    the model should be single, double or long double precision.  The default
    is double precision, *np.dtype('d')*.

    Set *sasmodels.ALLOW_SINGLE_PRECISION_DLLS* to False if single precision
    models are not allowed as DLLs.

    Set *sasmodels.kerneldll.SAS_DLL_PATH* to the compiled dll output path.
    Alternatively, set the environment variable *SAS_DLL_PATH*.
    The default is in ~/.sasmodels/compiled_models.

    *system* is a bool that controls whether these are the precompiled DLLs
    that would be shipped with a binary distribution.
    """
    if dtype == F16:
        raise ValueError("16 bit floats not supported")
    if dtype == F32 and not ALLOW_SINGLE_PRECISION_DLLS:
        dtype = F64  # Force 64-bit dll.
    # Note: dtype may be F128 for long double precision.

    dll = dll_path(model_info, dtype)

    if not os.path.exists(dll):
        need_recompile = True
    else:
        dll_time = os.path.getmtime(dll)
        newest_source = generate.dll_timestamp(model_info)
        need_recompile = dll_time < newest_source
    if need_recompile:
        # Make sure the DLL path exists.
        if not os.path.exists(SAS_DLL_PATH):
            os.makedirs(SAS_DLL_PATH)
        source = generate.convert_type(source, dtype)
        if not system:
            basename = splitext(os.path.basename(dll))[0] + "_"
            system_fd, filename = tempfile.mkstemp(suffix=".c", prefix=basename)
            with os.fdopen(system_fd, "w") as file_handle:
                file_handle.write(source)
        else:
            filename = splitext(dll)[0] + ".c"
            with open(filename, 'w') as file_handle:
                file_handle.write(source)
        compile_model(source=filename, output=dll)
        # Comment the following to keep the generated C file.
        # Note: If there is a syntax error then compile raises an error
        # and the source file will not be deleted.
        os.unlink(filename)
        #print("saving compiled file in %r"%filename)
    return dll


def load_dll(source, model_info, dtype=F64):
    # type: (str, ModelInfo, np.dtype) -> "DllModel"
    """
    Create and load a dll corresponding to the source, info pair returned
    from :func:`sasmodels.generate.make` compiled for the target precision.

    See :func:`make_dll` for details on controlling the dll path and the
    allowed floating point precision.
    """
    filename = make_dll(source, model_info, dtype=dtype)
    return DllModel(filename, model_info, dtype=dtype)


class DllModel(KernelModel):
    """
    ctypes wrapper for a single model.

    *source* and *model_info* are the model source and interface as returned
    from :func:`gen.make`.

    *dtype* is the desired model precision.  Any numpy dtype for single
    or double precision floats will do, such as 'f', 'float32' or 'single'
    for single and 'd', 'float64' or 'double' for double.  Double precision
    is an optional extension which may not be available on all devices.

    Call :meth:`release` when done with the kernel.
    """
    def __init__(self, dllpath, model_info, dtype=generate.F32):
        # type: (str, ModelInfo, np.dtype) -> None
        self.info = model_info
        self.dllpath = dllpath
        self._dll = None  # type: ct.CDLL
        self._kernels = None  # type: List[Callable, Callable]
        self.dtype = np.dtype(dtype)

    def _load_dll(self):
        # type: () -> None
        try:
            self._dll = ct.CDLL(self.dllpath)
        except:
            annotate_exception("while loading "+self.dllpath)
            raise

        float_type = (ct.c_float if self.dtype == generate.F32
                      else ct.c_double if self.dtype == generate.F64
                      else ct.c_longdouble)

        # int, int, int, int*, double*, double*, double*, double*, double
        argtypes = [ct.c_int32]*3 + [ct.c_void_p]*4 + [float_type, ct.c_int32]
        names = [generate.kernel_name(self.info, variant)
                 for variant in ("Iq", "Iqxy", "Imagnetic")]
        self._kernels = [self._dll[name] for name in names]
        for k in self._kernels:
            k.argtypes = argtypes

    def __getstate__(self):
        # type: () -> Tuple[ModelInfo, str]
        return self.info, self.dllpath

    def __setstate__(self, state):
        # type: (Tuple[ModelInfo, str]) -> None
        self.info, self.dllpath = state
        self._dll = None

    def make_kernel(self, q_vectors):
        # type: (List[np.ndarray]) -> DllKernel
        q_input = PyInput(q_vectors, self.dtype)
        # Note: DLL is lazy loaded.
        if self._dll is None:
            self._load_dll()
        is_2d = len(q_vectors) == 2
        kernel = self._kernels[1:3] if is_2d else [self._kernels[0]]*2
        return DllKernel(kernel, self.info, q_input)

    def release(self):
        # type: () -> None
        """
        Release any resources associated with the model.
        """
        dll_handle = self._dll._handle
        if os.name == 'nt':
            ct.windll.kernel32.FreeLibrary(dll_handle)
        else:
            _ct.dlclose(dll_handle)
        del self._dll
        self._dll = None


class DllKernel(Kernel):
    """
    Callable SAS kernel.

    *kernel* is the c function to call.

    *model_info* is the module information

    *q_input* is the DllInput q vectors at which the kernel should be
    evaluated.

    The resulting call method takes the *pars*, a list of values for
    the fixed parameters to the kernel, and *pd_pars*, a list of (value, weight)
    vectors for the polydisperse parameters.  *cutoff* determines the
    integration limits: any points with combined weight less than *cutoff*
    will not be calculated.

    Call :meth:`release` when done with the kernel instance.
    """
    def __init__(self, kernel, model_info, q_input):
        # type: (Callable[[], np.ndarray], ModelInfo, PyInput) -> None
        dtype = q_input.dtype
        self.q_input = q_input
        self.kernel = kernel

        # Attributes accessed from the outside.
        self.dim = '2d' if q_input.is_2d else '1d'
        self.info = model_info
        self.dtype = dtype

        # Converter to translate input to target type.
        self._as_dtype = (np.float32 if dtype == generate.F32
                          else np.float64 if dtype == generate.F64
                          else np.float128)

        # Holding place for the returned value.
        nout = 2 if self.info.have_Fq else 1
        extra_q = 4  # Total weight, form volume, shell volume and R_eff.
        self.result = np.empty(self.q_input.nq*nout + extra_q, dtype)

    def _call_kernel(self, call_details, values, cutoff, magnetic,
                     radius_effective_mode):
        # type: (CallDetails, np.ndarray, float, bool, int) -> np.ndarray

        # Setup kernel function and arguments.
        kernel = self.kernel[1 if magnetic else 0]
        kernel_args = [
            self.q_input.nq,  # Number of inputs.
            None,  # Placeholder for pd_start.
            None,  # Placeholder for pd_stop.
            call_details.buffer.ctypes.data,  # Problem definition.
            values.ctypes.data,  # Parameter values.
            self.q_input.q.ctypes.data,  # Q values.
            self.result.ctypes.data,   # Result storage.
            self._as_dtype(cutoff),  # Probability cutoff.
            radius_effective_mode,  # R_eff mode.
        ]

        # Call kernel and retrieve results.
        #print("Calling DLL")
        #call_details.show(values)
        step = 100
        # TODO: Do we need the explicit sleep like the OpenCL and CUDA loops?
        for start in range(0, call_details.num_eval, step):
            stop = min(start + step, call_details.num_eval)
            kernel_args[1:3] = [start, stop]
            kernel(*kernel_args) # type: ignore

    def release(self):
        # type: () -> None
        """
        Release resources associated with the kernel.
        """
        # TODO: OpenCL/CUDA allocate q_input in __init__ and free it in release.
        # Should we be doing the same for DLL?
        #self.q_input.release()
        pass

    def __del__(self):
        # type: () -> None
        self.release()