source: sasmodels/compare.py @ a42fec0

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

Speed-up of 3X, compare.py working

  • Property mode set to 100644
File size: 5.5 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import datetime
5
6from sasmodel import SasModel, load_data, set_beam_stop, plot_data
7
8
9TIC = None
10def tic():
11    global TIC
12    TIC = datetime.datetime.now()
13
14def toc():
15    now = datetime.datetime.now()
16    return (now-TIC).total_seconds()
17
18def 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
38def sasview_eval(model, data):
39    theory = model.evalDistribution([data.qx_data, data.qy_data])
40    return theory
41
42def 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('December/DEC07098.DAT')
49    set_beam_stop(data, 0.004)
50
51    dtype = "float"
52    pars = dict(
53        scale=.08, radius=64.1, length=266.96, sldCyl=.291e-6, sldSolv=5.77e-6, background=.1,
54        cyl_theta=0, cyl_phi=0,
55        radius_pd=0.1, radius_pd_n=10, radius_pd_nsigma=3,
56        length_pd=0.1,length_pd_n=5, length_pd_nsigma=3,
57        cyl_theta_pd=0.1, cyl_theta_pd_n=5, cyl_theta_pd_nsigma=3,
58        cyl_phi_pd=0.1, cyl_phi_pd_n=10, cyl_phi_pd_nsigma=3,
59        )
60
61    model = sasview_model('Cylinder', **pars)
62    tic()
63    for i in range(N):
64        cpu = sasview_eval(model, data)
65    cpu_time = toc()*1000./N
66
67
68    from Models.code_cylinder import GpuCylinder
69    model = SasModel(data, GpuCylinder, dtype='f', **pars)
70    tic()
71    for i in range(N):
72        gpu = model.theory()
73    gpu_time = toc()*1000./N
74
75    print "gpu/cpu", max(gpu/cpu)
76    cpu *= max(gpu/cpu)
77    relerr = (gpu - cpu)/cpu
78    print "max(|(ocl-omp)/ocl|)", max(abs(relerr))
79    print "omp t=%.1f ms"%cpu_time
80    print "ocl t=%.1f ms"%gpu_time
81
82    plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time)
83    plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time)
84    plt.subplot(133); plot_data(data, relerr); plt.title("relerr x 10^8"); plt.colorbar()
85    plt.show()
86
87def ellipse(N=4):
88    import sys
89    import matplotlib.pyplot as plt
90
91    if len(sys.argv) > 1:
92        N = int(sys.argv[1])
93    data = load_data('DEC07106.DAT')
94    set_beam_stop(data, 0.004)
95
96    pars = dict(scale=.027, radius_a=60, radius_b=180, sldEll=.297e-6, sldSolv=5.773e-6, background=4.9,
97                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,
98                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,
99                axis_phi_pd_n=6, axis_phi_pd_nsigma=3,)
100
101    model = sasview_model('Ellipsoid', **pars)
102    tic()
103    for i in range(N):
104        cpu = sasview_eval(model, data)
105    cpu_time = toc()*1000./N
106
107    from Models.code_ellipse import GpuEllipse
108    model = SasModel(data, GpuEllipse, dtype='f', **pars)
109    tic()
110    for i in range(N):
111        gpu = model.theory()
112    gpu_time = toc()*1000./N
113
114    relerr = (gpu - cpu)/cpu
115    print "max(|(ocl-omp)/ocl|)", max(abs(relerr))
116    print "omp t=%.1f ms"%cpu_time
117    print "ocl t=%.1f ms"%gpu_time
118
119    plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time)
120    plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time)
121    plt.subplot(133); plot_data(data, 1e8*relerr); plt.title("relerr x 10^8"); plt.colorbar()
122    plt.show()
123
124def coreshell(N=1):
125    import sys
126    import matplotlib.pyplot as plt
127
128    if len(sys.argv) > 1:
129        N = int(sys.argv[1])
130    data = load_data('December/DEC07098.DAT')
131    set_beam_stop(data, 0.004)
132
133    pars = dict(scale= 1.77881e-06, radius=325, thickness=25, length=34.2709,
134                 core_sld=1e-6, shell_sld=.291e-6, solvent_sld=7.105e-6,
135                 background=223.827, axis_theta=90, axis_phi=0,
136                 axis_theta_pd=15.8,
137                 radius_pd=0.1, radius_pd_n=1, radius_pd_nsigma=0,
138                 length_pd=0.1, length_pd_n=1, length_pd_nsigma=0,
139                 thickness_pd=0.1, thickness_pd_n=1, thickness_pd_nsigma=0,
140                 axis_theta_pd_n=20, axis_theta_pd_nsigma=3,
141                 axis_phi_pd=0.0008748, axis_phi_pd_n=60, axis_phi_pd_nsigma=3,)
142
143    model = sasview_model('CoreShellCylinder', **pars)
144    tic()
145    for i in range(N):
146        cpu = sasview_eval(model, data)
147    cpu_time = toc()*1000./N
148
149    from Models.code_coreshellcyl import GpuCoreShellCylinder
150    model = SasModel(data, GpuCoreShellCylinder, dtype='f', **pars)
151    tic()
152    for i in range(N):
153        gpu = model.theory()
154    gpu_time = toc()*1000./N
155
156    relerr = (gpu - cpu)/cpu
157    print "max(|(ocl-omp)/ocl|)", max(abs(relerr))
158    print "omp t=%.1f ms"%cpu_time
159    print "ocl t=%.1f ms"%gpu_time
160
161    plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time)
162    plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time)
163    plt.subplot(133); plot_data(data, 1e8*relerr); plt.title("relerr x 10^8"); plt.colorbar()
164    plt.show()
165
166if __name__ == "__main__":
167    coreshell()
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
Note: See TracBrowser for help on using the repository browser.