source: sasmodels/sasmodels/kernel_iq.cl @ 4f1f876

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 4f1f876 was 4f1f876, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

Intel GPU wants data vectors to follow cache alignment

  • Property mode set to 100644
File size: 11.5 KB
Line 
1
2/*
3    ##########################################################
4    #                                                        #
5    #   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   #
6    #   !!                                              !!   #
7    #   !!  KEEP THIS CODE CONSISTENT WITH KERNELPY.PY  !!   #
8    #   !!                                              !!   #
9    #   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   #
10    #                                                        #
11    ##########################################################
12*/
13
14#ifndef _PAR_BLOCK_ // protected block so we can include this code twice.
15#define _PAR_BLOCK_
16
17typedef struct {
18#if MAX_PD > 0
19    int32_t pd_par[MAX_PD];     // id of the nth polydispersity variable
20    int32_t pd_length[MAX_PD];  // length of the nth polydispersity weight vector
21    int32_t pd_offset[MAX_PD];  // offset of pd weights in the value & weight vector
22    int32_t pd_stride[MAX_PD];  // stride to move to the next index at this level
23#endif // MAX_PD > 0
24    int32_t pd_prod;            // total number of voxels in hypercube
25    int32_t pd_sum;             // total length of the weights vector
26    int32_t num_active;         // number of non-trivial pd loops
27    int32_t theta_par;          // id of spherical correction variable
28} ProblemDetails;
29
30// Intel HD 4000 needs private arrays to be a multiple of 4 long
31typedef struct {
32    PARAMETER_TABLE
33} ParameterTable;
34typedef union {
35    ParameterTable table;
36    double vector[4*((NUM_PARS+3)/4)];
37} ParameterBlock;
38#endif // _PAR_BLOCK_
39
40
41#if defined(MAGNETIC) && NUM_MAGNETIC>0
42
43// Return value restricted between low and high
44static double clip(double value, double low, double high)
45{
46  return (value < low ? low : (value > high ? high : value));
47}
48
49// Compute spin cross sections given in_spin and out_spin
50// To convert spin cross sections to sld b:
51//     uu * (sld - m_sigma_x);
52//     dd * (sld + m_sigma_x);
53//     ud * (m_sigma_y + 1j*m_sigma_z);
54//     du * (m_sigma_y - 1j*m_sigma_z);
55static void set_spins(double in_spin, double out_spin, double spins[4])
56{
57  in_spin = clip(in_spin, 0.0, 1.0);
58  out_spin = clip(out_spin, 0.0, 1.0);
59  spins[0] = sqrt(sqrt((1.0-in_spin) * (1.0-out_spin))); // dd
60  spins[1] = sqrt(sqrt((1.0-in_spin) * out_spin));       // du
61  spins[2] = sqrt(sqrt(in_spin * (1.0-out_spin)));       // ud
62  spins[3] = sqrt(sqrt(in_spin * out_spin));             // uu
63}
64
65static double mag_sld(double qx, double qy, double p,
66                       double mx, double my, double sld)
67{
68    const double perp = qy*mx - qx*my;
69    return sld + perp*p;
70}
71
72#endif // MAGNETIC
73
74kernel
75void KERNEL_NAME(
76    int32_t nq,                 // number of q values
77    const int32_t pd_start,     // where we are in the polydispersity loop
78    const int32_t pd_stop,      // where we are stopping in the polydispersity loop
79    global const ProblemDetails *details,
80    global const double *values,
81    global const double *q, // nq q values, with padding to boundary
82    global double *result,  // nq+1 return values, again with padding
83    const double cutoff     // cutoff in the polydispersity weight product
84    )
85{
86
87  // who we are and what element we are working with
88  const int q_index = get_global_id(0);
89  if (q_index >= nq) return;
90
91  // Storage for the current parameter values.  These will be updated as we
92  // walk the polydispersity cube.  local_values will be aliased to pvec.
93  ParameterBlock local_values;
94
95  // Fill in the initial variables
96  for (int i=0; i < NUM_PARS; i++) {
97    local_values.vector[i] = values[2+i];
98//if (q_index==0) printf("p%d = %g\n",i, local_values.vector[i]);
99  }
100
101#if defined(MAGNETIC) && NUM_MAGNETIC>0
102  // Location of the sld parameters in the parameter vector.
103  // These parameters are updated with the effective sld due to magnetism.
104  #if NUM_MAGNETIC > 3
105  const int32_t slds[] = { MAGNETIC_PARS };
106  #endif
107
108  // TODO: could precompute these outside of the kernel.
109  // Interpret polarization cross section.
110  //     up_frac_i = values[NUM_PARS+2];
111  //     up_frac_f = values[NUM_PARS+3];
112  //     up_angle = values[NUM_PARS+4];
113  double spins[4];
114  double cos_mspin, sin_mspin;
115  set_spins(values[NUM_PARS+2], values[NUM_PARS+3], spins);
116  SINCOS(-values[NUM_PARS+4]*M_PI_180, sin_mspin, cos_mspin);
117#endif // MAGNETIC
118
119  double pd_norm, this_result;
120  if (pd_start == 0) {
121    pd_norm = this_result = 0.0;
122  } else {
123    pd_norm = result[nq];
124    this_result = result[q_index];
125  }
126//if (q_index==0) printf("start %d %g %g\n", pd_start, pd_norm, this_result);
127
128#if MAX_PD>0
129  global const double *pd_value = values + NUM_VALUES + 2;
130  global const double *pd_weight = pd_value + details->pd_sum;
131#endif
132
133  // Jump into the middle of the polydispersity loop
134#if MAX_PD>4
135  int n4=details->pd_length[4];
136  int i4=(pd_start/details->pd_stride[4])%n4;
137  const int p4=details->pd_par[4];
138  global const double *v4 = pd_value + details->pd_offset[4];
139  global const double *w4 = pd_weight + details->pd_offset[4];
140#endif
141#if MAX_PD>3
142  int n3=details->pd_length[3];
143  int i3=(pd_start/details->pd_stride[3])%n3;
144  const int p3=details->pd_par[3];
145  global const double *v3 = pd_value + details->pd_offset[3];
146  global const double *w3 = pd_weight + details->pd_offset[3];
147//if (q_index==0) printf("offset %d: %d %d\n", 3, details->pd_offset[3], NUM_VALUES);
148#endif
149#if MAX_PD>2
150  int n2=details->pd_length[2];
151  int i2=(pd_start/details->pd_stride[2])%n2;
152  const int p2=details->pd_par[2];
153  global const double *v2 = pd_value + details->pd_offset[2];
154  global const double *w2 = pd_weight + details->pd_offset[2];
155#endif
156#if MAX_PD>1
157  int n1=details->pd_length[1];
158  int i1=(pd_start/details->pd_stride[1])%n1;
159  const int p1=details->pd_par[1];
160  global const double *v1 = pd_value + details->pd_offset[1];
161  global const double *w1 = pd_weight + details->pd_offset[1];
162#endif
163#if MAX_PD>0
164  int n0=details->pd_length[0];
165  int i0=(pd_start/details->pd_stride[0])%n0;
166  const int p0=details->pd_par[0];
167  global const double *v0 = pd_value + details->pd_offset[0];
168  global const double *w0 = pd_weight + details->pd_offset[0];
169#endif
170
171
172#if MAX_PD>0
173  const int theta_par = details->theta_par;
174  const bool fast_theta = (theta_par == p0);
175  const bool slow_theta = (theta_par >= 0 && !fast_theta);
176  double spherical_correction = 1.0;
177#else
178  // Note: if not polydisperse the weights cancel and we don't need the
179  // spherical correction.
180  const double spherical_correction = 1.0;
181#endif
182
183  int step = pd_start;
184
185
186#if MAX_PD>4
187  const double weight5 = 1.0;
188  while (i4 < n4) {
189    local_values.vector[p4] = v4[i4];
190    double weight4 = w4[i4] * weight5;
191//if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 4, p4, i4, n4, local_values.vector[p4], weight4);
192#elif MAX_PD>3
193    const double weight4 = 1.0;
194#endif
195#if MAX_PD>3
196  while (i3 < n3) {
197    local_values.vector[p3] = v3[i3];
198    double weight3 = w3[i3] * weight4;
199//if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 3, p3, i3, n3, local_values.vector[p3], weight3);
200#elif MAX_PD>2
201    const double weight3 = 1.0;
202#endif
203#if MAX_PD>2
204  while (i2 < n2) {
205    local_values.vector[p2] = v2[i2];
206    double weight2 = w2[i2] * weight3;
207//if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 2, p2, i2, n2, local_values.vector[p2], weight2);
208#elif MAX_PD>1
209    const double weight2 = 1.0;
210#endif
211#if MAX_PD>1
212  while (i1 < n1) {
213    local_values.vector[p1] = v1[i1];
214    double weight1 = w1[i1] * weight2;
215//if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 1, p1, i1, n1, local_values.vector[p1], weight1);
216#elif MAX_PD>0
217    const double weight1 = 1.0;
218#endif
219#if MAX_PD>0
220  if (slow_theta) { // Theta is not in inner loop
221    spherical_correction = fmax(fabs(cos(M_PI_180*local_values.vector[theta_par])), 1.e-6);
222  }
223  while(i0 < n0) {
224    local_values.vector[p0] = v0[i0];
225    double weight0 = w0[i0] * weight1;
226//if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 0, p0, i0, n0, local_values.vector[p0], weight0);
227    if (fast_theta) { // Theta is in inner loop
228      spherical_correction = fmax(fabs(cos(M_PI_180*local_values.vector[p0])), 1.e-6);
229    }
230#else
231    const double weight0 = 1.0;
232#endif
233
234//if (q_index == 0) {printf("step:%d of %d, pars:",step,pd_stop); for (int i=0; i < NUM_PARS; i++) printf("p%d=%g ",i, local_values.vector[i]); printf("\n"); }
235//if (q_index == 0) printf("sphcor: %g\n", spherical_correction);
236
237    #ifdef INVALID
238    if (!INVALID(local_values.table))
239    #endif
240    {
241      // Accumulate I(q)
242      // Note: weight==0 must always be excluded
243      if (weight0 > cutoff) {
244        // spherical correction is set at a minimum of 1e-6, otherwise there
245        // would be problems looking at models with theta=90.
246        const double weight = weight0 * spherical_correction;
247        pd_norm += weight * CALL_VOLUME(local_values.table);
248
249#if defined(MAGNETIC) && NUM_MAGNETIC > 0
250        const double qx = q[2*q_index];
251        const double qy = q[2*q_index+1];
252        const double qsq = qx*qx + qy*qy;
253
254        // Constant across orientation, polydispersity for given qx, qy
255        double scattering = 0.0;
256        // TODO: what is the magnetic scattering at q=0
257        if (qsq > 1.e-16) {
258          double p[4];  // spin_i, spin_f
259          p[0] = (qy*cos_mspin + qx*sin_mspin)/qsq;
260          p[3] = -p[0];
261          p[1] = p[2] = (qy*sin_mspin - qx*cos_mspin)/qsq;
262
263          for (int index=0; index<4; index++) {
264            const double xs = spins[index];
265            if (xs > 1.e-8) {
266              const int spin_flip = (index==1) || (index==2);
267              const double pk = p[index];
268              for (int axis=0; axis<=spin_flip; axis++) {
269                #define M1 NUM_PARS+5
270                #define M2 NUM_PARS+8
271                #define M3 NUM_PARS+13
272                #define SLD(_M_offset, _sld_offset) \
273                    local_values.vector[_sld_offset] = xs * (axis \
274                    ? (index==1 ? -values[_M_offset+2] : values[_M_offset+2]) \
275                    : mag_sld(qx, qy, pk, values[_M_offset], values[_M_offset+1], \
276                              (spin_flip ? 0.0 : values[_sld_offset+2])))
277                #if NUM_MAGNETIC==1
278                    SLD(M1, MAGNETIC_PAR1);
279                #elif NUM_MAGNETIC==2
280                    SLD(M1, MAGNETIC_PAR1);
281                    SLD(M2, MAGNETIC_PAR2);
282                #elif NUM_MAGNETIC==3
283                    SLD(M1, MAGNETIC_PAR1);
284                    SLD(M2, MAGNETIC_PAR2);
285                    SLD(M3, MAGNETIC_PAR3);
286                #else
287                for (int sk=0; sk<NUM_MAGNETIC; sk++) {
288                    SLD(M1+3*sk, slds[sk]);
289                }
290                #endif
291                scattering += CALL_IQ(q, q_index, local_values.table);
292              }
293            }
294          }
295        }
296#else  // !MAGNETIC
297        const double scattering = CALL_IQ(q, q_index, local_values.table);
298#endif // !MAGNETIC
299        this_result += weight * scattering;
300      }
301    }
302    ++step;
303#if MAX_PD>0
304    if (step >= pd_stop) break;
305    ++i0;
306  }
307  i0 = 0;
308#endif
309#if MAX_PD>1
310    if (step >= pd_stop) break;
311    ++i1;
312  }
313  i1 = 0;
314#endif
315#if MAX_PD>2
316    if (step >= pd_stop) break;
317    ++i2;
318  }
319  i2 = 0;
320#endif
321#if MAX_PD>3
322    if (step >= pd_stop) break;
323    ++i3;
324  }
325  i3 = 0;
326#endif
327#if MAX_PD>4
328    if (step >= pd_stop) break;
329    ++i4;
330  }
331  i4 = 0;
332#endif
333
334//if (q_index==0) printf("res: %g/%g\n", this_result, pd_norm);
335  // Remember the current result and the updated norm.
336  result[q_index] = this_result;
337  if (q_index == 0) result[nq] = pd_norm;
338}
Note: See TracBrowser for help on using the repository browser.