• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

sst/elements/genericProc/programs/MTGL/mtgl/random.h

Go to the documentation of this file.
00001 /*  _________________________________________________________________________
00002  *
00003  *  MTGL: The MultiThreaded Graph Library
00004  *  Copyright (c) 2008 Sandia Corporation.
00005  *  This software is distributed under the BSD License.
00006  *  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00007  *  the U.S. Government retains certain rights in this software.
00008  *  For more information, see the README file in the top MTGL directory.
00009  *  _________________________________________________________________________
00010  */
00011 
00012 /****************************************************************************/
00013 /*! \file random.h
00014 
00015     \author Jon Berry (jberry@sandia.gov)
00016 
00017     \date 12/4/2007
00018 */
00019 /****************************************************************************/
00020 
00021 #ifndef MTGL_RANDOM_H
00022 #define MTGL_RANDOM_H
00023 
00024 #include <cstdlib>
00025 
00026 #include <mtgl/util.hpp>
00027 #include <mtgl/rand_fill.hpp>
00028 
00029 namespace mtgl {
00030 
00031 class random {
00032 public:
00033   random(long int sd = 0, int sz = 5000) : seed(sd), size(sz)
00034   {
00035     // No seed for prand?
00036     _store = (random_value*) malloc(size * sizeof(random_value));
00037     rand_fill::generate(size, _store);
00038     current = 0;
00039     refills = 0;
00040     numTaken = 0;
00041   }
00042 
00043   /// \brief Return the rank'th random value in a sequence of sequences of
00044   ///        random values.
00045   random_value nthValue(int rank)  // Rank is which number in the sequence
00046   {                                // you want.
00047     random_value result;
00048     bool my_values_ready = false;
00049     int index = rank % size;
00050     int turn  = rank / size;
00051 
00052     while (!my_values_ready)
00053     {
00054       int nrefills = mt_readff(refills);
00055       if (nrefills == turn) my_values_ready = true;
00056     }
00057 
00058     result = _store[index];
00059     int nt = mt_incr(numTaken, 1);
00060 
00061     if (nt == size - 1)
00062     {
00063       rand_fill::generate(size, _store);  // Really need to seed!
00064       mt_incr(numTaken, -numTaken);
00065       mt_incr(refills, 1);
00066     }
00067 
00068     return result;
00069   }
00070 
00071   ~random() { free(_store); }
00072 
00073 private:
00074   long int seed;
00075   int size;
00076   int current;
00077   int numTaken;
00078   random_value* _store;
00079   int refills;
00080 };
00081 
00082 }
00083 
00084 #endif

Generated on Fri Oct 22 2010 11:02:23 for SST by  doxygen 1.7.1