OMP 不启动线程

omp doens't launch thread

本文关键字:线程 启动 OMP      更新时间:2023-10-16

openMp 曾经在 6 个线程上处理我的项目,现在(我不知道为什么(,该程序是单线程的。我的代码非常简单,我只在一个 cpp 文件中使用 openMp,我声明 #include <omp.h>

那么要并行化的函数是:

#pragma omp parallel for collapse(2) num_threads(IntervalMapEstimator::m_num_thread)
for (int cell_index_x = m_min_cell_index_sensor_rot_x; cell_index_x <= m_max_cell_index_sensor_rot_x; cell_index_x++)
{
    for (int cell_index_y = m_min_cell_index_sensor_rot_y; cell_index_y <= m_max_cell_index_sensor_rot_y; cell_index_y++)
    {
        //use for debug
        omp_set_num_threads (5);
        std::cout << "omp_get_num_threads  = " <<  omp_get_num_threads ()<< std::endl;
        std::cout << "omp_get_max_threads = " << omp_get_max_threads ()<< std::endl;
        if(split_points) {
            extract_relevant_points_from_angle_lists(relevant_points, pointcloud_ff_polar_angle_lists, cell_min_angle_sensor_rot, cell_max_angle_sensor_rot);
        } else {
            extract_relevant_points_multithread_with_localvector(relevant_points, pointcloud, cell_min_angle_sensor_rot, cell_max_angle_sensor_rot);
        }
    }
}

omp_get_num_threads返回 1 个线程 omp_get_max_threads返回 5 IntervalMapEstimator::m_num_thread设置为 6

任何领导将不胜感激。

编辑 1:我修改了我的代码,但问题仍然存在,程序仍在单线程中运行。 omp_get_num_threads返回 1
omp_get_max_threads返回 8

有没有办法知道运行时有多少线程可用?

#pragma omp parallel for collapse(2) 
    for (int cell_index_x = m_min_cell_index_sensor_rot_x; cell_index_x <= m_max_cell_index_sensor_rot_x; cell_index_x++)
    {
        for (int cell_index_y = m_min_cell_index_sensor_rot_y; cell_index_y <= m_max_cell_index_sensor_rot_y; cell_index_y++)
        {
            std::cout << "omp_get_num_threads  = " <<  omp_get_num_threads ()<< std::endl;
            std::cout << "omp_get_max_threads = " << omp_get_max_threads ()<< std::endl;

                extract_relevant_points(relevant_points, pointcloud, cell_min_angle_sensor_rot, cell_max_angle_sensor_rot);
            }
    }

我刚刚看到我的计算机开始内存不足,这可能是问题的一部分吗?

根据 https://msdn.microsoft.com/en-us/library/bx15e8hb.aspx:

如果在禁用线程数动态调整时遇到并行区域,并且为并行区域请求的线程数超过运行时系统可以提供的数量,则程序的行为是实现定义的。例如,实现可以中断程序的执行,或者可以序列化并行区域。

您请求 6 个线程,实现只能提供 5 个,因此可以自由地做它想做的事。

我也非常确定你不应该在并行区域内更改线程数,所以你的omp_set_num_threads充其量什么都不做,最坏的情况是在你的脸上爆炸。

我根据另一篇文章找到了答案:为什么编译器忽略 OpenMP 编译指示?

最后,这是一个简单的库错误,我没有添加到编译器中,我没有注意到它,因为我正在使用 cmake 进行编译,所以我不必直接键入该行。此外,我使用catkin_make进行编译,因此我没有警告,而控制台中只有错误。

所以基本上,要使用 openMp,您必须将 -fopenmp 作为参数添加到编译器中,如果您不这样做......好吧,编译器只是忽略了这些行。