[5378e40] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | import numpy as np |
---|
| 5 | import math |
---|
| 6 | import pyopencl as cl |
---|
| 7 | from weights import GaussianDispersion |
---|
| 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 | |
---|
| 16 | def __init__(self, qx, qy): |
---|
| 17 | |
---|
| 18 | self.qx = np.asarray(qx, np.float32) |
---|
| 19 | self.qy = np.asarray(qy, np.float32) |
---|
| 20 | #create context, queue, and build program |
---|
| 21 | self.ctx = cl.create_some_context() |
---|
| 22 | self.queue = cl.CommandQueue(self.ctx) |
---|
| 23 | self.prg = cl.Program(self.ctx, open('Kernel-Lamellar.cpp').read()).build() |
---|
| 24 | |
---|
| 25 | #buffers |
---|
| 26 | mf = cl.mem_flags |
---|
| 27 | self.qx_b = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx) |
---|
| 28 | self.qy_b = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy) |
---|
| 29 | self.res_b = cl.Buffer(self.ctx, mf.WRITE_ONLY, qx.nbytes) |
---|
| 30 | self.res = np.empty_like(self.qx) |
---|
| 31 | |
---|
| 32 | def eval(self, pars): |
---|
| 33 | |
---|
| 34 | bi_thick = GaussianDispersion(int(pars['bi_thick_pd_n']), pars['bi_thick_pd'], pars['bi_thick_pd_nsigma']) |
---|
| 35 | bi_thick.value, bi_thick.weight = bi_thick.get_weights(pars['bi_thick'], 0, 1000, True) |
---|
| 36 | |
---|
| 37 | sum, norm = 0.0, 0.0 |
---|
| 38 | sub = pars['sld_bi'] - pars['sld_sol'] |
---|
| 39 | |
---|
| 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, np.float32(bi_thick.value[i]), |
---|
| 42 | np.float32(pars['scale']), np.float32(sub), np.float32(pars['background']), 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 | def lamellar_fit(self, pars, b_n=10, b_w=.1, sigma=3): |
---|
| 51 | |
---|
| 52 | bi_thick = GaussianDispersion(b_n, b_w, sigma) |
---|
| 53 | bi_thick.value, bi_thick.weight = bi_thick.get_weights(pars.bi_thick, 0, 1000, True) |
---|
| 54 | |
---|
| 55 | sum, norm = 0.0, 0.0 |
---|
| 56 | |
---|
| 57 | for i in xrange(len(bi_thick.weight)): |
---|
| 58 | self.prg.LamellarKernel(self.queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, np.float32(bi_thick.value[i]), |
---|
| 59 | np.float32(pars.scale), np.float32(pars.sld_bi), np.float32(pars.sld_sol), |
---|
| 60 | np.float32(pars.background), np.uint32(self.qx.size)) |
---|
| 61 | cl.enqueue_copy(self.queue, self.res, self.res_b) |
---|
| 62 | |
---|
| 63 | sum += bi_thick.weight[i]*self.res |
---|
| 64 | norm += bi_thick.weight[i] |
---|
| 65 | |
---|
| 66 | return sum/norm + pars.background |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | def demo(): |
---|
| 70 | |
---|
| 71 | from time import time |
---|
| 72 | import matplotlib.pyplot as plt |
---|
| 73 | |
---|
| 74 | #create qx and qy evenly spaces |
---|
| 75 | qx = np.linspace(-.01, .01, 128) |
---|
| 76 | qy = np.linspace(-.01, .01, 128) |
---|
| 77 | qx, qy = np.meshgrid(qx, qy) |
---|
| 78 | |
---|
| 79 | #saved shape of qx |
---|
| 80 | r_shape = qx.shape |
---|
| 81 | |
---|
| 82 | #reshape for calculation; resize as float32 |
---|
| 83 | qx = qx.flatten() |
---|
| 84 | qy = qy.flatten() |
---|
| 85 | |
---|
| 86 | pars = LamellarParameters(scale=1, bi_thick=100, sld_bi=.291e-6, sld_sol=5.77e-6, background=0) |
---|
| 87 | t = time() |
---|
| 88 | result = GpuLamellar(qx, qy) |
---|
| 89 | result.x = result.lamellar_fit(pars, b_n=35, b_w=.1, sigma=3) |
---|
| 90 | result.x = np.reshape(result.x, r_shape) |
---|
| 91 | tt = time() |
---|
| 92 | |
---|
| 93 | print("Time taken: %f" % (tt - t)) |
---|
| 94 | |
---|
| 95 | f = open("r.txt", "w") |
---|
| 96 | for x in xrange(len(r_shape)): |
---|
| 97 | f.write(str(result.x[x])) |
---|
| 98 | f.write("\n") |
---|
| 99 | |
---|
| 100 | plt.pcolormesh(np.log10(result.x)) |
---|
| 101 | plt.show() |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | if __name__ == "__main__": |
---|
| 105 | demo() |
---|
| 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 | |
---|