IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 20399


Ignore:
Timestamp:
Oct 26, 2008, 2:21:25 PM (18 years ago)
Author:
eugene
Message:

adding threading for psphotGuessModels (threads still clashing...)

Location:
branches/eam_branch_20081026/psphot
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branch_20081026/psphot/doc/notes.txt

    r18555 r20399  
     1
     22008.10.26
     3
     4  (note: this is unoptimized)
     5  0 threads: built models for 3921 objects: 15.223455 sec
     6             built models for 2868 objects: 10.698732 sec
     7
     8  2 threads: built models for 3921 objects: 37.529655 sec
     9             built models for 2862 objects: 28.142142 sec
     10
     11  (clearly, the threading is doing bad things, at least without optimization..)
     12
     13  old code (no threading): 
     14             built models for 3923 objects: 15.265046 sec
     15             built models for 2842 objects: 10.840331 sec
     16
     17  (so, the threaded version without threading is OK)
    118
    2192008.06.25
  • branches/eam_branch_20081026/psphot/src/Makefile.am

    r20079 r20399  
    1818
    1919libpsphot_la_SOURCES = \
     20        psphotSetThreads.c             \
    2021        psphotErrorCodes.c             \
    2122        psphotImageQuality.c           \
  • branches/eam_branch_20081026/psphot/src/psphot.c

    r14655 r20399  
    1212    psphotErrorRegister();              // register our error codes/messages
    1313    psphotModelClassInit ();            // load implementation-specific models
     14    psphotSetThreads ();
     15
    1416
    1517    // load command-line arguments, options, and system config data
  • branches/eam_branch_20081026/psphot/src/psphot.h

    r20080 r20399  
    1313#define PSPHOT_RECIPE_PSF_FAKE_ALLOW "PSF.FAKE.ALLOW" // Name for recipe component permitting fake PSFs
    1414
     15typedef struct {
     16    pmReadout *readout;
     17    psArray *sources;
     18    pmPSF *psf;
     19    psRegion *region;
     20    psMaskType maskVal;
     21    psMaskType markVal;
     22} psphotGuessModelForRegionArgs;
    1523
    1624// top-level psphot functions
     
    4149bool            psphotMomentsStats (pmReadout *readout, psMetadata *recipe, psArray *sources);
    4250bool            psphotFitSourcesLinear (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf, bool final);
    43 bool            psphotGuessModels (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf);
     51bool            psphotGuessModels (pmConfig *config, pmReadout *readout, psArray *sources, pmPSF *psf);
     52bool            psphotGuessModelForRegion (psphotGuessModelForRegionArgs *args);
    4453bool            psphotBlendFit (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf);
    4554bool            psphotReplaceUnfitSources (psArray *sources, psMaskType maskVal);
     
    5160bool            psphotExtendedSourceAnalysis (pmReadout *readout, psArray *sources, psMetadata *recipe);
    5261bool            psphotExtendedSourceFits (pmReadout *readout, psArray *sources, psMetadata *recipe);
     62bool            psphotSetThreads ();
    5363
    5464// used by psphotFindDetections
  • branches/eam_branch_20081026/psphot/src/psphotArguments.c

    r19869 r20399  
    2929    // these options override the PSPHOT recipe values loaded from recipe files
    3030    psMetadata *options = pmConfigRecipeOptions (config, PSPHOT_RECIPE);
     31
     32    // Number of threads
     33    if ((N = psArgumentGet(argc, argv, "-threads"))) {
     34        psArgumentRemove(N, &argc, argv);
     35        int nThreads = atoi(argv[N]);
     36        psMetadataAddS32(config->arguments, PS_LIST_TAIL, "NTHREADS", 0, "number of psphot threads", nThreads);
     37        psArgumentRemove(N, &argc, argv);
     38
     39        // create the thread pool with number of desired threads, supplying our thread launcher function
     40        // XXX need to determine the number of threads from the config data
     41        psThreadPoolInit (nThreads);
     42    }
     43    psphotSetThreads();
    3144
    3245    // run the test model (requires X,Y coordinate)
  • branches/eam_branch_20081026/psphot/src/psphotCleanup.c

    r14655 r20399  
    55    psFree (config);
    66
     7    psThreadPoolFinalize ();
    78    psTimerStop ();
    89    psMemCheckCorruption (stderr, true);
  • branches/eam_branch_20081026/psphot/src/psphotGuessModels.c

    r19881 r20399  
    11# include "psphotInternal.h"
     2
     3bool psphotChooseCellSizes (int *Cx, int *Cy, pmReadout *readout, int nThreads);
     4psphotGuessModelForRegionArgs *psphotGuessModelForRegionArgsAlloc();
     5bool psphotGuessModelForRegion (psphotGuessModelForRegionArgs *args);
    26
    37// A guess for when the moments aren't available
     
    1620}
    1721
     22# define NFILL 4
     23
    1824// construct an initial PSF model for each object
    19 bool psphotGuessModels (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf) {
     25bool psphotGuessModels (pmConfig *config, pmReadout *readout, psArray *sources, pmPSF *psf) {
    2026
    2127    bool status;
    2228
    2329    psTimerStart ("psphot");
     30
     31    // select the appropriate recipe information
     32    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     33    assert (recipe);
     34
     35    int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
     36    if (!status) {
     37        nThreads = 0;
     38    }
    2439
    2540    // bit-masks to test for good/bad pixels
     
    3752    psphotInitRadiusPSF (recipe, psf->type);
    3853
     54    // the strategy here is to divide the image into 2x2 blocks of cells and cycle through
     55    // the four discontiguous sets of cells, threading all within a set and blocking between
     56    // sets
     57
     58    // we divide the image region into 2*2 blocks of size Nx*Ny, the image will have
     59    // Cx*Cy blocks so that (2Nx)Cx = numCols, (2Ny)Cy = numRows.  We want to choose Cx and
     60    // Cy so that (2Nx)Cx * (2Ny)Cy = 4 * NFILL * nThreads -- each of the four sets of cells
     61    // has enough cells to allow NFILL cells for each thread (to better distribute heavy and
     62    // light load cells
     63   
     64    // choose Cx, Cy:
     65    int Cx = 1, Cy = 1;
     66    psphotChooseCellSizes (&Cx, &Cy, readout, nThreads);
     67
     68    // the array runs from readout->image->col0 to readout->image->col0 + readout->image->numCols
     69    int Xo = readout->image->col0;
     70    int Yo = readout->image->row0;
     71    int Nx = readout->image->numCols / (2*Cx);
     72    int Ny = readout->image->numRows / (2*Cy);
     73
     74    // we can thread all of the cells within a set, but need to block between sets
     75    for (int jy = 0; jy < 2; jy++) {
     76        for (int jx = 0; jx < 2; jx++) {
     77
     78            // generate jobs for all of the Cx*Cy cells in this set
     79            for (int ix = 0; ix < Cx; ix++) {
     80                for (int iy = 0; iy < Cy; iy++) {
     81               
     82                    int x0 = (2*ix + jx)*Nx + Xo;
     83                    int x1 = x0 + Nx;
     84
     85                    int y0 = (2*iy + jy)*Ny + Yo;
     86                    int y1 = y0 + Ny;
     87
     88                    if (readout->image->numCols + Xo - x1 < Nx) {
     89                        x1 = readout->image->numCols + Xo;
     90                    }
     91                    if (readout->image->numRows + Yo - y1 < Ny) {
     92                        y1 = readout->image->numRows + Yo;
     93                    }
     94
     95                    // fprintf (stderr, "launch %d,%d - %d,%d\n", x0, y0, x1, y1);
     96                   
     97                    psRegion *cellRegion = psRegionAlloc (x0, x1, y0, y1);
     98                    *cellRegion = psRegionForImage (readout->image, *cellRegion);
     99                    // if we got the math above right, this should be a NOP
     100                                                                             
     101                    psphotGuessModelForRegionArgs *args = psphotGuessModelForRegionArgsAlloc();
     102                    args->readout = psMemIncrRefCounter(readout);
     103                    args->sources = psMemIncrRefCounter(sources);
     104                    args->psf     = psMemIncrRefCounter(psf);
     105                    args->region  = psMemIncrRefCounter(cellRegion);
     106
     107                    args->maskVal = maskVal;
     108                    args->markVal = markVal;
     109
     110                    // allocate a job -- if threads are not defined, this just runs the job
     111                    psThreadJob *job = psThreadJobAlloc ("PSPHOT_GUESS_MODEL");
     112                    psArrayAdd(job->args, 1, args);
     113                    if (!psThreadJobAddPending(job)) {
     114                        psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     115                        return false;
     116                    }
     117                    psFree(cellRegion);
     118                    psFree(job);
     119                    psFree(args);
     120                }
     121            }
     122
     123            // wait for the threads to finish and manage results
     124            // wait here for the threaded jobs to finish
     125            // fprintf (stderr, "wait for threads (%d, %d)\n", jx, jy);
     126            if (!psThreadPoolWait (false)) {
     127                psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     128                return false;
     129            }
     130
     131            // we have only supplied one type of job, so we can assume the types here
     132            psThreadJob *job = NULL;
     133            while ((job = psThreadJobGetDone()) != NULL) {
     134                // we have no returned data from this operation
     135                if (job->args->n < 1) {
     136                    fprintf (stderr, "error with job\n");
     137                }
     138                psFree(job);
     139            }
     140        }
     141    }
     142   
     143    psLogMsg ("psphot.models", 4, "built models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot"));
     144    return true;
     145}
     146
     147static void psphotGuessModelForRegionArgsFree(psphotGuessModelForRegionArgs *args)
     148{
     149    psFree(args->readout);
     150    psFree(args->sources);
     151    psFree(args->psf);
     152    psFree(args->region);
     153    return;
     154}
     155
     156psphotGuessModelForRegionArgs *psphotGuessModelForRegionArgsAlloc()
     157{
     158    psphotGuessModelForRegionArgs *args = psAlloc(sizeof(psphotGuessModelForRegionArgs));
     159    psMemSetDeallocator(args, (psFreeFunc)psphotGuessModelForRegionArgsFree);
     160
     161    args->readout = NULL;
     162    args->sources = NULL;
     163    args->psf = NULL;
     164    args->region = NULL;
     165
     166    args->maskVal = 0;
     167    args->markVal = 0;
     168    return args;
     169}
     170
     171// construct models only for sources in the specified region
     172bool psphotGuessModelForRegion (psphotGuessModelForRegionArgs *args) {
     173
     174    pmReadout *readout = args->readout;
     175    psArray *sources = args->sources;
     176    pmPSF *psf = args->psf;
     177    psRegion *region = args->region;
     178    psMaskType maskVal = args->maskVal;
     179    psMaskType markVal = args->markVal;
     180
     181    int nSrc = 0;
     182
    39183    for (int i = 0; i < sources->n; i++) {
    40184        pmSource *source = sources->data[i];
     
    43187        if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
    44188        if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
    45 
     189        if (!source->peak) continue;
     190
     191        if (source->peak->xf <  region->x0) continue;
     192        if (source->peak->yf <  region->y0) continue;
     193        if (source->peak->xf >= region->x1) continue;
     194        if (source->peak->yf >= region->y1) continue;
     195
     196        nSrc ++;
     197       
    46198        // XXX if a source is faint, it will not have moments measured.
    47199        // it must be modelled as a PSF.  In this case, we need to use
     
    53205        } else {
    54206            // use the source moments, etc to guess basic model parameters
    55             modelEXT = pmSourceModelGuess (source, psf->type);
     207            modelEXT = pmSourceModelGuess (source, psf->type); // ALLOC
    56208            if (!modelEXT) {
    57209                modelEXT = wildGuess(source, psf);
     
    68220
    69221        // set PSF parameters for this model (apply 2D shape model)
    70         pmModel *modelPSF = pmModelFromPSF (modelEXT, psf);
     222        pmModel *modelPSF = pmModelFromPSF (modelEXT, psf); // ALLOC
    71223        if (modelPSF == NULL) {
    72224            psError(PSPHOT_ERR_PSF, false,
     
    92244        // XXX need to define the guess flux?
    93245        // set the fit radius based on the object flux limit and the model
     246        // this function affects the mask pixels
    94247        psphotCheckRadiusPSF (readout, source, modelPSF, markVal);
    95248
     
    99252
    100253        pmSourceCacheModel (source, maskVal);
    101     }
    102     psLogMsg ("psphot.models", 4, "built models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot"));
     254
     255    }
     256
     257    // fprintf (stderr, "%d for region %lf,%lf - %lf,%lf\n", nSrc, region->x0, region->y0, region->x1, region->y1);
    103258    return true;
    104259}
    105260
    106 // XXX do we always know which model is supposed to be used?
     261bool psphotChooseCellSizes (int *Cx, int *Cy, pmReadout *readout, int nThreads) {
     262
     263    int nCells = nThreads * NFILL; // number of cells in a single set
     264    int C = sqrt(nCells) + 0.5;
     265   
     266    // we need to assign Cx and Cy based on the dimensionality of the image
     267    // crude way to find most evenly balanced factors of nCells:
     268    for (int i = C; i >= 1; i--) {
     269        int C1 = nCells / C;
     270        int C2 = nCells / C1;
     271        if (C1*C2 != nCells) continue;
     272
     273        if (readout->image->numRows > readout->image->numCols) {
     274            *Cx = PS_MAX (C1, C2);
     275            *Cy = PS_MIN (C1, C2);
     276        } else {
     277            *Cx = PS_MAX (C1, C2);
     278            *Cy = PS_MIN (C1, C2);
     279        }
     280        return true;
     281    }
     282    *Cx = 1;
     283    *Cy = 1;
     284
     285    return true;
     286}
  • branches/eam_branch_20081026/psphot/src/psphotReadout.c

    r20171 r20399  
    149149
    150150    // construct an initial model for each object
    151     psphotGuessModels (readout, sources, recipe, psf);
     151    psphotGuessModels (config, readout, sources, psf);
    152152
    153153    // XXX test output of models
     
    207207
    208208    // create full input models
    209     psphotGuessModels (readout, newSources, recipe, psf);
     209    psphotGuessModels (config, readout, newSources, psf);
    210210
    211211    // replace all sources so fit below applies to all at once
Note: See TracChangeset for help on using the changeset viewer.