Changeset 2770
- Timestamp:
- Jul 11, 2012, 4:44:45 PM (10 years ago)
- Location:
- trunk/yat/random
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/random/random.cc
r2768 r2770 34 34 namespace random { 35 35 36 boost::thread_specific_ptr<RNG> RNG::instance_;36 RNG* RNG::instance_=NULL; 37 37 38 38 RNG::RNG(void) 39 : rng_(gsl_rng_free) 39 40 { 40 41 // support rng/seed changes through environment vars 41 42 if (!gsl_rng_env_setup()) 42 43 throw utility::GSL_error("RNG::RNG unknown generator"); 43 if (!(rng_=gsl_rng_alloc(gsl_rng_default)))44 throw utility::GSL_error("RNG::RNG failed to allocate memory");45 44 } 46 45 … … 48 47 RNG::~RNG(void) 49 48 { 50 gsl_rng_free(rng_);51 rng_=NULL;52 49 } 53 50 … … 55 52 RNG* RNG::instance(void) 56 53 { 57 if (instance_ .get()==NULL)58 instance_ .reset(new RNG);59 return instance_ .get();54 if (instance_==NULL) 55 instance_ = new RNG; 56 return instance_; 60 57 } 61 58 … … 63 60 unsigned long RNG::max(void) const 64 61 { 65 return gsl_rng_max(rng _);62 return gsl_rng_max(rng()); 66 63 } 67 64 … … 69 66 unsigned long RNG::min(void) const 70 67 { 71 return gsl_rng_min(rng _);68 return gsl_rng_min(rng()); 72 69 } 73 70 … … 75 72 std::string RNG::name(void) const 76 73 { 77 return gsl_rng_name(rng _);74 return gsl_rng_name(rng()); 78 75 } 79 76 … … 81 78 const gsl_rng* RNG::rng(void) const 82 79 { 83 return rng_; 80 if (rng_.get()==NULL) 81 rng_alloc(); 82 return rng_.get(); 83 } 84 85 86 void RNG::rng_alloc(void) const 87 { 88 assert(rng_.get()==NULL); 89 gsl_rng* rng = gsl_rng_alloc(gsl_rng_default); 90 if (!rng) 91 throw utility::GSL_error("RNG failed to allocate memory"); 92 rng_.reset(rng); 84 93 } 85 94 … … 87 96 void RNG::seed(unsigned long s) const 88 97 { 89 gsl_rng_set(rng _,s);98 gsl_rng_set(rng(),s); 90 99 } 91 100 … … 108 117 int RNG::set_state(const RNG_state& state) 109 118 { 110 return gsl_rng_memcpy(rng_, state.rng()); 119 if (rng_.get()==NULL) 120 rng_alloc(); 121 return gsl_rng_memcpy(rng_.get(), state.rng()); 111 122 } 112 123 … … 117 128 clone(*rng->rng()); 118 129 } 119 130 120 131 121 132 RNG_state::RNG_state(const RNG_state& state) … … 123 134 clone(*state.rng()); 124 135 } 125 136 126 137 127 138 RNG_state::~RNG_state(void) -
trunk/yat/random/random.h
r2768 r2770 6 6 /* 7 7 Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson 8 Copyright (C) 2009, 2010, 2011 Peter Johansson8 Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson 9 9 10 10 This file is part of the yat library, http://dev.thep.lu.se/yat … … 82 82 { 83 83 public: 84 /**85 \brief destructor86 */87 // FIXME should be private(?) but have problems with88 // thread_specific_ptr<RNG> that want to destruct.89 virtual ~RNG(void);90 84 91 85 /// … … 171 165 RNG& operator=(const RNG&); 172 166 173 static boost::thread_specific_ptr<RNG> instance_; 174 gsl_rng* rng_; 167 virtual ~RNG(void); 168 void rng_alloc(void) const; 169 170 static RNG* instance_; 171 mutable boost::thread_specific_ptr<gsl_rng> rng_; 175 172 }; 176 173
Note: See TracChangeset
for help on using the changeset viewer.