source: sasmodels/Testers/test_code_ellipse.py @ 099e053

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 099e053 was 099e053, checked in by HMP1 <helen.park@…>, 10 years ago

further organizing

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