OpenMP学习笔记

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kagula086

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

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

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

打赏作者

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

抵扣说明:

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

余额充值