[dbb0048] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | import numpy as np |
---|
| 5 | import pyopencl as cl |
---|
[473183c] | 6 | |
---|
[dbb0048] | 7 | from weights import GaussianDispersion |
---|
[1726b21] | 8 | from sasmodel import card, set_precision, set_precision_1d, tic, toc |
---|
[a42fec0] | 9 | |
---|
[dbb0048] | 10 | |
---|
| 11 | class GpuCylinder(object): |
---|
| 12 | PARS = { |
---|
| 13 | 'scale':1,'radius':1,'length':1,'sldCyl':1e-6,'sldSolv':0,'background':0, |
---|
| 14 | 'cyl_theta':0,'cyl_phi':0, |
---|
| 15 | } |
---|
| 16 | PD_PARS = ['radius', 'length', 'cyl_theta', 'cyl_phi'] |
---|
| 17 | |
---|
| 18 | def __init__(self, qx, qy, dtype='float32'): |
---|
| 19 | |
---|
| 20 | #create context, queue, and build program |
---|
| 21 | ctx,_queue = card() |
---|
[ca6c007] | 22 | src, qx, qy = set_precision(open('Kernel/NR_BessJ1.cpp').read()+"\n"+open('Kernel/Kernel-Cylinder.cpp').read(), qx, qy, dtype=dtype) |
---|
[dbb0048] | 23 | self.prg = cl.Program(ctx, src).build() |
---|
| 24 | self.qx, self.qy = qx, qy |
---|
| 25 | |
---|
| 26 | #buffers |
---|
| 27 | mf = cl.mem_flags |
---|
| 28 | self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx) |
---|
| 29 | self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy) |
---|
[a42fec0] | 30 | self.res_b = cl.Buffer(ctx, cl.mem_flags.READ_WRITE, self.qx.nbytes) |
---|
[dbb0048] | 31 | self.res = np.empty_like(self.qx) |
---|
| 32 | |
---|
| 33 | def eval(self, pars): |
---|
| 34 | |
---|
[1726b21] | 35 | tic() |
---|
[dbb0048] | 36 | _ctx,queue = card() |
---|
[a42fec0] | 37 | self.res[:] = 0 |
---|
| 38 | cl.enqueue_copy(queue, self.res_b, self.res) |
---|
[dbb0048] | 39 | radius, length, cyl_theta, cyl_phi = \ |
---|
| 40 | [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) |
---|
| 41 | for base in GpuCylinder.PD_PARS] |
---|
| 42 | |
---|
| 43 | #Get the weights for each |
---|
| 44 | radius.value, radius.weight = radius.get_weights(pars['radius'], 0, 10000, True) |
---|
| 45 | length.value, length.weight = length.get_weights(pars['length'], 0, 10000, True) |
---|
[a42fec0] | 46 | cyl_theta.value, cyl_theta.weight = cyl_theta.get_weights(pars['cyl_theta'], -np.inf, np.inf, False) |
---|
| 47 | cyl_phi.value, cyl_phi.weight = cyl_phi.get_weights(pars['cyl_phi'], -np.inf, np.inf, False) |
---|
[dbb0048] | 48 | |
---|
| 49 | #Perform the computation, with all weight points |
---|
| 50 | sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0 |
---|
| 51 | size = len(cyl_theta.weight) |
---|
| 52 | sub = pars['sldCyl'] - pars['sldSolv'] |
---|
| 53 | |
---|
| 54 | real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 |
---|
| 55 | #Loop over radius, length, theta, phi weight points |
---|
| 56 | for i in xrange(len(radius.weight)): |
---|
| 57 | for j in xrange(len(length.weight)): |
---|
[1726b21] | 58 | |
---|
| 59 | vol += radius.weight[i]*length.weight[j]*pow(radius.value[i], 2)*length.value[j] |
---|
| 60 | norm_vol += radius.weight[i]*length.weight[j] |
---|
| 61 | |
---|
[dbb0048] | 62 | for k in xrange(len(cyl_theta.weight)): |
---|
| 63 | for l in xrange(len(cyl_phi.weight)): |
---|
| 64 | self.prg.CylinderKernel(queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, real(sub), |
---|
| 65 | real(radius.value[i]), real(length.value[j]), real(pars['scale']), |
---|
| 66 | real(radius.weight[i]), real(length.weight[j]), real(cyl_theta.weight[k]), |
---|
| 67 | real(cyl_phi.weight[l]), real(cyl_theta.value[k]), real(cyl_phi.value[l]), |
---|
| 68 | np.uint32(self.qx.size), np.uint32(size)) |
---|
[a42fec0] | 69 | |
---|
[dbb0048] | 70 | norm += radius.weight[i]*length.weight[j]*cyl_theta.weight[k]*cyl_phi.weight[l] |
---|
| 71 | |
---|
[1726b21] | 72 | |
---|
[dbb0048] | 73 | # if size > 1: |
---|
| 74 | # norm /= math.asin(1.0) |
---|
[a42fec0] | 75 | cl.enqueue_copy(queue, self.res, self.res_b) |
---|
| 76 | sum = self.res |
---|
[dbb0048] | 77 | if vol != 0.0 and norm_vol != 0.0: |
---|
| 78 | sum *= norm_vol/vol |
---|
| 79 | |
---|
[1726b21] | 80 | print toc()*1000, self.qx.shape[0] |
---|
[dbb0048] | 81 | return sum/norm+pars['background'] |
---|
| 82 | |
---|
| 83 | class OneDGpuCylinder(object): |
---|
| 84 | PARS = { |
---|
| 85 | 'scale':1,'radius':1,'length':1,'sldCyl':1e-6,'sldSolv':0,'background':0, |
---|
| 86 | 'bolim':0, 'uplim':90 |
---|
| 87 | } |
---|
| 88 | PD_PARS = ['radius', 'length'] |
---|
| 89 | |
---|
| 90 | def __init__(self, q, dtype='float32'): |
---|
| 91 | |
---|
| 92 | #create context, queue, and build program |
---|
| 93 | ctx,_queue = card() |
---|
[ca6c007] | 94 | trala = open('Kernel/NR_BessJ1.cpp').read()+"\n"+open('Kernel/OneDCyl_Kfun.cpp').read()+"\n"+open('Kernel/Kernel-OneDCylinder.cpp').read() |
---|
[dbb0048] | 95 | src, self.q = set_precision_1d(trala, q, dtype=dtype) |
---|
| 96 | self.prg = cl.Program(ctx, src).build() |
---|
| 97 | |
---|
| 98 | #buffers |
---|
| 99 | mf = cl.mem_flags |
---|
| 100 | self.q_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.q) |
---|
| 101 | self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, q.nbytes) |
---|
| 102 | self.res = np.empty_like(self.q) |
---|
| 103 | |
---|
| 104 | def eval(self, pars): |
---|
| 105 | |
---|
| 106 | _ctx,queue = card() |
---|
| 107 | radius, length = \ |
---|
| 108 | [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) |
---|
| 109 | for base in OneDGpuCylinder.PD_PARS] |
---|
| 110 | |
---|
| 111 | #Get the weights for each |
---|
| 112 | radius.value, radius.weight = radius.get_weights(pars['radius'], 0, 10000, True) |
---|
| 113 | length.value, length.weight = length.get_weights(pars['length'], 0, 10000, True) |
---|
| 114 | |
---|
| 115 | #Perform the computation, with all weight points |
---|
| 116 | sum, norm, vol = 0.0, 0.0, 0.0, |
---|
| 117 | sub = pars['sldCyl'] - pars['sldSolv'] |
---|
| 118 | |
---|
| 119 | real = np.float32 if self.q.dtype == np.dtype('float32') else np.float64 |
---|
| 120 | #Loop over radius, length, theta, phi weight points |
---|
| 121 | for r in xrange(len(radius.weight)): |
---|
| 122 | for l in xrange(len(length.weight)): |
---|
| 123 | self.prg.OneDCylKernel(queue, self.q.shape, None, self.q_b, self.res_b, real(sub), |
---|
| 124 | real(length.value[l]), real(radius.value[r]), real(pars['scale']), |
---|
| 125 | np.uint32(self.q.size), real(pars['uplim']), real(pars['bolim'])) |
---|
| 126 | cl.enqueue_copy(queue, self.res, self.res_b) |
---|
| 127 | sum += radius.weight[r]*length.weight[l]*self.res*pow(radius.value[r],2)*length.value[l] |
---|
| 128 | vol += radius.weight[r]*length.weight[l] *pow(radius.value[r],2)*length.value[l] |
---|
| 129 | norm += radius.weight[r]*length.weight[l] |
---|
| 130 | |
---|
| 131 | if vol != 0.0 and norm != 0.0: |
---|
| 132 | sum *= norm/vol |
---|
| 133 | |
---|
| 134 | return sum/norm + pars['background'] |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|