Changeset 33652 for trunk/Ohana/src/relastro/src/UpdateChips.c
- Timestamp:
- Apr 1, 2012, 3:00:19 PM (14 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/relastro/src/UpdateChips.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/relastro/src/UpdateChips.c
r32346 r33652 1 1 # include "relastro.h" 2 2 3 int plotChipFits (double *Ro, double *Do, char *mode, int Nimage); 3 4 int saveCenter (Image *image, double *Ro, double *Do, int im); 4 5 off_t getNextImageForThread (); 6 int UpdateChips_threaded (Catalog *catalog, int Ncatalog); 7 void *UpdateChips_worker (void *data); 8 9 enum {THREAD_RUN, THREAD_DONE}; 10 11 typedef struct { 12 int entry; 13 int state; 14 double *Ro; 15 double *Do; 16 char *mode; 17 off_t Nskip; 18 off_t Nmosaic; 19 off_t NnewFit; 20 off_t NoldFit; 21 Catalog *catalog; 22 int Ncatalog; 23 } ThreadInfo; 24 25 static Image *image = NULL; 26 static off_t Nimage = 0; 27 static off_t nextImage = 0; 28 29 // update astrometry of all chips relative to the average positions 30 // if NTHREADS is non-zero, call the threaded version of this function 5 31 int UpdateChips (Catalog *catalog, int Ncatalog) { 6 32 7 int Nskip, Nmosaic, NnewFit, NoldFit;33 off_t Nskip, Nmosaic, NnewFit, NoldFit; 8 34 9 35 /* we can measure new image parameters for each non-mosaic chip independently */ 10 off_t i, Nimage, Nraw, Nref, nFitAstr; 11 Image *image; 36 off_t i, Nraw, Nref, nFitAstr; 12 37 StarData *raw, *ref; 13 38 Coords *oldCoords; … … 16 41 char *mode; 17 42 43 if (NTHREADS) { 44 UpdateChips_threaded (catalog, Ncatalog); 45 return TRUE; 46 } 47 18 48 Nskip = Nmosaic = NnewFit = NoldFit = 0; 19 49 … … 25 55 ALLOCATE (mode, char, Nimage); 26 56 57 // XXX for faster processing in the future, this can be easily run in parallel 58 // each chip is fitted independently, so we could do N at once in parallel 27 59 for (i = 0; i < Nimage; i++) { 60 61 // XXX looks like everything below is thread safe : we can unroll this into a set of 62 // helper functions that grab the next available chip.... 28 63 29 64 /* skip all except WRP images */ … … 47 82 Nskip ++; 48 83 mode[i] = 0; 84 free (raw); 49 85 continue; 50 86 } … … 62 98 // fprintf (stderr, "image "OFF_T_FMT" : Nstars: "OFF_T_FMT"\n", i, Nraw); 63 99 if (!FitChip (raw, ref, Nraw, &image[i])) { 64 if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT" \n", image[i].name, i, Nraw);100 if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT" of %d\n", image[i].name, i, Nraw, image[i].nstar); 65 101 66 102 // restore status quo ante … … 105 141 106 142 plotChipFits (Ro, Do, mode, Nimage); 107 108 fprintf (stderr, "UpdateChips: %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", NnewFit, NoldFit, Nskip, Nmosaic); 143 free (Ro); 144 free (Do); 145 free (mode); 146 147 fprintf (stderr, "UpdateChips: %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", 148 (int) NnewFit, (int) NoldFit, (int) Nskip, (int) Nmosaic); 109 149 return (TRUE); 110 150 } 111 151 152 int UpdateChips_threaded (Catalog *catalog, int Ncatalog) { 153 154 int i; 155 off_t Nskip, Nmosaic, NnewFit, NoldFit; 156 double *Ro, *Do; 157 char *mode; 158 159 image = getimages (&Nimage, NULL); 160 nextImage = 0; 161 162 // save fit results for summary plot 163 ALLOCATE (Ro, double, Nimage); 164 ALLOCATE (Do, double, Nimage); 165 ALLOCATE (mode, char, Nimage); 166 167 pthread_attr_t attr; 168 pthread_attr_init (&attr); 169 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 170 171 pthread_t *threads; 172 ALLOCATE (threads, pthread_t, NTHREADS); 173 174 ThreadInfo *threadinfo; 175 ALLOCATE (threadinfo, ThreadInfo, NTHREADS); 176 177 // launch N worker threads 178 for (i = 0; i < NTHREADS; i++) { 179 threadinfo[i].entry = i; 180 threadinfo[i].state = THREAD_RUN; 181 threadinfo[i].Ro = Ro; 182 threadinfo[i].Do = Do; 183 threadinfo[i].mode = mode; 184 threadinfo[i].Nskip = 0; 185 threadinfo[i].Nmosaic = 0; 186 threadinfo[i].NnewFit = 0; 187 threadinfo[i].NoldFit = 0; 188 threadinfo[i].catalog = catalog; 189 threadinfo[i].Ncatalog = Ncatalog; 190 pthread_create (&threads[i], NULL, UpdateChips_worker, &threadinfo[i]); 191 } 192 pthread_attr_destroy (&attr); 193 194 // wait until all threads have finished 195 while (1) { 196 int allDone = TRUE; 197 for (i = 0; i < NTHREADS; i++) { 198 if (threadinfo[i].state == THREAD_RUN) allDone = FALSE; 199 } 200 if (allDone) { 201 break; 202 } 203 usleep (500000); 204 } 205 206 // all threads are done, free the threads array and grab the info 207 free (threads); 208 209 plotChipFits (Ro, Do, mode, Nimage); 210 free (Ro); 211 free (Do); 212 free (mode); 213 214 // report stats & summary from the threads 215 Nskip = Nmosaic = NnewFit = NoldFit = 0; 216 for (i = 0; i < NTHREADS; i++) { 217 fprintf (stderr, "UpdateChips thread %d : %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", 218 i, (int) threadinfo[i].NnewFit, (int) threadinfo[i].NoldFit, (int) threadinfo[i].Nskip, (int) threadinfo[i].Nmosaic); 219 220 NnewFit += threadinfo[i].NnewFit; 221 NoldFit += threadinfo[i].NoldFit; 222 Nskip += threadinfo[i].Nskip; 223 Nmosaic += threadinfo[i].Nmosaic; 224 } 225 free (threadinfo); 226 227 fprintf (stderr, "UpdateChips: %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", 228 (int) NnewFit, (int) NoldFit, (int) Nskip, (int) Nmosaic); 229 return (TRUE); 230 } 231 232 void *UpdateChips_worker (void *data) { 233 234 /* we can measure new image parameters for each non-mosaic chip independently */ 235 off_t Nraw, Nref, nFitAstr; 236 StarData *raw, *ref; 237 Coords *oldCoords; 238 float dXpixSys, dYpixSys; 239 240 ThreadInfo *threadinfo = data; 241 242 while (1) { 243 244 off_t i = getNextImageForThread(); 245 if (i == -1) { 246 threadinfo->state = THREAD_DONE; 247 return NULL; 248 } 249 250 /* skip all except WRP images */ 251 if (strcmp(&image[i].coords.ctype[4], "-WRP")) { 252 threadinfo->Nmosaic ++; 253 threadinfo->mode[i] = 0; 254 continue; 255 } 256 257 /* convert measure coordinates to raw entries */ 258 raw = getImageRaw (threadinfo->catalog, threadinfo->Ncatalog, i, &Nraw, MODE_MOSAIC); 259 if (!raw) { 260 threadinfo->Nskip ++; 261 threadinfo->mode[i] = 0; 262 continue; 263 } 264 265 /* convert average coordinates to ref entries */ 266 ref = getImageRef (threadinfo->catalog, threadinfo->Ncatalog, i, &Nref, MODE_MOSAIC); 267 if (!ref) { 268 threadinfo->Nskip ++; 269 threadinfo->mode[i] = 0; 270 free (raw); 271 continue; 272 } 273 274 // note that Nraw & Nref must be equal: if not, we made a programming error in one of these two functions. 275 assert (Nraw == Nref); 276 277 // save these in case of failure 278 saveCoords (&image[i].coords, i); 279 dXpixSys = image[i].dXpixSys; 280 dYpixSys = image[i].dYpixSys; 281 nFitAstr = image[i].nFitAstrom; 282 283 // FitChip does iterative, clipped fitting 284 // fprintf (stderr, "image "OFF_T_FMT" : Nstars: "OFF_T_FMT"\n", i, Nraw); 285 if (!FitChip (raw, ref, Nraw, &image[i])) { 286 if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT" of %d\n", image[i].name, i, Nraw, image[i].nstar); 287 288 // restore status quo ante 289 oldCoords = getCoords (i); 290 memcpy (&image[i].coords, oldCoords, sizeof(Coords)); 291 image[i].dXpixSys = dXpixSys; 292 image[i].dYpixSys = dYpixSys; 293 image[i].nFitAstrom = nFitAstr; 294 295 saveCenter (image, &threadinfo->Ro[i], &threadinfo->Do[i], i); 296 threadinfo->mode[i] = 1; 297 threadinfo->NoldFit ++; 298 free (raw); 299 free (ref); 300 continue; 301 } 302 303 if (!checkStarMap (i)) { 304 if (VERBOSE) fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name, i, Nraw); 305 // restore status quo ante 306 oldCoords = getCoords (i); 307 memcpy (&image[i].coords, oldCoords, sizeof(Coords)); 308 image[i].dXpixSys = dXpixSys; 309 image[i].dYpixSys = dYpixSys; 310 image[i].nFitAstrom = nFitAstr; 311 312 saveCenter (image, &threadinfo->Ro[i], &threadinfo->Do[i], i); 313 threadinfo->mode[i] = 2; 314 image[i].flags |= ID_IMAGE_ASTROM_POOR; 315 threadinfo->NoldFit ++; 316 free (raw); 317 free (ref); 318 continue; 319 } 320 321 saveCenter (image, &threadinfo->Ro[i], &threadinfo->Do[i], i); 322 threadinfo->mode[i] = 3; 323 threadinfo->NnewFit ++; 324 free (raw); 325 free (ref); 326 } 327 328 // we should never reach here... 329 return NULL; 330 } 331 332 // mutex to lock UpdateChips_worker operations 333 static pthread_mutex_t UpdateChips_mutex = PTHREAD_MUTEX_INITIALIZER; 334 335 // we have an array of chips (image, Nimage). we need to hand out images one at a time to 336 // the worker threads as they need 337 off_t getNextImageForThread () { 338 339 pthread_mutex_lock (&UpdateChips_mutex); 340 if (nextImage >= Nimage) { 341 pthread_mutex_unlock (&UpdateChips_mutex); 342 return (-1); 343 } 344 off_t thisImage = nextImage; 345 nextImage ++; 346 347 pthread_mutex_unlock (&UpdateChips_mutex); 348 return (thisImage); 349 } 350 351 // This function uses the mosaic array in MosaicOps.c to associate PHU + CHIP this is 352 // thread safe : the lookup (getMosaicForImage) is selecting an element of a previously 353 // allocated and assigned static array. This function does NOT use the thread-unsafe (and 354 // somewhat slower) functions in libdvo/src/mosaic_astrom.c to associate CHIP and PHU 112 355 int saveCenter (Image *image, double *Ro, double *Do, int im) { 113 356 … … 136 379 // note that for a Simple image, L,M = P,Q 137 380 XY_to_LM (&L, &M, X, Y, imcoords); 138 LM_to_RD (&R, &D, L, M, imcoords); 381 LM_to_RD (&R, &D, L, M, imcoords); // because of the block above, ctype is not -WRP 139 382 } else { 140 383 XY_to_LM (&L, &M, X, Y, imcoords); 141 384 XY_to_LM (&P, &Q, L, M, moscoords); 142 LM_to_RD (&R, &D, P, Q, moscoords); 143 } 144 145 double Rmid; 146 if (UserCatalog) { 147 Rmid = UserCatalogRA; 148 } else { 149 Rmid = 0.5*(UserPatch.Rmin + UserPatch.Rmax); 150 } 385 LM_to_RD (&R, &D, P, Q, moscoords); // moscoords.ctype is -DIS 386 } 387 388 double Rmid = 0.5*(UserPatch.Rmin + UserPatch.Rmax); 151 389 152 390 R = ohana_normalize_angle_to_midpoint (R, Rmid);
Note:
See TracChangeset
for help on using the changeset viewer.
