Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members

SeedHandler Class Reference

#include <abkseed.h>

Collaboration diagram for SeedHandler:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ~SeedHandler ()

Static Public Member Functions

void turnOffLogging ()
void overrideExternalSeed (unsigned extseed)
bool isInitialized ()

Private Member Functions

 SeedHandler (unsigned seed)
 SeedHandler (const char *locIdent, unsigned counterOverride=UINT_MAX)
void _init ()
void _chooseInitNondetSeed ()
void _initializeOutFile ()

Static Private Member Functions

void clean ()

Private Attributes

unsigned _seed
char * _locIdent
unsigned _counter
bool _isSeedMultipartite

Static Private Attributes

ofstream * _PseedOutFile = NULL
ifstream * _PseedInFile = NULL
unsigned _nextSeed = UINT_MAX
unsigned _externalSeed = UINT_MAX
unsigned _progOverrideExternalSeed = UINT_MAX
std::map< std::string, unsigned,
SeedHandlerCompareStrings
_counters
bool _loggingOff = false
bool _haveRandObj = false
bool _cleaned = false

Friends

class RandomRoot
class SeedCleaner

Constructor & Destructor Documentation

SeedHandler::SeedHandler unsigned  seed  )  [private]
 

Definition at line 175 of file seed.cxx.

References _init(), _loggingOff, _nextSeed, _PseedInFile, _PseedOutFile, _seed, and abkfatal.

00175                                      :
00176                _locIdent(NULL),_counter(UINT_MAX),_isSeedMultipartite(false)
00177     {
00178     _init();
00179     if (seed==UINT_MAX)
00180         _seed = _nextSeed++;
00181     else                     //deterministic seed, take value given unless
00182                              //seeds.in exists with valid value
00183         {
00184         if (_PseedInFile)
00185             {
00186             *_PseedInFile>>_seed;
00187             //cerr << "Seed read was " << _seed << endl;
00188 
00189             if (_PseedInFile->fail()) //if we've run out, go back to accepting
00190                                       //seeds given
00191                 {
00192                 _PseedInFile->close();
00193                 delete _PseedInFile;
00194                 _PseedInFile = NULL;
00195                 _seed=seed;
00196                 //cerr << "But read failed, substituting seed " << _seed;
00197                 }
00198 
00199             }
00200         else
00201             _seed = seed;
00202 
00203         if (!_loggingOff)
00204             {
00205             abkfatal(_PseedOutFile,"Internal error: unable to log "
00206                                    "random seed to file");
00207             *_PseedOutFile << _seed << endl;
00208             }
00209         }
00210     }

Here is the call graph for this function:

SeedHandler::SeedHandler const char *  locIdent,
unsigned  counterOverride = UINT_MAX
[private]
 

Definition at line 214 of file seed.cxx.

References _counter, _counters, _init(), and _locIdent.

00215                                                   :
00216             _isSeedMultipartite(true)
00217     {
00218     _init();
00219     _locIdent = strdup(locIdent);
00220 
00221     if (counterOverride!=UINT_MAX)
00222         _counter=counterOverride;
00223     else
00224         {
00225         std::map<std::string,unsigned,SeedHandlerCompareStrings >::iterator iC;
00226         iC = _counters.find(_locIdent);
00227         if (iC==_counters.end()) _counters[_locIdent]=_counter=0;
00228         else { iC->second++; _counter=iC->second; }
00229         }
00230     }

Here is the call graph for this function:

SeedHandler::~SeedHandler  ) 
 

Definition at line 234 of file seed.cxx.

References _isSeedMultipartite, _locIdent, and abkassert.

00235     {
00236     if (!_isSeedMultipartite)
00237         {
00238         abkassert(_locIdent==NULL,"Non-null _locIdent with old-style seed");
00239         }
00240     else
00241         {
00242           free(_locIdent);
00243           _locIdent=NULL;
00244         }
00245     }


Member Function Documentation

void SeedHandler::_chooseInitNondetSeed  )  [private]
 

Definition at line 291 of file seed.cxx.

References _externalSeed, _nextSeed, _progOverrideExternalSeed, _PseedInFile, abkfatal, abkwarn, and Timer::getUnixTime().

Referenced by _init().

