bioem_algorithm.h 11 KB
 qon committed Apr 04, 2014 1 2 ``````#ifndef BIOEM_ALGORITHM_H #define BIOEM_ALGORITHM_H `````` qon committed Apr 06, 2014 3 4 5 6 7 8 9 10 11 12 ``````//#include #ifndef BIOEM_GPUCODE //#define SSECODE //Explicit SSE code, not correct yet since loop counter is assumed multiple of 4, anyway not faster than autovectorized code. #endif #ifdef SSECODE #include #include #endif `````` qon committed Apr 04, 2014 13 14 15 `````` template __device__ static inline void compareRefMap(const int iRefMap, const int iOrient, const int iConv, const bioem_map& Mapconv, bioem_Probability* pProb, const bioem_param_device& param, const RefT& RefMap, `````` David Rohr committed Apr 08, 2014 16 `````` const int cent_x, const int cent_y, const int myShift = 0, const int nShifts2 = 0, const int myRef = 0, const bool threadActive = true) `````` qon committed Apr 04, 2014 17 18 19 20 21 22 23 24 ``````{ /**************************************************************************************/ /********************** Calculating BioEM Probability ********************************/ /************************* Loop of center displacement here ***************************/ // Taking into account the center displacement /*** Inizialzing crosscorrelations of calculated projected convolutions ***/ `````` qon committed Apr 06, 2014 25 26 27 28 29 ``````#ifdef SSECODE myfloat_t sum, sumsquare, crossproMapConv; __m128 sum_v = _mm_setzero_ps(), sumsquare_v = _mm_setzero_ps(), cross_v = _mm_setzero_ps(), d1, d2; #else myfloat_t sum=0.0; `````` qon committed Apr 04, 2014 30 31 `````` myfloat_t sumsquare=0.0; myfloat_t crossproMapConv=0.0; `````` qon committed Apr 06, 2014 32 ``````#endif `````` qon committed Apr 04, 2014 33 `````` /****** Loop over Pixels to calculate dot product and cross-correlations of displaced Ref Conv. Map***/ `````` David Rohr committed Apr 08, 2014 34 `````` myfloat_t logpro; `````` David Rohr committed Apr 09, 2014 35 `````` if (GPUAlgo != 2 || threadActive) `````` qon committed Apr 04, 2014 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 `````` { int iStart, jStart, iEnd, jEnd; if (cent_x < 0) { iStart = -cent_x; iEnd = param.NumberPixels; } else { iStart = 0; iEnd = param.NumberPixels - cent_x; } if (cent_y < 0) { jStart = -cent_y; jEnd = param.NumberPixels; } else { jStart = 0; jEnd = param.NumberPixels - cent_y; } for (int i = iStart; i < iEnd; i += 1) { `````` qon committed Apr 06, 2014 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 ``````#ifdef SSECODE const float* ptr1 = &Mapconv.points[i + cent_x][jStart + cent_y]; const float* ptr2 = RefMap.getp(iRefMap, i, jStart); int j; const int count = jEnd - jStart; for (j = 0;j <= count - 4;j += 4) { d1 = _mm_loadu_ps(ptr1); d2 = _mm_loadu_ps(ptr2); sum_v = _mm_add_ps(sum_v, d1); sumsquare_v = _mm_add_ps(sumsquare_v, _mm_mul_ps(d1, d1)); cross_v = _mm_add_ps(cross_v, _mm_mul_ps(d1, d2)); ptr1 += 4; ptr2 += 4; } #else `````` qon committed Apr 04, 2014 77 78 `````` for (int j = jStart; j < jEnd; j += 1) { `````` qon committed Apr 06, 2014 79 `````` const myfloat_t pointMap = Mapconv.points[i + cent_x][j + cent_y]; `````` qon committed Apr 04, 2014 80 81 82 83 84 85 86 `````` const myfloat_t pointRefMap = RefMap.get(iRefMap, i, j); crossproMapConv += pointMap * pointRefMap; // Crosscorrelation of calculated displaced map sum += pointMap; // Calculate Sum of pixels squared sumsquare += pointMap*pointMap; } `````` qon committed Apr 06, 2014 87 ``````#endif `````` qon committed Apr 04, 2014 88 `````` } `````` qon committed Apr 06, 2014 89 90 91 92 93 94 95 96 97 98 99 ``````#ifdef SSECODE sum_v = _mm_hadd_ps(sum_v, sum_v); sumsquare_v = _mm_hadd_ps(sumsquare_v, sumsquare_v); cross_v = _mm_hadd_ps(cross_v, cross_v); sum_v = _mm_hadd_ps(sum_v, sum_v); sumsquare_v = _mm_hadd_ps(sumsquare_v, sumsquare_v); cross_v = _mm_hadd_ps(cross_v, cross_v); sum = _mm_cvtss_f32(sum_v); sumsquare = _mm_cvtss_f32(sumsquare_v); crossproMapConv = _mm_cvtss_f32(cross_v); #endif `````` David Rohr committed Apr 09, 2014 100 `````` `````` David Rohr committed Apr 08, 2014 101 102 103 `````` /********** Calculating elements in BioEM Probability formula ********/ // Related to Reference calculated Projection const myfloat_t ForLogProb = (sumsquare * param.Ntotpi - sum * sum); `````` qon committed Apr 04, 2014 104 `````` `````` David Rohr committed Apr 08, 2014 105 106 107 `````` // Products of different cross-correlations (first element in formula) const myfloat_t firstele = param.Ntotpi * (RefMap.sumsquare_RefMap[iRefMap] * sumsquare-crossproMapConv * crossproMapConv) + 2 * RefMap.sum_RefMap[iRefMap] * sum * crossproMapConv - RefMap.sumsquare_RefMap[iRefMap] * sum * sum - RefMap.sum_RefMap[iRefMap] * RefMap.sum_RefMap[iRefMap] * sumsquare; `````` qon committed Apr 04, 2014 108 `````` `````` David Rohr committed Apr 08, 2014 109 110 111 112 113 114 115 116 `````` //******* Calculating log of Prob*********/ // As in fortran code: logpro=(3-Ntotpi)*0.5*log(firstele/pConvMap[iOrient].ForLogProbfromConv[iConv])+(Ntotpi*0.5-2)*log(Ntotpi-2)-0.5*log(pConvMap[iOrient].ForLogProbfromConv[iConv])+0.5*log(PI)+(1-Ntotpi*0.5)*(log(2*PI)+1); logpro = (3 - param.Ntotpi) * 0.5 * log(firstele) + (param.Ntotpi * 0.5 - 2) * log((param.Ntotpi - 2) * ForLogProb); } else { logpro = 0; } `````` qon committed Apr 04, 2014 117 118 119 120 121 122 123 124 `````` #ifdef BIOEM_GPUCODE if (GPUAlgo == 2) { extern __shared__ myfloat_t buf[]; myfloat_t* buf2 = &buf[myBlockDimX]; myfloat_t* buf3 = &buf2[myBlockDimX + 4 * myRef]; int* bufint = (int*) buf3; `````` David Rohr committed Apr 09, 2014 125 `````` `````` qon committed Apr 04, 2014 126 127 128 129 130 131 `````` buf[myThreadIdxX] = logpro; if (myShift == 0) { bufint[0] = 0; } __syncthreads(); `````` David Rohr committed Apr 09, 2014 132 `````` `````` qon committed Apr 04, 2014 133 134 135 136 137 `````` if (nShifts2 == CUDA_MAX_SHIFT_REDUCE) // 1024 { if (myShift < 512) if (buf[myThreadIdxX + 512] > buf[myThreadIdxX]) buf[myThreadIdxX] = buf[myThreadIdxX + 512]; __syncthreads(); } `````` David Rohr committed Apr 09, 2014 138 `````` `````` qon committed Apr 04, 2014 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 `````` if (nShifts2 >= 512) { if (myShift < 256) if (buf[myThreadIdxX + 256] > buf[myThreadIdxX]) buf[myThreadIdxX] = buf[myThreadIdxX + 256]; __syncthreads(); } if (nShifts2 >= 256) { if (myShift < 128) if (buf[myThreadIdxX + 128] > buf[myThreadIdxX]) buf[myThreadIdxX] = buf[myThreadIdxX + 128]; __syncthreads(); } if (nShifts2 >= 128) { if (myShift < 64) if (buf[myThreadIdxX + 64] > buf[myThreadIdxX]) buf[myThreadIdxX] = buf[myThreadIdxX + 64]; __syncthreads(); } if (myShift < 32) //Warp Size is 32, threads are synched automatically { volatile float* vbuf = buf; //Mem must be volatile such that memory access is not reordered if (nShifts2 >= 64 && vbuf[myThreadIdxX + 32] > vbuf[myThreadIdxX]) vbuf[myThreadIdxX] = vbuf[myThreadIdxX + 32]; if (nShifts2 >= 32 && vbuf[myThreadIdxX + 16] > vbuf[myThreadIdxX]) vbuf[myThreadIdxX] = vbuf[myThreadIdxX + 16]; if (nShifts2 >= 16 && vbuf[myThreadIdxX + 8] > vbuf[myThreadIdxX]) vbuf[myThreadIdxX] = vbuf[myThreadIdxX + 8]; if (nShifts2 >= 8 && vbuf[myThreadIdxX + 4] > vbuf[myThreadIdxX]) vbuf[myThreadIdxX] = vbuf[myThreadIdxX + 4]; if (nShifts2 >= 4 && vbuf[myThreadIdxX + 2] > vbuf[myThreadIdxX]) vbuf[myThreadIdxX] = vbuf[myThreadIdxX + 2]; if (nShifts2 >= 2 && vbuf[myThreadIdxX + 1] > vbuf[myThreadIdxX]) vbuf[myThreadIdxX] = vbuf[myThreadIdxX + 1]; if (myShift == 0 && iRefMap < RefMap.ntotRefMap) { const myfloat_t logpro_max = vbuf[myThreadIdxX]; if(pProb[iRefMap].Constoadd < logpro_max) { pProb[iRefMap].Total = pProb[iRefMap].Total * exp(-logpro_max + pProb[iRefMap].Constoadd); pProb[iRefMap].Constoadd = logpro_max; } if(pProb[iRefMap].ConstAngle[iOrient] < logpro_max) { pProb[iRefMap].forAngles[iOrient] = pProb[iRefMap].forAngles[iOrient] * exp(-logpro_max + pProb[iRefMap].ConstAngle[iOrient]); pProb[iRefMap].ConstAngle[iOrient] = logpro_max; } if(pProb[iRefMap].max_prob < logpro_max) { pProb[iRefMap].max_prob = logpro_max; pProb[iRefMap].max_prob_orient = iOrient; pProb[iRefMap].max_prob_conv = iConv; bufint[0] = 1; buf3[1] = logpro_max; } } } `````` David Rohr committed Apr 09, 2014 189 `````` `````` qon committed Apr 04, 2014 190 191 192 193 194 195 `````` __syncthreads(); if (bufint[0] == 1 && buf3[1] == logpro && iRefMap < RefMap.ntotRefMap && atomicAdd(&bufint[0], 1) == 1) { pProb[iRefMap].max_prob_cent_x = cent_x; pProb[iRefMap].max_prob_cent_y = cent_y; } `````` David Rohr committed Apr 09, 2014 196 `````` `````` qon committed Apr 04, 2014 197 `````` __syncthreads(); `````` David Rohr committed Apr 09, 2014 198 `````` `````` qon committed Apr 04, 2014 199 200 201 202 203 204 `````` if (iRefMap < RefMap.ntotRefMap) { buf[myThreadIdxX] = exp(logpro - pProb[iRefMap].Constoadd); buf2[myThreadIdxX] = exp(logpro - pProb[iRefMap].ConstAngle[iOrient]); } __syncthreads(); `````` David Rohr committed Apr 09, 2014 205 `````` `````` qon committed Apr 04, 2014 206 207 208 209 210 211 212 213 214 `````` if (nShifts2 == CUDA_MAX_SHIFT_REDUCE) // 1024 { if (myShift < 512) { buf[myThreadIdxX] += buf[myThreadIdxX + 512]; buf2[myThreadIdxX] += buf2[myThreadIdxX + 512]; } __syncthreads(); } `````` David Rohr committed Apr 09, 2014 215 `````` `````` qon committed Apr 04, 2014 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 `````` if (nShifts2 >= 512) { if (myShift < 256) { buf[myThreadIdxX] += buf[myThreadIdxX + 256]; buf2[myThreadIdxX] += buf2[myThreadIdxX + 256]; } __syncthreads(); } if (nShifts2 >= 256) { if (myShift < 128) { buf[myThreadIdxX] += buf[myThreadIdxX + 128]; buf2[myThreadIdxX] += buf2[myThreadIdxX + 128]; } __syncthreads(); } if (nShifts2 >= 128) { if (myShift < 64) { buf[myThreadIdxX] += buf[myThreadIdxX + 64]; buf2[myThreadIdxX] += buf2[myThreadIdxX + 64]; } __syncthreads(); } if (myShift < 32) //Warp Size is 32, threads are synched automatically { volatile float* vbuf = buf; //Mem must be volatile such that memory access is not reordered volatile float* vbuf2 = buf2; if (nShifts2 >= 64) { vbuf[myThreadIdxX] += vbuf[myThreadIdxX + 32]; vbuf2[myThreadIdxX] += vbuf2[myThreadIdxX + 32]; } if (nShifts2 >= 32) { vbuf[myThreadIdxX] += vbuf[myThreadIdxX + 16]; vbuf2[myThreadIdxX] += vbuf2[myThreadIdxX + 16]; } if (nShifts2 >= 16) { vbuf[myThreadIdxX] += vbuf[myThreadIdxX + 8]; vbuf2[myThreadIdxX] += vbuf2[myThreadIdxX + 8]; } if (nShifts2 >= 8) { vbuf[myThreadIdxX] += vbuf[myThreadIdxX + 4]; vbuf2[myThreadIdxX] += vbuf2[myThreadIdxX + 4]; } if (nShifts2 >= 4) { vbuf[myThreadIdxX] += vbuf[myThreadIdxX + 2]; vbuf2[myThreadIdxX] += vbuf2[myThreadIdxX + 2]; } if (nShifts2 >= 2) { vbuf[myThreadIdxX] += vbuf[myThreadIdxX + 1]; vbuf2[myThreadIdxX] += vbuf2[myThreadIdxX + 1]; } if (myShift == 0 && iRefMap < RefMap.ntotRefMap) { pProb[iRefMap].Total += vbuf[myThreadIdxX]; pProb[iRefMap].forAngles[iOrient] += vbuf2[myThreadIdxX]; } } } else #endif /***** Summing & Storing total/Orientation Probabilites for each map ************/ { /******* Summing total Probabilities *************/ /******* Need a constant because of numerical divergence*****/ if(pProb[iRefMap].Constoadd < logpro) { pProb[iRefMap].Total = pProb[iRefMap].Total * exp(-logpro + pProb[iRefMap].Constoadd); pProb[iRefMap].Constoadd = logpro; } pProb[iRefMap].Total += exp(logpro - pProb[iRefMap].Constoadd); //Summing probabilities for each orientation if(pProb[iRefMap].ConstAngle[iOrient] < logpro) { pProb[iRefMap].forAngles[iOrient] = pProb[iRefMap].forAngles[iOrient] * exp(-logpro + pProb[iRefMap].ConstAngle[iOrient]); pProb[iRefMap].ConstAngle[iOrient] = logpro; } pProb[iRefMap].forAngles[iOrient] += exp(logpro - pProb[iRefMap].ConstAngle[iOrient]); /********** Getting parameters that maximize the probability ***********/ if(pProb[iRefMap].max_prob < logpro) { pProb[iRefMap].max_prob = logpro; pProb[iRefMap].max_prob_cent_x = cent_x; pProb[iRefMap].max_prob_cent_y = cent_y; pProb[iRefMap].max_prob_orient = iOrient; pProb[iRefMap].max_prob_conv = iConv; } } } template __device__ static inline void compareRefMapShifted(const int iRefMap, const int iOrient, const int iConv, const bioem_map& Mapconv, bioem_Probability* pProb, const bioem_param_device& param, const RefT& RefMap) { for (int cent_x = -param.maxDisplaceCenter; cent_x <= param.maxDisplaceCenter; cent_x=cent_x+param.GridSpaceCenter) { for (int cent_y = -param.maxDisplaceCenter; cent_y <= param.maxDisplaceCenter; cent_y=cent_y+param.GridSpaceCenter) { compareRefMap(iRefMap, iOrient, iConv, Mapconv, pProb, param, RefMap, cent_x, cent_y); } } } #endif``````