1 | r""" |
---|
2 | C types wrapper for sasview models. |
---|
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*. |
---|
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 | |
---|
23 | `http://www.microsoft.com/en-us/download/details.aspx?id=44266`_ |
---|
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. |
---|
45 | """ |
---|
46 | |
---|
47 | import sys |
---|
48 | import os |
---|
49 | import tempfile |
---|
50 | import ctypes as ct |
---|
51 | from ctypes import c_void_p, c_int, c_longdouble, c_double, c_float |
---|
52 | |
---|
53 | import numpy as np |
---|
54 | |
---|
55 | from . import generate |
---|
56 | from .kernelpy import PyInput, PyModel |
---|
57 | from .exception import annotate_exception |
---|
58 | |
---|
59 | # Compiler platform details |
---|
60 | if sys.platform == 'darwin': |
---|
61 | #COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp" |
---|
62 | COMPILE = "gcc -shared -fPIC -std=c99 -O2 -Wall %(source)s -o %(output)s -lm" |
---|
63 | elif os.name == 'nt': |
---|
64 | # call vcvarsall.bat before compiling to set path, headers, libs, etc. |
---|
65 | if "VCINSTALLDIR" in os.environ: |
---|
66 | # MSVC compiler is available, so use it. OpenMP requires a copy of |
---|
67 | # vcomp90.dll on the path. One may be found here: |
---|
68 | # C:/Windows/winsxs/x86_microsoft.vc90.openmp*/vcomp90.dll |
---|
69 | # Copy this to the python directory and uncomment the OpenMP COMPILE |
---|
70 | # TODO: remove intermediate OBJ file created in the directory |
---|
71 | # TODO: maybe don't use randomized name for the c file |
---|
72 | CC = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG /Tp%(source)s " |
---|
73 | LN = "/link /DLL /INCREMENTAL:NO /MANIFEST /OUT:%(output)s" |
---|
74 | if "SAS_OPENMP" in os.environ: |
---|
75 | COMPILE = " ".join((CC, "/openmp", LN)) |
---|
76 | else: |
---|
77 | COMPILE = " ".join((CC, LN)) |
---|
78 | else: |
---|
79 | COMPILE = "gcc -shared -fPIC -std=c99 -O2 -Wall %(source)s -o %(output)s -lm" |
---|
80 | if "SAS_OPENMP" in os.environ: |
---|
81 | COMPILE = COMPILE + " -fopenmp" |
---|
82 | else: |
---|
83 | COMPILE = "cc -shared -fPIC -fopenmp -std=c99 -O2 -Wall %(source)s -o %(output)s -lm" |
---|
84 | |
---|
85 | DLL_PATH = tempfile.gettempdir() |
---|
86 | |
---|
87 | ALLOW_SINGLE_PRECISION_DLLS = False |
---|
88 | |
---|
89 | |
---|
90 | def dll_path(info, dtype="double"): |
---|
91 | """ |
---|
92 | Path to the compiled model defined by *info*. |
---|
93 | """ |
---|
94 | from os.path import join as joinpath, split as splitpath, splitext |
---|
95 | basename = splitext(splitpath(info['filename'])[1])[0] |
---|
96 | if np.dtype(dtype) == generate.F32: |
---|
97 | basename += "32" |
---|
98 | elif np.dtype(dtype) == generate.F64: |
---|
99 | basename += "64" |
---|
100 | else: |
---|
101 | basename += "128" |
---|
102 | return joinpath(DLL_PATH, basename+'.so') |
---|
103 | |
---|
104 | |
---|
105 | def make_dll(source, info, dtype="double"): |
---|
106 | """ |
---|
107 | Load the compiled model defined by *kernel_module*. |
---|
108 | |
---|
109 | Recompile if any files are newer than the model file. |
---|
110 | |
---|
111 | *dtype* is a numpy floating point precision specifier indicating whether |
---|
112 | the model should be single or double precision. The default is double |
---|
113 | precision. |
---|
114 | |
---|
115 | The DLL is not loaded until the kernel is called so models can |
---|
116 | be defined without using too many resources. |
---|
117 | |
---|
118 | Set *sasmodels.kerneldll.DLL_PATH* to the compiled dll output path. |
---|
119 | The default is the system temporary directory. |
---|
120 | |
---|
121 | Set *sasmodels.ALLOW_SINGLE_PRECISION_DLLS* to True if single precision |
---|
122 | models are allowed as DLLs. |
---|
123 | """ |
---|
124 | dtype = np.dtype(dtype) |
---|
125 | if dtype == generate.F32 and not ALLOW_SINGLE_PRECISION_DLLS: |
---|
126 | dtype = generate.F64 # Force 64-bit dll |
---|
127 | |
---|
128 | if callable(info.get('Iq',None)): |
---|
129 | return PyModel(info) |
---|
130 | |
---|
131 | if dtype == generate.F32: # 32-bit dll |
---|
132 | source = generate.use_single(source) |
---|
133 | tempfile_prefix = 'sas_'+info['name']+'32_' |
---|
134 | elif dtype == generate.F64: |
---|
135 | tempfile_prefix = 'sas_'+info['name']+'64_' |
---|
136 | else: |
---|
137 | source = generate.use_long_double(source) |
---|
138 | tempfile_prefix = 'sas_'+info['name']+'128_' |
---|
139 | |
---|
140 | source_files = generate.sources(info) + [info['filename']] |
---|
141 | dll= dll_path(info, dtype) |
---|
142 | newest = max(os.path.getmtime(f) for f in source_files) |
---|
143 | if not os.path.exists(dll) or os.path.getmtime(dll)<newest: |
---|
144 | # Replace with a proper temp file |
---|
145 | fid, filename = tempfile.mkstemp(suffix=".c",prefix=tempfile_prefix) |
---|
146 | os.fdopen(fid,"w").write(source) |
---|
147 | command = COMPILE%{"source":filename, "output":dll} |
---|
148 | print "Compile command:",command |
---|
149 | status = os.system(command) |
---|
150 | if status != 0 or not os.path.exists(dll): |
---|
151 | raise RuntimeError("compile failed. File is in %r"%filename) |
---|
152 | else: |
---|
153 | ## uncomment the following to keep the generated c file |
---|
154 | os.unlink(filename); print "saving compiled file in %r"%filename |
---|
155 | return dll |
---|
156 | |
---|
157 | |
---|
158 | def load_dll(source, info, dtype="double"): |
---|
159 | """ |
---|
160 | Create and load a dll corresponding to the source,info pair returned |
---|
161 | from :func:`sasmodels.generate.make` compiled for the target precision. |
---|
162 | |
---|
163 | See :func:`make_dll` for details on controlling the dll path and the |
---|
164 | allowed floating point precision. |
---|
165 | """ |
---|
166 | filename = make_dll(source, info, dtype=dtype) |
---|
167 | return DllModel(filename, info, dtype=dtype) |
---|
168 | |
---|
169 | |
---|
170 | IQ_ARGS = [c_void_p, c_void_p, c_int] |
---|
171 | IQXY_ARGS = [c_void_p, c_void_p, c_void_p, c_int] |
---|
172 | |
---|
173 | class DllModel(object): |
---|
174 | """ |
---|
175 | ctypes wrapper for a single model. |
---|
176 | |
---|
177 | *source* and *info* are the model source and interface as returned |
---|
178 | from :func:`gen.make`. |
---|
179 | |
---|
180 | *dtype* is the desired model precision. Any numpy dtype for single |
---|
181 | or double precision floats will do, such as 'f', 'float32' or 'single' |
---|
182 | for single and 'd', 'float64' or 'double' for double. Double precision |
---|
183 | is an optional extension which may not be available on all devices. |
---|
184 | |
---|
185 | Call :meth:`release` when done with the kernel. |
---|
186 | """ |
---|
187 | def __init__(self, dllpath, info, dtype=generate.F32): |
---|
188 | self.info = info |
---|
189 | self.dllpath = dllpath |
---|
190 | self.dll = None |
---|
191 | self.dtype = np.dtype(dtype) |
---|
192 | |
---|
193 | def _load_dll(self): |
---|
194 | Nfixed1d = len(self.info['partype']['fixed-1d']) |
---|
195 | Nfixed2d = len(self.info['partype']['fixed-2d']) |
---|
196 | Npd1d = len(self.info['partype']['pd-1d']) |
---|
197 | Npd2d = len(self.info['partype']['pd-2d']) |
---|
198 | |
---|
199 | #print "dll",self.dllpath |
---|
200 | try: |
---|
201 | self.dll = ct.CDLL(self.dllpath) |
---|
202 | except Exception, exc: |
---|
203 | annotate_exception(exc, "while loading "+self.dllpath) |
---|
204 | raise |
---|
205 | |
---|
206 | fp = (c_float if self.dtype == generate.F32 |
---|
207 | else c_double if self.dtype == generate.F64 |
---|
208 | else c_longdouble) |
---|
209 | pd_args_1d = [c_void_p, fp] + [c_int]*Npd1d if Npd1d else [] |
---|
210 | pd_args_2d= [c_void_p, fp] + [c_int]*Npd2d if Npd2d else [] |
---|
211 | self.Iq = self.dll[generate.kernel_name(self.info, False)] |
---|
212 | self.Iq.argtypes = IQ_ARGS + pd_args_1d + [fp]*Nfixed1d |
---|
213 | |
---|
214 | self.Iqxy = self.dll[generate.kernel_name(self.info, True)] |
---|
215 | self.Iqxy.argtypes = IQXY_ARGS + pd_args_2d + [fp]*Nfixed2d |
---|
216 | |
---|
217 | def __getstate__(self): |
---|
218 | return {'info': self.info, 'dllpath': self.dllpath, 'dll': None} |
---|
219 | |
---|
220 | def __setstate__(self, state): |
---|
221 | self.__dict__ = state |
---|
222 | |
---|
223 | def __call__(self, q_input): |
---|
224 | if self.dtype != q_input.dtype: |
---|
225 | raise TypeError("data is %s kernel is %s" % (q_input.dtype, self.dtype)) |
---|
226 | if self.dll is None: self._load_dll() |
---|
227 | kernel = self.Iqxy if q_input.is_2D else self.Iq |
---|
228 | return DllKernel(kernel, self.info, q_input) |
---|
229 | |
---|
230 | # pylint: disable=no-self-use |
---|
231 | def make_input(self, q_vectors): |
---|
232 | """ |
---|
233 | Make q input vectors available to the model. |
---|
234 | |
---|
235 | Note that each model needs its own q vector even if the case of |
---|
236 | mixture models because some models may be OpenCL, some may be |
---|
237 | ctypes and some may be pure python. |
---|
238 | """ |
---|
239 | return PyInput(q_vectors, dtype=self.dtype) |
---|
240 | |
---|
241 | def release(self): |
---|
242 | pass # TODO: should release the dll |
---|
243 | |
---|
244 | |
---|
245 | class DllKernel(object): |
---|
246 | """ |
---|
247 | Callable SAS kernel. |
---|
248 | |
---|
249 | *kernel* is the c function to call. |
---|
250 | |
---|
251 | *info* is the module information |
---|
252 | |
---|
253 | *q_input* is the DllInput q vectors at which the kernel should be |
---|
254 | evaluated. |
---|
255 | |
---|
256 | The resulting call method takes the *pars*, a list of values for |
---|
257 | the fixed parameters to the kernel, and *pd_pars*, a list of (value,weight) |
---|
258 | vectors for the polydisperse parameters. *cutoff* determines the |
---|
259 | integration limits: any points with combined weight less than *cutoff* |
---|
260 | will not be calculated. |
---|
261 | |
---|
262 | Call :meth:`release` when done with the kernel instance. |
---|
263 | """ |
---|
264 | def __init__(self, kernel, info, q_input): |
---|
265 | self.info = info |
---|
266 | self.q_input = q_input |
---|
267 | self.kernel = kernel |
---|
268 | self.res = np.empty(q_input.nq, q_input.dtype) |
---|
269 | dim = '2d' if q_input.is_2D else '1d' |
---|
270 | self.fixed_pars = info['partype']['fixed-'+dim] |
---|
271 | self.pd_pars = info['partype']['pd-'+dim] |
---|
272 | |
---|
273 | # In dll kernel, but not in opencl kernel |
---|
274 | self.p_res = self.res.ctypes.data |
---|
275 | |
---|
276 | def __call__(self, fixed_pars, pd_pars, cutoff): |
---|
277 | real = (np.float32 if self.q_input.dtype == generate.F32 |
---|
278 | else np.float64 if self.q_input.dtype == generate.F64 |
---|
279 | else np.float128) |
---|
280 | |
---|
281 | nq = c_int(self.q_input.nq) |
---|
282 | if pd_pars: |
---|
283 | cutoff = real(cutoff) |
---|
284 | loops_N = [np.uint32(len(p[0])) for p in pd_pars] |
---|
285 | loops = np.hstack(pd_pars) |
---|
286 | loops = np.ascontiguousarray(loops.T, self.q_input.dtype).flatten() |
---|
287 | p_loops = loops.ctypes.data |
---|
288 | dispersed = [p_loops, cutoff] + loops_N |
---|
289 | else: |
---|
290 | dispersed = [] |
---|
291 | fixed = [real(p) for p in fixed_pars] |
---|
292 | args = self.q_input.q_pointers + [self.p_res, nq] + dispersed + fixed |
---|
293 | #print pars |
---|
294 | self.kernel(*args) |
---|
295 | |
---|
296 | return self.res |
---|
297 | |
---|
298 | def release(self): |
---|
299 | pass |
---|