如何在ubuntu 12.04中为openCV项目设置openMP多线程? [英] How to set openMP for Multithreading in ubuntu 12.04 for an openCV project?
问题描述
我想使用 #pragma omp parallel
在我的 for循环
之一中执行多线程。所以,我写了下面的代码:
#pragma omp parallel for
for(int i = 0; i< ; square-> total; i ++)
{
......
}
我的项目有一个CMakeList.txt和Makefile。 我不明白,我如何告诉编译器和链接器使用openMP?
更新:我已经使用以下代码编辑了我的 CMakeList.txt
find_package(OpenMP)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS$ {CMAKE_C_FLAGS} $ {OpenMP_C_FLAGS})
set(CMAKE_CXX_FLAGS$ {CMAKE_CXX_FLAGS} $ {OpenMP_CXX_FLAGS})
endif()
<然后我做了 cmake ..
并在终端得到了以下结果:
- C编译器身份验证是GNU
- CXX编译器标识是GNU
- 检查工作的C编译器:/ usr / bin / gcc
$ b - 检查工作的C编译器:/ usr / bin / gcc - works
<检测C编译器ABI信息检测C编译器ABI信息完成
- 检测C编译器ABI信息
检查工作CXX编译器:/ usr / bin / c ++- 检查工作CXX编译器:/ usr / bin / c ++ - works
$ b - 检测CXX编译器ABI信息
- 检测CXX编译器ABI信息 - 完成
- 试试OpenMP C flag = [-fopenmp]
- 执行测试OpenMP_FLAG_DETECTED
- 执行测试OpenMP_FLAG_DETECTED - 成功
- 尝试OpenMP CXX标志= [-fopenmp]
- 执行测试OpenMP_FLAG_DETECTED
- - 执行测试OpenMP_FLAG_DETECTED - 成功
- 找到OpenMP:-fopenmp
- 配置完成
- 生成完成
@ user2440724
我写了代码示例,如下所示:
#include <串GT;
#include< iostream>
#include< vector>
#include
使用namespace std;
使用命名空间cv;
// -------------------------------------------- --------------
// MAIN
// ----------------------- -----------------------------------
int main(int argc,char * argv [] )
{
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i = 0; i <10; i ++)
{
std :: cout<< A<<的std :: ENDL;
}
getchar();
返回0;
}
在vs2010中,您需要启用openmp,如下所示:
好的Linux教程在这里:< a href =http://goulassoup.wordpress.com/2011/10/28/openmp-tutorial/ =nofollow noreferrer> http://goulassoup.wordpress.com/2011/10/28/openmp- tutorial /
和GNU OpenMP文档: http:/ /gcc.gnu.org/onlinedocs/libgomp/index.html#toc_Top
I want to do multi threading in one of my for loop
using #pragma omp parallel
. So, i am writing the following code:
#pragma omp parallel for
for(int i=0; i<square->total; i++)
{
......
}
My project have a CMakeList.txt and Makefile. I don't understand, how do i tell the compiler and linker to use openMP?
Updates:
I have edited my CMakeList.txt
with following code
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
and then i did cmake ..
and got the following result at terminal:
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Try OpenMP C flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Success
-- Try OpenMP CXX flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Success
-- Found OpenMP: -fopenmp
-- Configuring done
-- Generating done
@user2440724
I wrote the code sample as you ask:
#include <string>
#include <iostream>
#include <vector>
#include <omp.h>
using namespace std;
using namespace cv;
//----------------------------------------------------------
// MAIN
//----------------------------------------------------------
int main(int argc, char* argv[])
{
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int i = 0; i < 10; i++)
{
std::cout << "A" << std::endl;
}
getchar();
return 0;
}
in vs2010 you need enable openmp as shown below:
Good tutorial for linux here: http://goulassoup.wordpress.com/2011/10/28/openmp-tutorial/ And GNU OpenMP docs: http://gcc.gnu.org/onlinedocs/libgomp/index.html#toc_Top
这篇关于如何在ubuntu 12.04中为openCV项目设置openMP多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!