Changeset 2770


Ignore:
Timestamp:
Jul 11, 2012, 4:44:45 PM (10 years ago)
Author:
Peter
Message:

change back to RNG being a singleton, which now stores gsl_rng* in a thread_specific_ptr<gsl_rng>. refs #568

Location:
trunk/yat/random
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/random/random.cc

    r2768 r2770  
    3434namespace random {
    3535
    36   boost::thread_specific_ptr<RNG> RNG::instance_;
     36  RNG* RNG::instance_=NULL;
    3737
    3838  RNG::RNG(void)
     39    : rng_(gsl_rng_free)
    3940  {
    4041      // support rng/seed changes through environment vars
    4142    if (!gsl_rng_env_setup())
    4243      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");
    4544  }
    4645
     
    4847  RNG::~RNG(void)
    4948  {
    50     gsl_rng_free(rng_);
    51     rng_=NULL;
    5249  }
    5350
     
    5552  RNG* RNG::instance(void)
    5653  {
    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_;
    6057  }
    6158
     
    6360  unsigned long RNG::max(void) const
    6461  {
    65     return gsl_rng_max(rng_);
     62    return gsl_rng_max(rng());
    6663  }
    6764
     
    6966  unsigned long RNG::min(void) const
    7067  {
    71     return gsl_rng_min(rng_);
     68    return gsl_rng_min(rng());
    7269  }
    7370
     
    7572  std::string RNG::name(void) const
    7673  {
    77     return gsl_rng_name(rng_);
     74    return gsl_rng_name(rng());
    7875  }
    7976
     
    8178  const gsl_rng* RNG::rng(void) const
    8279  {
    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);
    8493  }
    8594
     
    8796  void RNG::seed(unsigned long s) const
    8897  {
    89     gsl_rng_set(rng_,s);
     98    gsl_rng_set(rng(),s);
    9099  }
    91100
     
    108117  int RNG::set_state(const RNG_state& state)
    109118  {
    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());
    111122  }
    112123
     
    117128    clone(*rng->rng());
    118129  }
    119  
     130
    120131
    121132  RNG_state::RNG_state(const RNG_state& state)
     
    123134    clone(*state.rng());
    124135  }
    125  
     136
    126137
    127138  RNG_state::~RNG_state(void)
  • trunk/yat/random/random.h

    r2768 r2770  
    66/*
    77  Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
    8   Copyright (C) 2009, 2010, 2011 Peter Johansson
     8  Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson
    99
    1010  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    8282  {
    8383  public:
    84     /**
    85        \brief destructor
    86      */
    87     // FIXME should be private(?) but have problems with
    88     // thread_specific_ptr<RNG> that want to destruct.
    89     virtual ~RNG(void);
    9084
    9185    ///
     
    171165    RNG& operator=(const RNG&);
    172166
    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_;
    175172  };
    176173
Note: See TracChangeset for help on using the changeset viewer.