c++ - 使用 NS3 和 std::thread 的并行模拟

标签 c++ multithreading stdthread ns-3

我正在使用 NS3 框架以各种配置运行 Wi-Fi 模拟。我想使用 std::thread 在一个进程中同时运行许多(数百个)模拟。

这是我的代码,修改了一些配置:

void simulation(RateAdaptation    rate_adaptation,
                const bool        channel_fading,
                // In meters, between AP and station.
                const uint32_t    distance)
{
  ns3::SeedManager::SetSeed(++seed);

  ns3::NodeContainer station_node, ap_node;
  station_node.Create(1);
  ap_node.Create(1);

  ns3::YansWifiChannelHelper channel;
  channel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
  channel.AddPropagationLoss( "ns3::LogDistancePropagationLossModel");



  // About 130 lines of more configuration for the simulation and
  // function parameters.



  ns3::Simulator::Stop(ns3::Seconds(10.0));

  ns3::Ptr<ns3::FlowMonitor> flowmon;
  ns3::FlowMonitorHelper *flowmonHelper = new ns3::FlowMonitorHelper();
  flowmon = flowmonHelper->InstallAll();

  ns3::Simulator::Run();
  ns3::Simulator::Destroy();
  flow_output(flowmon, flowmonHelper);
}

int main(int argc, char *argv[])
{
  std::vector<std::thread> jobs;

  for (uint32_t i = 0; i < 20; ++i)
  {
    uint32_t varying_distance = 5*(i+1);

    jobs.push_back(std::thread(simulation,
                               RateAdaptation::Aarf,
                               false,
                               varying_distance));
  }

  for (auto it = jobs.begin(); it < jobs.end(); ++it)
  {
    it.join();
  }

  return 0;
}

当我只为工作中的一项工作运行此代码时,它运行完美,但对于任何更大的数字(比如两个),我得到如下输出:

assert failed. cond="SystemThread::Equals (m_main)", msg="Simulator::ScheduleDestroy Thread-unsafe invocation!", file=../src/core/model/default-simulator-impl.cc, line=289
terminate called without an active exception
Command ['[redacted]'] terminated with signal SIGIOT. Run it under a debugger to get more information (./waf --run <program> --comm    and-template="gdb --args %s <args>").

而且确实在 Valgrind 中运行它会返回数百个问题。

两个问题:

  • 有没有可能我做错了什么,NS3 应该支持这个?
  • 是否知道 NS3 不能并行运行多个模拟?

最佳答案

简短的回答:你不能用 ns-3 做到这一点。

长答案:使用多进程而不是多线程(ns-3 库维护不受多线程并发保护的全局状态)。

我建议使用 ns-3 python 包装器轻松设置并行多处理作业。如果您想在默认跟踪设置之外执行跟踪,这可能不是完全微不足道的(在这种情况下,您可能需要在 C++ 中进行自己的跟踪并在 python 中进行拓扑设置)

关于c++ - 使用 NS3 和 std::thread 的并行模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35543906/

相关文章:

c++ - @parm 在 C++ 中是什么意思或暗示?

java - Camel 队列中的线程 DSL 行为

java - 中断 future 如何与单线程执行器一起工作?

c++ - 关于 std::thread 类的问题

c++ - 在 C++ 中使用 std::thread 动态创建线程

c++ - 带有抽象类的 Python ctypes

c++ - 将 [[no_unique_address]] 与空基类继承结合使用时出现意外结果

c++ - 如何防止在 c 中的 cin 之后生成新的 rand()?

python - 调用 thread.join() 会阻塞异步上下文中的事件循环吗?