OMP 不启动线程
omp doens't launch thread
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 作为参数添加到编译器中,如果您不这样做......好吧,编译器只是忽略了这些行。
- 使用 std::string () const 函数启动线程或未来
- 如何创建线程序列以按照启动顺序执行任务?
- 启动类函数作为失去引用的线程
- 重新启动后,线程无法在 while 循环中再次运行
- 如何在C++上启动异步线程
- 计时器是否从另一个线程启动?
- 如何在 MFC 应用程序中启动 ZeroMQ 线程?
- C++ POCO - 如何在不使用 run() 方法的情况下启动线程池上的线程?
- 为什么 pthread_create() 返回 0 但线程从未启动
- 为什么 cuda-gdb 会启动多个线程?
- 对于每个线程,random_device是否以不同的状态启动?
- 线程启动延迟 - 通知所有未唤醒所有线程
- 等待多个线程启动,start() 未捕获
- Qt多线程启动线程错误
- 用信号通知线程启动特定的函数
- C++简单的线程启动问题
- - C++ - 使用计时器线程启动异常,精度毫秒
- 传递字符串作为线程启动例程参数:c++
- 错误:无法从另一个线程启动计时器.使用未声明计时器的QFuture
- QT线程:获取QObject::startTimer:计时器无法从另一个线程启动警告