OpenMP 多核编程(转载)
轉載時請注明原文出處(http://blog.sina.com.cn/wyw1976)及作者郵箱()
?
???(1)打開VS2008,新建一個空白的Win32 Console Application工程,添加源文件中并輸入如下的代碼:
??(2)上面是一個最簡單的OpenMP程序,它將并行的輸出5個整數,可是實際的輸出結果如下:
???結果好像是串行的輸出,并沒有看出并行的效果嘛!原因在于VS2008缺省是將OpenMP的支持關閉,打開的方法如下, 將OpenMP Support 項設為Yes,即可:
(3)重新編譯,沒有任何問題,可是運行或調試該程序,卻報錯:"This application has failed to start because VCOMP90D.DLL was not found. Re-installing the application may fix this problem", 如下圖所示:
原因是沒有找到VCOMP90D.dll,實際上,你可以在C:\Windows\winsxs\目錄下找到VCOMP90D.dll,例如在我的機器上,它所在的具體目錄為:
C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugOpenMP_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_72b673b0
?
現在我們需要在VS工程設置中指定該文件即可,properties->configuration properties->Linker->Manifest File->Additional Manifest Dependencies輸入如下字符串:
"type='win32' name ='Microsoft.VC90.DebugOpenMP' version ='9.0.21022.8' processorArchitecture ='x86' publicKeyToken= '1fc8b3b9a1e18e3b' "
注意:字符串中的值來源于VCOMP90D的父目錄,而且在輸入時,單引號和雙引號不能少。
?
(4)重新編譯,成功運行,結果如下:
????這才是并行運行的效果!!!!
需要說明的是:
???(1)上面的情況針對的工程配置是Debug,如果是Release, 它會報錯“VCOMP90.DLL was not found”, 只需在winsxs中找到該文件,并按照同樣的方法更改“Additional Manifest Dependencies”即可,例如:
"type='win32' name ='Microsoft.VC90.OpenMP' version ='9.0.21022.8' processorArchitecture ='x86' publicKeyToken= '1fc8b3b9a1e18e3b' "
(2)上面的情況只是針對VS2008自帶的編譯器,對于其他的編譯器,可能不一樣,例如如果選用Intel的編譯器,就不需要設置“Additional Manifest Dependencies”
?
?
?
#include "stdafx.h"
#include <iostream>
#include <omp.h>
void tt( int a )
{
std::cout<< a <<std::endl;
}
int main( )
{
std::cout<<" thread: number:"<< ?omp_get_thread_num() << std::endl;
//#pragma omp parallel for
//for( int i=0;i<40 ;i++ )
int i=0;
#pragma omp parallel
{
if( i<=0 )
{
std::cout<<" i="<< i++ <<std::endl;
}
std::cout<<" threadnum="<< omp_get_thread_num() ?<<std::endl;
}
?
int a;
std::cin>>a;
return 0;
}
轉載于:https://www.cnblogs.com/xibao/archive/2013/05/23/3095347.html
總結
以上是生活随笔為你收集整理的OpenMP 多核编程(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python核心编程6-14习题的解题思
- 下一篇: 参数位置关于shell