IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33570


Ignore:
Timestamp:
Mar 20, 2012, 6:16:29 AM (14 years ago)
Author:
eugene
Message:

adding threaded option to UpdateChips

Location:
branches/eam_branches/ipp-20111122/Ohana/src/relastro
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20111122/Ohana/src/relastro/include/relastro.h

    r33567 r33570  
    44# include <signal.h>
    55# include <assert.h>
     6# include <pthread.h>
    67
    78// choose off_t or int depending on full-scale relphot analysis resources
     
    159160int    RESET;
    160161int    NLOOP;
     162int    NTHREADS;
    161163int    UPDATE;
    162164int    PLOTSTUFF;
  • branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/UpdateChips.c

    r33568 r33570  
    11# include "relastro.h"
     2
    23int plotChipFits (double *Ro, double *Do, char *mode, int Nimage);
    34int saveCenter (Image *image, double *Ro, double *Do, int im);
    4 
     5off_t getNextImageForThread ();
     6int UpdateChips_threaded (Catalog *catalog, int Ncatalog);
     7void *UpdateChips_worker (void *data);
     8
     9enum {THREAD_RUN, THREAD_DONE};
     10
     11typedef 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
     25static Image *image = NULL;
     26static off_t Nimage = 0;
     27static 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
    531int UpdateChips (Catalog *catalog, int Ncatalog) {
    632
    7   int Nskip, Nmosaic, NnewFit, NoldFit;
     33  off_t Nskip, Nmosaic, NnewFit, NoldFit;
    834
    935  /* 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;
    1237  StarData *raw, *ref;
    1338  Coords *oldCoords;
     
    1540  double *Ro, *Do;
    1641  char *mode;
     42
     43  if (NTHREADS) {
     44    UpdateChips_threaded (catalog, Ncatalog);
     45    return TRUE;
     46  }
    1747
    1848  Nskip = Nmosaic = NnewFit = NoldFit = 0;
     
    108138
    109139  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);
    112146  return (TRUE);
    113147}
    114148
     149int 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
     228void *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
     329static 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
     333off_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
    115351int saveCenter (Image *image, double *Ro, double *Do, int im) {
    116352
     
    139375    // note that for a Simple image, L,M = P,Q
    140376    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
    142378  } else {
    143379    XY_to_LM (&L, &M, X, Y, imcoords);
    144380    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
    146382  }
    147383
  • branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/args.c

    r33546 r33570  
    360360    remove_argument (N, &argc, argv);
    361361    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]);
    362369    remove_argument (N, &argc, argv);
    363370  }
Note: See TracChangeset for help on using the changeset viewer.