1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | import numpy as np |
---|
5 | import pyopencl as cl |
---|
6 | from weights import GaussianDispersion |
---|
7 | from sasmodel import card |
---|
8 | import hi |
---|
9 | |
---|
10 | def set_precision(src, qx, qy, dtype): |
---|
11 | qx = np.ascontiguousarray(qx, dtype=dtype) |
---|
12 | qy = np.ascontiguousarray(qy, dtype=dtype) |
---|
13 | if np.dtype(dtype) == np.dtype('float32'): |
---|
14 | header = """\ |
---|
15 | #define real float |
---|
16 | """ |
---|
17 | else: |
---|
18 | header = """\ |
---|
19 | #pragma OPENCL EXTENSION cl_khr_fp64: enable |
---|
20 | #define real double |
---|
21 | """ |
---|
22 | return header+src, qx, qy |
---|
23 | |
---|
24 | class GpuEllipse(object): |
---|
25 | PARS = { |
---|
26 | 'scale':1, 'radius_a':1, 'radius_b':1, 'sldEll':1e-6, 'sldSolv':0, 'background':0, 'axis_theta':0, 'axis_phi':0, |
---|
27 | } |
---|
28 | PD_PARS = ['radius_a', 'radius_b', 'axis_theta', 'axis_phi'] |
---|
29 | |
---|
30 | def __init__(self, qx, qy, dtype='float32'): |
---|
31 | |
---|
32 | ctx,_queue = card() |
---|
33 | src, qx, qy = set_precision(open('TEST-Kernel-Ellipse.cpp').read(), qx, qy, dtype=dtype) |
---|
34 | self.prg = cl.Program(ctx, src).build() |
---|
35 | self.qx, self.qy = qx, qy |
---|
36 | place = np.ascontiguousarray(hi.place, dtype=int) |
---|
37 | #buffers |
---|
38 | mf = cl.mem_flags |
---|
39 | self.place_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=place) |
---|
40 | self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy) |
---|
41 | self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx) |
---|
42 | self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes) |
---|
43 | self.res = np.empty_like(self.qx) |
---|
44 | |
---|
45 | def eval(self, pars): |
---|
46 | #b_n = radius_b # want, a_n = radius_a # want, etc |
---|
47 | ctx,queue = card() |
---|
48 | radius_a, radius_b, axis_theta, axis_phi = \ |
---|
49 | [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) |
---|
50 | for base in GpuEllipse.PD_PARS] |
---|
51 | |
---|
52 | radius_a.value, radius_a.weight = radius_a.get_weights(pars['radius_a'], 0, 1000, True) |
---|
53 | radius_b.value, radius_b.weight = radius_b.get_weights(pars['radius_b'], 0, 1000, True) |
---|
54 | axis_theta.value, axis_theta.weight = axis_theta.get_weights(pars['axis_theta'], -90, 180, False) |
---|
55 | axis_phi.value, axis_phi.weight = axis_phi.get_weights(pars['axis_phi'], -90, 180, False) |
---|
56 | |
---|
57 | |
---|
58 | #Perform the computation, with all weight points |
---|
59 | sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0 |
---|
60 | size = len(axis_theta.weight) |
---|
61 | sub = pars['sldEll'] - pars['sldSolv'] |
---|
62 | real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 |
---|
63 | |
---|
64 | x = [radius_a.value, radius_a.weight, radius_b.value, radius_b.weight, axis_theta.value, |
---|
65 | axis_theta.weight, axis_phi.value, axis_phi.weight] |
---|
66 | array = np.hstack(x) |
---|
67 | |
---|
68 | array_b = cl.Buffer(ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=array) |
---|
69 | |
---|
70 | self.prg.EllipsoidKernel(queue, self.qx.shape, None, self.qx_b, self.qy_b, self.place_b, array_b, self.res_b, |
---|
71 | real(pars['scale']), real(sub), np.uint32(self.qx.size), np.uint32(len(axis_theta.weight))) |
---|
72 | #copy result back from buffer |
---|
73 | cl.enqueue_copy(queue, self.res, self.res_b) |
---|
74 | |
---|
75 | |
---|
76 | a = open("answer.txt", "w") |
---|
77 | for x in xrange(len(self.res)): |
---|
78 | a.write(str(self.res)) |
---|
79 | a.write("\n") |
---|
80 | |
---|
81 | |
---|
82 | return self.res+pars['background'] |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | |
---|