Changeset c71da73 in sasmodels
- Timestamp:
- Mar 9, 2015 4:23:07 PM (10 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:
- bec0e75
- Parents:
- 1b50776 (diff), 750ffa5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
compare.py
rb89f519 r750ffa5 14 14 from sasmodels import kerneldll 15 15 from sasmodels.convert import revert_model 16 kerneldll.ALLOW_SINGLE_PRECISION_DLLS = True 16 17 17 18 # List of available models -
sasmodels/bumps_model.py
r3c56da87 r750ffa5 365 365 if 'theory' not in self._cache: 366 366 if self._fn is None: 367 input_value= self.model.make_input(self._fn_inputs)368 self._fn = self.model( input_value)367 q_input= self.model.make_input(self._fn_inputs) 368 self._fn = self.model(q_input) 369 369 370 370 fixed_pars = [getattr(self, p).value for p in self._fn.fixed_pars] -
sasmodels/kernel_template.c
r95e861b r750ffa5 12 12 #include <cstdio> 13 13 #include <cmath> 14 using namespace std; 14 15 #if defined(_MSC_VER) 15 #define kernel extern "C" __declspec( dllexport ) 16 # define kernel extern "C" __declspec( dllexport ) 17 inline double trunc(double x) { return x>=0?floor(x):-floor(-x); } 16 18 #else 17 # define kernel extern "C"19 # define kernel extern "C" 18 20 #endif 19 using namespace std; 20 inline void SINCOS(double angle, double &svar, double &cvar) 21 { svar=sin(angle); cvar=cos(angle); } 21 inline void SINCOS(double angle, double &svar, double &cvar) { svar=sin(angle); cvar=cos(angle); } 22 22 # else 23 23 #include <stdio.h> 24 #include <math.h> 25 #if defined(_MSC_VER) 26 #define kernel __declspec( dllexport ) 27 #else 24 #include <tgmath.h> // C99 type-generic math, so sin(float) => sinf 25 // MSVC doesn't support C99, so no need for dllexport on C99 branch 28 26 #define kernel 29 #endif 30 #define SINCOS(angle,svar,cvar) do {svar=sin(angle);cvar=cos(angle);} while (0) 27 #define SINCOS(angle,svar,cvar) do {const double _t_=angle; svar=sin(_t_);cvar=cos(_t_);} while (0) 31 28 # endif 32 #if defined(_MSC_VER)33 inline double trunc(double x) { return x>=0?floor(x):-floor(-x); }34 #endif35 29 # define global 36 30 # define local 37 31 # define constant const 32 // OpenCL powr(a,b) = C99 pow(a,b), b >= 0 33 // OpenCL pown(a,b) = C99 pow(a,b), b integer 38 34 # define powr(a,b) pow(a,b) 39 35 # define pown(a,b) pow(a,b) … … 42 38 # define SINCOS(angle,svar,cvar) svar=sincos(angle,&cvar) 43 39 # else 44 # define SINCOS(angle,svar,cvar) do { svar=sin(angle);cvar=cos(angle);} while (0)40 # define SINCOS(angle,svar,cvar) do {const double _t_=angle; svar=sin(_t_);cvar=cos(_t_);} while (0) 45 41 # endif 46 42 #endif … … 126 122 const double weight = IQ_WEIGHT_PRODUCT; 127 123 if (weight > cutoff) { 128 const double I= Iq(qi, IQ_PARAMETERS);129 if ( I>=0.0) { // scattering cannot be negative130 ret += weight* I;124 const double scattering = Iq(qi, IQ_PARAMETERS); 125 if (scattering >= 0.0) { // scattering cannot be negative 126 ret += weight*scattering; 131 127 norm += weight; 132 128 #ifdef VOLUME_PARAMETERS … … 136 132 #endif 137 133 } 138 //else { printf("exclude qx,qy,I:%%g,%%g,%%g\n",qi, I); }134 //else { printf("exclude qx,qy,I:%%g,%%g,%%g\n",qi,scattering); } 139 135 } 140 136 IQ_CLOSE_LOOPS … … 199 195 if (weight > cutoff) { 200 196 201 const double I= Iqxy(qxi, qyi, IQXY_PARAMETERS);202 if ( I>=0.0) { // scattering cannot be negative197 const double scattering = Iqxy(qxi, qyi, IQXY_PARAMETERS); 198 if (scattering >= 0.0) { // scattering cannot be negative 203 199 // TODO: use correct angle for spherical correction 204 200 // Definition of theta and phi are probably reversed relative to the … … 211 207 const double spherical_correction 212 208 = (Ntheta>1 ? fabs(cos(M_PI_180*theta))*M_PI_2:1.0); 213 ret += spherical_correction * weight * I;209 ret += spherical_correction * weight * scattering; 214 210 #else 215 ret += weight * I;211 ret += weight * scattering; 216 212 #endif 217 213 norm += weight; … … 222 218 norm_vol += vol_weight; 223 219 } 224 //else { printf("exclude qx,qy,I:%%g,%%g,%%g\n",qi, I); }220 //else { printf("exclude qx,qy,I:%%g,%%g,%%g\n",qi,scattering); } 225 221 } 226 222 IQXY_CLOSE_LOOPS -
sasmodels/kernelcl.py
r3c56da87 r750ffa5 68 68 #open(info['name']+'.cl','w').write(source) 69 69 #source = open(info['name']+'.cl','r').read() 70 return GpuModel(source, info, dtype)70 return GpuModel(source, info, np.dtype(dtype)) 71 71 72 72 ENV = None … … 92 92 Return the size of an execution batch for *kernel* running on *queue*. 93 93 """ 94 return kernel.get_work_group_info(cl.kernel_work_group_info.PREFERRED_WORK_GROUP_SIZE_MULTIPLE, 95 queue.device) 94 return kernel.get_work_group_info( 95 cl.kernel_work_group_info.PREFERRED_WORK_GROUP_SIZE_MULTIPLE, 96 queue.device) 96 97 97 98 def _stretch_input(vector, dtype, extra=1e-3, boundary=32): … … 219 220 self.info = info 220 221 self.source = source 221 self.dtype = dtype222 self.dtype = np.dtype(dtype) 222 223 self.program = None # delay program creation 223 224 … … 230 231 self.__dict__ = state.copy() 231 232 232 def __call__(self, input_value):233 if self.dtype != input_value.dtype:234 raise TypeError("data and kernel have different types")233 def __call__(self, q_input): 234 if self.dtype != q_input.dtype: 235 raise TypeError("data is %s kernel is %s" % (q_input.dtype, self.dtype)) 235 236 if self.program is None: 236 237 compiler = environment().compile_program 237 238 self.program = compiler(self.info['name'], self.source, self.dtype) 238 kernel_name = generate.kernel_name(self.info, input_value.is_2D)239 kernel_name = generate.kernel_name(self.info, q_input.is_2D) 239 240 kernel = getattr(self.program, kernel_name) 240 return GpuKernel(kernel, self.info, input_value)241 return GpuKernel(kernel, self.info, q_input) 241 242 242 243 def release(self): -
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): -
sasmodels/kernelpy.py
r3c56da87 r750ffa5 43 43 self.is_2D = (len(q_vectors) == 2) 44 44 self.q_vectors = [np.ascontiguousarray(q, self.dtype) for q in q_vectors] 45 self.q_pointers = [q.ctypes.data for q in q_vectors]45 self.q_pointers = [q.ctypes.data for q in self.q_vectors] 46 46 47 47 def release(self): -
sasmodels/models/HayterMSAsq_kernel.c
rab87a12 r48f0194 37 37 Perm=dialec*FrSpPerm; //in C^2/(N m^2) 38 38 charge=zz*Elcharge; //in Coulomb (C) 39 SIdiam = diam*1 E-10; //in m39 SIdiam = diam*1.0E-10; //in m 40 40 Vp=4.0*pi/3.0*(SIdiam/2.0)*(SIdiam/2.0)*(SIdiam/2.0); //in m^3 41 cs=csalt*6.022E23*1 E3; //# salt molecules/m^341 cs=csalt*6.022E23*1.0E3; //# salt molecules/m^3 42 42 43 43 // Compute the derived values of : -
sasmodels/model_test.py
rddfe69c r1b50776 115 115 if not ispy and ('opencl' in loaders and load_model_cl): 116 116 test_name = "Model: %s, Kernel: OpenCL"%model_name 117 test_method = "test_%s_opencl" % model_name 117 118 test = ModelTestCase(test_name, model_definition, 118 load_model_cl, tests )119 load_model_cl, tests, test_method) 119 120 #print "defining", test_name 120 121 suite.addTest(test) … … 123 124 if ispy or ('dll' in loaders and load_model_dll): 124 125 test_name = "Model: %s, Kernel: dll"%model_name 126 test_method = "test_%s_dll" % model_name 125 127 test = ModelTestCase(test_name, model_definition, 126 load_model_dll, tests) 127 #print "defining", test_name 128 load_model_dll, tests, test_method) 128 129 suite.addTest(test) 129 130 … … 132 133 def _hide_model_case_from_nosetests(): 133 134 class ModelTestCase(unittest.TestCase): 134 def __init__(self, test_name, definition, loader, tests): 135 unittest.TestCase.__init__(self) 136 135 def __init__(self, test_name, definition, loader, tests, test_method): 137 136 self.test_name = test_name 138 137 self.definition = definition … … 140 139 self.tests = tests 141 140 142 def runTest(self): 141 setattr(self, test_method, self._runTest) 142 unittest.TestCase.__init__(self, test_method) 143 144 def _runTest(self): 143 145 try: 144 146 model = self.loader(self.definition) … … 191 193 192 194 193 # let nosetests sniff out the tests194 def model_tests():195 tests = make_suite(['opencl','dll'],['all'])196 for test_i in tests:197 yield test_i.runTest198 195 199 196 def main():
Note: See TracChangeset
for help on using the changeset viewer.