[dbb0048] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | import numpy as np |
---|
| 5 | import pyopencl as cl |
---|
[ca6c007] | 6 | from Models.weights import GaussianDispersion |
---|
[a42fec0] | 7 | from sasmodel import set_precision |
---|
[dbb0048] | 8 | |
---|
| 9 | |
---|
| 10 | class GpuLamellar(object): |
---|
| 11 | PARS = { |
---|
| 12 | 'scale':1, 'bi_thick':1, 'sld_bi':1e-6, 'sld_sol':0, 'background':0, |
---|
| 13 | } |
---|
| 14 | PD_PARS = {'bi_thick'} |
---|
| 15 | def __init__(self, qx, qy, dtype='float32'): |
---|
| 16 | |
---|
| 17 | #create context, queue, and build program |
---|
| 18 | self.ctx = cl.create_some_context() |
---|
| 19 | self.queue = cl.CommandQueue(self.ctx) |
---|
[ca6c007] | 20 | src,qx,qy = set_precision(open('Kernel/Kernel-Lamellar.cpp').read(), qx, qy, dtype=dtype) |
---|
[dbb0048] | 21 | self.prg = cl.Program(self.ctx, src).build() |
---|
| 22 | self.qx, self.qy = qx, qy |
---|
| 23 | |
---|
| 24 | #buffers |
---|
| 25 | mf = cl.mem_flags |
---|
| 26 | self.qx_b = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx) |
---|
| 27 | self.qy_b = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy) |
---|
| 28 | self.res_b = cl.Buffer(self.ctx, mf.WRITE_ONLY, qx.nbytes) |
---|
| 29 | self.res = np.empty_like(self.qx) |
---|
| 30 | |
---|
| 31 | def eval(self, pars): |
---|
| 32 | |
---|
| 33 | bi_thick = GaussianDispersion(int(pars['bi_thick_pd_n']), pars['bi_thick_pd'], pars['bi_thick_pd_nsigma']) |
---|
| 34 | bi_thick.value, bi_thick.weight = bi_thick.get_weights(pars['bi_thick'], 0, 10000, True) |
---|
| 35 | |
---|
| 36 | sum, norm = 0.0, 0.0 |
---|
| 37 | sub = pars['sld_bi'] - pars['sld_sol'] |
---|
| 38 | |
---|
| 39 | real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 |
---|
| 40 | for i in xrange(len(bi_thick.weight)): |
---|
| 41 | self.prg.LamellarKernel(self.queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, real(bi_thick.value[i]), |
---|
| 42 | real(pars['scale']), real(sub), np.uint32(self.qx.size)) |
---|
| 43 | cl.enqueue_copy(self.queue, self.res, self.res_b) |
---|
| 44 | |
---|
| 45 | sum += bi_thick.weight[i]*self.res |
---|
| 46 | norm += bi_thick.weight[i] |
---|
| 47 | |
---|
| 48 | return sum/norm + pars['background'] |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | |
---|