[8a20be5] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | import datetime |
---|
| 5 | from sasmodel import SasModel, load_data, set_beam_stop, plot_data |
---|
| 6 | |
---|
| 7 | TIC = None |
---|
| 8 | def tic(): |
---|
| 9 | global TIC |
---|
| 10 | TIC = datetime.datetime.now() |
---|
| 11 | |
---|
| 12 | def toc(): |
---|
| 13 | now = datetime.datetime.now() |
---|
| 14 | return (now-TIC).total_seconds() |
---|
| 15 | |
---|
| 16 | def sasview_model(modelname, **pars): |
---|
| 17 | modelname = modelname.capitalize()+"Model" |
---|
| 18 | sans = __import__('sans.models.'+modelname) |
---|
| 19 | ModelClass = getattr(getattr(sans.models,modelname,None),modelname,None) |
---|
| 20 | if ModelClass is None: |
---|
| 21 | raise ValueError("could not find model %r in sans.models"%modelname) |
---|
| 22 | model = ModelClass() |
---|
| 23 | |
---|
| 24 | for k,v in pars.items(): |
---|
| 25 | if k.endswith("_pd"): |
---|
| 26 | model.dispersion[k[:-3]]['width'] = v |
---|
| 27 | elif k.endswith("_pd_n"): |
---|
| 28 | model.dispersion[k[:-5]]['npts'] = v |
---|
| 29 | elif k.endswith("_pd_nsigma"): |
---|
| 30 | model.dispersion[k[:-10]]['nsigmas'] = v |
---|
| 31 | else: |
---|
| 32 | model.setParam(k, v) |
---|
| 33 | return model |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | def sasview_eval(model, data): |
---|
| 37 | theory = model.evalDistribution([data.qx_data, data.qy_data]) |
---|
| 38 | return theory |
---|
| 39 | |
---|
| 40 | def demo(N=1): |
---|
| 41 | import sys |
---|
| 42 | import matplotlib.pyplot as plt |
---|
| 43 | import numpy as np |
---|
| 44 | |
---|
| 45 | if len(sys.argv) > 1: |
---|
| 46 | N = int(sys.argv[1]) |
---|
| 47 | data = load_data('JUN03289.DAT') |
---|
| 48 | set_beam_stop(data, 0.004) |
---|
| 49 | |
---|
| 50 | pars = dict( |
---|
| 51 | scale=1, radius=64.1, length=266.96, sldCyl=.291e-6, sldSolv=5.77e-6, background=0, |
---|
| 52 | cyl_theta=0, cyl_phi=0, radius_pd=0.1, radius_pd_n=10, radius_pd_nsigma=3,length_pd=0.1, |
---|
| 53 | length_pd_n=5, length_pd_nsigma=3, cyl_theta_pd=0.1, cyl_theta_pd_n=5, cyl_theta_pd_nsigma=3, |
---|
| 54 | cyl_phi_pd=0.1, cyl_phi_pd_n=10, cyl_phi_pd_nsigma=3, |
---|
| 55 | ) |
---|
| 56 | |
---|
| 57 | model = sasview_model('cylinder', **pars) |
---|
| 58 | tic() |
---|
| 59 | for i in range(N): |
---|
| 60 | cpu = sasview_eval(model, data) |
---|
| 61 | cpu_time = toc()*1000./N |
---|
| 62 | |
---|
| 63 | from cylcode import GpuCylinder |
---|
| 64 | model = SasModel(data, GpuCylinder, dtype='f', **pars) |
---|
| 65 | tic() |
---|
| 66 | for i in range(N): |
---|
| 67 | gpu = model.theory() |
---|
| 68 | gpu_time = toc()*1000./N |
---|
| 69 | |
---|
| 70 | relerr = (gpu - cpu)/cpu |
---|
| 71 | print "max(|(ocl-omp)/ocl|)", max(abs(relerr)) |
---|
| 72 | print "omp t=%.1f ms"%cpu_time |
---|
| 73 | print "ocl t=%.1f ms"%gpu_time |
---|
| 74 | |
---|
| 75 | plt.subplot(131); plot_data(data, cpu); plt.title("omp t=%.1f ms"%cpu_time) |
---|
| 76 | plt.subplot(132); plot_data(data, gpu); plt.title("ocl t=%.1f ms"%gpu_time) |
---|
| 77 | plt.subplot(133); plot_data(data, 1e8*relerr); plt.title("relerr x 10^8"); plt.colorbar() |
---|
| 78 | plt.show() |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | if __name__ == "__main__": |
---|
| 82 | demo() |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | |
---|