OpenMP 多核编程

第一次编写OpenMP程序所遇到的问题(1) (2010-07-02 06:49:29)转载标签: 杂谈分类: 并行

转载时请注明原文出处(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;

}

posted @ 2013-05-23 17:35  xibao  阅读(336)  评论(0编辑  收藏  举报