00292     {
00293 #ifdef __GNUC__
00294 #if( __GNUC__ <= 3)
00295     _PseedInFile = new ifstream("seeds.in",ios::in);
00296 #else
00297     _PseedInFile = new ifstream("seeds.in",ios::in|ios::nocreate);
00298 #endif
00299 #else
00300     _PseedInFile = new ifstream("seeds.in",ios::in);
00301 #endif
00302 
00303     if (!*_PseedInFile)
00304         {
00305         delete _PseedInFile;
00306         _PseedInFile=NULL;
00307         }
00308 
00309     if (_PseedInFile)
00310         {
00311         unsigned DummyInUseLine;
00312         cerr<<"File seeds.in exists and will override all seeds, including "
00313               "hardcoded ones " << endl;
00314         *_PseedInFile >> DummyInUseLine;
00315         abkwarn(!DummyInUseLine,"File seeds.in appears to come from a process "
00316                                 "that never completed\n");
00317         *_PseedInFile >> _nextSeed;
00318         //cerr << "Init nondet seed read was "<<_nextSeed << endl;
00319         abkfatal(!(_PseedInFile->fail()),"File seeds.in exists but program "
00320                                          "was unable to read initial seed "
00321                                          "therefrom.");
00322         abkwarn(_progOverrideExternalSeed==UINT_MAX,
00323             "\nThere is a programmatic override of the external seed,"
00324             " which will be ignored because seeds.in exists");
00325         }
00326     else if (_progOverrideExternalSeed==UINT_MAX)
00327         {
00328         Timer tm;
00329 
00330         char buf[255];
00331 #if defined(WIN32)
00332         int procID=_getpid();
00333         LARGE_INTEGER hiPrecTime;
00334         ::QueryPerformanceCounter(&hiPrecTime);
00335         LONGLONG hiP1=hiPrecTime.QuadPart;
00336         sprintf(buf,"%g %d %I64d",tm.getUnixTime(),procID,hiP1);
00337 #else
00338         unsigned procID=getpid();
00339         unsigned rndbuf[2];
00340         FILE *rnd=fopen("/dev/random","r");
00341         fread(rndbuf,sizeof(rndbuf),1,rnd);
00342         fclose(rnd);
00343         sprintf(buf,"%g %d %d %d",tm.getUnixTime(),procID,rndbuf[0],rndbuf[1]);
00344 #endif
00345 
00346         MD5 hash(buf);
00347         _nextSeed=hash;
00348         }
00349     else
00350         {
00351         _nextSeed=_progOverrideExternalSeed;
00352         }
00353     _externalSeed=_nextSeed;
00354     }

Here is the call graph for this function:

void SeedHandler::_init  )  [private]
 

Definition at line 153 of file seed.cxx.

References _chooseInitNondetSeed(), _cleaned, _haveRandObj, _initializeOutFile(), _loggingOff, and abkfatal.

Referenced by SeedHandler().

00154     {
00155     abkfatal(!_cleaned,"Can't create random object"
00156                        " after calling SeedHandler::clean()");
00157 
00158     if (!_haveRandObj)
00159         {
00160         _chooseInitNondetSeed();
00161 
00162         if (!_loggingOff)
00163             {
00164             _initializeOutFile();
00165             }
00166         }
00167 
00168     _haveRandObj=true;
00169 
00170     }

Here is the call graph for this function:

void SeedHandler::_initializeOutFile  )  [private]
 

Definition at line 359 of file seed.cxx.

References _loggingOff, _nextSeed, _PseedOutFile, and abkwarn.

Referenced by _init().

00360     {
00361     unsigned inUse;
00362 
00363 #ifdef __GNUC__
00364 #if( __GNUC__ < 3)
00365     ifstream outFileAsInFile("seeds.out",ios::in|ios::nocreate);
00366 #else
00367     ifstream outFileAsInFile("seeds.out",ios::in);
00368 #endif
00369 #else
00370     ifstream outFileAsInFile("seeds.out",ios::in);
00371 #endif
00372 
00373     if (!!outFileAsInFile)
00374         {
00375 
00376         outFileAsInFile >> inUse;
00377         if (outFileAsInFile.fail())
00378             {
00379             char errtxt[1023];
00380 
00381             sprintf(errtxt,"Unable to determine whether file seeds.out is"
00382                            " in use; random seeds from "
00383                            "this process will not be logged.\n  Initial non-"
00384                            "deterministic seed is %u",_nextSeed);
00385 
00386             abkwarn(0,errtxt);
00387             _loggingOff=true;
00388             }
00389         else
00390             {
00391             if (inUse)
00392                 {
00393                 char errtxt[1023];
00394 
00395                 sprintf(errtxt,"File seeds.out is in use; random seeds from "
00396                                "this process will not be logged.\nInitial non-"
00397                                "deterministic seed is %u.\nIf there is no "
00398                                "other process running in this directory, then "
00399                                "an earlier\nprocess may have terminated\n"
00400                                "abnormally. "
00401                                "You should remove or rename seeds.out\n"
00402                                ,_nextSeed);
00403 
00404                 abkwarn(0,errtxt);
00405                 _loggingOff=true;
00406                 }
00407             }
00408         }
00409         outFileAsInFile.close();
00410 
00411         if (!_loggingOff)
00412             {
00413             _PseedOutFile=new ofstream("seeds.out",ios::out); //overwrite file
00414 
00415             if (!_PseedOutFile)
00416                 {
00417                 char errtxt[1023];
00418 
00419                 sprintf(errtxt,"Unable to create file seeds.out; random seeds from "
00420                                "this process will not be logged.\n  Initial non-"
00421                                "deterministic seed is %u",_nextSeed);
00422 
00423                 abkwarn(0,errtxt);
00424                 _loggingOff=true;
00425                 delete _PseedOutFile;_PseedOutFile=NULL;
00426                 }
00427 
00428             *_PseedOutFile << "1" << endl; //mark as in use
00429 
00430             *_PseedOutFile << _nextSeed << endl; //initial nondet seed
00431             }
00432 
00433     }

