Replace src/wifi usage of RandomVariable with RandomVariableStream
authorMitch Watrous
Tue, 14 Aug 2012 13:52:17 -0700
changeset 8981 7e1c95c4d1a7
parent 8980 0202e0b04282
child 8982 a85e48eb2acb
Replace src/wifi usage of RandomVariable with RandomVariableStream
src/wifi/helper/wifi-helper.cc
src/wifi/helper/wifi-helper.h
src/wifi/helper/yans-wifi-helper.cc
src/wifi/helper/yans-wifi-helper.h
src/wifi/model/dca-txop.cc
src/wifi/model/dca-txop.h
src/wifi/model/edca-txop-n.cc
src/wifi/model/edca-txop-n.h
src/wifi/model/minstrel-wifi-manager.cc
src/wifi/model/minstrel-wifi-manager.h
src/wifi/model/random-stream.cc
src/wifi/model/random-stream.h
src/wifi/model/wifi-phy.cc
src/wifi/model/wifi-phy.h
src/wifi/model/yans-wifi-channel.cc
src/wifi/model/yans-wifi-channel.h
src/wifi/model/yans-wifi-phy.cc
src/wifi/model/yans-wifi-phy.h
--- a/src/wifi/helper/wifi-helper.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/helper/wifi-helper.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -22,6 +22,10 @@
 #include "wifi-helper.h"
 #include "ns3/wifi-net-device.h"
 #include "ns3/wifi-mac.h"
+#include "ns3/regular-wifi-mac.h"
+#include "ns3/dca-txop.h"
+#include "ns3/edca-txop-n.h"
+#include "ns3/minstrel-wifi-manager.h"
 #include "ns3/wifi-phy.h"
 #include "ns3/wifi-remote-station-manager.h"
 #include "ns3/wifi-channel.h"
@@ -31,6 +35,7 @@
 #include "ns3/mobility-model.h"
 #include "ns3/log.h"
 #include "ns3/config.h"
+#include "ns3/pointer.h"
 #include "ns3/simulator.h"
 #include "ns3/names.h"
 
@@ -163,4 +168,57 @@
   LogComponentEnable ("YansWifiPhy", LOG_LEVEL_ALL);
 }
 
+int64_t
+WifiHelper::AssignStreams (NetDeviceContainer c, int64_t stream)
+{
+  int64_t currentStream = stream;
+  Ptr<NetDevice> netDevice;
+  for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
+    {
+      netDevice = (*i);
+      Ptr<WifiNetDevice> wifi = DynamicCast<WifiNetDevice> (netDevice);
+      if (wifi)
+        {
+          // Handle any random numbers in the PHY objects.
+          currentStream += wifi->GetPhy ()->AssignStreams (currentStream);
+
+          // Handle any random numbers in the station managers.
+          Ptr<WifiRemoteStationManager> manager = wifi->GetRemoteStationManager ();
+          Ptr<MinstrelWifiManager> minstrel = DynamicCast<MinstrelWifiManager> (manager);
+          if (minstrel)
+            {
+              currentStream += minstrel->AssignStreams (currentStream);
+            }
+
+          // Handle any random numbers in the MAC objects.
+          Ptr<WifiMac> mac = wifi->GetMac ();
+          Ptr<RegularWifiMac> rmac = DynamicCast<RegularWifiMac> (mac);
+          if (rmac)
+            {
+              PointerValue ptr;
+              rmac->GetAttribute ("DcaTxop", ptr);
+              Ptr<DcaTxop> dcaTxop = ptr.Get<DcaTxop> ();
+              currentStream += dcaTxop->AssignStreams (currentStream);
+
+              rmac->GetAttribute ("VO_EdcaTxopN", ptr);
+              Ptr<EdcaTxopN> vo_edcaTxopN = ptr.Get<EdcaTxopN> ();
+              currentStream += vo_edcaTxopN->AssignStreams (currentStream);
+
+              rmac->GetAttribute ("VI_EdcaTxopN", ptr);
+              Ptr<EdcaTxopN> vi_edcaTxopN = ptr.Get<EdcaTxopN> ();
+              currentStream += vi_edcaTxopN->AssignStreams (currentStream);
+
+              rmac->GetAttribute ("BE_EdcaTxopN", ptr);
+              Ptr<EdcaTxopN> be_edcaTxopN = ptr.Get<EdcaTxopN> ();
+              currentStream += be_edcaTxopN->AssignStreams (currentStream);
+
+              rmac->GetAttribute ("BK_EdcaTxopN", ptr);
+              Ptr<EdcaTxopN> bk_edcaTxopN = ptr.Get<EdcaTxopN> ();
+              currentStream += bk_edcaTxopN->AssignStreams (currentStream);
+            }
+        }
+    }
+  return (currentStream - stream);
+}
+
 } // namespace ns3
