[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 |
---|
[ca6c007] | 8 | from sasmodel import card |
---|
[473183c] | 9 | |
---|
[dbb0048] | 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 |
---|
| 24 | |
---|
| 25 | class GpuTriEllipse: |
---|
| 26 | PARS = {'scale':1, 'axisA':35, 'axisB':100, 'axisC':400, 'sldEll':1e-6, 'sldSolv':6.3e-6, 'background':0, |
---|
| 27 | 'theta':0, 'phi':0, 'psi':0} |
---|
| 28 | |
---|
| 29 | PD_PARS = ['axisA', 'axisB', 'axisC', 'theta', 'phi', 'psi'] |
---|
| 30 | |
---|
| 31 | def __init__(self, qx, qy, dtype='float32'): |
---|
| 32 | ctx,_queue = card() |
---|
[ca6c007] | 33 | src, qx, qy = set_precision(open('Kernel/Kernel-TriaxialEllipse.cpp').read(), qx, qy, dtype=dtype) |
---|
[dbb0048] | 34 | self.prg = cl.Program(ctx, src).build() |
---|
| 35 | self.qx, self.qy = qx, qy |
---|
| 36 | |
---|
| 37 | #buffers |
---|
| 38 | mf = cl.mem_flags |
---|
| 39 | self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx) |
---|
| 40 | self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy) |
---|
| 41 | self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes) |
---|
| 42 | self.res = np.empty_like(self.qx) |
---|
| 43 | |
---|
| 44 | def eval(self, pars): |
---|
| 45 | |
---|
| 46 | _ctx,queue = card() |
---|
| 47 | axisA, axisB, axisC, theta, phi, psi = \ |
---|
| 48 | [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) |
---|
| 49 | for base in GpuTriEllipse.PD_PARS] |
---|
| 50 | |
---|
| 51 | axisA.value, axisA.weight = axisA.get_weights(pars['axisA'], 0, 10000, True) |
---|
| 52 | axisB.value, axisB.weight = axisB.get_weights(pars['axisB'], 0, 10000, True) |
---|
| 53 | axisC.value, axisC.weight = axisC.get_weights(pars['axisC'], 0, 10000, True) |
---|
| 54 | theta.value, theta.weight = theta.get_weights(pars['theta'], -90, 180, False) |
---|
| 55 | phi.value, phi.weight = phi.get_weights(pars['phi'], -90, 180, False) |
---|
| 56 | psi.value, psi.weight = psi.get_weights(pars['psi'], -90, 180, False) |
---|
| 57 | |
---|
| 58 | sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0 |
---|
| 59 | size = len(theta.weight) |
---|
| 60 | sub = pars['sldEll'] - pars['sldSolv'] |
---|
| 61 | |
---|
| 62 | real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 |
---|
| 63 | for a in xrange(len(axisA.weight)): |
---|
| 64 | for b in xrange(len(axisB.weight)): |
---|
| 65 | for c in xrange(len(axisC.weight)): |
---|
| 66 | for t in xrange(len(theta.weight)): |
---|
| 67 | for i in xrange(len(phi.weight)): |
---|
| 68 | for s in xrange(len(psi.weight)): |
---|
| 69 | self.prg.TriaxialEllipseKernel(queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, |
---|
| 70 | real(sub), real(pars['scale']), real(axisA.value[a]), real(axisB.value[b]), |
---|
| 71 | real(axisC.value[c]), real(phi.value[i]), real(theta.value[t]), real(psi.value[s]), |
---|
| 72 | real(axisA.weight[a]), real(axisB.weight[b]), real(axisC.weight[c]), real(psi.weight[s]), |
---|
| 73 | real(phi.weight[i]), real(theta.weight[t]), np.uint32(self.qx.size), np.uint32(size)) |
---|
| 74 | cl.enqueue_copy(queue, self.res, self.res_b) |
---|
| 75 | sum += self.res |
---|
| 76 | |
---|
| 77 | vol += axisA.weight[a]*axisB.weight[b]*axisC.weight[c]*axisA.value[a]*axisB.value[b]*axisC.value[c] |
---|
| 78 | norm_vol += axisA.weight[a]*axisB.weight[b]*axisC.weight[c] |
---|
| 79 | norm += axisA.weight[a]*axisB.weight[b]*axisC.weight[c]*theta.weight[t]*phi.weight[i]*psi.weight[s] |
---|
| 80 | |
---|
| 81 | # if size > 1: |
---|
| 82 | # norm /= asin(1.0) |
---|
| 83 | |
---|
| 84 | if vol != 0.0 and norm_vol != 0.0: |
---|
| 85 | sum *= norm_vol/vol |
---|
| 86 | |
---|
| 87 | return sum/norm + pars['background'] |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 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 | |
---|