Changeset 750ffa5 in sasmodels for sasmodels/kerneldll.py
- Timestamp:
- Mar 9, 2015 4:04:55 PM (9 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 3a45c2c
- Parents:
- 48f0194
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/kerneldll.py
r3c56da87 r750ffa5 1 1 """ 2 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*. 3 7 """ 8 4 9 import sys 5 10 import os 6 11 import tempfile 7 12 import ctypes as ct 8 from ctypes import c_void_p, c_int, c_double 13 from ctypes import c_void_p, c_int, c_double, c_float 9 14 10 15 import numpy as np … … 36 41 DLL_PATH = tempfile.gettempdir() 37 42 38 39 def dll_path(info): 43 ALLOW_SINGLE_PRECISION_DLLS = False 44 45 46 def dll_path(info, dtype="double"): 40 47 """ 41 48 Path to the compiled model defined by *info*. … … 43 50 from os.path import join as joinpath, split as splitpath, splitext 44 51 basename = splitext(splitpath(info['filename'])[1])[0] 52 if np.dtype(dtype) == generate.F32: 53 basename += "32" 45 54 return joinpath(DLL_PATH, basename+'.so') 46 55 47 56 48 def load_model(kernel_module, dtype= None):57 def load_model(kernel_module, dtype="double"): 49 58 """ 50 59 Load the compiled model defined by *kernel_module*. … … 57 66 be defined without using too many resources. 58 67 """ 68 if not ALLOW_SINGLE_PRECISION_DLLS: dtype = "double" # Force 64-bit dll 69 dtype = np.dtype(dtype) 70 59 71 source, info = generate.make(kernel_module) 60 72 if callable(info.get('Iq',None)): 61 73 return PyModel(info) 74 75 if dtype == generate.F32: # 32-bit dll 76 source = generate.use_single(source) 77 tempfile_prefix = 'sas_'+info['name']+'32_' 78 else: 79 tempfile_prefix = 'sas_'+info['name']+'_' 80 62 81 source_files = generate.sources(info) + [info['filename']] 82 dllpath = dll_path(info, dtype) 63 83 newest = max(os.path.getmtime(f) for f in source_files) 64 dllpath = dll_path(info)65 84 if not os.path.exists(dllpath) or os.path.getmtime(dllpath)<newest: 66 85 # Replace with a proper temp file 67 fid, filename = tempfile.mkstemp(suffix=".c",prefix= "sas_"+info['name'])86 fid, filename = tempfile.mkstemp(suffix=".c",prefix=tempfile_prefix) 68 87 os.fdopen(fid,"w").write(source) 69 88 command = COMPILE%{"source":filename, "output":dllpath} … … 76 95 #os.unlink(filename); print "saving compiled file in %r"%filename 77 96 pass 78 return DllModel(dllpath, info )97 return DllModel(dllpath, info, dtype=dtype) 79 98 80 99 … … 96 115 Call :meth:`release` when done with the kernel. 97 116 """ 98 def __init__(self, dllpath, info ):117 def __init__(self, dllpath, info, dtype=generate.F32): 99 118 self.info = info 100 119 self.dllpath = dllpath 101 120 self.dll = None 121 self.dtype = np.dtype(dtype) 102 122 103 123 def _load_dll(self): … … 110 130 self.dll = ct.CDLL(self.dllpath) 111 131 112 pd_args_1d = [c_void_p, c_double] + [c_int]*Npd1d if Npd1d else [] 113 pd_args_2d= [c_void_p, c_double] + [c_int]*Npd2d if Npd2d else [] 132 fp = c_float if self.dtype == generate.F32 else c_double 133 pd_args_1d = [c_void_p, fp] + [c_int]*Npd1d if Npd1d else [] 134 pd_args_2d= [c_void_p, fp] + [c_int]*Npd2d if Npd2d else [] 114 135 self.Iq = self.dll[generate.kernel_name(self.info, False)] 115 self.Iq.argtypes = IQ_ARGS + pd_args_1d + [ c_double]*Nfixed1d136 self.Iq.argtypes = IQ_ARGS + pd_args_1d + [fp]*Nfixed1d 116 137 117 138 self.Iqxy = self.dll[generate.kernel_name(self.info, True)] 118 self.Iqxy.argtypes = IQXY_ARGS + pd_args_2d + [ c_double]*Nfixed2d139 self.Iqxy.argtypes = IQXY_ARGS + pd_args_2d + [fp]*Nfixed2d 119 140 120 141 def __getstate__(self): … … 125 146 126 147 def __call__(self, q_input): 148 if self.dtype != q_input.dtype: 149 raise TypeError("data is %s kernel is %s" % (q_input.dtype, self.dtype)) 127 150 if self.dll is None: self._load_dll() 128 151 kernel = self.Iqxy if q_input.is_2D else self.Iq … … 138 161 ctypes and some may be pure python. 139 162 """ 140 return PyInput(q_vectors, dtype= F64)163 return PyInput(q_vectors, dtype=self.dtype) 141 164 142 165 def release(self):
Note: See TracChangeset
for help on using the changeset viewer.