Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4MTHepRandom.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id:$
28 //
29 #if __clang__
30  #if ((defined(G4MULTITHREADED) && !defined(G4USE_STD11)) || \
31  !__has_feature(cxx_thread_local)) || !__has_feature(c_atomic)
32  #define CLANG_NOSTDTLS
33  #endif
34 #endif
35 
36 #if (defined(G4MULTITHREADED) && \
37  (!defined(G4USE_STD11) || (defined(CLANG_NOSTDTLS) || defined(__INTEL_COMPILER))))
38 
41 
42 #include "G4MTHepRandom.hh"
43 #include "G4Threading.hh"
44 #include "G4AutoLock.hh"
45 
46 G4ThreadLocal CLHEP::HepRandomEngine* G4MTHepRandom::theEngine = 0;
47 G4ThreadLocal G4MTHepRandom* G4MTHepRandom::theGenerator = 0;
48 G4ThreadLocal G4int G4MTHepRandom::isActive = 0 ;
49 
50 //---------------------------- HepRandom ---------------------------------
51 
53 : deleteEngine(false)
54 {
55  createInstance();
56 }
57 
59 : deleteEngine(false)
60 {
61  createInstance();
62  setTheSeed(seed);
63 }
64 
66 : deleteEngine(false)
67 {
68  theGenerator = this;
69  theEngine = &algorithm;
70  isActive = 1;
71 }
72 
74 : deleteEngine(true)
75 {
76  createInstance();
77  theEngine = algorithm;
78 }
79 
81 {
82  if ( deleteEngine ) { delete theEngine; theEngine = 0; }
83  //if ( theGenerator ) { delete theGenerator; theGenerator = 0; }
84 }
85 
87 {
88  return theEngine->flat();
89 }
90 
91 void G4MTHepRandom::flatArray(const G4int size, G4double* vect)
92 {
93  theEngine->flatArray(size,vect);
94 }
95 
97 {
98  return flat();
99 }
100 
101 std::ostream & operator<< (std::ostream & os, const G4MTHepRandom & dist)
102 {
103  return dist.put(os);
104 }
105 
106 std::istream & operator>> (std::istream & is, G4MTHepRandom & dist)
107 {
108  return dist.get(is);
109 }
110 
111 std::ostream & G4MTHepRandom::put(std::ostream & os) const
112 {
113  return os;
114 }
115 std::istream & G4MTHepRandom::get(std::istream & is)
116 {
117  return is;
118 }
119 
120 // --------------------------
121 // Static methods definitions
122 // --------------------------
123 
125 {
126  createInstance();
127  theEngine->setSeed(seed,lux);
128 }
129 
131 {
132  createInstance();
133  return theEngine->getSeed();
134 }
135 
136 void G4MTHepRandom::setTheSeeds(const G4long* seeds, G4int aux)
137 {
138  createInstance();
139  theEngine->setSeeds(seeds,aux);
140 }
141 
143 {
144  createInstance();
145  return theEngine->getSeeds();
146 }
147 
149 {
150  if (!isActive) { isActive = G4MTHepRandom::createInstanceOnce(); }
151  return theGenerator;
152 }
153 
155 {
156  if (!isActive) { isActive = G4MTHepRandom::createInstanceOnce(); }
157  return theEngine;
158 }
159 
161 {
162  theEngine = theNewEngine;
163 }
164 
165 void G4MTHepRandom::saveEngineStatus( const char filename[] )
166 {
167  createInstance();
168  theEngine->saveStatus( filename );
169 }
170 
171 void G4MTHepRandom::restoreEngineStatus( const char filename[] )
172 {
173  createInstance();
174  theEngine->restoreStatus( filename );
175 }
176 
177 std::ostream& G4MTHepRandom::saveFullState ( std::ostream & os )
178 {
179  os << *getTheEngine();
180  return os;
181 }
182 
183 std::istream& G4MTHepRandom::restoreFullState ( std::istream & is )
184 {
185  is >> *getTheEngine();
186  return is;
187 }
188 
189 std::ostream& G4MTHepRandom::saveStaticRandomStates ( std::ostream & os )
190 {
192 }
193 
194 std::istream& G4MTHepRandom::restoreStaticRandomStates ( std::istream & is )
195 {
197 }
198 
200 {
201  createInstance();
202  theEngine->showStatus();
203 }
204 
206 {
207  if (!isActive) { isActive = G4MTHepRandom::createInstanceOnce(); }
208  if (theGenerator) return 1; // should always be true
209  return 0;
210 }
211 
212 namespace {
213  G4Mutex jamesrandom = G4MUTEX_INITIALIZER;
214 }
215 
217 {
218  if (isActive) return isActive;
219  isActive = 1;
220 
221 
222  static G4ThreadLocal CLHEP::HepJamesRandom *defaultEngine = 0;
223  if (!defaultEngine) {
224  G4AutoLock l(&jamesrandom);
225  defaultEngine = new CLHEP::HepJamesRandom;
226  }
227 
228  if ( !theEngine ) { theEngine = defaultEngine; }
229  if ( !theGenerator ) { theGenerator = new G4MTHepRandom( theEngine ); }
230 
231  return 1;
232 }
233 
234 #endif
static G4long getTheSeed()
static CLHEP::HepRandomEngine * getTheEngine()
virtual ~G4MTHepRandom()
virtual void showStatus() const =0
std::shared_ptr< HepRandom > theGenerator
Definition: Random.cc:72
static G4MTHepRandom * getTheGenerator()
std::shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:73
static G4int createInstance()
static void setTheSeeds(const G4long *seeds, G4int aux=-1)
static constexpr double lux
Definition: G4SIunits.hh:327
static void setTheEngine(CLHEP::HepRandomEngine *theNewEngine)
long G4long
Definition: G4Types.hh:80
virtual double flat()=0
static void saveEngineStatus(const char filename[]="Config.conf")
#define G4ThreadLocal
Definition: tls.hh:89
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
const long * getSeeds() const
Definition: RandomEngine.h:116
static void setTheSeed(G4long seed, G4int lux=3)
static std::istream & restoreFullState(std::istream &is)
static std::ostream & saveStaticRandomStates(std::ostream &os)
long seed
Definition: chem4.cc:68
static std::istream & restoreStaticRandomStates(std::istream &is)
virtual void restoreStatus(const char filename[]="Config.conf")=0
static G4int createInstanceOnce()
virtual void setSeed(long seed, int)=0
static void showEngineStatus()
static std::ostream & saveFullState(std::ostream &os)
long getSeed() const
Definition: RandomEngine.h:113
static std::istream & restore(std::istream &is)
G4int G4Mutex
Definition: G4Threading.hh:173
G4double flat()
virtual void setSeeds(const long *seeds, int)=0
virtual std::ostream & put(std::ostream &os) const
static std::ostream & save(std::ostream &os)
static void restoreEngineStatus(const char filename[]="Config.conf")
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)
std::istream & operator>>(std::istream &, BasicVector3D< float > &)
virtual std::istream & get(std::istream &is)
static const G4long * getTheSeeds()
virtual void saveStatus(const char filename[]="Config.conf") const =0
virtual G4double operator()()
double G4double
Definition: G4Types.hh:76
void flatArray(const G4int size, G4double *vect)
virtual void flatArray(const int size, double *vect)=0