[8a20be5] | 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 |
---|
[8faffcd] | 8 | from sasmodel import card |
---|
[d772f5d] | 9 | from Gauss import Gauss76Wt, Gauss76Z |
---|
[8faffcd] | 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 |
---|
[8a20be5] | 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 | |
---|
[8faffcd] | 31 | def __init__(self, qx, qy, dtype='float32'): |
---|
[8a20be5] | 32 | |
---|
| 33 | #create context, queue, and build program |
---|
[8faffcd] | 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) |
---|
[2de9a5e] | 37 | self.prg = cl.Program(ctx, src).build() |
---|
| 38 | self.qx, self.qy = qx, qy |
---|
[8a20be5] | 39 | |
---|
| 40 | |
---|
| 41 | #buffers |
---|
| 42 | mf = cl.mem_flags |
---|
[8faffcd] | 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) |
---|
[d772f5d] | 45 | G = np.ascontiguousarray(Gauss76Wt, dtype=dtype) |
---|
| 46 | Z = np.ascontiguousarray(Gauss76Z, dtype=dtype) |
---|
[2de9a5e] | 47 | self.Gauss76W_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G) |
---|
| 48 | self.Gauss76Z_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=Z) |
---|
[8faffcd] | 49 | self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes) |
---|
[8a20be5] | 50 | self.res = np.empty_like(self.qx) |
---|
| 51 | self.vol_i = float(0.0) |
---|
[8faffcd] | 52 | self.vol_b = cl.Buffer(ctx, mf.WRITE_ONLY, self.vol_i.nbytes) |
---|
[8a20be5] | 53 | |
---|
| 54 | def eval(self, pars): |
---|
| 55 | |
---|
[8faffcd] | 56 | _ctx,queue = card() |
---|
[8a20be5] | 57 | rad_cyl,length,rad_cap,theta,phi = \ |
---|
| 58 | [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) |
---|
| 59 | for base in GpuCapCylinder.PD_PARS] |
---|
| 60 | |
---|
| 61 | rad_cyl.value, rad_cyl.weight = rad_cyl.get_weights(pars['rad_cyl'], 0, 1000, True) |
---|
| 62 | rad_cap.value, rad_cap.weight = rad_cap.get_weights(pars['rad_cap'], 0, 1000, True) |
---|
| 63 | length.value, length.weight = length.get_weights(pars['length'], 0, 1000, True) |
---|
| 64 | theta.value, theta.weight = theta.get_weights(pars['theta'], -90, 180, False) |
---|
| 65 | phi.value, phi.weight = phi.get_weights(pars['phi'], -90, 180, False) |
---|
| 66 | |
---|
| 67 | sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0 |
---|
| 68 | size = len(theta.weight) |
---|
[8a21ba3] | 69 | sub = pars['sld_capcyl']-pars['sld_solv'] |
---|
| 70 | real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 |
---|
[8a20be5] | 71 | |
---|
| 72 | for i in xrange(len(rad_cyl.weight)): |
---|
| 73 | for m in xrange(len(rad_cap.weight)): |
---|
| 74 | for j in xrange(len(length.weight)): |
---|
| 75 | for k in xrange(len(theta.weight)): |
---|
| 76 | for l in xrange(len(phi.weight)): |
---|
| 77 | |
---|
[8faffcd] | 78 | self.prg.CapCylinderKernel(queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, |
---|
[8a21ba3] | 79 | self.vol_b, real(rad_cyl.value[i]), real(rad_cap.value[m]), real(length.value[j]), |
---|
| 80 | real(theta.value[k]), real(phi.value[l]), real(sub), real(pars['scale']), |
---|
| 81 | real(phi.weight[l]), real(theta.weight[k]), real(rad_cap.weight[m]), |
---|
| 82 | real(rad_cyl.weight[i]), real(length.weight[j]), np.uint32(self.qx.size), np.uint32(size), |
---|
[2de9a5e] | 83 | self.Gauss76W_b, self.Gauss76Z_b) |
---|
[8a20be5] | 84 | |
---|
[8faffcd] | 85 | cl.enqueue_copy(queue, self.res, self.res_b) |
---|
| 86 | cl.enqueue_copy(queue, self.vol_i, self.vol_b) |
---|
[8a20be5] | 87 | |
---|
| 88 | sum += self.res |
---|
| 89 | vol += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]*self.vol_i |
---|
| 90 | norm_vol += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m] |
---|
| 91 | norm += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]*theta.weight[k]*phi.weight[l] |
---|
| 92 | |
---|
| 93 | if size > 1: |
---|
| 94 | norm /= asin(1.0) |
---|
| 95 | |
---|
| 96 | if vol != 0.0 and norm_vol != 0.0: |
---|
| 97 | sum *= norm_vol/vol |
---|
| 98 | |
---|
| 99 | return sum/norm + pars['background'] |
---|
| 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 | |
---|
| 139 | |
---|