--- a/src/wifi/helper/wifi-helper.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/helper/wifi-helper.h	Tue Aug 14 13:52:17 2012 -0700
@@ -171,6 +171,19 @@
    */
   static void EnableLogComponents (void);
 
+  /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model. Return the number of streams (possibly zero) that
+  * have been assigned. The Install() method should have previously been
+  * called by the user.
+  *
+  * \param c NetDeviceContainer of the set of net devices for which the 
+  *          WifiNetDevice should be modified to use a fixed stream
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this helper
+  */
+  int64_t AssignStreams (NetDeviceContainer c, int64_t stream);
+
 private:
   ObjectFactory m_stationManager;
   enum WifiPhyStandard m_standard;
--- a/src/wifi/helper/yans-wifi-helper.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/helper/yans-wifi-helper.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -536,4 +536,11 @@
   Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyTransmitSinkWithContext, stream));
 }
 
+int64_t
+YansWifiPhyHelper::AssignStreams (int64_t stream)
+{
+  NS_LOG_FUNCTION (this << stream);
+  return m_channel->AssignStreams (stream);
+}
+
 } // namespace ns3
--- a/src/wifi/helper/yans-wifi-helper.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/helper/yans-wifi-helper.h	Tue Aug 14 13:52:17 2012 -0700
@@ -230,6 +230,16 @@
    */
   void SetPcapDataLinkType (enum SupportedPcapDataLinkTypes dlt);
 
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  int64_t AssignStreams (int64_t stream);
+
 private:
   /**
    * \param node the node on which we wish to create a wifi PHY
--- a/src/wifi/model/dca-txop.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/dca-txop.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -242,6 +242,14 @@
   StartAccessIfNeeded ();
 }
 
+int64_t
+DcaTxop::AssignStreams (int64_t stream)
+{
+  NS_LOG_FUNCTION (this << stream);
+  m_rng->AssignStreams (stream);
+  return 1;
+}
+
 void
 DcaTxop::RestartAccessIfNeeded (void)
 {
--- a/src/wifi/model/dca-txop.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/dca-txop.h	Tue Aug 14 13:52:17 2012 -0700
@@ -107,6 +107,16 @@
    */
   void Queue (Ptr<const Packet> packet, const WifiMacHeader &hdr);
 
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  int64_t AssignStreams (int64_t stream);
+
 private:
   class TransmissionListener;
   class NavListener;
@@ -163,7 +173,6 @@
   TransmissionListener *m_transmissionListener;
   RandomStream *m_rng;
 
-
   bool m_accessOngoing;
   Ptr<const Packet> m_currentPacket;
   WifiMacHeader m_currentHdr;
--- a/src/wifi/model/edca-txop-n.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/edca-txop-n.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -1104,6 +1104,15 @@
 
   PushFront (packet, hdr);
 }
