Changeset 8a20be5 in sasmodels for cylcode.py
- Timestamp:
- Jul 10, 2014 3:05:08 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:
- 8faffcd
- Parents:
- 5378e40
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified cylcode.py ¶
r5378e40 r8a20be5 6 6 import pyopencl as cl 7 7 from weights import GaussianDispersion 8 from sasmodel import card 8 9 10 def set_precision(src, qx, qy, dtype): 11 qx = np.ascontiguousarray(qx, dtype=dtype) 12 qy = np.ascontiguousarray(qy, dtype=dtype) 13 if np.dtype(dtype) == np.dtype('float32'): 14 header = """\ 15 #define real float 16 """ 17 else: 18 header = """\ 19 #pragma OPENCL EXTENSION cl_khr_fp64: enable 20 #define real double 21 """ 22 return header+src, qx, qy 9 23 10 24 class GpuCylinder(object): … … 15 29 PD_PARS = ['radius', 'length', 'cyl_theta', 'cyl_phi'] 16 30 17 def __init__(self, qx, qy ):31 def __init__(self, qx, qy, dtype='float32'): 18 32 19 self.qx = np.asarray(qx, np.float32)20 self.qy = np.asarray(qy, np.float32)21 33 #create context, queue, and build program 22 self.ctx = cl.create_some_context() 23 self.queue = cl.CommandQueue(self.ctx) 24 self.prg = cl.Program(self.ctx, open('Kernel-Cylinder.cpp').read()).build() 34 ctx,_queue = card() 35 src, qx, qy = set_precision(open('NR_BessJ1.cpp').read()+"\n"+open('Kernel-Cylinder.cpp').read(), qx, qy, dtype=dtype) 36 self.prg = cl.Program(ctx, src).build() 37 self.qx, self.qy = qx, qy 25 38 26 39 #buffers 27 40 mf = cl.mem_flags 28 self.qx_b = cl.Buffer( self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx)29 self.qy_b = cl.Buffer( self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy)30 self.res_b = cl.Buffer( self.ctx, mf.WRITE_ONLY, qx.nbytes)41 self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx) 42 self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy) 43 self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes) 31 44 self.res = np.empty_like(self.qx) 32 45 33 46 def eval(self, pars): 34 47 35 radius,length,cyl_theta,cyl_phi = \ 48 _ctx,queue = card() 49 radius, length, cyl_theta, cyl_phi = \ 36 50 [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) 37 51 for base in GpuCylinder.PD_PARS] … … 48 62 sub = pars['sldCyl'] - pars['sldSolv'] 49 63 64 real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 50 65 #Loop over radius, length, theta, phi weight points 51 66 for i in xrange(len(radius.weight)): … … 54 69 for l in xrange(len(cyl_phi.weight)): 55 70 56 self.prg.CylinderKernel(self.queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, np.float32(sub), 57 np.float32(radius.value[i]), np.float32(length.value[j]), np.float32(pars['scale']), 58 np.float32(radius.weight[i]), np.float32(length.weight[j]), np.float32(cyl_theta.weight[k]), 59 np.float32(cyl_phi.weight[l]), np.float32(cyl_theta.value[k]), np.float32(cyl_phi.value[l]), 71 72 self.prg.CylinderKernel(queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, real(sub), 73 real(radius.value[i]), real(length.value[j]), real(pars['scale']), 74 real(radius.weight[i]), real(length.weight[j]), real(cyl_theta.weight[k]), 75 real(cyl_phi.weight[l]), real(cyl_theta.value[k]), real(cyl_phi.value[l]), 60 76 np.uint32(self.qx.size), np.uint32(size)) 61 cl.enqueue_copy( self.queue, self.res, self.res_b)77 cl.enqueue_copy(queue, self.res, self.res_b) 62 78 sum += self.res 63 79 vol += radius.weight[i]*length.weight[j]*pow(radius.value[i], 2)*length.value[j]
Note: See TracChangeset
for help on using the changeset viewer.