1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | from sasmodel import SasModel, load_data, set_beam_stop, plot_data, tic, toc |
---|
5 | import numpy as np |
---|
6 | |
---|
7 | |
---|
8 | def sasview_model(modelname, **pars): |
---|
9 | modelname = modelname+"Model" |
---|
10 | sans = __import__('sans.models.'+modelname) |
---|
11 | ModelClass = getattr(getattr(sans.models,modelname,None),modelname,None) |
---|
12 | if ModelClass is None: |
---|
13 | raise ValueError("could not find model %r in sans.models"%modelname) |
---|
14 | model = ModelClass() |
---|
15 | |
---|
16 | for k,v in pars.items(): |
---|
17 | if k.endswith("_pd"): |
---|
18 | model.dispersion[k[:-3]]['width'] = v |
---|
19 | elif k.endswith("_pd_n"): |
---|
20 | model.dispersion[k[:-5]]['npts'] = v |
---|
21 | elif k.endswith("_pd_nsigma"): |
---|
22 | model.dispersion[k[:-10]]['nsigmas'] = v |
---|
23 | else: |
---|
24 | model.setParam(k, v) |
---|
25 | return model |
---|
26 | |
---|
27 | |
---|
28 | def sasview_eval(model, data): |
---|
29 | theory = model.evalDistribution([data.qx_data, data.qy_data]) |
---|
30 | return theory |
---|
31 | |
---|
32 | def plot(data, cpumodel, gpumodel, N, pars): |
---|
33 | |
---|
34 | model = SasModel(data, gpumodel, dtype='f', **pars) |
---|
35 | tic() |
---|
36 | for i in range(N): |
---|
37 | #pars['scale'] = np.random.rand() |
---|
38 | model.update() |
---|
39 | gpu = model.theory() |
---|
40 | gpu_time = toc()*1000./N |
---|
41 | print "ocl t=%.1f ms"%gpu_time |
---|
42 | tic() |
---|
43 | for i in range(N): |
---|
44 | cpu = sasview_eval(cpumodel, data) |
---|
45 | cpu_time = toc()*1000./N |
---|
46 | print "omp t=%.1f ms"%cpu_time |
---|
47 | |
---|
48 | import matplotlib.pyplot as plt |
---|
49 | |
---|
50 | print "gpu/cpu", max(gpu/cpu) |
---|
51 | cpu *= max(gpu/cpu) |
---|
52 | relerr = (gpu - cpu)/cpu |
---|
53 | print "max(|(ocl-omp)/ocl|)", max(abs(relerr)) |
---|
54 | |
---|
55 | |
---|
56 | plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time) |
---|
57 | plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time) |
---|
58 | plt.subplot(133); plot_data(data, relerr); plt.title("relerr x 10^8"); plt.colorbar() |
---|
59 | plt.show() |
---|
60 | |
---|
61 | def newlen(N): |
---|
62 | import sys |
---|
63 | |
---|
64 | if len(sys.argv) > 1: |
---|
65 | N = int(sys.argv[1]) |
---|
66 | data = load_data('December/DEC07098.DAT') |
---|
67 | set_beam_stop(data, 0.004) |
---|
68 | |
---|
69 | return data, N |
---|
70 | |
---|
71 | def cyl(N=1): |
---|
72 | |
---|
73 | dtype = "float" |
---|
74 | pars = dict( |
---|
75 | scale=.003, radius=64.1, length=66.96, sldCyl=.291e-6, sldSolv=5.77e-6, background=.1, |
---|
76 | cyl_theta=90, cyl_phi=0, |
---|
77 | radius_pd=0.1, radius_pd_n=10, radius_pd_nsigma=3, |
---|
78 | length_pd=0.1,length_pd_n=5, length_pd_nsigma=3, |
---|
79 | cyl_theta_pd=0.1, cyl_theta_pd_n=5, cyl_theta_pd_nsigma=3, |
---|
80 | cyl_phi_pd=0.1, cyl_phi_pd_n=10, cyl_phi_pd_nsigma=3) |
---|
81 | |
---|
82 | from Models.code_cylinder_f import GpuCylinder as gpumodel |
---|
83 | model = sasview_model('Cylinder', **pars) |
---|
84 | data, N = newlen(N) |
---|
85 | |
---|
86 | plot(data, model, gpumodel, N, pars) |
---|
87 | |
---|
88 | |
---|
89 | def ellipse(N=1): |
---|
90 | |
---|
91 | pars = dict(scale=.027, radius_a=60, radius_b=180, sldEll=.297e-6, sldSolv=5.773e-6, background=4.9, |
---|
92 | axis_theta=0, axis_phi=90, |
---|
93 | radius_a_pd=0.1, radius_a_pd_n=10, radius_a_pd_nsigma=3, |
---|
94 | radius_b_pd=0.1, radius_b_pd_n=10, radius_b_pd_nsigma=3, |
---|
95 | axis_theta_pd=0.1, axis_theta_pd_n=6, axis_theta_pd_nsigma=3, |
---|
96 | axis_phi_pd=0.1, axis_phi_pd_n=6, axis_phi_pd_nsigma=3,) |
---|
97 | |
---|
98 | from Models.code_ellipse import GpuEllipse as gpumodel |
---|
99 | model = sasview_model('Ellipsoid', **pars) |
---|
100 | |
---|
101 | data, N = newlen(N) |
---|
102 | plot(data, model, gpumodel, N, pars) |
---|
103 | |
---|
104 | def coreshell(N=1): |
---|
105 | |
---|
106 | data, N = newlen(N) |
---|
107 | |
---|
108 | pars = dict(scale= 1.77881e-06, radius=325, thickness=25, length=34.2709, |
---|
109 | core_sld=1e-6, shell_sld=.291e-6, solvent_sld=7.105e-6, |
---|
110 | background=223.827, axis_theta=90, axis_phi=0, |
---|
111 | axis_theta_pd=15.8, |
---|
112 | radius_pd=0.1, radius_pd_n=10, radius_pd_nsigma=3, |
---|
113 | length_pd=0.1, length_pd_n=10, length_pd_nsigma=3, |
---|
114 | thickness_pd=0.1, thickness_pd_n=5, thickness_pd_nsigma=3, |
---|
115 | axis_theta_pd_n=20, axis_theta_pd_nsigma=5, |
---|
116 | axis_phi_pd=0.0008748, axis_phi_pd_n=5, axis_phi_pd_nsigma=3,) |
---|
117 | |
---|
118 | model = sasview_model('CoreShellCylinder', **pars) |
---|
119 | from Models.code_coreshellcyl import GpuCoreShellCylinder as gpumodel |
---|
120 | |
---|
121 | plot(data, model, gpumodel, N, pars) |
---|
122 | |
---|
123 | def trellipse(N=1): |
---|
124 | |
---|
125 | data, N = newlen(N) |
---|
126 | |
---|
127 | pars = dict(scale=0.08, semi_axisA=15, semi_axisB=20, semi_axisC=500, |
---|
128 | sldEll=7.105e-6, sldSolv=.291e-6, |
---|
129 | background=5, axis_theta=0, axis_phi=0, axis_psi=0, |
---|
130 | axis_theta_pd=20, axis_theta_pd_n=10, axis_theta_pd_nsigma=3, |
---|
131 | axis_phi_pd=.1, axis_phi_pd_n=10, axis_phi_pd_nsigma=3, |
---|
132 | axis_psi_pd=30, axis_psi_pd_n=5, axis_psi_pd_nsigma=3, |
---|
133 | semi_axisA_pd=.1, semi_axisA_pd_n=5, semi_axisA_pd_nsigma=3, |
---|
134 | semi_axisB_pd=.1, semi_axisB_pd_n=5, semi_axisB_pd_nsigma=3, |
---|
135 | semi_axisC_pd=.1, semi_axisC_pd_n=5, semi_axisC_pd_nsigma=3,) |
---|
136 | |
---|
137 | model = sasview_model('TriaxialEllipsoid', **pars) |
---|
138 | from Models.code_triaxialellipse import GpuTriEllipse as gpumodel |
---|
139 | |
---|
140 | plot(data, model, gpumodel, N, pars) |
---|
141 | |
---|
142 | def lamellar(N=1): |
---|
143 | |
---|
144 | data, N = newlen(N) |
---|
145 | |
---|
146 | pars = dict(scale=0.08, |
---|
147 | bi_thick=19.2946, |
---|
148 | sld_bi=5.38e-6,sld_sol=7.105e-6, |
---|
149 | background=0.003, |
---|
150 | bi_thick_pd= 0.37765, bi_thick_pd_n=40, bi_thick_pd_nsigma=3) |
---|
151 | |
---|
152 | model = sasview_model('Lamellar', **pars) |
---|
153 | from Models.code_lamellar import GpuLamellar as gpumodel |
---|
154 | |
---|
155 | plot(data, model, gpumodel, N, pars) |
---|
156 | |
---|
157 | def cap(N=1): |
---|
158 | |
---|
159 | data, N = newlen(N) |
---|
160 | |
---|
161 | pars = dict(scale=.08, rad_cyl=20, rad_cap=40, len_cyl=400, |
---|
162 | sld_capcyl=1e-6, sld_solv=6.3e-6, |
---|
163 | background=0, theta=0, phi=0, |
---|
164 | rad_cyl_pd=.1, rad_cyl_pd_n=10, rad_cyl_pd_nsigma=3, |
---|
165 | rad_cap_pd=.1, rad_cap_pd_n=10, rad_cap_pd_nsigma=3, |
---|
166 | len_cyl_pd=.1, len_cyl_pd_n=3, len_cyl_pd_nsigma=3, |
---|
167 | theta_pd=.1, theta_pd_n=3, theta_pd_nsigma=3, |
---|
168 | phi_pd=.1, phi_pd_n=3, phi_pd_nsigma=3) |
---|
169 | |
---|
170 | |
---|
171 | model = sasview_model('CappedCylinder', **pars) |
---|
172 | from Models.code_capcyl import GpuCapCylinder as gpumodel |
---|
173 | |
---|
174 | plot(data, model, gpumodel, N, pars) |
---|
175 | |
---|
176 | |
---|
177 | if __name__ == "__main__": |
---|
178 | cyl(2) |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | |
---|