Expose RNG seed/run # to attributes; bindings update
authorRaj Bhattacharjea <raj.b@gatech.edu>
Tue, 20 Jan 2009 14:00:05 -0500
changeset 4221 73b49aa25221
parent 4220 bdce17c3a0e8
child 4222 509a818e8cdf
Expose RNG seed/run # to attributes; bindings update
bindings/python/ns3_module_simulator.py
src/core/rng-stream.cc
src/core/rng-stream.h
--- a/bindings/python/ns3_module_simulator.py	Tue Jan 20 11:41:57 2009 -0500
+++ b/bindings/python/ns3_module_simulator.py	Tue Jan 20 14:00:05 2009 -0500
@@ -55,6 +55,8 @@
     module.add_class('ListScheduler', parent=root_module['ns3::Scheduler'])
     ## map-scheduler.h: ns3::MapScheduler [class]
     module.add_class('MapScheduler', parent=root_module['ns3::Scheduler'])
+    ## ns2-calendar-scheduler.h: ns3::Ns2CalendarScheduler [class]
+    module.add_class('Ns2CalendarScheduler', parent=root_module['ns3::Scheduler'])
     ## realtime-simulator-impl.h: ns3::RealtimeSimulatorImpl [class]
     module.add_class('RealtimeSimulatorImpl', parent=root_module['ns3::SimulatorImpl'])
     ## realtime-simulator-impl.h: ns3::RealtimeSimulatorImpl::SynchronizationMode [enumeration]
@@ -127,6 +129,7 @@
     register_Ns3HeapScheduler_methods(root_module, root_module['ns3::HeapScheduler'])
     register_Ns3ListScheduler_methods(root_module, root_module['ns3::ListScheduler'])
     register_Ns3MapScheduler_methods(root_module, root_module['ns3::MapScheduler'])
+    register_Ns3Ns2CalendarScheduler_methods(root_module, root_module['ns3::Ns2CalendarScheduler'])
     register_Ns3RealtimeSimulatorImpl_methods(root_module, root_module['ns3::RealtimeSimulatorImpl'])
     return
 
@@ -718,7 +721,9 @@
     return
 
 def register_Ns3SchedulerEventKey_methods(root_module, cls):
+    cls.add_binary_comparison_operator('!=')
     cls.add_binary_comparison_operator('<')
+    cls.add_binary_comparison_operator('>')
     ## scheduler.h: ns3::Scheduler::EventKey::EventKey() [constructor]
     cls.add_constructor([])
     ## scheduler.h: ns3::Scheduler::EventKey::EventKey(ns3::Scheduler::EventKey const & arg0) [copy constructor]
@@ -1289,6 +1294,42 @@
     cls.add_copy_constructor()
     return
 
+def register_Ns3Ns2CalendarScheduler_methods(root_module, cls):
+    ## ns2-calendar-scheduler.h: static ns3::TypeId ns3::Ns2CalendarScheduler::GetTypeId() [member function]
+    cls.add_method('GetTypeId', 
+                   'ns3::TypeId', 
+                   [], 
+                   is_static=True)
+    ## ns2-calendar-scheduler.h: ns3::Ns2CalendarScheduler::Ns2CalendarScheduler() [constructor]
+    cls.add_constructor([])
+    ## ns2-calendar-scheduler.h: void ns3::Ns2CalendarScheduler::Insert(ns3::Scheduler::Event const & ev) [member function]
+    cls.add_method('Insert', 
+                   'void', 
+                   [param('ns3::Scheduler::Event const &', 'ev')], 
+                   is_virtual=True)
+    ## ns2-calendar-scheduler.h: bool ns3::Ns2CalendarScheduler::IsEmpty() const [member function]
+    cls.add_method('IsEmpty', 
+                   'bool', 
+                   [], 
+                   is_const=True, is_virtual=True)
+    ## ns2-calendar-scheduler.h: ns3::Scheduler::Event ns3::Ns2CalendarScheduler::PeekNext() const [member function]
+    cls.add_method('PeekNext', 
+                   'ns3::Scheduler::Event', 
+                   [], 
+                   is_const=True, is_virtual=True)
+    ## ns2-calendar-scheduler.h: ns3::Scheduler::Event ns3::Ns2CalendarScheduler::RemoveNext() [member function]
+    cls.add_method('RemoveNext', 
+                   'ns3::Scheduler::Event', 
+                   [], 
+                   is_virtual=True)
+    ## ns2-calendar-scheduler.h: void ns3::Ns2CalendarScheduler::Remove(ns3::Scheduler::Event const & ev) [member function]
+    cls.add_method('Remove', 
+                   'void', 
+                   [param('ns3::Scheduler::Event const &', 'ev')], 
+                   is_virtual=True)
+    cls.add_copy_constructor()
+    return
+
 def register_Ns3RealtimeSimulatorImpl_methods(root_module, cls):
     ## realtime-simulator-impl.h: static ns3::TypeId ns3::RealtimeSimulatorImpl::GetTypeId() [member function]
     cls.add_method('GetTypeId', 
--- a/src/core/rng-stream.cc	Tue Jan 20 11:41:57 2009 -0500
+++ b/src/core/rng-stream.cc	Tue Jan 20 14:00:05 2009 -0500
@@ -21,6 +21,8 @@
 #include <cstdlib>
 #include <iostream>
 #include "rng-stream.h"
+#include "global-value.h"
+#include "integer.h"
 using namespace std;
 
 namespace
@@ -199,7 +201,14 @@
     }
 }
 
