source: sasmodels/Models/code_ellipse.py @ 8cdb9f1

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 8cdb9f1 was a42fec0, checked in by HMP1 <helen.park@…>, 10 years ago

Speed-up of 3X, compare.py working

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[dbb0048]1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import numpy as np
5import pyopencl as cl
[473183c]6
[dbb0048]7from weights import GaussianDispersion
[a42fec0]8from sasmodel import card, set_precision
[dbb0048]9
10class GpuEllipse(object):
11    PARS = {
12    'scale':1, 'radius_a':1, 'radius_b':1, 'sldEll':1e-6, 'sldSolv':0, 'background':0, 'axis_theta':0, 'axis_phi':0,
13    }
14    PD_PARS = ['radius_a', 'radius_b', 'axis_theta', 'axis_phi']
15
16    def __init__(self, qx, qy, dtype='float32'):
17
18        ctx,_queue = card()
[ca6c007]19        src, qx, qy = set_precision(open('Kernel/Kernel-Ellipse.cpp').read(), qx, qy, dtype=dtype)
[dbb0048]20        self.prg = cl.Program(ctx, src).build()
21        self.qx, self.qy = qx, qy
22
23        #buffers
24        mf = cl.mem_flags
25        self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx)
26        self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy)
27        self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes)
28        self.res = np.empty_like(self.qx)
29
30    def eval(self, pars):
31    #b_n = radius_b # want, a_n = radius_a # want, etc
32        _ctx,queue = card()
[a42fec0]33        self.res[:] = 0
34        cl.enqueue_copy(queue, self.res_b, self.res)
[dbb0048]35        radius_a, radius_b, axis_theta, axis_phi = \
36            [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma'])
37             for base in GpuEllipse.PD_PARS]
38
39        radius_a.value, radius_a.weight = radius_a.get_weights(pars['radius_a'], 0, 10000, True)
40        radius_b.value, radius_b.weight = radius_b.get_weights(pars['radius_b'], 0, 10000, True)
41        axis_theta.value, axis_theta.weight = axis_theta.get_weights(pars['axis_theta'], -90, 180, False)
42        axis_phi.value, axis_phi.weight = axis_phi.get_weights(pars['axis_phi'], -90, 180, False)
43
44        #Perform the computation, with all weight points
45        sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0
46        size = len(axis_theta.weight)
47        sub = pars['sldEll'] - pars['sldSolv']
48        real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64
49
50        #Loop over radius weight points
51        for i in xrange(len(radius_a.weight)):
52            #Loop over length weight points
53            for j in xrange(len(radius_b.weight)):
54                #Average over theta distribution
55                for k in xrange(len(axis_theta.weight)):
56                    #Average over phi distribution
57                    for l in xrange(len(axis_phi.weight)):
58                        #call the kernel
59                        self.prg.EllipsoidKernel(queue, self.qx.shape, None, real(radius_a.weight[i]),
60                                        real(radius_b.weight[j]), real(axis_theta.weight[k]),
61                                        real(axis_phi.weight[l]), real(pars['scale']), real(radius_a.value[i]),
62                                        real(radius_b.value[j]), real(sub), real(axis_theta.value[k]),
63                                        real(axis_phi.value[l]), self.qx_b, self.qy_b, self.res_b,
64                                        np.uint32(self.qx.size), np.uint32(len(axis_theta.weight)))
[a42fec0]65
[dbb0048]66                        vol += radius_a.weight[i]*radius_b.weight[j]*pow(radius_b.value[j], 2)*radius_a.value[i]
67                        norm_vol += radius_a.weight[i]*radius_b.weight[j]
68                        norm += radius_a.weight[i]*radius_b.weight[j]*axis_theta.weight[k]*axis_phi.weight[l]
69        # Averaging in theta needs an extra normalization
70        # factor to account for the sin(theta) term in the
71        # integration (see documentation).
72
73    #    if size > 1:
74     #       norm /= math.asin(1.0)
[a42fec0]75        cl.enqueue_copy(queue, self.res, self.res_b)
76        sum += self.res
[dbb0048]77        if vol != 0.0 and norm_vol != 0.0:
78            sum *= norm_vol/vol
79
80        return sum/norm+pars['background']
81
82
83def demo():
84    from time import time
85    import matplotlib.pyplot as plt
86
87    #create qx and qy evenly spaces
88    qx = np.linspace(-.02, .02, 128)
89    qy = np.linspace(-.02, .02, 128)
90    qx, qy = np.meshgrid(qx, qy)
91
92    #saved shape of qx
93    r_shape = qx.shape
94    #reshape for calculation; resize as float32
95    qx = qx.flatten()
96    qy = qy.flatten()
97
98    #int main
99    pars = EllipsoidParameters(.027, 60, 180, .297e-6, 5.773e-06, 4.9, 0, 90)
100
101    t = time()
102    result = GpuEllipse(qx, qy)
103    result.x = result.ellipsoid_fit(qx, qy, pars, b_n=35, t_n=35, a_n=1, p_n=1, sigma=3, b_w=.1, t_w=.1, a_w=.1, p_w=.1)
104    result.x = np.reshape(result.x, r_shape)
105    tt = time()
106    print("Time taken: %f" % (tt - t))
107
108    plt.pcolormesh(result.x)
109    plt.show()
110
111
112if __name__ == "__main__":
113    demo()
114
115
116
117
Note: See TracBrowser for help on using the repository browser.