IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 24, 2009, 10:54:29 AM (17 years ago)
Author:
eugene
Message:

clean up threading model for psphotGuessModel (defined standard way to split sources into regions); extending the threading to psphotMagnitudes, psphotApReset, psphotBlendFit, psphotSourceStats

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/psphotSourceStats.c

    r20453 r21166  
    11# include "psphotInternal.h"
    22
    3 psArray *psphotSourceStats (pmReadout *readout, psMetadata *recipe, pmDetections *detections)
    4 {
    5     bool     status = false;
     3psArray *psphotSourceStats (pmConfig *config, pmReadout *readout, pmDetections *detections) {
     4
     5    bool status = false;
    66    psArray *sources = NULL;
    7     float BIG_RADIUS;
    87
    98    psTimerStart ("psphot.stats");
    109
    11     // bit-masks to test for good/bad pixels
    12     psMaskType maskVal = psMetadataLookupU8(&status, recipe, "MASK.PSPHOT");
    13     assert (maskVal);
    14 
    15     // bit-mask to mark pixels not used in analysis
    16     psMaskType markVal = psMetadataLookupU8(&status, recipe, "MARK.PSPHOT");
    17     assert (markVal);
    18 
    19     // maskVal is used to test for rejected pixels, and must include markVal
    20     maskVal |= markVal;
     10    // select the appropriate recipe information
     11    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     12    assert (recipe);
     13
     14    // determine the number of allowed threads
     15    int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
     16    if (!status) {
     17        nThreads = 0;
     18    }
     19    // nThreads = 0; // XXX until testing is complete, do not thread this function
    2120
    2221    // determine properties (sky, moments) of initial sources
    23     float INNER    = psMetadataLookupF32 (&status, recipe, "SKY_INNER_RADIUS");
    24     if (!status) return NULL;
    2522    float OUTER    = psMetadataLookupF32 (&status, recipe, "SKY_OUTER_RADIUS");
    26     if (!status) return NULL;
    27     float RADIUS   = psMetadataLookupF32 (&status, recipe, "PSF_MOMENTS_RADIUS");
    28     if (!status) return NULL;
    29     float MIN_SN   = psMetadataLookupF32 (&status, recipe, "MOMENTS_SN_MIN");
    3023    if (!status) return NULL;
    3124    char *breakPt  = psMetadataLookupStr (&status, recipe, "BREAK_POINT");
     
    3831    }
    3932
     33    // generate the array of sources, define the associated pixel
    4034    sources = psArrayAllocEmpty (peaks->n);
    4135
     36    for (int i = 0; i < peaks->n; i++) {
     37
     38        pmPeak *peak = peaks->data[i];
     39        if (peak->assigned) continue;
     40
     41        // create a new source
     42        pmSource *source = pmSourceAlloc();
     43
     44        // add the peak
     45        source->peak = psMemIncrRefCounter(peak);
     46
     47        // allocate space for moments
     48        source->moments = pmMomentsAlloc();
     49
     50        // allocate image, weight, mask arrays for each peak (square of radius OUTER)
     51        pmSourceDefinePixels (source, readout, source->peak->x, source->peak->y, OUTER);
     52
     53        peak->assigned = true;
     54        psArrayAdd (sources, 100, source);
     55        psFree (source);
     56    }
     57
     58    if (!strcasecmp (breakPt, "PEAKS")) {
     59        psLogMsg ("psphot", PS_LOG_INFO, "%ld sources : %f sec\n", sources->n, psTimerMark ("psphot.stats"));
     60        psLogMsg ("psphot", PS_LOG_INFO, "break point PEAKS, skipping MOMENTS\n");
     61        psphotVisualShowMoments (sources);
     62        return sources;
     63    }
     64
     65    // threaded measurement of the source magnitudes
    4266    int Nfail = 0;
    4367    int Nmoments = 0;
    44     for (int i = 0; i < peaks->n; i++) {
    45 
    46         pmPeak *peak = peaks->data[i];
    47         if (peak->assigned) continue;
    48 
    49         // create a new source, add peak
    50         pmSource *source = pmSourceAlloc();
    51         source->peak = psMemIncrRefCounter(peak);
    52 
    53         // allocate image, weight, mask arrays for each peak (square of radius OUTER)
    54         pmSourceDefinePixels (source, readout, source->peak->x, source->peak->y, OUTER);
    55         if (!strcasecmp (breakPt, "PEAKS")) {
    56             peak->assigned = true;
    57             psArrayAdd (sources, 100, source);
    58             psFree (source);
    59             continue;
    60         }
     68
     69    // choose Cx, Cy (see psphotThreadTools.c for overview of the concepts)
     70    int Cx = 1, Cy = 1;
     71    psphotChooseCellSizes (&Cx, &Cy, readout, nThreads);
     72
     73    psArray *cellGroups = psphotAssignSources (Cx, Cy, sources);
     74
     75    for (int i = 0; i < cellGroups->n; i++) {
     76
     77        psArray *cells = cellGroups->data[i];
     78
     79        for (int j = 0; j < cells->n; j++) {
     80
     81            // allocate a job -- if threads are not defined, this just runs the job
     82            psThreadJob *job = psThreadJobAlloc ("PSPHOT_SOURCE_STATS");
     83
     84            psArrayAdd(job->args, 1, cells->data[j]); // sources
     85            psArrayAdd(job->args, 1, recipe);
     86            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nmoments
     87            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nfail
     88
     89            if (!psThreadJobAddPending(job)) {
     90                psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     91                psFree (job);
     92                return NULL;
     93            }
     94            psFree(job);
     95
     96        }
     97
     98        // wait for the threads to finish and manage results
     99        if (!psThreadPoolWait (false)) {
     100            psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     101            return NULL;
     102        }
     103
     104        // we have only supplied one type of job, so we can assume the types here
     105        psThreadJob *job = NULL;
     106        while ((job = psThreadJobGetDone()) != NULL) {
     107            if (job->args->n < 1) {
     108                fprintf (stderr, "error with job\n");
     109            } else {
     110                psScalar *scalar = NULL;
     111                scalar = job->args->data[2];
     112                Nmoments += scalar->data.S32;
     113                scalar = job->args->data[3];
     114                Nfail += scalar->data.S32;
     115            }
     116            psFree(job);
     117        }
     118    }
     119
     120    psFree (cellGroups);
     121
     122    psLogMsg ("psphot", PS_LOG_INFO, "%ld sources, %d moments, %d failed: %f sec\n", sources->n, Nmoments, Nfail, psTimerMark ("psphot.stats"));
     123
     124    psphotVisualShowMoments (sources);
     125
     126    return (sources);
     127}
     128
     129bool psphotSourceStats_Threaded (psThreadJob *job) {
     130
     131    bool status = false;
     132    float BIG_RADIUS;
     133    psScalar *scalar = NULL;
     134
     135    psArray *sources                = job->args->data[0];
     136    psMetadata *recipe              = job->args->data[1];
     137
     138    float INNER    = psMetadataLookupF32 (&status, recipe, "SKY_INNER_RADIUS");
     139    if (!status) return false;
     140    float MIN_SN   = psMetadataLookupF32 (&status, recipe, "MOMENTS_SN_MIN");
     141    if (!status) return false;
     142    float RADIUS   = psMetadataLookupF32 (&status, recipe, "PSF_MOMENTS_RADIUS");
     143    if (!status) return false;
     144
     145    // bit-masks to test for good/bad pixels
     146    psMaskType maskVal = psMetadataLookupU8(&status, recipe, "MASK.PSPHOT");
     147    assert (maskVal);
     148
     149    // bit-mask to mark pixels not used in analysis
     150    psMaskType markVal = psMetadataLookupU8(&status, recipe, "MARK.PSPHOT");
     151    assert (markVal);
     152
     153    // maskVal is used to test for rejected pixels, and must include markVal
     154    maskVal |= markVal;
     155
     156    // threaded measurement of the sources moments
     157    int Nfail = 0;
     158    int Nmoments = 0;
     159    for (int i = 0; i < sources->n; i++) {
     160        pmSource *source = sources->data[i];
    61161
    62162        // skip faint sources for moments measurement
    63163        if (source->peak->SN < MIN_SN) {
    64             peak->assigned = true;
    65             psArrayAdd (sources, 100, source);
    66             psFree (source);
    67164            continue;
    68165        }
     
    72169        status = pmSourceLocalSky (source, PS_STAT_SAMPLE_MEDIAN, INNER, maskVal, markVal);
    73170        if (!status) {
    74           psFree (source);
    75           Nfail ++;
    76           psErrorClear();
    77           continue;
     171            psErrorClear(); // XXX re-consider the errors raised here
     172            Nfail ++;
     173            continue;
    78174        }
    79175
     
    82178        status = pmSourceLocalSkyVariance (source, PS_STAT_SAMPLE_MEDIAN, INNER, maskVal, markVal);
    83179        if (!status) {
    84           psFree (source);
    85           Nfail ++;
    86           psErrorClear();
    87           continue;
     180            Nfail ++;
     181            psErrorClear();
     182            continue;
    88183        }
    89184
     
    91186        status = pmSourceMoments (source, RADIUS);
    92187        if (status) {
    93             // add to the source array
    94             peak->assigned = true;
    95             psArrayAdd (sources, 100, source);
    96             psFree (source);
    97188            Nmoments ++;
    98189            continue;
     
    105196        status = pmSourceMoments (source, BIG_RADIUS);
    106197        if (status) {
    107             // add to the source array
    108             peak->assigned = true;
    109             psArrayAdd (sources, 100, source);
    110             psFree (source);
    111198            Nmoments ++;
    112199            continue;
    113200        }
    114201
    115         psFree (source);
    116202        Nfail ++;
    117203        psErrorClear();
     
    119205    }
    120206
    121     psLogMsg ("psphot", PS_LOG_INFO, "%ld sources, %d moments, %d failed: %f sec\n", sources->n, Nmoments, Nfail, psTimerMark ("psphot.stats"));
    122 
    123     psphotVisualShowMoments (sources);
    124 
    125     return (sources);
     207    // change the value of a scalar on the array (wrap this and put it in psArray.h)
     208    scalar = job->args->data[2];
     209    scalar->data.S32 = Nmoments;
     210
     211    scalar = job->args->data[3];
     212    scalar->data.S32 = Nfail;
     213   
     214    return true;
    126215}
    127 
    128 // XXX EAM : filter out bad peaks (eg, no valid pixels available for sky)
Note: See TracChangeset for help on using the changeset viewer.