IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 21163


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

allow separate fitSets for separate threads

Location:
trunk/psModules/src/objects
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/objects/pmSourceFitSet.c

    r20937 r21163  
    66 *  @author GLG, MHPCC
    77 *
    8  *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2008-12-08 02:51:14 $
     8 *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2009-01-24 20:52:26 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4545/********************* Source Model Set Functions ***************************/
    4646
    47 static pmSourceFitSetData *thisSet = NULL;
     47// these functions currently need to use this static variable because of the way psMinimizeLMM
     48// is implemented.  We could re-work that structure, but for now it is probably easier to make
     49// this thread safe by pre-allocating separate static variables for each thread.
     50
     51static psArray *fitSets = NULL;
     52static pthread_mutex_t fitSetInitMutex = PTHREAD_MUTEX_INITIALIZER;
     53
     54// call this before launching the threads
     55bool pmSourceFitSetInit (int nThreads) {
     56
     57    if (!fitSets) {
     58        fitSets = psArrayAlloc (PS_MAX (1, nThreads));
     59    }
     60
     61    // the allocated elements should be NULL on psArrayAlloc,
     62    // and a previously allocated array of fitSets should have been cleared
     63    // before pmSourceFitSetInit is called
     64    for (int i = 0; i < fitSets->n; i++) {
     65        psAssert (fitSets->data[i] == NULL, "failure to init or clear fitSets?");
     66    }
     67    return true;
     68}
     69
     70void pmSourceFitSetDone () {
     71    psFree (fitSets);
     72}
    4873
    4974static void pmSourceFitSetDataFree (pmSourceFitSetData *set) {
     
    6287    psMemSetDeallocator(set, (psFreeFunc) pmSourceFitSetDataFree);
    6388
    64     set->modelSet = psMemIncrRefCounter (modelSet);
    65     set->paramSet = psArrayAlloc (modelSet->n);
    66     set->derivSet = psArrayAlloc (modelSet->n);
     89    set->modelSet  = psMemIncrRefCounter (modelSet);
     90    set->paramSet  = psArrayAlloc (modelSet->n);
     91    set->derivSet  = psArrayAlloc (modelSet->n);
    6792    set->nParamSet = 0;
     93    set->thread    = pthread_self();
    6894
    6995    for (int i = 0; i < modelSet->n; i++) {
     
    88114}
    89115
     116pmSourceFitSetData *pmSourceFitSetDataSet (psArray *modelSet) {
     117
     118    pmSourceFitSetData *thisSet = NULL;
     119
     120    psAssert (fitSets, "pmSourceFitSetInit not called");
     121
     122    // find the fitSet used by this thread
     123    pthread_t id = pthread_self();
     124
     125    // is our ID is already on the stack, abort.
     126    // we do not need to lock to do this....
     127    for (int i = 0; i < fitSets->n; i++) {
     128        thisSet = fitSets->data[i];
     129        if (!thisSet) continue;
     130        if (thisSet->thread == id) {
     131            break;
     132        }
     133        thisSet = NULL;
     134    }
     135    psAssert (thisSet == NULL, "pmSourceFitSetDataSet() called but previous entry not cleared");
     136
     137    thisSet = pmSourceFitSetDataAlloc(modelSet);
     138
     139    pthread_mutex_lock (&fitSetInitMutex);
     140         
     141    // find an entry that is NULL and place it there
     142    for (int i = 0; i < fitSets->n; i++) {
     143        if (fitSets->data[i]) continue;
     144        fitSets->data[i] = thisSet;
     145        pthread_mutex_unlock (&fitSetInitMutex);
     146        return thisSet;
     147    }
     148    pthread_mutex_unlock (&fitSetInitMutex);
     149    psAbort ("no empty slot for new pmSourceFitSetData");
     150    return NULL;
     151}
     152
     153pmSourceFitSetData *pmSourceFitSetDataGet () {
     154
     155    psAssert (fitSets, "pmSourceFitSetInit not called");
     156
     157    // find the fitSet used by this thread
     158    pthread_t id = pthread_self();
     159
     160    // can we find our fitSet?  we do not need to lock to do this....
     161    pmSourceFitSetData *thisSet = NULL;
     162    for (int i = 0; i < fitSets->n; i++) {
     163        thisSet = fitSets->data[i];
     164        if (!thisSet) continue;
     165        if (thisSet->thread == id) {
     166            break;
     167        }
     168        thisSet = NULL;
     169    }
     170    psAssert (thisSet != NULL, "pmSourceFitSetDataGet() called, but no entry found");
     171
     172    return thisSet;
     173}
     174
     175void pmSourceFitSetDataClear () {
     176
     177    int i;
     178
     179    psAssert (fitSets, "pmSourceFitSetInit not called");
     180
     181    // find the fitSet used by this thread
     182    pthread_t id = pthread_self();
     183
     184    // can we find our fitSet?  we do not need to lock to do this....
     185    pmSourceFitSetData *thisSet = NULL;
     186    for (i = 0; i < fitSets->n; i++) {
     187        thisSet = fitSets->data[i];
     188        if (!thisSet) continue;
     189        if (thisSet->thread == id) {
     190            break;
     191        }
     192        thisSet = NULL;
     193    }
     194    psAssert (thisSet != NULL, "pmSourceFitSetDataClear() called, but no entry found");
     195
     196    psFree (thisSet);
     197    fitSets->data[i] = NULL;
     198    return;
     199}
    90200
    91201// this function is called with the full set of parameters and the beta values in a single vector
     
    93203                                float *betas)
    94204{
    95     PS_ASSERT_PTR_NON_NULL(thisSet, false);
     205    PS_ASSERT_PTR_NON_NULL(fitSets, false);
     206    pmSourceFitSetData *thisSet = pmSourceFitSetDataGet();
    96207
    97208    // nParam is the parameter in the full sequence.  determine which single model this comes from
     
    190301}
    191302
     303// set the model parameters for this fit set
    192304bool pmSourceFitSetValues (pmSourceFitSetData *set, const psVector *dparam,
    193305                           const psVector *param, pmSource *source,
     
    242354psF32 pmSourceFitSetFunction(psVector *deriv, const psVector *param, const psVector *x)
    243355{
    244     PS_ASSERT_PTR_NON_NULL(thisSet, NAN);
     356    pmSourceFitSetData *thisSet = pmSourceFitSetDataGet();
     357
    245358    float chisqSum = 0.0;
    246359    float chisqOne = 0.0;
     
    391504    yErr->n = nPix;
    392505
    393     // create the FitSet and set the initial parameter guesses
    394     thisSet = pmSourceFitSetDataAlloc (modelSet);
     506    // create the FitSet for this thread and set the initial parameter guesses
     507    pmSourceFitSetData *thisSet = pmSourceFitSetDataSet(modelSet);
    395508
    396509    // define param and deriv vectors for complete set of parameters
     
    431544        psFree (params);
    432545        psFree(constraint);
    433         psFree (thisSet);
    434         thisSet = NULL;
     546        pmSourceFitSetDataClear(); // frees thisSet and removes if from the array of fitSets
    435547        return(false);
    436548    }
     
    488600    psFree(params);
    489601    psFree(dparams);
    490     psFree(thisSet);
    491 
    492     thisSet = NULL;
     602    pmSourceFitSetDataClear(); // frees thisSet and removes if from the array of fitSets
    493603
    494604    bool rc = (onPic && fitStatus);
  • trunk/psModules/src/objects/pmSourceFitSet.h

    r15562 r21163  
    33 * @author EAM, IfA; GLG, MHPCC
    44 *
    5  * @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
    6  * @date $Date: 2007-11-10 01:09:20 $
     5 * @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
     6 * @date $Date: 2009-01-24 20:52:26 $
    77 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    88 */
     
    1919    psArray *derivSet;
    2020    int nParamSet;
     21    pthread_t thread;
    2122} pmSourceFitSetData;
     23
     24// use this function to init the fit sets based on the number of threads
     25bool pmSourceFitSetInit (int nThreads);
     26void pmSourceFitSetDone ();
    2227
    2328// initialize data for a group of object models
    2429pmSourceFitSetData *pmSourceFitSetDataAlloc (psArray *modelSet);
    2530bool psMemCheckSourceFitSetData(psPtr ptr);
     31
     32// functions for selecting the FitSet corresponding to the current thread
     33pmSourceFitSetData *pmSourceFitSetDataSet (psArray *modelSet);
     34pmSourceFitSetData *pmSourceFitSetDataGet ();
     35void pmSourceFitSetDataClear ();
    2636
    2737// function used to set limits for a group of models
Note: See TracChangeset for help on using the changeset viewer.