[d5ac45f] | 1 | Calculator Interface |
---|
| 2 | ==================== |
---|
| 3 | |
---|
| 4 | The environment needs to provide the following #defines: |
---|
| 5 | |
---|
| 6 | - USE_OPENCL is defined if running in opencl |
---|
| 7 | - KERNEL declares a function to be available externally |
---|
| 8 | - KERNEL_NAME is the name of the function being declared |
---|
[03cac08] | 9 | - MAX_PD is the maximum depth of the polydispersity loop |
---|
[d5ac45f] | 10 | - NPARS is the number of parameters in the kernel |
---|
| 11 | - PARAMETER_TABLE is the declaration of the parameters to the kernel:: |
---|
| 12 | |
---|
| 13 | Cylinder: |
---|
| 14 | |
---|
| 15 | #define PARAMETER_TABLE \ |
---|
| 16 | double length; \ |
---|
| 17 | double radius; \ |
---|
| 18 | double sld; \ |
---|
| 19 | double sld_solvent |
---|
| 20 | |
---|
| 21 | Note: scale and background are never included |
---|
| 22 | |
---|
| 23 | Multi-shell cylinder (10 shell max): |
---|
| 24 | |
---|
| 25 | #define PARAMETER_TABLE \ |
---|
| 26 | double num_shells; \ |
---|
| 27 | double length; \ |
---|
| 28 | double radius[10]; \ |
---|
| 29 | double sld[10]; \ |
---|
| 30 | double sld_solvent |
---|
| 31 | |
---|
[03cac08] | 32 | - CALL_IQ(q, i, pars) is the declaration of a call to the kernel:: |
---|
[d5ac45f] | 33 | |
---|
| 34 | Cylinder: |
---|
| 35 | |
---|
[03cac08] | 36 | #define CALL_IQ(q, i, var) Iq(q[i], \ |
---|
[d5ac45f] | 37 | var.length, \ |
---|
| 38 | var.radius, \ |
---|
| 39 | var.sld, \ |
---|
| 40 | var.sld_solvent) |
---|
| 41 | |
---|
| 42 | Multi-shell cylinder: |
---|
| 43 | |
---|
[03cac08] | 44 | #define CALL_IQ(q, i, var) Iq(q[i], \ |
---|
[d5ac45f] | 45 | var.num_shells, \ |
---|
| 46 | var.length, \ |
---|
| 47 | var.radius, \ |
---|
| 48 | var.sld, \ |
---|
| 49 | var.sld_solvent) |
---|
| 50 | |
---|
[03cac08] | 51 | Cylinder2D: |
---|
| 52 | |
---|
| 53 | #define CALL_IQ(q, i, var) Iqxy(q[2*i], q[2*i+1], \ |
---|
| 54 | var.length, \ |
---|
| 55 | var.radius, \ |
---|
| 56 | var.sld, \ |
---|
| 57 | var.sld_solvent, \ |
---|
| 58 | var.theta, \ |
---|
| 59 | var.phi) |
---|
| 60 | |
---|
[d5ac45f] | 61 | - CALL_VOLUME(var) is similar, but for calling the form volume:: |
---|
| 62 | |
---|
| 63 | #define CALL_VOLUME(var) \ |
---|
| 64 | form_volume(var.length, var.radius) |
---|
| 65 | |
---|
| 66 | - INVALID(var) is a test for model parameters in the correct range:: |
---|
| 67 | |
---|
| 68 | Cylinder: |
---|
| 69 | |
---|
| 70 | #define INVALID(var) 0 |
---|
| 71 | |
---|
| 72 | BarBell: |
---|
| 73 | |
---|
| 74 | #define INVALID(var) (var.bell_radius > var.radius) |
---|
| 75 | |
---|
| 76 | Model with complicated constraints: |
---|
| 77 | |
---|
| 78 | inline bool constrained(p1, p2, p3) { return expression; } |
---|
| 79 | #define INVALID(var) constrained(var.p1, var.p2, var.p3) |
---|
| 80 | |
---|
| 81 | Our design supports a limited number of polydispersity loops, wherein |
---|
| 82 | we need to cycle through the values of the polydispersity, calculate |
---|
| 83 | the I(q, p) for each combination of parameters, and perform a normalized |
---|
| 84 | weighted sum across all the weights. Parameters may be passed to the |
---|
| 85 | underlying calculation engine as scalars or vectors, but the polydispersity |
---|
| 86 | calculator treats the parameter set as one long vector. |
---|
| 87 | |
---|
| 88 | Let's assume we have 6 parameters in the model, with two polydisperse:: |
---|
| 89 | |
---|
| 90 | 0: scale {scl = constant} |
---|
| 91 | 1: background {bkg = constant} |
---|
| 92 | 5: length {l = vector of 30pts} |
---|
| 93 | 4: radius {r = vector of 10pts} |
---|
| 94 | 3: sld {s = constant/(radius**2*length)} |
---|
| 95 | 2: sld_solvent {s2 = constant} |
---|
| 96 | |
---|
| 97 | This generates the following call to the kernel (where x stands for an |
---|
| 98 | arbitrary value that is not used by the kernel evaluator):: |
---|
| 99 | |
---|
| 100 | NPARS = 4 // scale and background are in all models |
---|
| 101 | problem { |
---|
| 102 | pd_par = {5, 4, x, x} // parameters *radius* and *length* vary |
---|
| 103 | pd_length = {30, 10, 0, 0} // *length* has more, so it is first |
---|
| 104 | pd_offset = {10, 0, x, x} // *length* starts at index 10 in weights |
---|
| 105 | pd_stride = {1, 30, 300, 300} // cumulative product of pd length |
---|
| 106 | pd_isvol = {1, 1, x, x} // true if weight is a volume weight |
---|
| 107 | par_offset = {2, 3, 303, 313} // parameter offsets |
---|
| 108 | par_coord = {0, 3, 2, 1} // bitmap of parameter dependencies |
---|
| 109 | fast_coord_index = {5, 3, x, x} |
---|
| 110 | fast_coord_count = 2 // two parameters vary with *length* distribution |
---|
| 111 | theta_var = -1 // no spherical correction |
---|
| 112 | fast_theta = 0 // spherical correction angle is not pd 1 |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | weight = { l0, .., l29, r0, .., r9} |
---|
| 116 | pars = { scl, bkg, l0, ..., l29, r0, r1, ..., r9, |
---|
| 117 | s[l0,r0], ... s[l0,r9], s[l1,r0], ... s[l29,r9] , s2} |
---|
| 118 | |
---|
| 119 | nq = 130 |
---|
| 120 | q = { q0, q1, ..., q130, x, x } # pad to 8 element boundary |
---|
| 121 | result = {r1, ..., r130, norm, vol, vol_norm, x, x, x, x, x, x, x} |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | The polydisperse parameters are stored in as an array of parameter |
---|
| 125 | indices, one for each polydisperse parameter, stored in pd_par[n]. |
---|
| 126 | Non-polydisperse parameters do not appear in this array. Each polydisperse |
---|
| 127 | parameter has a weight vector whose length is stored in pd_length[n], |
---|
| 128 | The weights are stored in a contiguous vector of weights for all |
---|
| 129 | parameters, with the starting position for the each parameter stored |
---|
| 130 | in pd_offset[n]. The values corresponding to the weights are stored |
---|
| 131 | together in a separate weights[] vector, with offset stored in |
---|
| 132 | par_offset[pd_par[n]]. Polydisperse parameters should be stored in |
---|
| 133 | decreasing order of length for highest efficiency. |
---|
| 134 | |
---|
| 135 | We limit the number of polydisperse dimensions to MAX_PD (currently 4). |
---|
| 136 | This cuts the size of the structure in half compared to allowing a |
---|
| 137 | separate polydispersity for each parameter. This will help a little |
---|
| 138 | bit for models with large numbers of parameters, such as the onion model. |
---|
| 139 | |
---|
| 140 | Parameters may be coordinated. That is, we may have the value of one |
---|
| 141 | parameter depend on a set of other parameters, some of which may be |
---|
| 142 | polydisperse. For example, if sld is inversely proportional to the |
---|
| 143 | volume of a cylinder, and the length and radius are independently |
---|
| 144 | polydisperse, then for each combination of length and radius we need a |
---|
| 145 | separate value for the sld. The caller must provide a coordination table |
---|
| 146 | for each parameter containing the value for each parameter given the |
---|
| 147 | value of the polydisperse parameters v1, v2, etc. The tables for each |
---|
| 148 | parameter are arranged contiguously in a vector, with offset[k] giving the |
---|
| 149 | starting location of parameter k in the vector. Each parameter defines |
---|
| 150 | coord[k] as a bit mask indicating which polydispersity parameters the |
---|
| 151 | parameter depends upon. Usually this is zero, indicating that the parameter |
---|
| 152 | is independent, but for the cylinder example given, the bits for the |
---|
| 153 | radius and length polydispersity parameters would both be set, the result |
---|
| 154 | being a (#radius x #length) table, or maybe a (#length x #radius) table |
---|
| 155 | if length comes first in the polydispersity table. |
---|
| 156 | |
---|
| 157 | NB: If we can guarantee that a compiler and OpenCL driver are available, |
---|
| 158 | we could instead create the coordination function on the fly for each |
---|
| 159 | parameter, saving memory and transfer time, but requiring a C compiler |
---|
| 160 | as part of the environment. |
---|
| 161 | |
---|
| 162 | In ordering the polydisperse parameters by decreasing length we can |
---|
| 163 | iterate over the longest dispersion weight vector first. All parameters |
---|
| 164 | coordinated with this weight vector (the 'fast' parameters), can be |
---|
| 165 | updated with a simple increment to the next position in the parameter |
---|
| 166 | value table. The indices of these parameters is stored in fast_coord_index[], |
---|
| 167 | with fast_coord_count being the number of fast parameters. A total |
---|
| 168 | of NPARS slots is allocated to allow for the case that all parameters |
---|
| 169 | are coordinated with the fast index, though this will likely be mostly |
---|
| 170 | empty. When the fast increment count reaches the end of the weight |
---|
| 171 | vector, then the index of the second polydisperse parameter must be |
---|
| 172 | incremented, and all of its coordinated parameters updated. Because this |
---|
| 173 | operation is not in the inner loop, a slower algorithm can be used. |
---|
| 174 | |
---|
| 175 | If there is no polydispersity we pretend that it is polydisperisty with one |
---|
| 176 | parameter, pd_start=0 and pd_stop=1. We may or may not short circuit the |
---|
| 177 | calculation in this case, depending on how much time it saves. |
---|
| 178 | |
---|
| 179 | The problem details structure can be allocated and sent in as an integer |
---|
| 180 | array using the read-only flag. This allows us to copy it once per fit |
---|
| 181 | along with the weights vector, since features such as the number of |
---|
| 182 | polydisperity elements per pd parameter or the coordinated won't change |
---|
| 183 | between function evaluations. A new parameter vector is sent for |
---|
| 184 | each I(q) evaluation. |
---|
| 185 | |
---|
| 186 | To protect against expensive evaluations taking all the GPU resource |
---|
| 187 | on large fits, the entire polydispersity will not be computed at once. |
---|
| 188 | Instead, a start and stop location will be sent, indicating where in the |
---|
| 189 | polydispersity loop the calculation should start and where it should |
---|
| 190 | stop. We can do this for arbitrary start/stop points since we have |
---|
| 191 | unwound the nested loop. Instead, we use the same technique as array |
---|
| 192 | index translation, using div and mod to figure out the i,j,k,... |
---|
| 193 | indices in the virtual nested loop. |
---|
| 194 | |
---|
| 195 | The results array will be initialized to zero for polydispersity loop |
---|
| 196 | entry zero, and preserved between calls to [start, stop] so that the |
---|
| 197 | results accumulate by the time the loop has completed. Background and |
---|
| 198 | scale will be applied when the loop reaches the end. This does require |
---|
| 199 | that the results array be allocated read-write, which is less efficient |
---|
| 200 | for the GPU, but it makes the calling sequence much more manageable. |
---|
| 201 | |
---|
| 202 | Scale and background cannot be coordinated with other polydisperse parameters |
---|
| 203 | |
---|
| 204 | Oriented objects in 2-D need a spherical correction on the angular variation |
---|
| 205 | in order to preserve the 'surface area' of the weight distribution. |
---|
| 206 | |
---|
| 207 | TODO: cutoff |
---|
[03cac08] | 208 | |
---|
| 209 | For accuracy we may want to introduce Kahan summation into the integration:: |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | double accumulated_error = 0.0; |
---|
| 213 | ... |
---|
| 214 | #if USE_KAHAN_SUMMATION |
---|
| 215 | const double y = next - accumulated_error; |
---|
| 216 | const double t = ret + y; |
---|
| 217 | accumulated_error = (t - ret) - y; |
---|
| 218 | ret = t; |
---|
| 219 | #else |
---|
| 220 | ret += next; |
---|
| 221 | #endif |
---|