Geant4 Cross Reference |
1 // -*- C++ -*- 1 // -*- C++ -*- 2 // 2 // 3 // ------------------------------------------- 3 // ----------------------------------------------------------------------- 4 // HEP Random 4 // HEP Random 5 // --- HepRandom --- 5 // --- HepRandom --- 6 // class implementation f 6 // class implementation file 7 // ------------------------------------------- 7 // ----------------------------------------------------------------------- 8 // This file is part of Geant4 (simulation too 8 // This file is part of Geant4 (simulation toolkit for HEP). 9 9 10 // =========================================== 10 // ======================================================================= 11 // Gabriele Cosmo - Created: 5th September 199 11 // Gabriele Cosmo - Created: 5th September 1995 12 // - Minor corrections: 31st Oc 12 // - Minor corrections: 31st October 1996 13 // - Added methods for engine s 13 // - Added methods for engine status: 19th November 1996 14 // - HepRandom defined as singl 14 // - HepRandom defined as singleton, constructors are 15 // kept public for backward c 15 // kept public for backward compatibility: 27th Feb 1998 16 // - Relocated Poisson and Gaus 16 // - Relocated Poisson and Gauss data and simplified 17 // initialisation of static g 17 // initialisation of static generator: 5th Jan 1999 18 // =========================================== 18 // ======================================================================= 19 19 >> 20 #include <assert.h> 20 #include "CLHEP/Random/MixMaxRng.h" 21 #include "CLHEP/Random/MixMaxRng.h" 21 #include "CLHEP/Random/Random.h" 22 #include "CLHEP/Random/Random.h" 22 #include "CLHEP/Random/StaticRandomStates.h" 23 #include "CLHEP/Random/StaticRandomStates.h" 23 #include "CLHEP/Utility/memory.h" 24 #include "CLHEP/Utility/memory.h" 24 #include "CLHEP/Utility/thread_local.h" 25 #include "CLHEP/Utility/thread_local.h" 25 #include "CLHEP/Utility/use_atomic.h" 26 #include "CLHEP/Utility/use_atomic.h" 26 27 27 // ----------------------------- 28 // ----------------------------- 28 // Static members initialisation 29 // Static members initialisation 29 // ----------------------------- 30 // ----------------------------- 30 31 31 #include "CLHEP/Random/SeedTable.h" 32 #include "CLHEP/Random/SeedTable.h" 32 << 33 #include <assert.h> << 34 #include <iostream> << 35 #include <type_traits> << 36 #include <string> << 37 33 38 namespace CLHEP { 34 namespace CLHEP { 39 35 40 namespace { 36 namespace { 41 37 42 struct defaults { 38 struct defaults { 43 39 44 defaults() 40 defaults() 45 : theGenerator( &theDefaultGenerator, 41 : theGenerator( &theDefaultGenerator, do_nothing_deleter() ) 46 , theEngine ( &theDefaultEngine, do_ 42 , theEngine ( &theDefaultEngine, do_nothing_deleter() ) 47 { } 43 { } 48 44 49 defaults(defaults const& other) = delete 45 defaults(defaults const& other) = delete; 50 defaults const& operator=(defaults const 46 defaults const& operator=(defaults const&) = delete; 51 47 52 void resetEngine( HepRandomEngine * new 48 void resetEngine( HepRandomEngine * newEngine ) { 53 theEngine.reset( newEngine ); 49 theEngine.reset( newEngine ); 54 } 50 } 55 51 56 void resetEngine( HepRandomEngine & new 52 void resetEngine( HepRandomEngine & newEngine ) { 57 theEngine.reset( &newEngine, do_nothin 53 theEngine.reset( &newEngine, do_nothing_deleter() ); 58 } 54 } 59 55 60 bool ensureInitialized() { 56 bool ensureInitialized() { 61 assert( theGenerator.get() != 0 && th 57 assert( theGenerator.get() != 0 && theEngine.get() != 0 ); 62 return true; 58 return true; 63 } 59 } 64 60 65 ~defaults() 61 ~defaults() 66 { } 62 { } 67 63 68 private: 64 private: 69 65 70 HepRandom theDefaultGenerator; 66 HepRandom theDefaultGenerator; 71 MixMaxRng theDefaultEngine; 67 MixMaxRng theDefaultEngine; 72 68 73 public: 69 public: 74 70 75 std::shared_ptr<HepRandom > theGen 71 std::shared_ptr<HepRandom > theGenerator; 76 std::shared_ptr<HepRandomEngine> theEng 72 std::shared_ptr<HepRandomEngine> theEngine; 77 }; // defaults 73 }; // defaults 78 74 79 75 80 #ifdef CLHEP_USE_ATOMIC 76 #ifdef CLHEP_USE_ATOMIC 81 77 82 // The ThreadSafeDefaultCache is used only 78 // The ThreadSafeDefaultCache is used only by the function named theDefaults. 83 // It is a singly linked list that is inte 79 // It is a singly linked list that is intended to hold one object of 84 // type "defaults" per thread. 80 // type "defaults" per thread. 85 81 86 class ThreadSafeDefaultsCache { 82 class ThreadSafeDefaultsCache { 87 public: 83 public: 88 84 89 ThreadSafeDefaultsCache(); 85 ThreadSafeDefaultsCache(); 90 86 91 // The destructor deletes the objects of 87 // The destructor deletes the objects of type "defaults" 92 ~ThreadSafeDefaultsCache(); 88 ~ThreadSafeDefaultsCache(); 93 89 94 // Creates new objects and adds them to 90 // Creates new objects and adds them to the linked list in a thread safe manner. 95 defaults* createNewDefaults(); 91 defaults* createNewDefaults(); 96 92 97 // Note that there are no other function 93 // Note that there are no other functions. No erasing or moving or other accessors. 98 94 99 private: 95 private: 100 96 101 class DefaultsNode { 97 class DefaultsNode { 102 public: 98 public: 103 DefaultsNode(DefaultsNode* iNext); 99 DefaultsNode(DefaultsNode* iNext); 104 DefaultsNode const* next() const { ret 100 DefaultsNode const* next() const { return next_; } 105 void setNext(DefaultsNode* v) { next_ 101 void setNext(DefaultsNode* v) { next_ = v; } 106 defaults* addressOfDefaults() { return 102 defaults* addressOfDefaults() { return &defaults_; } 107 private: 103 private: 108 DefaultsNode* next_; 104 DefaultsNode* next_; 109 defaults defaults_; 105 defaults defaults_; 110 }; 106 }; 111 107 112 // points to first node in the linked li 108 // points to first node in the linked list 113 std::atomic<DefaultsNode*> front_; 109 std::atomic<DefaultsNode*> front_; 114 }; 110 }; 115 111 116 ThreadSafeDefaultsCache::ThreadSafeDefault 112 ThreadSafeDefaultsCache::ThreadSafeDefaultsCache() : 117 front_(nullptr) { 113 front_(nullptr) { 118 } 114 } 119 115 120 defaults* ThreadSafeDefaultsCache::createN 116 defaults* ThreadSafeDefaultsCache::createNewDefaults() { 121 DefaultsNode* expected = front_.load(); 117 DefaultsNode* expected = front_.load(); 122 DefaultsNode* newNode = new DefaultsNode 118 DefaultsNode* newNode = new DefaultsNode(expected); 123 while (!front_.compare_exchange_strong(e 119 while (!front_.compare_exchange_strong(expected, newNode)) { 124 // another thread changed front_ befor 120 // another thread changed front_ before us so try again 125 newNode->setNext(expected); 121 newNode->setNext(expected); 126 } 122 } 127 return newNode->addressOfDefaults(); 123 return newNode->addressOfDefaults(); 128 } 124 } 129 125 130 ThreadSafeDefaultsCache::DefaultsNode::Def 126 ThreadSafeDefaultsCache::DefaultsNode::DefaultsNode(DefaultsNode* iNext) : 131 next_(iNext), 127 next_(iNext), 132 defaults_() { 128 defaults_() { 133 } 129 } 134 130 135 ThreadSafeDefaultsCache::~ThreadSafeDefaul 131 ThreadSafeDefaultsCache::~ThreadSafeDefaultsCache() { 136 DefaultsNode const* node = front_.load() 132 DefaultsNode const* node = front_.load(); 137 while (node) { 133 while (node) { 138 DefaultsNode const* next = node->next( 134 DefaultsNode const* next = node->next(); 139 delete node; 135 delete node; 140 node = next; 136 node = next; 141 } 137 } 142 } 138 } 143 139 144 defaults & theDefaults() { 140 defaults & theDefaults() { 145 141 146 // We need to have different engines on 142 // We need to have different engines on different threads because 147 // the engines are not thread safe. One 143 // the engines are not thread safe. One cannot generate random numbers 148 // using the same engine on different th 144 // using the same engine on different threads simultaneously. 149 // Originally we had the defaults object 145 // Originally we had the defaults object itself as a thread local, 150 // but that was failing because on Mac O 146 // but that was failing because on Mac OSX there is not full 151 // support for thread locals yet. Objec 147 // support for thread locals yet. Objects containing std::shared_ptr 152 // in thread local storage were causing 148 // in thread local storage were causing failures. So now we create 153 // a container of them that is a functio 149 // a container of them that is a function static (not thread local) 154 // and the thread local contains only a 150 // and the thread local contains only a pointer to an object in the 155 // container. 151 // container. 156 static ThreadSafeDefaultsCache defaultsF 152 static ThreadSafeDefaultsCache defaultsForAllThreads; 157 153 158 // A pointer for each thread to defaults 154 // A pointer for each thread to defaults object built for each thread. 159 static CLHEP_THREAD_LOCAL defaults* theD 155 static CLHEP_THREAD_LOCAL defaults* theDefaults = defaultsForAllThreads.createNewDefaults(); 160 156 161 return *theDefaults; 157 return *theDefaults; 162 } 158 } 163 #else 159 #else 164 160 165 // This version is used with old compilers 161 // This version is used with old compilers not supporting atomics. 166 // In that case, the code should not be ex 162 // In that case, the code should not be executed in more than one thread. 167 defaults & theDefaults() { 163 defaults & theDefaults() { 168 static defaults theDefaults; 164 static defaults theDefaults; 169 return theDefaults; 165 return theDefaults; 170 } 166 } 171 167 172 #endif 168 #endif 173 169 174 } // namespace 170 } // namespace 175 171 176 //---------------------------- HepRandom ----- 172 //---------------------------- HepRandom --------------------------------- 177 173 178 HepRandom::HepRandom() 174 HepRandom::HepRandom() 179 { } 175 { } 180 176 181 HepRandom::HepRandom(long seed) 177 HepRandom::HepRandom(long seed) 182 { 178 { 183 setTheSeed(seed); 179 setTheSeed(seed); 184 } 180 } 185 181 186 HepRandom::HepRandom(HepRandomEngine & algorit 182 HepRandom::HepRandom(HepRandomEngine & algorithm) 187 { 183 { 188 theDefaults().resetEngine( algorithm ); 184 theDefaults().resetEngine( algorithm ); 189 } 185 } 190 186 191 HepRandom::HepRandom(HepRandomEngine * algorit 187 HepRandom::HepRandom(HepRandomEngine * algorithm) 192 { 188 { 193 theDefaults().resetEngine( algorithm ); 189 theDefaults().resetEngine( algorithm ); 194 } 190 } 195 191 196 HepRandom::~HepRandom() 192 HepRandom::~HepRandom() 197 { } 193 { } 198 194 199 double HepRandom::flat() 195 double HepRandom::flat() 200 { 196 { 201 return theDefaults().theEngine->flat(); 197 return theDefaults().theEngine->flat(); 202 } 198 } 203 199 204 void HepRandom::flatArray(const int size, doub 200 void HepRandom::flatArray(const int size, double* vect) 205 { 201 { 206 theDefaults().theEngine->flatArray(size,vect 202 theDefaults().theEngine->flatArray(size,vect); 207 } 203 } 208 204 209 double HepRandom::operator()() { 205 double HepRandom::operator()() { 210 return flat(); 206 return flat(); 211 } 207 } 212 208 213 std::string HepRandom::name() const {return "H 209 std::string HepRandom::name() const {return "HepRandom";} 214 HepRandomEngine & HepRandom::engine() { 210 HepRandomEngine & HepRandom::engine() { 215 std::cerr << "HepRandom::engine() called -- 211 std::cerr << "HepRandom::engine() called -- there is no assigned engine!\n"; 216 return *theDefaults().theEngine.get(); 212 return *theDefaults().theEngine.get(); 217 } 213 } 218 214 219 std::ostream & operator<< (std::ostream & os, 215 std::ostream & operator<< (std::ostream & os, const HepRandom & dist) { 220 return dist.put(os); 216 return dist.put(os); 221 } 217 } 222 218 223 std::istream & operator>> (std::istream & is, 219 std::istream & operator>> (std::istream & is, HepRandom & dist) { 224 return dist.get(is); 220 return dist.get(is); 225 } 221 } 226 222 227 std::ostream & HepRandom::put(std::ostream & o 223 std::ostream & HepRandom::put(std::ostream & os) const {return os;} 228 std::istream & HepRandom::get(std::istream & i 224 std::istream & HepRandom::get(std::istream & is) {return is;} 229 225 230 // -------------------------- 226 // -------------------------- 231 // Static methods definitions 227 // Static methods definitions 232 // -------------------------- 228 // -------------------------- 233 229 234 void HepRandom::setTheSeed(long seed, int lux) 230 void HepRandom::setTheSeed(long seed, int lux) 235 { 231 { 236 theDefaults().theEngine->setSeed(seed,lux); 232 theDefaults().theEngine->setSeed(seed,lux); 237 } 233 } 238 234 239 long HepRandom::getTheSeed() 235 long HepRandom::getTheSeed() 240 { 236 { 241 return theDefaults().theEngine->getSeed(); 237 return theDefaults().theEngine->getSeed(); 242 } 238 } 243 239 244 void HepRandom::setTheSeeds(const long* seeds, 240 void HepRandom::setTheSeeds(const long* seeds, int aux) 245 { 241 { 246 theDefaults().theEngine->setSeeds(seeds,aux) 242 theDefaults().theEngine->setSeeds(seeds,aux); 247 } 243 } 248 244 249 const long* HepRandom::getTheSeeds () 245 const long* HepRandom::getTheSeeds () 250 { 246 { 251 return theDefaults().theEngine->getSeeds(); 247 return theDefaults().theEngine->getSeeds(); 252 } 248 } 253 249 254 void HepRandom::getTheTableSeeds(long* seeds, 250 void HepRandom::getTheTableSeeds(long* seeds, int index) 255 { 251 { 256 if ((index >= 0) && (index < 215)) { 252 if ((index >= 0) && (index < 215)) { 257 seeds[0] = seedTable[index][0]; 253 seeds[0] = seedTable[index][0]; 258 seeds[1] = seedTable[index][1]; 254 seeds[1] = seedTable[index][1]; 259 } 255 } 260 else seeds = NULL; 256 else seeds = NULL; 261 } 257 } 262 258 263 HepRandom * HepRandom::getTheGenerator() 259 HepRandom * HepRandom::getTheGenerator() 264 { 260 { 265 return theDefaults().theGenerator.get(); 261 return theDefaults().theGenerator.get(); 266 } 262 } 267 263 268 HepRandomEngine * HepRandom::getTheEngine() 264 HepRandomEngine * HepRandom::getTheEngine() 269 { 265 { 270 return theDefaults().theEngine.get(); 266 return theDefaults().theEngine.get(); 271 } 267 } 272 268 273 void HepRandom::setTheEngine (HepRandomEngine* 269 void HepRandom::setTheEngine (HepRandomEngine* theNewEngine) 274 { 270 { 275 theDefaults().theEngine.reset( theNewEngine, 271 theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() ); 276 } 272 } 277 273 278 void HepRandom::saveEngineStatus( const char f 274 void HepRandom::saveEngineStatus( const char filename[] ) 279 { 275 { 280 theDefaults().theEngine->saveStatus( filenam 276 theDefaults().theEngine->saveStatus( filename ); 281 } 277 } 282 278 283 void HepRandom::restoreEngineStatus( const cha 279 void HepRandom::restoreEngineStatus( const char filename[] ) 284 { 280 { 285 theDefaults().theEngine->restoreStatus( file 281 theDefaults().theEngine->restoreStatus( filename ); 286 } 282 } 287 283 288 std::ostream& HepRandom::saveFullState ( std:: 284 std::ostream& HepRandom::saveFullState ( std::ostream & os ) { 289 os << *getTheEngine(); 285 os << *getTheEngine(); 290 return os; 286 return os; 291 } 287 } 292 288 293 std::istream& HepRandom::restoreFullState ( st 289 std::istream& HepRandom::restoreFullState ( std::istream & is ) { 294 is >> *getTheEngine(); 290 is >> *getTheEngine(); 295 return is; 291 return is; 296 } 292 } 297 293 298 std::ostream& HepRandom::saveStaticRandomState 294 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) { 299 return StaticRandomStates::save(os); 295 return StaticRandomStates::save(os); 300 } 296 } 301 297 302 std::istream& HepRandom::restoreStaticRandomSt 298 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) { 303 return StaticRandomStates::restore(is); 299 return StaticRandomStates::restore(is); 304 } 300 } 305 301 306 void HepRandom::showEngineStatus() 302 void HepRandom::showEngineStatus() 307 { 303 { 308 theDefaults().theEngine->showStatus(); 304 theDefaults().theEngine->showStatus(); 309 } 305 } 310 306 311 int HepRandom::createInstance() 307 int HepRandom::createInstance() 312 { 308 { 313 return static_cast<int>( theDefaults().ensur 309 return static_cast<int>( theDefaults().ensureInitialized() ); 314 } 310 } 315 311 316 } // namespace CLHEP 312 } // namespace CLHEP 317 313