+
+int64_t
+EdcaTxopN::AssignStreams (int64_t stream)
+{
+  NS_LOG_FUNCTION (this << stream);
+  m_rng->AssignStreams (stream);
+  return 1;
+}
+
 void
 EdcaTxopN::DoStart ()
 {
--- a/src/wifi/model/edca-txop-n.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/edca-txop-n.h	Tue Aug 14 13:52:17 2012 -0700
@@ -150,6 +150,16 @@
   void SetBlockAckInactivityTimeout (uint16_t timeout);
   void SendDelbaFrame (Mac48Address addr, uint8_t tid, bool byOriginator);
 
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  int64_t AssignStreams (int64_t stream);
+
 private:
   void DoStart ();
   /**
--- a/src/wifi/model/minstrel-wifi-manager.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/minstrel-wifi-manager.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -30,7 +30,6 @@
 
 #include "minstrel-wifi-manager.h"
 #include "wifi-phy.h"
-#include "ns3/random-variable.h"
 #include "ns3/simulator.h"
 #include "ns3/log.h"
 #include "ns3/uinteger.h"
@@ -121,6 +120,8 @@
 
 MinstrelWifiManager::MinstrelWifiManager ()
 {
+  m_uniformRandomVariable = CreateObject<UniformRandomVariable> ();
+
   m_nsupported = 0;
 }
 
@@ -140,6 +141,14 @@
   WifiRemoteStationManager::SetupPhy (phy);
 }
 
+int64_t
+MinstrelWifiManager::AssignStreams (int64_t stream)
+{
+  NS_LOG_FUNCTION (this << stream);
+  m_uniformRandomVariable->SetStream (stream);
+  return 1;
+}
+
 Time
 MinstrelWifiManager::GetCalcTxTime (WifiMode mode) const
 {
@@ -490,7 +499,7 @@
   uint32_t idx;
 
   /// for determining when to try a sample rate
-  UniformVariable coinFlip (0, 100);
+  int coinFlip = m_uniformRandomVariable->GetInteger (0, 100) % 2;
 
   /**
    * if we are below the target of look around rate percentage, look around
@@ -498,7 +507,7 @@
    * all at once until it reaches the look around rate
    */
   if ( (((100 * station->m_sampleCount) / (station->m_sampleCount + station->m_packetCount )) < m_lookAroundRate)
-       && ((int)coinFlip.GetValue ()) % 2 == 1 )
+       && (coinFlip == 1) )
     {
 
       /// now go through the table and find an index rate
@@ -746,8 +755,8 @@
            * The next two lines basically tries to generate a random number
            * between 0 and the number of available rates
            */
-          UniformVariable uv (0, numSampleRates);
-          newIndex = (i + (uint32_t)uv.GetValue ()) % numSampleRates;
+          int uv = m_uniformRandomVariable->GetInteger (0, numSampleRates);
+          newIndex = (i + uv) % numSampleRates;
 
           /// this loop is used for filling in other uninitilized places
           while (m_sampleTable[newIndex][col] != 0)
--- a/src/wifi/model/minstrel-wifi-manager.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/minstrel-wifi-manager.h	Tue Aug 14 13:52:17 2012 -0700
@@ -26,9 +26,7 @@
 #include "wifi-remote-station-manager.h"
 #include "wifi-mode.h"
 #include "ns3/nstime.h"
-#include <vector>
-
-
+#include "ns3/random-variable-stream.h"
 
 namespace ns3 {
 
@@ -96,6 +94,16 @@
 
   virtual void SetupPhy (Ptr<WifiPhy> phy);
 
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  int64_t AssignStreams (int64_t stream);
+
 private:
   // overriden from base class
   virtual WifiRemoteStation * DoCreateStation (void) const;
@@ -157,6 +165,9 @@
   uint32_t m_sampleCol;  ///< number of sample columns
   uint32_t m_pktLen;  ///< packet length used  for calculate mode TxTime
   uint32_t m_nsupported;  ///< modes supported
+
+  /// Provides uniform random variables.
+  Ptr<UniformRandomVariable> m_uniformRandomVariable;
 };
 
 } // namespace ns3
--- a/src/wifi/model/random-stream.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/random-stream.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -30,15 +30,22 @@
 
 
 RealRandomStream::RealRandomStream ()
-  : m_stream (UniformVariable ())
 {
+  m_stream = CreateObject<UniformRandomVariable> ();
 }
+
 uint32_t
 RealRandomStream::GetNext (uint32_t min, uint32_t max)
 {
-  return m_stream.GetInteger (min, max);
+  return m_stream->GetInteger (min, max);
 }
 
+int64_t
+RealRandomStream::AssignStreams (int64_t stream)
+{
+  m_stream->SetStream (stream);
+  return 1;
+}
 
 void
 TestRandomStream::AddNext (uint32_t v)
@@ -55,4 +62,10 @@
   return next;
 }
 
+int64_t
+TestRandomStream::AssignStreams (int64_t stream)
+{
+  return 0;
+}
+
 } // namespace ns3
--- a/src/wifi/model/random-stream.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/random-stream.h	Tue Aug 14 13:52:17 2012 -0700
@@ -22,7 +22,7 @@
 
 #include <stdint.h>
 #include <list>
-#include "ns3/random-variable.h"
+#include "ns3/random-variable-stream.h"
 
 namespace ns3 {
 
@@ -35,6 +35,16 @@
 public:
   virtual ~RandomStream ();
   virtual uint32_t GetNext (uint32_t min, uint32_t max) = 0;
+
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  virtual int64_t AssignStreams (int64_t stream) = 0;
 };
 
 class RealRandomStream : public RandomStream
@@ -42,8 +52,20 @@
 public:
   RealRandomStream ();
   virtual uint32_t GetNext (uint32_t min, uint32_t max);
+
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  virtual int64_t AssignStreams (int64_t stream);
+
 private:
-  UniformVariable m_stream;
+  /// Provides uniform random variables.
+  Ptr<UniformRandomVariable> m_stream;
 };
 
 class TestRandomStream : public RandomStream
@@ -51,6 +73,17 @@
 public:
   void AddNext (uint32_t v);
   virtual uint32_t GetNext (uint32_t min, uint32_t max);
+
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  virtual int64_t AssignStreams (int64_t stream);
+
 private:
   std::list<uint32_t> m_nexts;
 };
--- a/src/wifi/model/wifi-phy.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/wifi-phy.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -24,7 +24,6 @@
 #include "wifi-preamble.h"
 #include "ns3/simulator.h"
 #include "ns3/packet.h"
