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 | |
---|
17 | typedef 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 par_offset[NPARS]; // offset of par value blocks in the value & weight vector |
---|
25 | int32_t par_coord[NPARS]; // ids of the coordination parameters |
---|
26 | int32_t pd_coord[NPARS]; // polydispersity coordination bitvector |
---|
27 | int32_t num_active; // number of non-trivial pd loops |
---|
28 | int32_t total_pd; // total number of voxels in hypercube |
---|
29 | int32_t num_coord; // number of coordinated parameters |
---|
30 | int32_t theta_par; // id of spherical correction variable |
---|
31 | } ProblemDetails; |
---|
32 | |
---|
33 | typedef struct { |
---|
34 | PARAMETER_TABLE; |
---|
35 | } ParameterBlock; |
---|
36 | #endif |
---|
37 | |
---|
38 | |
---|
39 | kernel |
---|
40 | void KERNEL_NAME( |
---|
41 | int32_t nq, // number of q values |
---|
42 | const int32_t pd_start, // where we are in the polydispersity loop |
---|
43 | const int32_t pd_stop, // where we are stopping in the polydispersity loop |
---|
44 | global const ProblemDetails *details, |
---|
45 | global const double *weights, |
---|
46 | global const double *values, |
---|
47 | global const double *q, // nq q values, with padding to boundary |
---|
48 | global double *result, // nq+3 return values, again with padding |
---|
49 | const double cutoff // cutoff in the polydispersity weight product |
---|
50 | ) |
---|
51 | { |
---|
52 | // Storage for the current parameter values. These will be updated as we |
---|
53 | // walk the polydispersity cube. |
---|
54 | ParameterBlock local_values; // current parameter values |
---|
55 | double *pvec = (double *)(&local_values); // Alias named parameters with a vector |
---|
56 | double norm; |
---|
57 | |
---|
58 | // who we are and what element we are working with |
---|
59 | const int q_index = get_global_id(0); |
---|
60 | |
---|
61 | // number of active loops |
---|
62 | const int num_active = details->num_active; |
---|
63 | |
---|
64 | // Fill in the initial variables |
---|
65 | for (int k=0; k < NPARS; k++) { |
---|
66 | pvec[k] = values[details->par_offset[k]]; |
---|
67 | } |
---|
68 | |
---|
69 | // Monodisperse computation |
---|
70 | if (num_active == 0) { |
---|
71 | #ifdef INVALID |
---|
72 | if (INVALID(local_values)) { return; } |
---|
73 | #endif |
---|
74 | norm = CALL_VOLUME(local_values); |
---|
75 | |
---|
76 | double scale, background; |
---|
77 | scale = values[0]; |
---|
78 | background = values[1]; |
---|
79 | |
---|
80 | if (q_index < nq) { |
---|
81 | double scattering = CALL_IQ(q, q_index, local_values); |
---|
82 | result[q_index] = (norm>0. ? scale*scattering/norm + background : background); |
---|
83 | } |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | #if MAX_PD > 0 |
---|
88 | |
---|
89 | double this_result; |
---|
90 | |
---|
91 | //printf("Entering polydispersity from %d to %d\n", pd_start, pd_stop); |
---|
92 | // norm will be shared across all threads. |
---|
93 | |
---|
94 | // need product of weights at every Iq calc, so keep product of |
---|
95 | // weights from the outer loops so that weight = partial_weight * fast_weight |
---|
96 | double partial_weight; // product of weight w4*w3*w2 but not w1 |
---|
97 | double spherical_correction; // cosine correction for latitude variation |
---|
98 | double weight; // product of partial_weight*w1*spherical_correction |
---|
99 | |
---|
100 | // Location in the polydispersity hypercube, one index per dimension. |
---|
101 | int pd_index[MAX_PD]; |
---|
102 | |
---|
103 | // Location of the coordinated parameters in their own sub-cubes. |
---|
104 | int offset[NPARS]; |
---|
105 | |
---|
106 | // Number of coordinated indices |
---|
107 | const int num_coord = details->num_coord; |
---|
108 | |
---|
109 | // Number of elements in the longest polydispersity loop |
---|
110 | const int fast_length = details->pd_length[0]; |
---|
111 | |
---|
112 | // Trigger the reset behaviour that happens at the end the fast loop |
---|
113 | // by setting the initial index >= weight vector length. |
---|
114 | pd_index[0] = fast_length; |
---|
115 | |
---|
116 | // Default the spherical correction to 1.0 in case it is not otherwise set |
---|
117 | spherical_correction = 1.0; |
---|
118 | |
---|
119 | // Since we are no longer looping over the entire polydispersity hypercube |
---|
120 | // for each q, we need to track the result and normalization values between |
---|
121 | // calls. This means initializing them to 0 at the start and accumulating |
---|
122 | // them between calls. |
---|
123 | norm = pd_start == 0 ? 0.0 : result[nq]; |
---|
124 | if (q_index < nq) { |
---|
125 | this_result = pd_start == 0 ? 0.0 : result[q_index]; |
---|
126 | } |
---|
127 | |
---|
128 | // Loop over the weights then loop over q, accumulating values |
---|
129 | for (int loop_index=pd_start; loop_index < pd_stop; loop_index++) { |
---|
130 | // check if fast loop needs to be reset |
---|
131 | if (pd_index[0] == fast_length) { |
---|
132 | //printf("should be here with %d active\n", num_active); |
---|
133 | |
---|
134 | // Compute position in polydispersity hypercube |
---|
135 | for (int k=0; k < num_active; k++) { |
---|
136 | pd_index[k] = (loop_index/details->pd_stride[k])%details->pd_length[k]; |
---|
137 | //printf("pd_index[%d] = %d\n",k,pd_index[k]); |
---|
138 | } |
---|
139 | |
---|
140 | // need to compute the product of the weights. If the vector were really |
---|
141 | // long, we could split the work into groups, with each thread taking |
---|
142 | // every nth weight, but there really is no call for it here. We could |
---|
143 | // also do some clever pair-wise multiplication similar to parallel |
---|
144 | // prefix, but again simpler is probably faster since n is likely small. |
---|
145 | // Compute partial weights |
---|
146 | partial_weight = 1.0; |
---|
147 | //printf("partial weight %d: ", loop_index); |
---|
148 | for (int k=1; k < num_active; k++) { |
---|
149 | double wi = weights[details->pd_offset[k] + pd_index[k]]; |
---|
150 | //printf("pd[%d]=par[%d]=%g ", k, details->pd_par[k], wi); |
---|
151 | partial_weight *= wi; |
---|
152 | } |
---|
153 | //printf("\n"); |
---|
154 | |
---|
155 | // Update parameter offsets in weight vector |
---|
156 | //printf("slow %d: ", loop_index); |
---|
157 | for (int k=0; k < num_coord; k++) { |
---|
158 | int par = details->par_coord[k]; |
---|
159 | int coord = details->pd_coord[k]; |
---|
160 | int this_offset = details->par_offset[par]; |
---|
161 | int block_size = 1; |
---|
162 | for (int bit=0; coord != 0; bit++) { |
---|
163 | if (coord&1) { |
---|
164 | this_offset += block_size * pd_index[bit]; |
---|
165 | block_size *= details->pd_length[bit]; |
---|
166 | } |
---|
167 | coord >>= 1; |
---|
168 | } |
---|
169 | offset[par] = this_offset; |
---|
170 | pvec[par] = values[this_offset]; |
---|
171 | //printf("par[%d]=v[%d]=%g \n", k, offset[k], pvec[k]); |
---|
172 | // if theta is not coordinated with fast index, precompute spherical correction |
---|
173 | if (par == details->theta_par && !(details->par_coord[k]&1)) { |
---|
174 | spherical_correction = fmax(fabs(cos(M_PI_180*pvec[details->theta_par])), 1.e-6); |
---|
175 | } |
---|
176 | } |
---|
177 | //printf("\n"); |
---|
178 | } |
---|
179 | |
---|
180 | // Update fast parameters |
---|
181 | //printf("fast %d: ", loop_index); |
---|
182 | for (int k=0; k < num_coord; k++) { |
---|
183 | if (details->pd_coord[k]&1) { |
---|
184 | const int par = details->par_coord[k]; |
---|
185 | pvec[par] = values[offset[par]++]; |
---|
186 | //printf("p[%d]=v[%d]=%g ", par, offset[par]-1, pvec[par]); |
---|
187 | // if theta is coordinated with fast index, compute spherical correction each time |
---|
188 | if (par == details->theta_par) { |
---|
189 | spherical_correction = fmax(fabs(cos(M_PI_180*pvec[details->theta_par])), 1.e-6); |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | //printf("\n"); |
---|
194 | |
---|
195 | // Increment fast index |
---|
196 | const double wi = weights[details->pd_offset[0] + pd_index[0]]; |
---|
197 | weight = partial_weight*wi; |
---|
198 | pd_index[0]++; |
---|
199 | |
---|
200 | #ifdef INVALID |
---|
201 | if (INVALID(local_values)) continue; |
---|
202 | #endif |
---|
203 | |
---|
204 | // Accumulate I(q) |
---|
205 | // Note: weight==0 must always be excluded |
---|
206 | if (weight > cutoff) { |
---|
207 | // spherical correction has some nasty effects when theta is +90 or -90 |
---|
208 | // where it becomes zero. If the entirety of the correction |
---|
209 | weight *= spherical_correction; |
---|
210 | norm += weight * CALL_VOLUME(local_values); |
---|
211 | |
---|
212 | const double scattering = CALL_IQ(q, q_index, local_values); |
---|
213 | this_result += weight*scattering; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | if (q_index < nq) { |
---|
218 | if (pd_stop >= details->total_pd) { |
---|
219 | // End of the PD loop we can normalize |
---|
220 | double scale, background; |
---|
221 | scale = values[0]; |
---|
222 | background = values[1]; |
---|
223 | result[q_index] = (norm>0. ? scale*this_result/norm + background : background); |
---|
224 | } else { |
---|
225 | // Partial result, so remember it but don't normalize it. |
---|
226 | result[q_index] = this_result; |
---|
227 | } |
---|
228 | |
---|
229 | // Remember the updated norm. |
---|
230 | if (q_index == 0) result[nq] = norm; |
---|
231 | } |
---|
232 | |
---|
233 | #endif // MAX_PD > 0 |
---|
234 | } |
---|