source: sasmodels/sasmodels/kerneldll.py @ 03cac08

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 03cac08 was 03cac08, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

new generator produces code that compiles

  • Property mode set to 100644
File size: 11.0 KB
Line 
1r"""
2DLL driver for C kernels
3
4The global attribute *ALLOW_SINGLE_PRECISION_DLLS* should be set to *True* if
5you wish to allow single precision floating point evaluation for the compiled
6models, otherwise it defaults to *False*.
7
8The compiler command line is stored in the attribute *COMPILE*, with string
9substitutions for %(source)s and %(output)s indicating what to compile and
10where to store it.  The actual command is system dependent.
11
12On windows systems, you have a choice of compilers.  *MinGW* is the GNU
13compiler toolchain, available in packages such as anaconda and PythonXY,
14or available stand alone. This toolchain has had difficulties on some
15systems, and may or may not work for you.  In order to build DLLs, *gcc*
16must be on your path.  If the environment variable *SAS_OPENMP* is given
17then -fopenmp is added to the compiler flags.  This requires a version
18of MinGW compiled with OpenMP support.
19
20An alternative toolchain uses the Microsoft Visual C++ compiler, available
21free from microsoft:
22
23    `<http://www.microsoft.com/en-us/download/details.aspx?id=44266>`_
24
25Again, this requires that the compiler is available on your path.  This is
26done by running vcvarsall.bat in a windows terminal.  Install locations are
27system dependent, such as:
28
29    C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat
30
31or maybe
32
33    C:\Users\yourname\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat
34
35And again, the environment variable *SAS_OPENMP* controls whether OpenMP is
36used to compile the C code.  This requires the Microsoft vcomp90.dll library,
37which doesn't seem to be included with the compiler, nor does there appear
38to be a public download location.  There may be one on your machine already
39in a location such as:
40
41    C:\Windows\winsxs\x86_microsoft.vc90.openmp*\vcomp90.dll
42
43If you copy this onto your path, such as the python directory or the install
44directory for this application, then OpenMP should be supported.
45"""
46from __future__ import print_function
47
48import sys
49import os
50import tempfile
51import ctypes as ct
52from ctypes import c_void_p, c_int, c_longdouble, c_double, c_float
53
54import numpy as np
55
56from . import generate
57from .kernelpy import PyInput, PyModel
58from .exception import annotate_exception
59
60# Compiler platform details
61if sys.platform == 'darwin':
62    #COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp"
63    COMPILE = "gcc -shared -fPIC -std=c99 -O2 -Wall %(source)s -o %(output)s -lm -fno-unknown-pragmas"
64elif os.name == 'nt':
65    # call vcvarsall.bat before compiling to set path, headers, libs, etc.
66    if "VCINSTALLDIR" in os.environ:
67        # MSVC compiler is available, so use it.  OpenMP requires a copy of
68        # vcomp90.dll on the path.  One may be found here:
69        #       C:/Windows/winsxs/x86_microsoft.vc90.openmp*/vcomp90.dll
70        # Copy this to the python directory and uncomment the OpenMP COMPILE
71        # TODO: remove intermediate OBJ file created in the directory
72        # TODO: maybe don't use randomized name for the c file
73        CC = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG /Tp%(source)s "
74        LN = "/link /DLL /INCREMENTAL:NO /MANIFEST /OUT:%(output)s"
75        if "SAS_OPENMP" in os.environ:
76            COMPILE = " ".join((CC, "/openmp", LN))
77        else:
78            COMPILE = " ".join((CC, LN))
79    else:
80        COMPILE = "gcc -shared -fPIC -std=c99 -O2 -Wall %(source)s -o %(output)s -lm"
81        if "SAS_OPENMP" in os.environ:
82            COMPILE += " -fopenmp"
83        else:
84            COMPILE += " -fWno-unknown-pragmas"
85else:
86    COMPILE = "cc -shared -fPIC -fopenmp -std=c99 -O2 -Wall %(source)s -o %(output)s -lm"
87
88DLL_PATH = tempfile.gettempdir()
89
90ALLOW_SINGLE_PRECISION_DLLS = True
91
92
93def dll_path(model_info, dtype="double"):
94    """
95    Path to the compiled model defined by *model_info*.
96    """
97    from os.path import join as joinpath, split as splitpath, splitext
98    basename = splitext(splitpath(model_info['filename'])[1])[0]
99    if np.dtype(dtype) == generate.F32:
100        basename += "32"
101    elif np.dtype(dtype) == generate.F64:
102        basename += "64"
103    else:
104        basename += "128"
105    return joinpath(DLL_PATH, basename+'.so')
106
107
108def make_dll(source, model_info, dtype="double"):
109    """
110    Load the compiled model defined by *kernel_module*.
111
112    Recompile if any files are newer than the model file.
113
114    *dtype* is a numpy floating point precision specifier indicating whether
115    the model should be single or double precision.  The default is double
116    precision.
117
118    The DLL is not loaded until the kernel is called so models can
119    be defined without using too many resources.
120
121    Set *sasmodels.kerneldll.DLL_PATH* to the compiled dll output path.
122    The default is the system temporary directory.
123
124    Set *sasmodels.ALLOW_SINGLE_PRECISION_DLLS* to True if single precision
125    models are allowed as DLLs.
126    """
127    if callable(model_info.get('Iq', None)):
128        return PyModel(model_info)
129
130    dtype = np.dtype(dtype)
131    if dtype == generate.F16:
132        raise ValueError("16 bit floats not supported")
133    if dtype == generate.F32 and not ALLOW_SINGLE_PRECISION_DLLS:
134        dtype = generate.F64  # Force 64-bit dll
135
136    if dtype == generate.F32: # 32-bit dll
137        tempfile_prefix = 'sas_' + model_info['name'] + '32_'
138    elif dtype == generate.F64:
139        tempfile_prefix = 'sas_' + model_info['name'] + '64_'
140    else:
141        tempfile_prefix = 'sas_' + model_info['name'] + '128_'
142
143    source = generate.convert_type(source, dtype)
144    source_files = generate.model_sources(model_info) + [model_info['filename']]
145    dll = dll_path(model_info, dtype)
146    newest = max(os.path.getmtime(f) for f in source_files)
147    if not os.path.exists(dll) or os.path.getmtime(dll) < newest:
148        # Replace with a proper temp file
149        fid, filename = tempfile.mkstemp(suffix=".c", prefix=tempfile_prefix)
150        os.fdopen(fid, "w").write(source)
151        command = COMPILE%{"source":filename, "output":dll}
152        print("Compile command: "+command)
153        status = os.system(command)
154        if status != 0 or not os.path.exists(dll):
155            raise RuntimeError("compile failed.  File is in %r"%filename)
156        else:
157            ## comment the following to keep the generated c file
158            os.unlink(filename)
159            #print("saving compiled file in %r"%filename)
160    return dll
161
162
163def load_dll(source, model_info, dtype="double"):
164    """
165    Create and load a dll corresponding to the source, info pair returned
166    from :func:`sasmodels.generate.make` compiled for the target precision.
167
168    See :func:`make_dll` for details on controlling the dll path and the
169    allowed floating point precision.
170    """
171    filename = make_dll(source, model_info, dtype=dtype)
172    return DllModel(filename, model_info, dtype=dtype)
173
174
175IQ_ARGS = [c_void_p, c_void_p, c_int]
176IQXY_ARGS = [c_void_p, c_void_p, c_void_p, c_int]
177
178class DllModel(object):
179    """
180    ctypes wrapper for a single model.
181
182    *source* and *model_info* are the model source and interface as returned
183    from :func:`gen.make`.
184
185    *dtype* is the desired model precision.  Any numpy dtype for single
186    or double precision floats will do, such as 'f', 'float32' or 'single'
187    for single and 'd', 'float64' or 'double' for double.  Double precision
188    is an optional extension which may not be available on all devices.
189
190    Call :meth:`release` when done with the kernel.
191    """
192    def __init__(self, dllpath, model_info, dtype=generate.F32):
193        self.info = model_info
194        self.dllpath = dllpath
195        self.dll = None
196        self.dtype = np.dtype(dtype)
197
198    def _load_dll(self):
199        Nfixed1d = len(self.info['partype']['fixed-1d'])
200        Nfixed2d = len(self.info['partype']['fixed-2d'])
201        Npd1d = len(self.info['partype']['pd-1d'])
202        Npd2d = len(self.info['partype']['pd-2d'])
203
204        #print("dll", self.dllpath)
205        try:
206            self.dll = ct.CDLL(self.dllpath)
207        except Exception as exc:
208            annotate_exception(exc, "while loading "+self.dllpath)
209            raise
210
211        fp = (c_float if self.dtype == generate.F32
212              else c_double if self.dtype == generate.F64
213              else c_longdouble)
214        pd_args_1d = [c_void_p, fp] + [c_int]*Npd1d if Npd1d else []
215        pd_args_2d = [c_void_p, fp] + [c_int]*Npd2d if Npd2d else []
216        self.Iq = self.dll[generate.kernel_name(self.info, False)]
217        self.Iq.argtypes = IQ_ARGS + pd_args_1d + [fp]*Nfixed1d
218
219        self.Iqxy = self.dll[generate.kernel_name(self.info, True)]
220        self.Iqxy.argtypes = IQXY_ARGS + pd_args_2d + [fp]*Nfixed2d
221
222    def __getstate__(self):
223        return self.info, self.dllpath
224
225    def __setstate__(self, state):
226        self.info, self.dllpath = state
227        self.dll = None
228
229    def __call__(self, q_vectors):
230        q_input = PyInput(q_vectors, self.dtype)
231        if self.dll is None: self._load_dll()
232        kernel = self.Iqxy if q_input.is_2d else self.Iq
233        return DllKernel(kernel, self.info, q_input)
234
235    def release(self):
236        """
237        Release any resources associated with the model.
238        """
239        pass # TODO: should release the dll
240
241
242class DllKernel(object):
243    """
244    Callable SAS kernel.
245
246    *kernel* is the c function to call.
247
248    *model_info* is the module information
249
250    *q_input* is the DllInput q vectors at which the kernel should be
251    evaluated.
252
253    The resulting call method takes the *pars*, a list of values for
254    the fixed parameters to the kernel, and *pd_pars*, a list of (value, weight)
255    vectors for the polydisperse parameters.  *cutoff* determines the
256    integration limits: any points with combined weight less than *cutoff*
257    will not be calculated.
258
259    Call :meth:`release` when done with the kernel instance.
260    """
261    def __init__(self, kernel, model_info, q_input):
262        self.info = model_info
263        self.q_input = q_input
264        self.kernel = kernel
265        self.res = np.empty(q_input.nq, q_input.dtype)
266        dim = '2d' if q_input.is_2d else '1d'
267        self.fixed_pars = model_info['partype']['fixed-' + dim]
268        self.pd_pars = model_info['partype']['pd-' + dim]
269
270        # In dll kernel, but not in opencl kernel
271        self.p_res = self.res.ctypes.data
272
273    def __call__(self, fixed_pars, pd_pars, cutoff):
274        real = (np.float32 if self.q_input.dtype == generate.F32
275                else np.float64 if self.q_input.dtype == generate.F64
276                else np.float128)
277
278        nq = c_int(self.q_input.nq)
279        if pd_pars:
280            cutoff = real(cutoff)
281            loops_N = [np.uint32(len(p[0])) for p in pd_pars]
282            loops = np.hstack(pd_pars)
283            loops = np.ascontiguousarray(loops.T, self.q_input.dtype).flatten()
284            p_loops = loops.ctypes.data
285            dispersed = [p_loops, cutoff] + loops_N
286        else:
287            dispersed = []
288        fixed = [real(p) for p in fixed_pars]
289        args = self.q_input.q_pointers + [self.p_res, nq] + dispersed + fixed
290        #print(pars)
291        self.kernel(*args)
292
293        return self.res
294
295    def release(self):
296        """
297        Release any resources associated with the kernel.
298        """
299        pass
Note: See TracBrowser for help on using the repository browser.