1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | import numpy as np |
---|
5 | from math import asin |
---|
6 | import pyopencl as cl |
---|
7 | from weights import GaussianDispersion |
---|
8 | from sasmodel import card |
---|
9 | from Capcyl_Gauss import Gauss76Wt, Gauss76Z |
---|
10 | |
---|
11 | def set_precision(src, qx, qy, dtype): |
---|
12 | qx = np.ascontiguousarray(qx, dtype=dtype) |
---|
13 | qy = np.ascontiguousarray(qy, dtype=dtype) |
---|
14 | if np.dtype(dtype) == np.dtype('float32'): |
---|
15 | header = """\ |
---|
16 | #define real float |
---|
17 | """ |
---|
18 | else: |
---|
19 | header = """\ |
---|
20 | #pragma OPENCL EXTENSION cl_khr_fp64: enable |
---|
21 | #define real double |
---|
22 | """ |
---|
23 | return header+src, qx, qy |
---|
24 | |
---|
25 | class GpuCapCylinder(object): |
---|
26 | PARS = {'scale':1, 'rad_cyl':1, 'rad_cap':1, 'length':1, 'sld_capcyl':1e-6, 'sld_solv':0, 'background':0, |
---|
27 | 'theta':0, 'phi':0} |
---|
28 | |
---|
29 | PD_PARS = ['rad_cyl', 'length', 'rad_cap', 'theta', 'phi'] |
---|
30 | |
---|
31 | def __init__(self, qx, qy, dtype='float32'): |
---|
32 | |
---|
33 | #create context, queue, and build program |
---|
34 | ctx,_queue = card() |
---|
35 | trala = open('NR_BessJ1.cpp').read()+"\n"+open('Capcyl_Kfun.cpp').read()+"\n"+open('Kernel-Cylinder.cpp').read() |
---|
36 | src, qx, qy = set_precision(trala, qx, qy, dtype=dtype) |
---|
37 | self.prg = cl.Program(ctx, src).build() |
---|
38 | self.qx, self.qy = qx, qy |
---|
39 | |
---|
40 | |
---|
41 | #buffers |
---|
42 | mf = cl.mem_flags |
---|
43 | self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx) |
---|
44 | self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy) |
---|
45 | G, Z = Gauss76Wt, Gauss76Z |
---|
46 | self.Gauss76W_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G) |
---|
47 | self.Gauss76Z_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=Z) |
---|
48 | self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes) |
---|
49 | self.res = np.empty_like(self.qx) |
---|
50 | self.vol_i = float(0.0) |
---|
51 | self.vol_b = cl.Buffer(ctx, mf.WRITE_ONLY, self.vol_i.nbytes) |
---|
52 | |
---|
53 | def eval(self, pars): |
---|
54 | |
---|
55 | _ctx,queue = card() |
---|
56 | rad_cyl,length,rad_cap,theta,phi = \ |
---|
57 | [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) |
---|
58 | for base in GpuCapCylinder.PD_PARS] |
---|
59 | |
---|
60 | rad_cyl.value, rad_cyl.weight = rad_cyl.get_weights(pars['rad_cyl'], 0, 1000, True) |
---|
61 | rad_cap.value, rad_cap.weight = rad_cap.get_weights(pars['rad_cap'], 0, 1000, True) |
---|
62 | length.value, length.weight = length.get_weights(pars['length'], 0, 1000, True) |
---|
63 | theta.value, theta.weight = theta.get_weights(pars['theta'], -90, 180, False) |
---|
64 | phi.value, phi.weight = phi.get_weights(pars['phi'], -90, 180, False) |
---|
65 | |
---|
66 | sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0 |
---|
67 | size = len(theta.weight) |
---|
68 | sub = pars['sld_capcyl']-pars['sld_solv'] |
---|
69 | real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 |
---|
70 | |
---|
71 | for i in xrange(len(rad_cyl.weight)): |
---|
72 | for m in xrange(len(rad_cap.weight)): |
---|
73 | for j in xrange(len(length.weight)): |
---|
74 | for k in xrange(len(theta.weight)): |
---|
75 | for l in xrange(len(phi.weight)): |
---|
76 | |
---|
77 | self.prg.CapCylinderKernel(queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, |
---|
78 | self.vol_b, real(rad_cyl.value[i]), real(rad_cap.value[m]), real(length.value[j]), |
---|
79 | real(theta.value[k]), real(phi.value[l]), real(sub), real(pars['scale']), |
---|
80 | real(phi.weight[l]), real(theta.weight[k]), real(rad_cap.weight[m]), |
---|
81 | real(rad_cyl.weight[i]), real(length.weight[j]), np.uint32(self.qx.size), np.uint32(size), |
---|
82 | self.Gauss76W_b, self.Gauss76Z_b) |
---|
83 | |
---|
84 | cl.enqueue_copy(queue, self.res, self.res_b) |
---|
85 | cl.enqueue_copy(queue, self.vol_i, self.vol_b) |
---|
86 | |
---|
87 | sum += self.res |
---|
88 | vol += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]*self.vol_i |
---|
89 | norm_vol += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m] |
---|
90 | norm += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]*theta.weight[k]*phi.weight[l] |
---|
91 | |
---|
92 | if size > 1: |
---|
93 | norm /= asin(1.0) |
---|
94 | |
---|
95 | if vol != 0.0 and norm_vol != 0.0: |
---|
96 | sum *= norm_vol/vol |
---|
97 | |
---|
98 | return sum/norm + pars['background'] |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | |
---|