【问题标题】:efficiency boost::thread group效率提升::线程组
【发布时间】:2018-09-02 04:41:14
【问题描述】:

我想提高我的程序的效率,为此我尝试并行化可能的任务。我正在使用点云,所以我们谈论的是大数据,大约 17000 点。

这是之前的代码:

void Estimator::extract_relevant_points(std::vector<Point3D>& relevant_points, std::vector<Point3D>& pointcloud, doubleIE cell_min_angle_sensor_rot, doubleIE cell_max_angle_sensor_rot)
    {
    for(int i = 0; i < pointcloud.size(); i++) {
       //Doing my operations ...
    }
}

现在的代码是:

void Estimator::extract_relevant_points_multithread(std::vector<Point3D>& relevant_points, std::vector<Point3D>& pointcloud, doubleIE cell_min_angle_sensor_rot, doubleIE cell_max_angle_sensor_rot)
    {
        std::cout << "pointcloud = " << pointcloud.size ()<< std::endl;

        boost::thread_group group;

        for(int i = 0; i < pointcloud.size(); i++) {
            group.create_thread(boost::bind(&IntervalMapEstimator::extract_relevant_point_in_thread, i,boost::ref(relevant_points), boost::ref(pointcloud), boost::cref(cell_min_angle_sensor_rot), boost::cref(cell_max_angle_sensor_rot)));
        }
        std::cout << "size group before join = " << group.size ()<< std::endl;
        group.join_all();
        std::cout << "size group after join = " << group.size ()<< std::endl;
    }

void Estimator::extract_relevant_point_in_thread(int i, std::vector<Point3D>& relevant_points, std::vector<Point3D>& pointcloud, doubleIE cell_min_angle_sensor_rot, doubleIE cell_max_angle_sensor_rot)
    {
        //Doing my operations
    }

但是新代码在运行时崩溃了,我不明白为什么。有没有人有想法? 我还放了一点时间来获得花在这个功能上的时间,似乎新版本比第一个版本花费了更多时间……也不明白为什么……

【问题讨论】:

    标签: multithreading boost boost-thread point-clouds


    【解决方案1】:

    看起来这段代码为 17000 个点中的每一个创建了一个线程。这势必会使您的应用程序崩溃,因为每个线程至少需要 2MB 堆栈,总计 34GB 堆栈,除非您的 RAM 比这更多。

    使用gcc parallel algorithmsIntel TBB parallel algorithmsIntel TBB task scheduler

    【讨论】:

      【解决方案2】:

      感谢您的回答。

      我想我可以用线程池模式解决这个问题,你认为这行得通吗? 但是同时运行多少个线程也更有效率呢?
      我的 CPU 有 8 个核心,所以我猜一下。

      TBB 看起来很有趣,但我更愿意自己做。

      【讨论】:

      • 最后,我会尝试使用 TBB 库。这样,代码就针对每台机器进行了优化。
      猜你喜欢
      • 2015-10-20
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 2010-11-17
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      相关资源
      最近更新 更多