Changes in / [49f92c1:e930946] in sasmodels
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
extra/pylint.rc
r49f92c1 rc85db69 57 57 58 58 # Disable the message(s) with the given id(s). 59 disable=W0702,W0613 59 disable=W0702,W0613,W0703 60 60 61 61 [REPORTS] … … 343 343 344 344 # Minimum number of public methods for a class (see R0903). 345 min-public-methods= 2345 min-public-methods=0 346 346 347 347 # Maximum number of public methods for a class (see R0904). -
sasmodels/kernelcl.py
r664c8e7 rc85db69 32 32 context = cl.create_some_context(interactive=False) 33 33 del context 34 except Exception, exc:34 except Exception, exc: 35 35 warnings.warn(str(exc)) 36 36 raise RuntimeError("OpenCL not available") … … 39 39 40 40 from . import generate 41 from .kernelpy import Py Input, PyModel41 from .kernelpy import PyModel 42 42 43 43 F64_DEFS = """\ … … 63 63 """ 64 64 source, info = generate.make(kernel_module) 65 if callable(info.get('Iq', None)):65 if callable(info.get('Iq', None)): 66 66 return PyModel(info) 67 67 ## for debugging, save source to a .cl file, edit it, and reload as model … … 110 110 device.min_data_type_align_size//4. 111 111 """ 112 remainder = vector.size %boundary112 remainder = vector.size % boundary 113 113 if remainder != 0: 114 114 size = vector.size + (boundary - remainder) 115 vector = np.hstack((vector, [extra] *(size-vector.size)))115 vector = np.hstack((vector, [extra] * (size - vector.size))) 116 116 return np.ascontiguousarray(vector, dtype=dtype) 117 117 … … 126 126 """ 127 127 dtype = np.dtype(dtype) 128 if dtype ==generate.F64 and not all(has_double(d) for d in context.devices):128 if dtype == generate.F64 and not all(has_double(d) for d in context.devices): 129 129 raise RuntimeError("Double precision not supported for devices") 130 130 … … 135 135 if context.devices[0].type == cl.device_type.GPU: 136 136 header += "#define USE_SINCOS\n" 137 program = cl.Program(context, header+source).build()137 program = cl.Program(context, header + source).build() 138 138 return program 139 139 … … 173 173 try: 174 174 self.context = cl.create_some_context(interactive=False) 175 except Exception, exc:175 except Exception, exc: 176 176 warnings.warn(str(exc)) 177 177 warnings.warn("pyopencl.create_some_context() failed") … … 230 230 self.__dict__ = state.copy() 231 231 232 def __call__(self, input ):233 if self.dtype != input .dtype:232 def __call__(self, input_value): 233 if self.dtype != input_value.dtype: 234 234 raise TypeError("data and kernel have different types") 235 235 if self.program is None: 236 self.program = environment().compile_program(self.info['name'], self.source, self.dtype)237 kernel_name = generate.kernel_name(self.info, input .is_2D)236 self.program = environment().compile_program(self.info['name'], self.source, self.dtype) 237 kernel_name = generate.kernel_name(self.info, input_value.is_2D) 238 238 kernel = getattr(self.program, kernel_name) 239 return GpuKernel(kernel, self.info, input )239 return GpuKernel(kernel, self.info, input_value) 240 240 241 241 def release(self): … … 285 285 self.q_vectors = [_stretch_input(q, self.dtype, 32) for q in q_vectors] 286 286 self.q_buffers = [ 287 cl.Buffer(env.context, 287 cl.Buffer(env.context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=q) 288 288 for q in self.q_vectors 289 289 ] … … 320 320 self.res = np.empty(input.nq, input.dtype) 321 321 dim = '2d' if input.is_2D else '1d' 322 self.fixed_pars = info['partype']['fixed-' +dim]323 self.pd_pars = info['partype']['pd-' +dim]322 self.fixed_pars = info['partype']['fixed-' + dim] 323 self.pd_pars = info['partype']['pd-' + dim] 324 324 325 325 # Inputs and outputs for each kernel call … … 327 327 env = environment() 328 328 self.loops_b = [cl.Buffer(env.context, mf.READ_WRITE, 329 2 *MAX_LOOPS*input.dtype.itemsize)329 2 * MAX_LOOPS * input.dtype.itemsize) 330 330 for _ in env.queues] 331 331 self.res_b = [cl.Buffer(env.context, mf.READ_WRITE, 332 input.global_size[0] *input.dtype.itemsize)332 input.global_size[0] * input.dtype.itemsize) 333 333 for _ in env.queues] 334 334 … … 344 344 cutoff = real(cutoff) 345 345 loops_N = [np.uint32(len(p[0])) for p in pd_pars] 346 loops = np.hstack(pd_pars) if pd_pars else np.empty(0, dtype=self.input.dtype)346 loops = np.hstack(pd_pars) if pd_pars else np.empty(0, dtype=self.input.dtype) 347 347 loops = np.ascontiguousarray(loops.T, self.input.dtype).flatten() 348 348 #print "loops",Nloops, loops … … 350 350 #import sys; print >>sys.stderr,"opencl eval",pars 351 351 #print "opencl eval",pars 352 if len(loops) > 2 *MAX_LOOPS:352 if len(loops) > 2 * MAX_LOOPS: 353 353 raise ValueError("too many polydispersity points") 354 354 -
sasmodels/kernelpy.py
rf734e7d rc85db69 1 1 import numpy as np 2 from numpy import pi, sin, cos, sqrt3 4 from .generate import F 32, F642 from numpy import pi, cos 3 4 from .generate import F64 5 5 6 6 class PyModel(object): 7 7 def __init__(self, info): 8 8 self.info = info 9 def __call__(self, input ):10 kernel = self.info['Iqxy'] if input .is_2D else self.info['Iq']11 return PyKernel(kernel, self.info, input )9 def __call__(self, input_value): 10 kernel = self.info['Iqxy'] if input_value.is_2D else self.info['Iq'] 11 return PyKernel(kernel, self.info, input_value) 12 12 def make_input(self, q_vectors): 13 13 return PyInput(q_vectors, dtype=F64) … … 38 38 self.dtype = dtype 39 39 self.is_2D = (len(q_vectors) == 2) 40 self.q_vectors = [np.ascontiguousarray(q, self.dtype) for q in q_vectors]40 self.q_vectors = [np.ascontiguousarray(q, self.dtype) for q in q_vectors] 41 41 self.q_pointers = [q.ctypes.data for q in q_vectors] 42 42 … … 73 73 if dim == '2d': 74 74 def vector_kernel(qx, qy, *args): 75 return np.array([kernel(qxi, qyi,*args) for qxi,qyi in zip(qx,qy)])75 return np.array([kernel(qxi, qyi, *args) for qxi, qyi in zip(qx, qy)]) 76 76 else: 77 77 def vector_kernel(q, *args): 78 return np.array([kernel(qi, *args) for qi in q])78 return np.array([kernel(qi, *args) for qi in q]) 79 79 self.kernel = vector_kernel 80 80 else: 81 81 self.kernel = kernel 82 fixed_pars = info['partype']['fixed-' +dim]83 pd_pars = info['partype']['pd-' +dim]82 fixed_pars = info['partype']['fixed-' + dim] 83 pd_pars = info['partype']['pd-' + dim] 84 84 vol_pars = info['partype']['volume'] 85 85 … … 87 87 pars = [p[0] for p in info['parameters'][2:]] 88 88 offset = len(self.input.q_vectors) 89 self.args = self.input.q_vectors + [None] *len(pars)90 self.fixed_index = np.array([pars.index(p) +offset for p in fixed_pars[2:]])91 self.pd_index = np.array([pars.index(p) +offset for p in pd_pars])92 self.vol_index = np.array([pars.index(p) +offset for p in vol_pars])93 try: self.theta_index = pars.index('theta') +offset89 self.args = self.input.q_vectors + [None] * len(pars) 90 self.fixed_index = np.array([pars.index(p) + offset for p in fixed_pars[2:]]) 91 self.pd_index = np.array([pars.index(p) + offset for p in pd_pars]) 92 self.vol_index = np.array([pars.index(p) + offset for p in vol_pars]) 93 try: self.theta_index = pars.index('theta') + offset 94 94 except ValueError: self.theta_index = -1 95 95 … … 105 105 # First two fixed 106 106 scale, background = fixed[:2] 107 for index, value in zip(self.fixed_index, fixed[2:]):107 for index, value in zip(self.fixed_index, fixed[2:]): 108 108 args[index] = float(value) 109 res = _loops(form, form_volume, cutoff, scale, background, 109 res = _loops(form, form_volume, cutoff, scale, background, args, 110 110 pd, self.pd_index, self.vol_index, self.theta_index) 111 111 … … 185 185 for k in range(stride[-1]): 186 186 # update polydispersity parameter values 187 fast_index = k %stride[0]187 fast_index = k % stride[0] 188 188 if fast_index == 0: # bottom loop complete ... check all other loops 189 189 if weight.size > 0: 190 for i, index, in enumerate(k%stride):190 for i, index, in enumerate(k % stride): 191 191 args[pd_index[i]] = pd[i][0][index] 192 192 weight[i] = pd[i][1][index] … … 202 202 if w > cutoff: 203 203 I = form(*args) 204 positive = (I >=0.0)204 positive = (I >= 0.0) 205 205 206 206 # Note: can precompute spherical correction if theta_index is not the fast index 207 207 # Correction factor for spherical integration p(theta) I(q) sin(theta) dtheta 208 208 #spherical_correction = abs(sin(pi*args[theta_index])) if theta_index>=0 else 1.0 209 spherical_correction = abs(cos(pi *args[theta_index]))*pi/2 if theta_index>=0 else 1.0209 spherical_correction = abs(cos(pi * args[theta_index])) * pi / 2 if theta_index >= 0 else 1.0 210 210 #spherical_correction = 1.0 211 ret += w *I*spherical_correction*positive212 norm += w *positive211 ret += w * I * spherical_correction * positive 212 norm += w * positive 213 213 214 214 # Volume normalization. … … 220 220 vol_args = [args[index] for index in vol_index] 221 221 vol_weight = np.prod(weight[vol_weight_index]) 222 vol += vol_weight *form_volume(*vol_args)*positive223 vol_norm += vol_weight *positive224 225 positive = (vol *vol_norm != 0.0)222 vol += vol_weight * form_volume(*vol_args) * positive 223 vol_norm += vol_weight * positive 224 225 positive = (vol * vol_norm != 0.0) 226 226 ret[positive] *= vol_norm[positive] / vol[positive] 227 result = scale *ret/norm+background227 result = scale * ret / norm + background 228 228 return result
Note: See TracChangeset
for help on using the changeset viewer.