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