Changeset 8a20be5 in sasmodels for cylcode.py


Ignore:
Timestamp:
Jul 10, 2014 3:05:08 PM (10 years ago)
Author:
HMP1 <helen.park@…>
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
Message:

Added a fit2 (fits two different models at different angles)
(preliminary) Added CoreshellCyl? and CapCyl? Kernels
(preliminary) Updated kernels to include functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified cylcode.py

    r5378e40 r8a20be5  
    66import pyopencl as cl 
    77from weights import GaussianDispersion 
     8from sasmodel import card 
    89 
     10def 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 
    923 
    1024class GpuCylinder(object): 
     
    1529    PD_PARS = ['radius', 'length', 'cyl_theta', 'cyl_phi'] 
    1630 
    17     def __init__(self, qx, qy): 
     31    def __init__(self, qx, qy, dtype='float32'): 
    1832 
    19         self.qx = np.asarray(qx, np.float32) 
    20         self.qy = np.asarray(qy, np.float32) 
    2133        #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 
    2538 
    2639        #buffers 
    2740        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) 
    3144        self.res = np.empty_like(self.qx) 
    3245 
    3346    def eval(self, pars): 
    3447 
    35         radius,length,cyl_theta,cyl_phi = \ 
     48        _ctx,queue = card() 
     49        radius, length, cyl_theta, cyl_phi = \ 
    3650            [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) 
    3751             for base in GpuCylinder.PD_PARS] 
     
    4862        sub = pars['sldCyl'] - pars['sldSolv'] 
    4963 
     64        real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 
    5065        #Loop over radius, length, theta, phi weight points 
    5166        for i in xrange(len(radius.weight)): 
     
    5469                    for l in xrange(len(cyl_phi.weight)): 
    5570 
    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]), 
    6076                                           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) 
    6278                        sum += self.res 
    6379                        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.