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