OpenMP学习笔记

OpenMP在C++中的应用与实践
Date: 2016-02-22
Author: kagula

Environment: 
[1]Core i7-4790K
[2]Win10-64bits
[3]VS2013 Update5
[4]GCC 4.4.7(CentOS 6.5自带)


Prologue:

  OpenMP适合单机多核cpu,使用非常方便。  多种主流编译器中VS2013支持的OpenMP版本最低,

虽然VS只支持到2.0,不过已经能满足需要。

  一个“#pragma omp”statement指示随后的一个code block由OpenMP管理。
  
第一个OpenMP程序
第一步:
[S1]
新建新的Win32 Console project with empty。
[S2]修改编译器选项
[Property Pages dialog box]->[Configuration Properties]->[C/C++]->
  [Language property page]->[OpenMP Support]
[S3]源代码清单
#include <omp.h>

#include <iostream>
using namespace std;

int main()
{
	cout << "处理器个数(含逻辑处理器)=>" << omp_get_num_procs() << endl;

	//omp关键词,表示随后的代码块由OpenMP负责管理。
	//paragma中的for,表示对for循环并行化.
#pragma omp parallel for
	for (int i = 0; i < 10; i++)
	{
		//core i7-4790k下可以看到for中的block被分配到0~7号不同的线程中分别运行。
		//这里共有8根线程,因为我们的core i7-4790k CPU有8个逻辑处理器。
		cout << "线程ID: " << omp_get_thread_num() << endl;
	}
	std::cout << "press any key to continue show next demo." << std::endl;
	cin.get();


	int sum = 0;
	int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

	//对for代码块中的sum变量进行线程保护
	//reduction关键词 只支持 +,-,*,&,|,&&,|| 这些运算符号
#pragma omp parallel for reduction(+:sum)
	for (int i = 0; i < 10; i++)
	{
		sum = sum + a[i];
	}
	std::cout << "sum: " << sum << std::endl;
	cin.get();


	return 0;
}



第二个OpenMP程序
对代码块进行线程保护
源代码清单如下:
#include <iostream>
using namespace std;

int main(){
	int max = 0;
	int a[10] = { 11, 2, 33, 49, 113, 20, 321, 250, 689, 16 };
#pragma omp parallel for
	for (int i = 0; i < 10; i++)
	{
		//多根线程会同时调用下面的代码行。
		int temp = a[i];

		//critical关键词让下面的代码块受到了线程保护。
        //即下面的代码块,只有在一根线程执行完毕后,另一根线程才能运行。
#pragma omp critical
		{
			if (temp > max)
				max = temp;
		}
	}
	cout << "max: " << max << std::endl;
	cin.get();

	return 0;
}



第三个OpenMP程序
omp block中有“并行、串行、并行...”。
源代码清单如下
#include <omp.h>
#include <iostream>

using namespace std;

int main()
{
	int a[5], i;

#pragma omp parallel
	{
		// Perform some computation.
#pragma omp for
		for (i = 0; i < 5; i++)
			a[i] = i * i;

		// Print intermediate results.
		// specifies a structured block that is executed by the master thread of the team
		// Other threads in the team do not execute the associated structured block.
#pragma omp master
		for (i = 0; i < 5; i++)
			cout << "a[" << i << "]=" << a[i] << endl;;

		// Wait all OpenMP threads done.
#pragma omp barrier

		// Continue with the computation.
#pragma omp for
		for (i = 0; i < 5; i++)
			a[i] += i;
	}

	cout << endl << endl;

	for (i = 0; i < 5;i++)
	{
		cout << "a[" << i << "]=" << a[i] << endl;
	}

	cin.get();
}



第四个OpenMP程序
多个不同内容的代码段同时运行
#include <omp.h>

#include <iostream>
using namespace std;

int main(){
#pragma omp parallel sections
	{
#pragma omp section
		{
			cout << "fucntion 1" << endl;
		}
#pragma omp section
		{
			cout << "fucntion 2" << endl;
		}

#pragma omp section
		{
			cout << "fucntion 3" << endl;
		}
#pragma omp section
		{
			cout << "fucntion 4" << endl;
		}
	}

	cin.get();

	return 0;
}

在CentOS下编译和运行

g++ -fopenmp main.cpp
./a.out

参考资料
[1]OpenMP Compilers支持情况
http://openmp.org/wp/openmp-compilers/

[2]OpenMP in Visual C++

里面介绍了OpenMP其它关键词

https://msdn.microsoft.com/en-us/library/tt15eb9t(v=vs.120).aspx
### WRF 数值模拟学习资源汇总 对于希望深入理解 Weather Research and Forecasting (WRF) 模型及其应用的研究人员来说,存在多种途径获取高质量的学习材料。这些资源不仅涵盖了理论基础,还包括实际操作指南。 #### 官方文档与教程 官方提供的文档是最权威的信息源之一。UCAR(University Corporation for Atmospheric Research)维护着详细的在线手册和教程,适合不同层次的使用者查阅[^2]。这类资料通常会随着版本更新而持续改进,确保用户能够获得最新的配置方法和技术细节说明。 #### 实战案例分析 通过具体的应用场景来掌握软件工具是非常有效的学习方式。例如,在研究台风菲特(Fitow)期间,有学者分享了完整的实验过程记录,包括但不限于数据准备、参数设定以及结果验证等多个环节的具体实现步骤。此类实战经验有助于加深对整个工作流的理解,并能帮助解决实践中遇到的实际问题。 #### 技术博客与社区讨论 互联网上有许多个人或团队撰写的关于WRF的技术文章,它们往往包含了作者独特的见解和个人积累的小技巧。特别是当涉及到特定环境下的部署或是优化性能等方面的话题时,来自一线开发者的建议尤为珍贵[^4]。此外,积极参与相关论坛交流也是快速提升技能的好办法;在这里可以找到志同道合的朋友共同探讨难题,互相启发思路。 #### 并行计算平台支持 鉴于气象预报任务常常涉及大规模的数据集处理及复杂物理过程仿真,因此熟悉高性能计算机(HPC)上的作业提交机制至关重要。针对这一点,某些机构提供了专门面向初学者编写的入门级指导材料,介绍了必要的Linux命令行操作知识以及MPI/OpenMP编程接口等内容[^3]。这使得即使是没有太多超级计算背景的人也能顺利开展基于集群系统的科研活动。 ```bash # 示例:登录远程服务器并执行简单命令 ssh username@remote_host cd /path/to/project_directory ls -l ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值