We have added new (as of late 2004) methods to the CLHEP Random classes allowing more convenient and natural saving and restoring of engines and distributions. Some inconvenient features of the original methods are:
saveStatus
and restoreStatus
methods are
based on a named file, so that each distribution saved must be placed in
its own file.
restoreStatus
method,
there can be no user-controlled flexibility in how to deal with trouble
(such as file not found) in opening that file.
We have added methods that stick to this idiom, which greatly helps matters. (In fact, the first two of these inconveniences are immediately resolved, and the structure is right for dealing with the third.)
We have also added engine factory capability to deal with anonymous engine saves, and we have given every engine the ability to save and restore to/from a vector of unsigned longs.
cerr
and setting the state of the restoring istream to "bad."
RandGauss::shoot()
.
She wishes to save/restore the full state (including underlying engine)
of just that distribution.
This is completely analogous to doing
RandGauss::saveState("filename"),
but it is desired that this state be added to a file rather than
placed in its own file.
shoot()
method of several static distributions)
and one or more instances of engines of various types.
To conserve I/O space, she wishes to separately save each engine and
the non-engine-dependant state of each distribution.
Since she knows which engines are used by which distributions, she will
be able to restore the full states of all the distributions after restoring
individual engine and distribution states.
shoot()
method of various static distributions.
They wish to make a single call to save/restore the states of all the
static distributions in the CLHEP Randoms package.
The program restoring the states of the distributions and
the common underlying engine has no a priori
knowledge of which type of the underlying engine has been set.
If the static distributions each used a separate engine, this method would need to ensure that states of shared engines would be saved only once, and that all needed engine instances would be saved. However, as currently implemented, every static distribution shares one common engine. Since the (non-engine) state of a distribution itself is miniscule, and only a couple of distributions contain state information other than non-standard distribution parameters (which do not apply to static distributions), the "wasted" cost of saving any unused static distributions is acceptable.
::shoot()
to use the static distribution, and
instance.fire()
to use
an instance.
For these added methods which deal with saving and restoring to
an fstream, the static methods all take the stream as an argument,
while the
instance methods use the
ostream <<
whatever
or
istream >>
whatever
syntax.
As in the case of variate generation, all the methods dealing with just an engine are based on an instance.
HepRandom
class:
static ostream& saveFullState (ostream& os) const; static istream& restoreFullState(istream& is);
shoot()
is invoked and any
additional cached data, such as the second gaussian in the Box-Mueller
method used by RandGauss.
static ostream& saveDistState (ostream& os) const; static istream& restoreDistState(istream& is);
shoot()
method,
so that if the engine used is also restored,
the full state will have been replicated.
For distributions which do not distributions contain cached data
(all distributions other than RandGauss, RandFlat, and RandBit) this method
has no effect.
static ostream& saveStaticRandomStates (ostream& os) const;
theEngine
used in the static shoot()
method of the distributions.
Saves the state of that engine, followed by the static state of each
distribution, such that the states
can be restored by
restoreStaticRandomStates
.
static istream& restoreStaticRandomStates(istream& is);
theEngine
in the static shoot()
method of the distributions.
If, at the point of calling
restoreStaticRandomStates
,
the common engine has been set to an engine of the type matching
that saved, then that engine's state will be changed to the saved state.
If the two engine types do not match, a new engine of the saved type will
be instantiated, and attached to the static distribuitions via a
setTheEngine()
call.
The previously existing engine
will not be deleted (since it might be shared by some distribution
objects), and the new engine is a post-startup new object that
won't automatically be deleted, so this may be detected by tools such
as Purify as a memory leak.
ostream& operator << (ostream& os, const HepRandom & distribution); istream& operator >> (istream& is, HepRandom & distribution);
fire()
method,
so that if the engine used is also restored,
the full state will have been replicated.
RandomEngine * HepRandom::engine();
string RandomEngine::name();
istream& RandomEngine::getName(istream&);
istream& RandomEngine::getState(istream&);
ostream& RandomEngine::put(ostream&); istream& RandomEngine::get(istream&);
vector < unsigned long > RandomEngine::put() const; bool RandomEngine::get(const vector&);
ostream& operator << (ostream& os, const RandomEngine & engine); istream& operator >> (istream& is, RandomEngine & engine);
static HepRandomEngine* HepRandomEngine::newEngine(istream& is); static HepRandomEngine* HepRandomEngine::newEngine (const vector& v);
RandEngine
,
the additional save/restore capability is still presented.
However, in such a case, the only way to achieve the restore is to
start in the original seed state, and fire off however many
variates were requested prior to the save. In the case of restoring
state based on a late event in a long job, this may be quite
inefficient. Therefore, we heavily recommend against using
RandEngine
in applications
involving saving and restoring engine states.
DualRand e(24681357); // Illustrating that the engine need not RandGauss::setTheEngine (&e); // be the default engine for RandGauss. ofstream fs(filename); // Open it, or pass an ofstream already in use codeUsingRandGaussManyTimes(); RandGauss::saveFullState(fs);The "restoring job" does:
DualRand e(); RandGauss::setTheEngine (e); ifstream file(filename); // Open it, or pass an ifstream already in use RandGauss::restoreFullState(fs);
DualRand e(24681357); RandGauss g(e); // Normal distribution, based on engine e ofstream fs(filename); codeUsing_g_ManyTimes(); fs << g.engine() << g;The "restoring job" does:
DualRand e(); RandGauss g(e); ifstream fs(filename); fs >> g.engine() >> g;
DualRand e1(24681357); MTwistEngine e2(135797531); RandGauss g(e1); RandFlat f(e1); RandBit b(e2); ofstream fs(filename); codeUsing_g_f_and_b(); fs << e1 << e2 << g << f << b;The "restoring job" does:
e = new DualRand(); RandGauss g(e); ifstream fs(filename); fs >> e1 >> e2 >> g >> f >> b;To save and restore successfully in the general case, you must know not only which types of engines underlie the various distributions, but also which distributions share an engine.
e = new DualRand(24681357); // illustrating that the some distributions RandGauss::setTheEngine (e); // need not use the default engine ofstream fs(filename); codeUsingVariousDistributions(); saveStaticRandomStates(fs);The "restoring job" does:
e = new DualRand(); RandGauss::setTheEngine (e); ifstream file(filename); restoreStaticRandomStates(fs);
DualRand * e1 = new DualRand(24681357); RanecuEngine * e2 = new RanecuEngine(24563291); HepRandomEngine *eptr; if (someControl) eptr = e1; else eptr = e2; RandGauss g(*eptr); ofstream fs(filename); codeUsing_g_ManyTimes(); fs << g.engine() << g; delete e1; delete e2; // (when g goes out of scope)The "restoring job" does:
ifstream fs(filename); HepRandomEngine * eptr = HepRandomEngine::newEngine(fs); if (eptr==0) throw "problem inputting an engine"; RandGauss g(*eptr); codeUsing_g(); // using g without knowing or caring // what type the engine is delete eptr; // (delete engine when g goes out of scope)
HepJamesRandom e = new HepJamesRandom(12343211); codeUsing_e (e); std::vector < unsigned long > v; v = e.put(); eventData.add_a_vector (v);The "restoring job" does:
eventData.add_a_vector (v); HepJamesRandom e; bool ok = e.get(v); if (!ok) throw "problem restoring an engine"; codeUsing_e(e);
Last modified: March 15, 2005