-
+static ns3::GlobalValue g_rngSeed ("RngSeed", 
+                                   "The global seed of all rng streams",
+                                   ns3::IntegerValue (1),
+                                   ns3::MakeIntegerChecker<uint32_t> ());
+static ns3::GlobalValue g_rngRun ("RngRun", 
+                                  "The run number used to modify the global seed",
+                                  ns3::IntegerValue (1),
+                                  ns3::MakeIntegerChecker<uint32_t> ());
 
 } // end of anonymous namespace
 
@@ -291,7 +300,61 @@
     return true;
 }
 
-
+void 
+RngStream::EnsureGlobalInitialized (void)
+{
+  static bool initialized = false;
+  if (!initialized)
+    {
+      initialized = true;
+      uint32_t seed;
+      uint32_t run;
+      // First, initialize ourselves from the global value.
+      {
+        IntegerValue value;
+        g_rngSeed.GetValue (value);
+        seed = value.Get ();
+        g_rngRun.GetValue (value);
+        run = value.Get ();
+      }
+      // then, in case we have NS_RNG set, override the global 
+      // value from the env var.
+      char *tmp = getenv ("NS_RNG");
+      if (tmp != 0)
+        {
+          std::string var = std::string (getenv ("NS_RNG"));
+          std::string::size_type colon = var.find (":");
+          if (colon != std::string::npos)
+            {
+              {
+                std::string seedString = var.substr (0, colon);
+                std::istringstream iss;
+                iss.str (seedString);
+                iss >> seed;
+              }
+              {
+                std::string runString = var.substr (colon+1,var.size ()-colon-1);
+                std::istringstream iss;
+                iss.str (runString);
+                iss >> run;
+              }
+            }
+          else
+            {
+              {
+                std::istringstream iss;
+                iss.str (var);
+                iss >> seed;
+              }
+            }
+        }
+      // finally, actually use these values to do something.
+      uint32_t seedArray [] = {seed, seed, seed, seed, seed, seed};
+      SetPackageSeed (seedArray);
+      // set to the chosen substream (run)
+      ResetNthSubstream (run);
+    }
+}
 
 //*************************************************************************
 // Public members of the class start here
@@ -311,6 +374,7 @@
 //
 RngStream::RngStream ()
 {
+  EnsureGlobalInitialized ();
    anti = false;
    incPrec = false;
    // Stream initialization moved to separate method.
--- a/src/core/rng-stream.h	Tue Jan 20 11:41:57 2009 -0500
+++ b/src/core/rng-stream.h	Tue Jan 20 14:00:05 2009 -0500
@@ -60,6 +60,7 @@
   bool anti, incPrec;
   double U01 ();
   double U01d ();
+  static void EnsureGlobalInitialized (void);
 private: //static data
   static double nextSeed[6];
 };