Worker Code Cleanup default tip
authorHagen Paul Pfeifer <hagen@jauu.net>
Sun Dec 21 19:14:22 2008 +0100 (2008-12-21)
changeset 40278f73c18ed9a1
parent 4026 e9c8f81ab5fc
Worker Code Cleanup

This pathc cleanup the code and
introducre more common default
values.
examples/worker-adhoc.cc
src/core/worker.h
     1.1 --- a/examples/worker-adhoc.cc	Tue Dec 16 00:59:36 2008 +0100
     1.2 +++ b/examples/worker-adhoc.cc	Sun Dec 21 19:14:22 2008 +0100
     1.3 @@ -28,12 +28,10 @@
     1.4  
     1.5  using namespace ns3;
     1.6  
     1.7 -#include <iostream>
     1.8 +#define	NUMER_ITERATIONS 6
     1.9 +#define	WORKERS (Worker::GetNumCores ())
    1.10  
    1.11 -#define	NUMER_ITERATIONS 10
    1.12 -#define	WORKERS 1 //(Worker::GetNumCores ())
    1.13 -
    1.14 -#define	COMMUNICATION_STOP 1000.0
    1.15 +#define	COMMUNICATION_STOP 100.0
    1.16  
    1.17  using namespace ns3;
    1.18  
    1.19 @@ -41,94 +39,94 @@
    1.20  {
    1.21    uint32_t i;
    1.22  
    1.23 -  std::cerr << "Worker: " << id << std::endl;
    1.24 -  for (i = Worker::ChunkStart (id, WORKERS, NUMER_ITERATIONS);
    1.25 -       i < Worker::ChunkEnd   (id, WORKERS, NUMER_ITERATIONS);
    1.26 +  for (i =  Worker::ChunkStart (id, WORKERS, NUMER_ITERATIONS);
    1.27 +       i <= Worker::ChunkEnd   (id, WORKERS, NUMER_ITERATIONS);
    1.28  	   i++)
    1.29    {
    1.30 +    std::cerr << "Worker " << id << " process job #"
    1.31 +	          << i << std::endl;
    1.32  
    1.33 -  NodeContainer c;
    1.34 -  c.Create (5);
    1.35 -  NodeContainer n02 = NodeContainer (c.Get(0), c.Get (2));
    1.36 -  NodeContainer n12 = NodeContainer (c.Get(1), c.Get (2));
    1.37 -  NodeContainer n32 = NodeContainer (c.Get(3), c.Get (2));
    1.38 -  NodeContainer n34 = NodeContainer (c.Get (3), c.Get (4));
    1.39 +    NodeContainer c;
    1.40 +    c.Create (5);
    1.41 +    NodeContainer n02 = NodeContainer (c.Get(0), c.Get (2));
    1.42 +    NodeContainer n12 = NodeContainer (c.Get(1), c.Get (2));
    1.43 +    NodeContainer n32 = NodeContainer (c.Get(3), c.Get (2));
    1.44 +    NodeContainer n34 = NodeContainer (c.Get (3), c.Get (4));
    1.45  
    1.46 -  InternetStackHelper internet;
    1.47 -  internet.Install (c);
    1.48 +    InternetStackHelper internet;
    1.49 +    internet.Install (c);
    1.50  
    1.51 -  // We create the channels first without any IP addressing information
    1.52 -  PointToPointHelper p2p;
    1.53 -  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
    1.54 -  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
    1.55 -  NetDeviceContainer nd02 = p2p.Install (n02);
    1.56 -  NetDeviceContainer nd12 = p2p.Install (n12);
    1.57 -  p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
    1.58 -  p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
    1.59 -  NetDeviceContainer nd32 = p2p.Install (n32);
    1.60 -  NetDeviceContainer nd34 = p2p.Install (n34);
    1.61 +    // We create the channels first without any IP addressing information
    1.62 +    PointToPointHelper p2p;
    1.63 +    p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
    1.64 +    p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
    1.65 +    NetDeviceContainer nd02 = p2p.Install (n02);
    1.66 +    NetDeviceContainer nd12 = p2p.Install (n12);
    1.67 +    p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
    1.68 +    p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
    1.69 +    NetDeviceContainer nd32 = p2p.Install (n32);
    1.70 +    NetDeviceContainer nd34 = p2p.Install (n34);
    1.71  
    1.72 -  Ipv4AddressHelper ipv4;
    1.73 -  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
    1.74 -  Ipv4InterfaceContainer i02 = ipv4.Assign (nd02);
    1.75 +    Ipv4AddressHelper ipv4;
    1.76 +    ipv4.SetBase ("10.1.1.0", "255.255.255.0");
    1.77 +    Ipv4InterfaceContainer i02 = ipv4.Assign (nd02);
    1.78  
    1.79 -  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
    1.80 -  Ipv4InterfaceContainer i12 = ipv4.Assign (nd12);
    1.81 +    ipv4.SetBase ("10.1.2.0", "255.255.255.0");
    1.82 +    Ipv4InterfaceContainer i12 = ipv4.Assign (nd12);
    1.83  
    1.84 -  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
    1.85 -  Ipv4InterfaceContainer i32 = ipv4.Assign (nd32);
    1.86 +    ipv4.SetBase ("10.1.3.0", "255.255.255.0");
    1.87 +    Ipv4InterfaceContainer i32 = ipv4.Assign (nd32);
    1.88  
    1.89 -  ipv4.SetBase ("10.1.4.0", "255.255.255.0");
    1.90 -  Ipv4InterfaceContainer i34 = ipv4.Assign (nd34);
    1.91 +    ipv4.SetBase ("10.1.4.0", "255.255.255.0");
    1.92 +    Ipv4InterfaceContainer i34 = ipv4.Assign (nd34);
    1.93  
    1.94 -  OlsrHelper olsr;
    1.95 -  olsr.InstallAll ();
    1.96 +    OlsrHelper olsr;
    1.97 +    olsr.InstallAll ();
    1.98  
    1.99 -  uint16_t port = 9;   // Discard port (RFC 863)
   1.100 +    uint16_t port = 9;   // Discard port (RFC 863)
   1.101  
   1.102 -  OnOffHelper onoff ("ns3::UdpSocketFactory",
   1.103 -                     InetSocketAddress (i34.GetAddress (1), port));
   1.104 -  onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
   1.105 -  onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
   1.106 +    OnOffHelper onoff ("ns3::UdpSocketFactory",
   1.107 +                       InetSocketAddress (i34.GetAddress (1), port));
   1.108 +    onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
   1.109 +    onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
   1.110  
   1.111 -  ApplicationContainer apps = onoff.Install (c.Get (0));
   1.112 -  apps.Start (Seconds (1.0));
   1.113 -  apps.Stop (Seconds (COMMUNICATION_STOP));
   1.114 +    ApplicationContainer apps = onoff.Install (c.Get (0));
   1.115 +    apps.Start (Seconds (1.0));
   1.116 +    apps.Stop (Seconds (COMMUNICATION_STOP));
   1.117  
   1.118 -  // Create a packet sink to receive these packets
   1.119 -  PacketSinkHelper sink ("ns3::UdpSocketFactory",
   1.120 -                         InetSocketAddress (Ipv4Address::GetAny (), port));
   1.121 +    // Create a packet sink to receive these packets
   1.122 +    PacketSinkHelper sink ("ns3::UdpSocketFactory",
   1.123 +                           InetSocketAddress (Ipv4Address::GetAny (), port));
   1.124  
   1.125 -  apps = sink.Install (c.Get (3));
   1.126 -  apps.Start (Seconds (1.0));
   1.127 -  apps.Stop (Seconds (COMMUNICATION_STOP));
   1.128 +    apps = sink.Install (c.Get (3));
   1.129 +    apps.Start (Seconds (1.0));
   1.130 +    apps.Stop (Seconds (COMMUNICATION_STOP));
   1.131  
   1.132 -  // Create a similar flow from n3 to n1, starting at time 1.1 seconds
   1.133 -  onoff.SetAttribute ("Remote",
   1.134 -                      AddressValue (InetSocketAddress (i12.GetAddress (0), port)));
   1.135 -  apps = onoff.Install (c.Get (3));
   1.136 -  apps.Start (Seconds (1.1));
   1.137 -  apps.Stop (Seconds (COMMUNICATION_STOP));
   1.138 +    // Create a similar flow from n3 to n1, starting at time 1.1 seconds
   1.139 +    onoff.SetAttribute ("Remote",
   1.140 +                        AddressValue (InetSocketAddress (i12.GetAddress (0), port)));
   1.141 +    apps = onoff.Install (c.Get (3));
   1.142 +    apps.Start (Seconds (1.1));
   1.143 +    apps.Stop (Seconds (COMMUNICATION_STOP));
   1.144  
   1.145 -  // Create a packet sink to receive these packets
   1.146 -  apps = sink.Install (c.Get (1));
   1.147 -  apps.Start (Seconds (1.1));
   1.148 -  apps.Stop (Seconds (COMMUNICATION_STOP));
   1.149 +    // Create a packet sink to receive these packets
   1.150 +    apps = sink.Install (c.Get (1));
   1.151 +    apps.Start (Seconds (1.1));
   1.152 +    apps.Stop (Seconds (COMMUNICATION_STOP));
   1.153  
   1.154 -  std::ofstream ascii;
   1.155 -  char outstr[128];
   1.156 -  sprintf(outstr, "simple-point-to-point-olsr-%d-%d.tr", id, i);
   1.157 -  ascii.open (outstr);
   1.158 -  PointToPointHelper::EnableAsciiAll (ascii);
   1.159 +    std::ofstream ascii;
   1.160 +    char outstr[128];
   1.161 +    sprintf(outstr, "simple-point-to-point-olsr-%d.tr", i);
   1.162 +    ascii.open (outstr);
   1.163 +    PointToPointHelper::EnableAsciiAll (ascii);
   1.164  
   1.165 -  Simulator::Stop (Seconds (COMMUNICATION_STOP));
   1.166 +    Simulator::Stop (Seconds (COMMUNICATION_STOP));
   1.167  
   1.168 -  Simulator::Run ();
   1.169 -  Simulator::Destroy ();
   1.170 +    Simulator::Run ();
   1.171 +    Simulator::Destroy ();
   1.172 +  }
   1.173  
   1.174    return;
   1.175 -  }
   1.176 -
   1.177  }
   1.178  
   1.179  
   1.180 @@ -145,8 +143,8 @@
   1.181    //	Quad Core   - 4
   1.182    //	...
   1.183    // GetNumCores() try to determine this automatically
   1.184 -  // via sysconf(3) or will fall back to 1 (a useful default)
   1.185 -  worker.SetWorkers (WORKERS );
   1.186 +  // via sysconf(3) or will fall back to 1 (an acceptable default)
   1.187 +  worker.SetWorkers (WORKERS);
   1.188    worker.AddCallback (MakeCallback (adhoc_scenario));
   1.189  
   1.190    worker.Run ();
     2.1 --- a/src/core/worker.h	Tue Dec 16 00:59:36 2008 +0100
     2.2 +++ b/src/core/worker.h	Sun Dec 21 19:14:22 2008 +0100
     2.3 @@ -38,6 +38,7 @@
     2.4  
     2.5    static inline uint32_t ChunkStart (uint32_t index, uint32_t worker, uint32_t max)
     2.6    {
     2.7 +	// FIXME handle the case sanely where max < worker
     2.8      return (max / worker) * index;
     2.9    }
    2.10  
    2.11 @@ -65,7 +66,7 @@
    2.12  
    2.13      // in the case of a sysconf error
    2.14      // we return 1. This should at least
    2.15 -    // guarantee a working default.
    2.16 +    // guarantee a acceptable default.
    2.17      if (cores < 1)
    2.18  	    return 1;
    2.19