void SeedHandler::clean  )  [static, private]
 

Definition at line 269 of file seed.cxx.

References _cleaned, _PseedInFile, and _PseedOutFile.

Referenced by SeedCleaner::~SeedCleaner().

00270     {
00271     _cleaned=true;
00272 
00273     if(_PseedOutFile)
00274         {
00275         _PseedOutFile->seekp(0,ios::beg); //rewind file
00276         _PseedOutFile->put('0');          //mark not in use
00277         _PseedOutFile->close();
00278         }
00279 
00280     if(_PseedInFile)_PseedInFile->close();
00281 
00282     delete _PseedOutFile;delete _PseedInFile;
00283     _PseedOutFile=NULL;
00284     _PseedInFile=NULL;
00285     }

bool SeedHandler::isInitialized  )  [inline, static]
 

Definition at line 173 of file abkseed.h.

References _haveRandObj.

00173 { return _haveRandObj; }

void SeedHandler::overrideExternalSeed unsigned  extseed  )  [static]
 

Definition at line 257 of file seed.cxx.

References _haveRandObj, _progOverrideExternalSeed, and abkfatal.

00258     {
00259     abkfatal(!_haveRandObj,"Can't call SeedHandler::overrideExternalSeed() "
00260         "after creating a random object");
00261     abkfatal(extseed != UINT_MAX,
00262         "Can't pass UINT_MAX to SeedHandler::overrideExternalSeed()");
00263     _progOverrideExternalSeed=extseed;
00264     }

void SeedHandler::turnOffLogging  )  [static]
 

Definition at line 249 of file seed.cxx.

References _loggingOff.

00250     {
00251     _loggingOff=true;
00252     }


Friends And Related Function Documentation

friend class RandomRoot [friend]
 

Definition at line 168 of file abkseed.h.

friend class SeedCleaner [friend]
 

Definition at line 169 of file abkseed.h.


Member Data Documentation

bool SeedHandler::_cleaned = false [static, private]
 

Definition at line 74 of file seed.cxx.

Referenced by _init(), and clean().

unsigned SeedHandler::_counter [private]
 

Definition at line 142 of file abkseed.h.

Referenced by RandomRoot::getCounter(), RandomRoot::RandomRoot(), and SeedHandler().

std::map< std::string, unsigned, SeedHandlerCompareStrings > SeedHandler::_counters [static, private]
 

Definition at line 78 of file seed.cxx.

Referenced by SeedHandler().

unsigned SeedHandler::_externalSeed = UINT_MAX [static, private]
 

Definition at line 75 of file seed.cxx.

Referenced by _chooseInitNondetSeed(), RandomRoot::getExternalSeed(), and RandomRoot::RandomRoot().

bool SeedHandler::_haveRandObj = false [static, private]
 

Definition at line 73 of file seed.cxx.

Referenced by _init(), isInitialized(), and overrideExternalSeed().

bool SeedHandler::_isSeedMultipartite [private]
 

Definition at line 143 of file abkseed.h.

Referenced by RandomRoot::getCounter(), RandomRoot::getLocIdent(), RandomRoot::getSeed(), and ~SeedHandler().

char* SeedHandler::_locIdent [private]
 

Definition at line 141 of file abkseed.h.

Referenced by RandomRoot::getLocIdent(), RandomRoot::RandomRoot(), SeedHandler(), and ~SeedHandler().

bool SeedHandler::_loggingOff = false [static, private]
 

Definition at line 72 of file seed.cxx.

Referenced by _init(), _initializeOutFile(), SeedHandler(), and turnOffLogging().

unsigned SeedHandler::_nextSeed = UINT_MAX [static, private]
 

Definition at line 71 of file seed.cxx.

Referenced by _chooseInitNondetSeed(), _initializeOutFile(), and SeedHandler().

unsigned SeedHandler::_progOverrideExternalSeed = UINT_MAX [static, private]
 

Definition at line 76 of file seed.cxx.

Referenced by _chooseInitNondetSeed(), and overrideExternalSeed().

ifstream * SeedHandler::_PseedInFile = NULL [static, private]
 

Definition at line 70 of file seed.cxx.

Referenced by _chooseInitNondetSeed(), clean(), and SeedHandler().

ofstream * SeedHandler::_PseedOutFile = NULL [static, private]
 

Definition at line 69 of file seed.cxx.

Referenced by _initializeOutFile(), clean(), and SeedHandler().

unsigned SeedHandler::_seed [private]
 

Definition at line 139 of file abkseed.h.

Referenced by RandomRoot::RandomRoot(), and SeedHandler().


The documentation for this class was generated from the following files:
Generated on Mon Apr 25 01:15:37 2005 for Parquete by doxygen 1.3.2