1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | import datetime |
---|
5 | |
---|
6 | from Models.sasmodel import SasModel, load_data, set_beam_stop, plot_data |
---|
7 | |
---|
8 | |
---|
9 | TIC = None |
---|
10 | def tic(): |
---|
11 | global TIC |
---|
12 | TIC = datetime.datetime.now() |
---|
13 | |
---|
14 | def toc(): |
---|
15 | now = datetime.datetime.now() |
---|
16 | return (now-TIC).total_seconds() |
---|
17 | |
---|
18 | def sasview_model(modelname, **pars): |
---|
19 | modelname = modelname+"Model" |
---|
20 | sans = __import__('sans.models.'+modelname) |
---|
21 | ModelClass = getattr(getattr(sans.models,modelname,None),modelname,None) |
---|
22 | if ModelClass is None: |
---|
23 | raise ValueError("could not find model %r in sans.models"%modelname) |
---|
24 | model = ModelClass() |
---|
25 | |
---|
26 | for k,v in pars.items(): |
---|
27 | if k.endswith("_pd"): |
---|
28 | model.dispersion[k[:-3]]['width'] = v |
---|
29 | elif k.endswith("_pd_n"): |
---|
30 | model.dispersion[k[:-5]]['npts'] = v |
---|
31 | elif k.endswith("_pd_nsigma"): |
---|
32 | model.dispersion[k[:-10]]['nsigmas'] = v |
---|
33 | else: |
---|
34 | model.setParam(k, v) |
---|
35 | return model |
---|
36 | |
---|
37 | |
---|
38 | def sasview_eval(model, data): |
---|
39 | theory = model.evalDistribution([data.qx_data, data.qy_data]) |
---|
40 | return theory |
---|
41 | |
---|
42 | def cyl(N=1): |
---|
43 | import sys |
---|
44 | import matplotlib.pyplot as plt |
---|
45 | |
---|
46 | if len(sys.argv) > 1: |
---|
47 | N = int(sys.argv[1]) |
---|
48 | data = load_data('JUN03289.DAT') |
---|
49 | set_beam_stop(data, 0.004) |
---|
50 | |
---|
51 | pars = dict( |
---|
52 | scale=1, radius=64.1, length=266.96, sldCyl=.291e-6, sldSolv=5.77e-6, background=0, |
---|
53 | cyl_theta=0, cyl_phi=0, radius_pd=0.1, radius_pd_n=10, radius_pd_nsigma=3,length_pd=0.1, |
---|
54 | length_pd_n=5, length_pd_nsigma=3, cyl_theta_pd=0.1, cyl_theta_pd_n=5, cyl_theta_pd_nsigma=3, |
---|
55 | cyl_phi_pd=0.1, cyl_phi_pd_n=10, cyl_phi_pd_nsigma=3, |
---|
56 | ) |
---|
57 | |
---|
58 | model = sasview_model('Cylinder', **pars) |
---|
59 | tic() |
---|
60 | for i in range(N): |
---|
61 | cpu = sasview_eval(model, data) |
---|
62 | cpu_time = toc()*1000./N |
---|
63 | |
---|
64 | from code_cylinder import GpuCylinder |
---|
65 | model = SasModel(data, GpuCylinder, dtype='f', **pars) |
---|
66 | tic() |
---|
67 | for i in range(N): |
---|
68 | gpu = model.theory() |
---|
69 | gpu_time = toc()*1000./N |
---|
70 | |
---|
71 | relerr = (gpu - cpu)/cpu |
---|
72 | print "max(|(ocl-omp)/ocl|)", max(abs(relerr)) |
---|
73 | print "omp t=%.1f ms"%cpu_time |
---|
74 | print "ocl t=%.1f ms"%gpu_time |
---|
75 | |
---|
76 | plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time) |
---|
77 | plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time) |
---|
78 | plt.subplot(133); plot_data(data, 1e8*relerr); plt.title("relerr x 10^8"); plt.colorbar() |
---|
79 | plt.show() |
---|
80 | |
---|
81 | def ellipse(N=4): |
---|
82 | import sys |
---|
83 | import matplotlib.pyplot as plt |
---|
84 | |
---|
85 | if len(sys.argv) > 1: |
---|
86 | N = int(sys.argv[1]) |
---|
87 | data = load_data('DEC07133.DAT') |
---|
88 | set_beam_stop(data, 0.004) |
---|
89 | |
---|
90 | pars = dict(scale=.027, radius_a=60, radius_b=180, sldEll=.297e-6, sldSolv=5.773e-6, background=4.9, |
---|
91 | axis_theta=0, axis_phi=90, radius_a_pd=0.1, radius_a_pd_n=10, radius_a_pd_nsigma=3, radius_b_pd=0.1, radius_b_pd_n=10, |
---|
92 | radius_b_pd_nsigma=3, axis_theta_pd=0.1, axis_theta_pd_n=6, axis_theta_pd_nsigma=3, axis_phi_pd=0.1, |
---|
93 | axis_phi_pd_n=6, axis_phi_pd_nsigma=3,) |
---|
94 | |
---|
95 | model = sasview_model('Ellipsoid', **pars) |
---|
96 | tic() |
---|
97 | for i in range(N): |
---|
98 | cpu = sasview_eval(model, data) |
---|
99 | cpu_time = toc()*1000./N |
---|
100 | |
---|
101 | from code_ellipse import GpuEllipse |
---|
102 | model = SasModel(data, GpuEllipse, dtype='f', **pars) |
---|
103 | tic() |
---|
104 | for i in range(N): |
---|
105 | gpu = model.theory() |
---|
106 | gpu_time = toc()*1000./N |
---|
107 | |
---|
108 | relerr = (gpu - cpu)/cpu |
---|
109 | print "max(|(ocl-omp)/ocl|)", max(abs(relerr)) |
---|
110 | print "omp t=%.1f ms"%cpu_time |
---|
111 | print "ocl t=%.1f ms"%gpu_time |
---|
112 | |
---|
113 | plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time) |
---|
114 | plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time) |
---|
115 | plt.subplot(133); plot_data(data, 1e8*relerr); plt.title("relerr x 10^8"); plt.colorbar() |
---|
116 | plt.show() |
---|
117 | |
---|
118 | def coreshell(N=4): |
---|
119 | import sys |
---|
120 | import matplotlib.pyplot as plt |
---|
121 | |
---|
122 | if len(sys.argv) > 1: |
---|
123 | N = int(sys.argv[1]) |
---|
124 | data = load_data('DEC07133.DAT') |
---|
125 | set_beam_stop(data, 0.004) |
---|
126 | |
---|
127 | pars = dict(scale= 1.77881e-06, radius=325, thickness=25, length=34.2709, |
---|
128 | core_sld=1e-6, shell_sld=.291e-6, solvent_sld=7.105e-6, |
---|
129 | background=223.827, axis_theta=90, axis_phi=0, |
---|
130 | axis_theta_pd=15.8, |
---|
131 | radius_pd=0.1, radius_pd_n=1, radius_pd_nsigma=0, |
---|
132 | length_pd=0.1, length_pd_n=1, length_pd_nsigma=0, |
---|
133 | thickness_pd=0.1, thickness_pd_n=1, thickness_pd_nsigma=0, |
---|
134 | axis_theta_pd_n=20, axis_theta_pd_nsigma=3, |
---|
135 | axis_phi_pd=0.0008748, axis_phi_pd_n=60, axis_phi_pd_nsigma=3,) |
---|
136 | |
---|
137 | model = sasview_model('CoreShellCylinder', **pars) |
---|
138 | tic() |
---|
139 | for i in range(N): |
---|
140 | cpu = sasview_eval(model, data) |
---|
141 | cpu_time = toc()*1000./N |
---|
142 | |
---|
143 | from code_coreshellcyl import GpuCoreShellCylinder |
---|
144 | model = SasModel(data, GpuCoreShellCylinder, dtype='f', **pars) |
---|
145 | tic() |
---|
146 | for i in range(N): |
---|
147 | gpu = model.theory() |
---|
148 | gpu_time = toc()*1000./N |
---|
149 | |
---|
150 | relerr = (gpu - cpu)/cpu |
---|
151 | print "max(|(ocl-omp)/ocl|)", max(abs(relerr)) |
---|
152 | print "omp t=%.1f ms"%cpu_time |
---|
153 | print "ocl t=%.1f ms"%gpu_time |
---|
154 | |
---|
155 | plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time) |
---|
156 | plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time) |
---|
157 | plt.subplot(133); plot_data(data, 1e8*relerr); plt.title("relerr x 10^8"); plt.colorbar() |
---|
158 | plt.show() |
---|
159 | |
---|
160 | if __name__ == "__main__": |
---|
161 | coreshell() |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|