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 | #define MAX_PD 4 // MAX_PD is the max number of polydisperse parameters |
---|
18 | |
---|
19 | typedef struct { |
---|
20 | int32_t pd_par[MAX_PD]; // index of the nth polydispersity variable |
---|
21 | int32_t pd_length[MAX_PD]; // length of the nth polydispersity weight vector |
---|
22 | int32_t pd_offset[MAX_PD]; // offset of pd weights in the par & weight vector |
---|
23 | int32_t pd_stride[MAX_PD]; // stride to move to the next index at this level |
---|
24 | int32_t pd_isvol[MAX_PD]; // True if parameter is a volume weighting parameter |
---|
25 | int32_t par_offset[NPARS]; // offset of par values in the par & weight vector |
---|
26 | int32_t par_coord[NPARS]; // polydispersity coordination bitvector |
---|
27 | int32_t fast_coord_index[NPARS]; // index of the fast coordination parameters |
---|
28 | int32_t fast_coord_count; // number of parameters coordinated with pd 1 |
---|
29 | int32_t theta_var; // id of spherical correction variable |
---|
30 | int32_t fast_theta; // true if spherical correction depends on pd 1 |
---|
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 *problem, |
---|
45 | global const double *weights, |
---|
46 | global const double *pars, |
---|
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 | local ParameterBlock local_pars; // current parameter values |
---|
55 | double *pvec = (double *)(&local_pars); // Alias named parameters with a vector |
---|
56 | |
---|
57 | local int offset[NPARS-2]; |
---|
58 | |
---|
59 | #if 1 // defined(USE_SHORTCUT_OPTIMIZATION) |
---|
60 | if (problem->pd_length[0] == 1) { |
---|
61 | // Shouldn't need to copy!! |
---|
62 | printf("copying\n"); |
---|
63 | for (int k=0; k < NPARS; k++) { |
---|
64 | pvec[k] = pars[k+2]; // skip scale and background |
---|
65 | } |
---|
66 | printf("calculating\n"); |
---|
67 | |
---|
68 | #ifdef USE_OPENMP |
---|
69 | #pragma omp parallel for |
---|
70 | #endif |
---|
71 | for (int i=0; i < nq; i++) { |
---|
72 | const double scattering = CALL_IQ(q, i, local_pars); |
---|
73 | result[i] += pars[0]*scattering + pars[1]; |
---|
74 | } |
---|
75 | printf("returning\n"); |
---|
76 | return; |
---|
77 | } |
---|
78 | printf("falling through\n"); |
---|
79 | #endif |
---|
80 | |
---|
81 | |
---|
82 | // Since we are no longer looping over the entire polydispersity hypercube |
---|
83 | // for each q, we need to track the normalization values for each q in a |
---|
84 | // separate work vector. |
---|
85 | double norm; // contains sum over weights |
---|
86 | double vol; // contains sum over volume |
---|
87 | double norm_vol; // contains weights over volume |
---|
88 | |
---|
89 | // Initialize the results to zero |
---|
90 | if (pd_start == 0) { |
---|
91 | norm_vol = 0.0; |
---|
92 | norm = 0.0; |
---|
93 | vol = 0.0; |
---|
94 | |
---|
95 | #ifdef USE_OPENMP |
---|
96 | #pragma omp parallel for |
---|
97 | #endif |
---|
98 | for (int i=0; i < nq; i++) { |
---|
99 | result[i] = 0.0; |
---|
100 | } |
---|
101 | } else { |
---|
102 | //Pulling values from previous segment |
---|
103 | norm = result[nq]; |
---|
104 | vol = result[nq+1]; |
---|
105 | norm_vol = result[nq+2]; |
---|
106 | } |
---|
107 | |
---|
108 | // Location in the polydispersity hypercube, one index per dimension. |
---|
109 | local int pd_index[MAX_PD]; |
---|
110 | |
---|
111 | // Trigger the reset behaviour that happens at the end the fast loop |
---|
112 | // by setting the initial index >= weight vector length. |
---|
113 | pd_index[0] = problem->pd_length[0]; |
---|
114 | |
---|
115 | |
---|
116 | // need product of weights at every Iq calc, so keep product of |
---|
117 | // weights from the outer loops so that weight = partial_weight * fast_weight |
---|
118 | double partial_weight = NAN; // product of weight w4*w3*w2 but not w1 |
---|
119 | double partial_volweight = NAN; |
---|
120 | double weight = 1.0; // set to 1 in case there are no weights |
---|
121 | double vol_weight = 1.0; // set to 1 in case there are no vol weights |
---|
122 | double spherical_correction = 1.0; // correction for latitude variation |
---|
123 | |
---|
124 | // Loop over the weights then loop over q, accumulating values |
---|
125 | for (int loop_index=pd_start; loop_index < pd_stop; loop_index++) { |
---|
126 | // check if indices need to be updated |
---|
127 | if (pd_index[0] >= problem->pd_length[0]) { |
---|
128 | |
---|
129 | // RESET INDICES |
---|
130 | pd_index[0] = loop_index%problem->pd_length[0]; |
---|
131 | partial_weight = 1.0; |
---|
132 | partial_volweight = 1.0; |
---|
133 | for (int k=1; k < MAX_PD; k++) { |
---|
134 | pd_index[k] = (loop_index%problem->pd_length[k])/problem->pd_stride[k]; |
---|
135 | const double wi = weights[problem->pd_offset[0]+pd_index[0]]; |
---|
136 | partial_weight *= wi; |
---|
137 | if (problem->pd_isvol[k]) partial_volweight *= wi; |
---|
138 | } |
---|
139 | for (int k=0; k < NPARS; k++) { |
---|
140 | int coord = problem->par_coord[k]; |
---|
141 | int this_offset = problem->par_offset[k]; |
---|
142 | int block_size = 1; |
---|
143 | for (int bit=0; bit < MAX_PD && coord != 0; bit++) { |
---|
144 | if (coord&1) { |
---|
145 | this_offset += block_size * pd_index[bit]; |
---|
146 | block_size *= problem->pd_length[bit]; |
---|
147 | } |
---|
148 | coord /= 2; |
---|
149 | } |
---|
150 | offset[k] = this_offset; |
---|
151 | pvec[k] = pars[this_offset]; |
---|
152 | } |
---|
153 | weight = partial_weight * weights[problem->pd_offset[0]+pd_index[0]]; |
---|
154 | if (problem->theta_var >= 0) { |
---|
155 | spherical_correction = fabs(cos(M_PI_180*pvec[problem->theta_var])); |
---|
156 | } |
---|
157 | if (!problem->fast_theta) { |
---|
158 | weight *= spherical_correction; |
---|
159 | } |
---|
160 | |
---|
161 | } else { |
---|
162 | |
---|
163 | // INCREMENT INDICES |
---|
164 | pd_index[0] += 1; |
---|
165 | const double wi = weights[problem->pd_offset[0]+pd_index[0]]; |
---|
166 | weight = partial_weight*wi; |
---|
167 | if (problem->pd_isvol[0]) vol_weight *= wi; |
---|
168 | for (int k=0; k < problem->fast_coord_count; k++) { |
---|
169 | pvec[problem->fast_coord_index[k]] |
---|
170 | = pars[offset[problem->fast_coord_index[k]]++]; |
---|
171 | } |
---|
172 | if (problem->fast_theta) { |
---|
173 | weight *= fabs(cos(M_PI_180*pvec[problem->theta_var])); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | #ifdef INVALID |
---|
178 | if (INVALID(local_pars)) continue; |
---|
179 | #endif |
---|
180 | |
---|
181 | // Accumulate I(q) |
---|
182 | // Note: weight==0 must always be excluded |
---|
183 | if (weight > cutoff) { |
---|
184 | norm += weight; |
---|
185 | vol += vol_weight * CALL_VOLUME(local_pars); |
---|
186 | norm_vol += vol_weight; |
---|
187 | |
---|
188 | #ifdef USE_OPENMP |
---|
189 | #pragma omp parallel for |
---|
190 | #endif |
---|
191 | for (int i=0; i < nq; i++) { |
---|
192 | const double scattering = CALL_IQ(q, i, local_pars); |
---|
193 | result[i] += weight*scattering; |
---|
194 | } |
---|
195 | } |
---|
196 | } |
---|
197 | //Makes a normalization avialable for the next round |
---|
198 | result[nq] = norm; |
---|
199 | result[nq+1] = vol; |
---|
200 | result[nq+2] = norm_vol; |
---|
201 | |
---|
202 | //End of the PD loop we can normalize |
---|
203 | if (pd_stop >= problem->pd_stride[MAX_PD-1]) { |
---|
204 | #ifdef USE_OPENMP |
---|
205 | #pragma omp parallel for |
---|
206 | #endif |
---|
207 | for (int i=0; i < nq; i++) { |
---|
208 | if (vol*norm_vol != 0.0) { |
---|
209 | result[i] *= norm_vol/vol; |
---|
210 | } |
---|
211 | result[i] = pars[0]*result[i]/norm + pars[1]; |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|