-#include "ns3/random-variable.h"
 #include "ns3/assert.h"
 #include "ns3/log.h"
 #include "ns3/double.h"
--- a/src/wifi/model/wifi-phy.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/wifi-phy.h	Tue Aug 14 13:52:17 2012 -0700
@@ -449,6 +449,16 @@
    */
   void NotifyMonitorSniffTx (Ptr<const Packet> packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble);
 
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  virtual int64_t AssignStreams (int64_t stream) = 0;
+
 
 private:
   /**
--- a/src/wifi/model/yans-wifi-channel.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/yans-wifi-channel.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -138,4 +138,16 @@
   m_phyList.push_back (phy);
 }
 
+int64_t
+YansWifiChannel::AssignStreams (int64_t stream)
+{
+  int64_t currentStream = stream;
+  for (PhyList::const_iterator i = m_phyList.begin (); i != m_phyList.end (); i++)
+    {
+      Ptr<YansWifiPhy> yans = (*i);
+      currentStream += yans->AssignStreams (currentStream);
+    }
+  return (currentStream - stream);
+}
+
 } // namespace ns3
--- a/src/wifi/model/yans-wifi-channel.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/yans-wifi-channel.h	Tue Aug 14 13:52:17 2012 -0700
@@ -84,6 +84,16 @@
   void Send (Ptr<YansWifiPhy> sender, Ptr<const Packet> packet, double txPowerDbm,
              WifiMode wifiMode, WifiPreamble preamble) const;
 
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  int64_t AssignStreams (int64_t stream);
+
 private:
   YansWifiChannel& operator = (const YansWifiChannel &);
   YansWifiChannel (const YansWifiChannel &);
--- a/src/wifi/model/yans-wifi-phy.cc	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/yans-wifi-phy.cc	Tue Aug 14 13:52:17 2012 -0700
@@ -26,7 +26,6 @@
 #include "error-rate-model.h"
 #include "ns3/simulator.h"
 #include "ns3/packet.h"
-#include "ns3/random-variable.h"
 #include "ns3/assert.h"
 #include "ns3/log.h"
 #include "ns3/double.h"
@@ -128,10 +127,10 @@
 YansWifiPhy::YansWifiPhy ()
   :  m_channelNumber (1),
     m_endRxEvent (),
-    m_random (0.0, 1.0),
     m_channelStartingFrequency (0)
 {
   NS_LOG_FUNCTION (this);
+  m_random = CreateObject<UniformRandomVariable> ();
   m_state = CreateObject<WifiPhyStateHelper> ();
 }
 
@@ -780,7 +779,7 @@
 
   NS_LOG_DEBUG ("mode=" << (event->GetPayloadMode ().GetDataRate ()) <<
                 ", snr=" << snrPer.snr << ", per=" << snrPer.per << ", size=" << packet->GetSize ());
-  if (m_random.GetValue () > snrPer.per)
+  if (m_random->GetValue () > snrPer.per)
     {
       NotifyRxEnd (packet);
       uint32_t dataRate500KbpsUnits = event->GetPayloadMode ().GetDataRate () / 500000;
@@ -797,4 +796,12 @@
       m_state->SwitchFromRxEndError (packet, snrPer.snr);
     }
 }
+
+int64_t
+YansWifiPhy::AssignStreams (int64_t stream)
+{
+  NS_LOG_FUNCTION (this << stream);
+  m_random->SetStream (stream);
+  return 1;
+}
 } // namespace ns3
--- a/src/wifi/model/yans-wifi-phy.h	Tue Aug 14 13:12:00 2012 -0700
+++ b/src/wifi/model/yans-wifi-phy.h	Tue Aug 14 13:52:17 2012 -0700
@@ -29,7 +29,7 @@
 #include "ns3/traced-callback.h"
 #include "ns3/nstime.h"
 #include "ns3/ptr.h"
-#include "ns3/random-variable.h"
+#include "ns3/random-variable-stream.h"
 #include "wifi-phy.h"
 #include "wifi-mode.h"
 #include "wifi-preamble.h"
@@ -141,6 +141,16 @@
   virtual Ptr<WifiChannel> GetChannel (void) const;
   virtual void ConfigureStandard (enum WifiPhyStandard standard);
 
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  int64_t AssignStreams (int64_t stream);
+
 private:
   YansWifiPhy (const YansWifiPhy &o);
   virtual void DoDispose (void);
@@ -213,7 +223,8 @@
   WifiModeList m_deviceRateSet;
 
   EventId m_endRxEvent;
-  UniformVariable m_random;
+  /// Provides uniform random variables.
+  Ptr<UniformRandomVariable> m_random;
   /// Standard-dependent center frequency of 0-th channel, MHz
   double m_channelStartingFrequency;
   Ptr<WifiPhyStateHelper> m_state;