00001 // -*- C++ -*- <this line is for emacs to recognize it as C++ code> 00002 /***************************************************************************** 00003 00004 The following code is derived, directly or indirectly, from the SystemC 00005 source code Copyright (c) 1996-2002 by all Contributors. 00006 All Rights reserved. 00007 00008 The contents of this file are subject to the restrictions and limitations 00009 set forth in the SystemC Open Source License Version 2.3 (the "License"); 00010 You may not use this file except in compliance with such restrictions and 00011 limitations. You may obtain instructions on how to receive a copy of the 00012 License at http://www.systemc.org/. Software distributed by Contributors 00013 under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 00014 ANY KIND, either express or implied. See the License for the specific 00015 language governing rights and limitations under the License. 00016 00017 *****************************************************************************/ 00018 00019 /***************************************************************************** 00020 00021 scv_random.h -- 00022 The public interface for unsigned random stream and seed management 00023 00024 - Provides global configuration methods for seed control 00025 - Allows specifying custom random number generation algorithms for 00026 all or specific random streams 00027 - Multiple independent random streams 00028 - Manage seed file registry for explicit seed control 00029 - To make a consistent interface for seed control all seeds are 00030 managed by using 'unsigned long long type' 00031 00032 Original Authors (Cadence Design Systems, Inc): 00033 Norris Ip, Dean Shea, John Rose, Jasvinder Singh, William Paulsen, 00034 John Pierce, Rachida Kebichi, Ted Elkind, David Bailey 00035 2002-09-23 00036 00037 *****************************************************************************/ 00038 00039 /***************************************************************************** 00040 00041 MODIFICATION LOG - modifiers, enter your name, affiliation, date and 00042 changes you are making here. 00043 00044 Name, Affiliation, Date: 00045 Description of Modification: 00046 00047 *****************************************************************************/ 00048 00049 #ifndef SCV_RANDOM_H 00050 #define SCV_RANDOM_H 00051 00052 #include "scv/scv_config.h" 00053 00054 #include <stdlib.h> 00055 #include <stdio.h> 00056 00057 #include <list> 00058 00059 #include "scv/_scv_data_structure.h" 00060 #include "scv/_scv_associative_array.h" 00061 00062 class _scv_random_impl; 00063 00064 class scv_random : public _scv_data_structure { 00065 public: // global configuration on algorithm and seed 00066 // value_generation_algorithm enum type specifies which 00067 // algorithm to use for generating unsigned random values 00068 // RAND - specifies using C rand() function 00069 // RAND32 - specifies using C rand() function 00070 // RAND48 - use jrand48() for uniform unsigned random streams 00071 // CUSTOM - use custom algorithm specified by using set_default_algorithm 00072 // for global configuration or set_algorithm for 00073 // specific random streams 00074 enum value_generation_algorithm { 00075 RAND, // C rand() will 00076 RAND32, // C rand() will 00077 RAND48, // jrand48 () 00078 CUSTOM // plugin a custom random number generation algorithm. 00079 }; 00080 00081 typedef unsigned int (*alg_func)(unsigned long long& next); 00082 00083 00084 // set_global_seed sets the global seed for randomization control 00085 // All random streams created explicitly or implicitly will have 00086 // seed based on the thread it is being created in and global 00087 // seed. So by setting just one global seed you can ensure same 00088 // random values for same test bench 00089 00090 static void set_global_seed (unsigned long long=1); 00091 static unsigned long long get_global_seed (void); 00092 00093 static unsigned long long pick_random_seed(unsigned long job_number=0); 00094 00095 // set_default_algorithm specifies default algorithm to use for 00096 // all the scv_random streams. You can override this setting using 00097 // set_algorithm for specific streams 00098 00099 static void set_default_algorithm(value_generation_algorithm alg, 00100 alg_func customAlg = NULL); 00101 public: // debugging and seed files management 00102 00103 // access the list of all current scv_random objects 00104 static void get_generators(list<scv_random *>& genList); 00105 00106 public: // constructors for independent random stream 00107 00108 // constructor to explicitly specify a unique name for random 00109 // stream. Names are used in the seed file registry to save and 00110 // restore seeds. Using unique names for random streams you can 00111 // have same random values in different simulation results even 00112 // with a slight change in the test bench 00113 // default value is set to "<anonymous"> if no name is specified 00114 00115 scv_random(const char* name = NULL); 00116 00117 // constructor to provide explicit seed value 00118 scv_random(unsigned long long seed); 00119 00120 // constructor to provide both explicit name and seed 00121 scv_random(const char* name, unsigned long long seed); 00122 00123 // copy constructor with means to provide explicit name and seed 00124 // If no name and seed is provided it will use the same algorithm 00125 // as other but with name as "anonymous" and create unique seed 00126 // based on thread and global seed 00127 00128 scv_random(const scv_random& other, 00129 const char* name= NULL, unsigned long long seed=0); 00130 00131 // virtual destructof because of inheritance 00132 virtual ~scv_random(); 00133 00134 public: // random value generation and configuration of individual random stream 00135 00136 // return new random value based on the algorithm specified 00137 unsigned int next(void); 00138 00139 // provide algorithm to generate random value for a specfic random stream. 00140 // by default use jrand48() 00141 00142 void set_algorithm(value_generation_algorithm m = RAND48, 00143 alg_func algorithm = NULL); 00144 00145 // return initial seed value for the random stream. 00146 // initial seed is the seed assigned to this object when it is created 00147 00148 unsigned long long get_initial_seed() const; 00149 00150 // return current seed value for this object 00151 // current seed value is the value that will be used to generate the next 00152 // unsigned random value 00153 // one way to use this information is to restart simulation starting from 00154 // the point it left before. 00155 unsigned long long get_current_seed() const; 00156 00157 // set current seed value to seed. 00158 void set_current_seed(unsigned long long seed); 00159 00160 public: // print the seeds of all current scv_random objects 00161 00162 // initial seed is the seed assigned to this object when it is created 00163 // current seed is the current seed value that will be used to generate 00164 // the next unsigned integer value 00165 00166 static void print_initial_seeds(const char* fileName); 00167 static void print_initial_seeds(ostream& = scv_out); 00168 static void print_current_seeds(const char* fileName); 00169 static void print_current_seeds(ostream& = scv_out); 00170 00171 public: // a monitor saves or restores seeds from a file 00172 00173 // starts global seed monitoring on. based on retrieve seeds will 00174 // be saved or retrieved from 'fileName' 00175 // if retrieve is false save seed values to 'fileName' 00176 // if retrieve is true obtain values of seeds from 'fileName' 00177 00178 static void seed_monitor_on(bool retrieve, const char* fileName); 00179 00180 static void seed_monitor_on(bool retrieve, const char* monitorName, FILE * file); 00181 00182 // set global seed monitoring off 00183 static void seed_monitor_off(); 00184 00185 public: // debugging interface 00186 00187 // look at scv_object_if.h for explanation of these methods 00188 const char *kind() const; 00189 void print(ostream& o=scv_out, int details=0, int indent=0) const; 00190 void show(int details=0, int indent=0) const; 00191 static int get_debug(); 00192 static void set_debug(int i); 00193 private: 00194 _scv_random_impl* _coreP; 00195 00196 private: 00197 static unsigned long long global_seed; 00198 static value_generation_algorithm global_alg_type; 00199 }; 00200 00201 00202 #endif
1.2.18