source: sasmodels/Models/code_lamellar.py @ ce27e21

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

Speed-up of 3X, compare.py working

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import numpy as np
5import pyopencl as cl
6from Models.weights import GaussianDispersion
7from sasmodel import set_precision
8
9
10class 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)
20        src,qx,qy = set_precision(open('Kernel/Kernel-Lamellar.cpp').read(), qx, qy, dtype=dtype)
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
Note: See TracBrowser for help on using the repository browser.