[2e44ac7] | 1 | |
---|
| 2 | /* |
---|
| 3 | ########################################################## |
---|
| 4 | # # |
---|
| 5 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # |
---|
| 6 | # !! !! # |
---|
| 7 | # !! KEEP THIS CODE CONSISTENT WITH KERNELPY.PY !! # |
---|
| 8 | # !! !! # |
---|
| 9 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # |
---|
| 10 | # # |
---|
| 11 | ########################################################## |
---|
| 12 | */ |
---|
| 13 | |
---|
[03cac08] | 14 | #ifndef _PAR_BLOCK_ // protected block so we can include this code twice. |
---|
| 15 | #define _PAR_BLOCK_ |
---|
[2e44ac7] | 16 | |
---|
| 17 | typedef struct { |
---|
[60eab2a] | 18 | #if MAX_PD > 0 |
---|
[a6f9577] | 19 | int32_t pd_par[MAX_PD]; // id of the nth polydispersity variable |
---|
[5cf3c33] | 20 | int32_t pd_length[MAX_PD]; // length of the nth polydispersity weight vector |
---|
[0a7e5eb4] | 21 | int32_t pd_offset[MAX_PD]; // offset of pd weights in the value & weight vector |
---|
[5cf3c33] | 22 | int32_t pd_stride[MAX_PD]; // stride to move to the next index at this level |
---|
[60eab2a] | 23 | #endif // MAX_PD > 0 |
---|
[5ff1b03] | 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 |
---|
[0a7e5eb4] | 30 | int32_t theta_par; // id of spherical correction variable |
---|
[2e44ac7] | 31 | } ProblemDetails; |
---|
| 32 | |
---|
| 33 | typedef struct { |
---|
[03cac08] | 34 | PARAMETER_TABLE; |
---|
[2e44ac7] | 35 | } ParameterBlock; |
---|
[03cac08] | 36 | #endif |
---|
| 37 | |
---|
[2e44ac7] | 38 | |
---|
[03cac08] | 39 | kernel |
---|
| 40 | void KERNEL_NAME( |
---|
[5cf3c33] | 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 |
---|
[2e44ac7] | 44 | global const ProblemDetails *problem, |
---|
| 45 | global const double *weights, |
---|
[0a7e5eb4] | 46 | global const double *values, |
---|
[2e44ac7] | 47 | global const double *q, // nq q values, with padding to boundary |
---|
[03cac08] | 48 | global double *result, // nq+3 return values, again with padding |
---|
[303d8d6] | 49 | const double cutoff // cutoff in the polydispersity weight product |
---|
[2e44ac7] | 50 | ) |
---|
| 51 | { |
---|
[10ddb64] | 52 | // Storage for the current parameter values. These will be updated as we |
---|
| 53 | // walk the polydispersity cube. |
---|
[0a7e5eb4] | 54 | local ParameterBlock local_values; // current parameter values |
---|
| 55 | double *pvec = (double *)(&local_values); // Alias named parameters with a vector |
---|
[2e44ac7] | 56 | |
---|
[5ff1b03] | 57 | // Fill in the initial variables |
---|
| 58 | #ifdef USE_OPENMP |
---|
| 59 | #pragma omp parallel for |
---|
| 60 | #endif |
---|
| 61 | for (int k=0; k < NPARS; k++) { |
---|
| 62 | pvec[k] = values[problem->par_offset[k]]; |
---|
| 63 | } |
---|
[3044216] | 64 | |
---|
[5ff1b03] | 65 | // If it is the first round initialize the result to zero, otherwise |
---|
| 66 | // assume that the previous result has been passed back. |
---|
| 67 | // Note: doing this even in the monodisperse case in order to handle the |
---|
| 68 | // rare case where the model parameters are invalid and zero is returned. |
---|
| 69 | // So slightly increased cost for slightly smaller code size. |
---|
| 70 | if (pd_start == 0) { |
---|
[3044216] | 71 | #ifdef USE_OPENMP |
---|
| 72 | #pragma omp parallel for |
---|
| 73 | #endif |
---|
[5ff1b03] | 74 | for (int i=0; i < nq+1; i++) { |
---|
| 75 | result[i] = 0.0; |
---|
[3044216] | 76 | } |
---|
| 77 | } |
---|
[60eab2a] | 78 | |
---|
[5ff1b03] | 79 | // Monodisperse computation |
---|
| 80 | if (problem->num_active == 0) { |
---|
| 81 | #ifdef INVALID |
---|
| 82 | if (INVALID(local_values)) { return; } |
---|
| 83 | #endif |
---|
[3044216] | 84 | |
---|
[5ff1b03] | 85 | const double norm = CALL_VOLUME(local_values); |
---|
[2e44ac7] | 86 | #ifdef USE_OPENMP |
---|
| 87 | #pragma omp parallel for |
---|
| 88 | #endif |
---|
[5ff1b03] | 89 | result[nq] = norm; // Total volume normalization |
---|
[3044216] | 90 | for (int i=0; i < nq; i++) { |
---|
[5ff1b03] | 91 | double scattering = CALL_IQ(q, i, local_values); |
---|
| 92 | result[i] = values[0]*scattering/norm + values[1]; |
---|
[2e44ac7] | 93 | } |
---|
[5ff1b03] | 94 | return; |
---|
[2e44ac7] | 95 | } |
---|
| 96 | |
---|
[5ff1b03] | 97 | #if MAX_PD > 0 |
---|
| 98 | //printf("Entering polydispersity from %d to %d\n", pd_start, pd_stop); |
---|
| 99 | // Since we are no longer looping over the entire polydispersity hypercube |
---|
| 100 | // for each q, we need to track the normalization values between calls. |
---|
| 101 | double norm = 0.0; |
---|
| 102 | |
---|
| 103 | // need product of weights at every Iq calc, so keep product of |
---|
| 104 | // weights from the outer loops so that weight = partial_weight * fast_weight |
---|
| 105 | double partial_weight = NAN; // product of weight w4*w3*w2 but not w1 |
---|
| 106 | double spherical_correction = 1.0; // cosine correction for latitude variation |
---|
| 107 | |
---|
[3044216] | 108 | // Location in the polydispersity hypercube, one index per dimension. |
---|
[03cac08] | 109 | local int pd_index[MAX_PD]; |
---|
[a10da8b] | 110 | |
---|
[5ff1b03] | 111 | // Location of the coordinated parameters in their own sub-cubes. |
---|
| 112 | local int offset[NPARS]; |
---|
[380e8c9] | 113 | |
---|
[f9245d4] | 114 | // Trigger the reset behaviour that happens at the end the fast loop |
---|
| 115 | // by setting the initial index >= weight vector length. |
---|
[5ff1b03] | 116 | const int fast_length = problem->pd_length[0]; |
---|
| 117 | pd_index[0] = fast_length; |
---|
[3044216] | 118 | |
---|
[2e44ac7] | 119 | // Loop over the weights then loop over q, accumulating values |
---|
| 120 | for (int loop_index=pd_start; loop_index < pd_stop; loop_index++) { |
---|
| 121 | // check if indices need to be updated |
---|
[5ff1b03] | 122 | if (pd_index[0] == fast_length) { |
---|
| 123 | //printf("should be here with %d active\n", problem->num_active); |
---|
[208f0a4] | 124 | |
---|
[5ff1b03] | 125 | // Compute position in polydispersity hypercube |
---|
| 126 | for (int k=0; k < problem->num_active; k++) { |
---|
| 127 | pd_index[k] = (loop_index/problem->pd_stride[k])%problem->pd_length[k]; |
---|
| 128 | //printf("pd_index[%d] = %d\n",k,pd_index[k]); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | // Compute partial weights |
---|
[2e44ac7] | 132 | partial_weight = 1.0; |
---|
[5ff1b03] | 133 | //printf("partial weight %d: ", loop_index); |
---|
| 134 | for (int k=1; k < problem->num_active; k++) { |
---|
| 135 | double wi = weights[problem->pd_offset[k] + pd_index[k]]; |
---|
| 136 | //printf("pd[%d]=par[%d]=%g ", k, problem->pd_par[k], wi); |
---|
[f78a2a1] | 137 | partial_weight *= wi; |
---|
[2e44ac7] | 138 | } |
---|
[5ff1b03] | 139 | //printf("\n"); |
---|
| 140 | |
---|
| 141 | // Update parameter offsets in weight vector |
---|
[ba32cdd] | 142 | //printf("slow %d: ", loop_index); |
---|
[5ff1b03] | 143 | for (int k=0; k < problem->num_coord; k++) { |
---|
| 144 | int par = problem->par_coord[k]; |
---|
| 145 | int coord = problem->pd_coord[k]; |
---|
| 146 | int this_offset = problem->par_offset[par]; |
---|
[2e44ac7] | 147 | int block_size = 1; |
---|
[5ff1b03] | 148 | for (int bit=0; coord != 0; bit++) { |
---|
[2e44ac7] | 149 | if (coord&1) { |
---|
| 150 | this_offset += block_size * pd_index[bit]; |
---|
[03cac08] | 151 | block_size *= problem->pd_length[bit]; |
---|
[2e44ac7] | 152 | } |
---|
[5ff1b03] | 153 | coord >>= 1; |
---|
| 154 | } |
---|
| 155 | offset[par] = this_offset; |
---|
| 156 | pvec[par] = values[this_offset]; |
---|
| 157 | //printf("par[%d]=v[%d]=%g \n", k, offset[k], pvec[k]); |
---|
| 158 | // if theta is not coordinated with fast index, precompute spherical correction |
---|
| 159 | if (par == problem->theta_par && !(problem->par_coord[k]&1)) { |
---|
| 160 | spherical_correction = fmax(fabs(cos(M_PI_180*pvec[problem->theta_par])), 1e-6); |
---|
[2e44ac7] | 161 | } |
---|
[03cac08] | 162 | } |
---|
[ba32cdd] | 163 | //printf("\n"); |
---|
[5ff1b03] | 164 | } |
---|
| 165 | |
---|
| 166 | // Increment fast index |
---|
| 167 | const double wi = weights[problem->pd_offset[0] + pd_index[0]++]; |
---|
| 168 | double weight = partial_weight*wi; |
---|
| 169 | //printf("fast %d: ", loop_index); |
---|
| 170 | for (int k=0; k < problem->num_coord; k++) { |
---|
| 171 | if (problem->pd_coord[k]&1) { |
---|
| 172 | const int par = problem->par_coord[k]; |
---|
| 173 | pvec[par] = values[offset[par]++]; |
---|
| 174 | //printf("p[%d]=v[%d]=%g ", par, offset[par]-1, pvec[par]); |
---|
| 175 | // if theta is coordinated with fast index, compute spherical correction each time |
---|
| 176 | if (par == problem->theta_par) { |
---|
| 177 | spherical_correction = fmax(fabs(cos(M_PI_180*pvec[problem->theta_par])), 1e-6); |
---|
| 178 | } |
---|
[2e44ac7] | 179 | } |
---|
| 180 | } |
---|
[5ff1b03] | 181 | //printf("\n"); |
---|
| 182 | |
---|
[3044216] | 183 | #ifdef INVALID |
---|
[0a7e5eb4] | 184 | if (INVALID(local_values)) continue; |
---|
[3044216] | 185 | #endif |
---|
[208f0a4] | 186 | |
---|
[303d8d6] | 187 | // Accumulate I(q) |
---|
| 188 | // Note: weight==0 must always be excluded |
---|
[10ddb64] | 189 | if (weight > cutoff) { |
---|
[5ff1b03] | 190 | // spherical correction has some nasty effects when theta is +90 or -90 |
---|
| 191 | // where it becomes zero. If the entirety of the correction |
---|
| 192 | weight *= spherical_correction; |
---|
| 193 | norm += weight * CALL_VOLUME(local_values); |
---|
[3044216] | 194 | |
---|
[10ddb64] | 195 | #ifdef USE_OPENMP |
---|
| 196 | #pragma omp parallel for |
---|
| 197 | #endif |
---|
[3044216] | 198 | for (int i=0; i < nq; i++) { |
---|
[0a7e5eb4] | 199 | const double scattering = CALL_IQ(q, i, local_values); |
---|
[3044216] | 200 | result[i] += weight*scattering; |
---|
| 201 | } |
---|
[03cac08] | 202 | } |
---|
[2e44ac7] | 203 | } |
---|
[ea1f14d] | 204 | |
---|
[380e8c9] | 205 | // Make normalization available for the next round |
---|
[5ff1b03] | 206 | result[nq] += norm; |
---|
[2e44ac7] | 207 | |
---|
[380e8c9] | 208 | // End of the PD loop we can normalize |
---|
[5ff1b03] | 209 | if (pd_stop >= problem->total_pd) { |
---|
[2e44ac7] | 210 | #ifdef USE_OPENMP |
---|
| 211 | #pragma omp parallel for |
---|
| 212 | #endif |
---|
[3044216] | 213 | for (int i=0; i < nq; i++) { |
---|
[0a7e5eb4] | 214 | result[i] = values[0]*result[i]/norm + values[1]; |
---|
[2e44ac7] | 215 | } |
---|
| 216 | } |
---|
[60eab2a] | 217 | #endif // MAX_PD > 0 |
---|
[2e44ac7] | 218 | } |
---|