Changeset 33570
- Timestamp:
- Mar 20, 2012, 6:16:29 AM (14 years ago)
- Location:
- branches/eam_branches/ipp-20111122/Ohana/src/relastro
- Files:
-
- 3 edited
-
include/relastro.h (modified) (2 diffs)
-
src/UpdateChips.c (modified) (4 diffs)
-
src/args.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20111122/Ohana/src/relastro/include/relastro.h
r33567 r33570 4 4 # include <signal.h> 5 5 # include <assert.h> 6 # include <pthread.h> 6 7 7 8 // choose off_t or int depending on full-scale relphot analysis resources … … 159 160 int RESET; 160 161 int NLOOP; 162 int NTHREADS; 161 163 int UPDATE; 162 164 int PLOTSTUFF; -
branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/UpdateChips.c
r33568 r33570 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; … … 15 40 double *Ro, *Do; 16 41 char *mode; 42 43 if (NTHREADS) { 44 UpdateChips_threaded (catalog, Ncatalog); 45 return TRUE; 46 } 17 47 18 48 Nskip = Nmosaic = NnewFit = NoldFit = 0; … … 108 138 109 139 plotChipFits (Ro, Do, mode, Nimage); 110 111 fprintf (stderr, "UpdateChips: %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", NnewFit, NoldFit, Nskip, Nmosaic); 140 free (Ro); 141 free (Do); 142 free (mode); 143 144 fprintf (stderr, "UpdateChips: %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", 145 (int) NnewFit, (int) NoldFit, (int) Nskip, (int) Nmosaic); 112 146 return (TRUE); 113 147 } 114 148 149 int UpdateChips_threaded (Catalog *catalog, int Ncatalog) { 150 151 int i; 152 off_t Nskip, Nmosaic, NnewFit, NoldFit; 153 double *Ro, *Do; 154 char *mode; 155 156 image = getimages (&Nimage, NULL); 157 158 // save fit results for summary plot 159 ALLOCATE (Ro, double, Nimage); 160 ALLOCATE (Do, double, Nimage); 161 ALLOCATE (mode, char, Nimage); 162 163 pthread_attr_t attr; 164 pthread_attr_init (&attr); 165 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 166 167 pthread_t *threads; 168 ALLOCATE (threads, pthread_t, NTHREADS); 169 170 ThreadInfo *threadinfo; 171 ALLOCATE (threadinfo, ThreadInfo, NTHREADS); 172 173 // launch N worker threads 174 for (i = 0; i < NTHREADS; i++) { 175 threadinfo[i].entry = i; 176 threadinfo[i].state = THREAD_RUN; 177 threadinfo[i].Ro = Ro; 178 threadinfo[i].Do = Do; 179 threadinfo[i].mode = mode; 180 threadinfo[i].Nskip = 0; 181 threadinfo[i].Nmosaic = 0; 182 threadinfo[i].NnewFit = 0; 183 threadinfo[i].NoldFit = 0; 184 threadinfo[i].catalog = catalog; 185 threadinfo[i].Ncatalog = Ncatalog; 186 pthread_create (&threads[i], NULL, UpdateChips_worker, &threadinfo[i]); 187 } 188 pthread_attr_destroy (&attr); 189 190 // wait until all threads have finished 191 while (1) { 192 int allDone = TRUE; 193 for (i = 0; i < NTHREADS; i++) { 194 if (threadinfo[i].state == THREAD_RUN) allDone = FALSE; 195 } 196 if (allDone) { 197 break; 198 } 199 usleep (500000); 200 } 201 202 // all threads are done, free the threads array and grab the info 203 free (threads); 204 205 plotChipFits (Ro, Do, mode, Nimage); 206 free (Ro); 207 free (Do); 208 free (mode); 209 210 // report stats & summary from the threads 211 Nskip = Nmosaic = NnewFit = NoldFit = 0; 212 for (i = 0; i < NTHREADS; i++) { 213 fprintf (stderr, "UpdateChips thread %d : %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", 214 i, (int) threadinfo[i].NnewFit, (int) threadinfo[i].NoldFit, (int) threadinfo[i].Nskip, (int) threadinfo[i].Nmosaic); 215 216 NnewFit += threadinfo[i].NnewFit; 217 NoldFit += threadinfo[i].NoldFit; 218 Nskip += threadinfo[i].Nskip; 219 Nmosaic += threadinfo[i].Nmosaic; 220 } 221 free (threadinfo); 222 223 fprintf (stderr, "UpdateChips: %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", 224 (int) NnewFit, (int) NoldFit, (int) Nskip, (int) Nmosaic); 225 return (TRUE); 226 } 227 228 void *UpdateChips_worker (void *data) { 229 230 /* we can measure new image parameters for each non-mosaic chip independently */ 231 off_t Nraw, Nref, nFitAstr; 232 StarData *raw, *ref; 233 Coords *oldCoords; 234 float dXpixSys, dYpixSys; 235 236 ThreadInfo *threadinfo = data; 237 238 while (1) { 239 240 off_t i = getNextImageForThread(); 241 if (i == -1) { 242 threadinfo->state = THREAD_DONE; 243 return NULL; 244 } 245 246 /* skip all except WRP images */ 247 if (strcmp(&image[i].coords.ctype[4], "-WRP")) { 248 threadinfo->Nmosaic ++; 249 threadinfo->mode[i] = 0; 250 continue; 251 } 252 253 /* convert measure coordinates to raw entries */ 254 raw = getImageRaw (threadinfo->catalog, threadinfo->Ncatalog, i, &Nraw, MODE_MOSAIC); 255 if (!raw) { 256 threadinfo->Nskip ++; 257 threadinfo->mode[i] = 0; 258 continue; 259 } 260 261 /* convert average coordinates to ref entries */ 262 ref = getImageRef (threadinfo->catalog, threadinfo->Ncatalog, i, &Nref, MODE_MOSAIC); 263 if (!ref) { 264 threadinfo->Nskip ++; 265 threadinfo->mode[i] = 0; 266 free (raw); 267 continue; 268 } 269 270 // note that Nraw & Nref must be equal: if not, we made a programming error in one of these two functions. 271 assert (Nraw == Nref); 272 273 // save these in case of failure 274 saveCoords (&image[i].coords, i); 275 dXpixSys = image[i].dXpixSys; 276 dYpixSys = image[i].dYpixSys; 277 nFitAstr = image[i].nFitAstrom; 278 279 // FitChip does iterative, clipped fitting 280 // fprintf (stderr, "image "OFF_T_FMT" : Nstars: "OFF_T_FMT"\n", i, Nraw); 281 if (!FitChip (raw, ref, Nraw, &image[i])) { 282 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); 283 284 // restore status quo ante 285 oldCoords = getCoords (i); 286 memcpy (&image[i].coords, oldCoords, sizeof(Coords)); 287 image[i].dXpixSys = dXpixSys; 288 image[i].dYpixSys = dYpixSys; 289 image[i].nFitAstrom = nFitAstr; 290 291 saveCenter (image, &threadinfo->Ro[i], &threadinfo->Do[i], i); 292 threadinfo->mode[i] = 1; 293 threadinfo->NoldFit ++; 294 free (raw); 295 free (ref); 296 continue; 297 } 298 299 if (!checkStarMap (i)) { 300 if (VERBOSE) fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name, i, Nraw); 301 // restore status quo ante 302 oldCoords = getCoords (i); 303 memcpy (&image[i].coords, oldCoords, sizeof(Coords)); 304 image[i].dXpixSys = dXpixSys; 305 image[i].dYpixSys = dYpixSys; 306 image[i].nFitAstrom = nFitAstr; 307 308 saveCenter (image, &threadinfo->Ro[i], &threadinfo->Do[i], i); 309 threadinfo->mode[i] = 2; 310 image[i].flags |= ID_IMAGE_ASTROM_POOR; 311 threadinfo->NoldFit ++; 312 free (raw); 313 free (ref); 314 continue; 315 } 316 317 saveCenter (image, &threadinfo->Ro[i], &threadinfo->Do[i], i); 318 threadinfo->mode[i] = 3; 319 threadinfo->NnewFit ++; 320 free (raw); 321 free (ref); 322 } 323 324 // we should never reach here... 325 return NULL; 326 } 327 328 // mutex to lock UpdateChips_worker operations 329 static pthread_mutex_t UpdateChips_mutex = PTHREAD_MUTEX_INITIALIZER; 330 331 // we have an array of chips (image, Nimage). we need to hand out images one at a time to 332 // the worker threads as they need 333 off_t getNextImageForThread () { 334 335 pthread_mutex_lock (&UpdateChips_mutex); 336 if (nextImage >= Nimage) { 337 pthread_mutex_unlock (&UpdateChips_mutex); 338 return (-1); 339 } 340 off_t thisImage = nextImage; 341 nextImage ++; 342 343 pthread_mutex_unlock (&UpdateChips_mutex); 344 return (thisImage); 345 } 346 347 // This function uses the mosaic array in MosaicOps.c to associate PHU + CHIP this is 348 // thread safe : the lookup (getMosaicForImage) is selecting an element of a previously 349 // allocated and assigned static array. This function does NOT use the thread-unsafe (and 350 // somewhat slower) functions in libdvo/src/mosaic_astrom.c to associate CHIP and PHU 115 351 int saveCenter (Image *image, double *Ro, double *Do, int im) { 116 352 … … 139 375 // note that for a Simple image, L,M = P,Q 140 376 XY_to_LM (&L, &M, X, Y, imcoords); 141 LM_to_RD (&R, &D, L, M, imcoords); 377 LM_to_RD (&R, &D, L, M, imcoords); // because of the block above, ctype is not -WRP 142 378 } else { 143 379 XY_to_LM (&L, &M, X, Y, imcoords); 144 380 XY_to_LM (&P, &Q, L, M, moscoords); 145 LM_to_RD (&R, &D, P, Q, moscoords); 381 LM_to_RD (&R, &D, P, Q, moscoords); // moscoords.ctype is -DIS 146 382 } 147 383 -
branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/args.c
r33546 r33570 360 360 remove_argument (N, &argc, argv); 361 361 NLOOP = atof (argv[N]); 362 remove_argument (N, &argc, argv); 363 } 364 365 NTHREADS = 0; 366 if ((N = get_argument (argc, argv, "-nthreads"))) { 367 remove_argument (N, &argc, argv); 368 NTHREADS = atof (argv[N]); 362 369 remove_argument (N, &argc, argv); 363 370 }
Note:
See TracChangeset
for help on using the changeset viewer.
