openMP学习笔记(一)

声明:欢迎任何人和组织转载本blog中文章,但必须标记文章原始链接和作者信息。

本文链接:http://blog.csdn.net/li_007/archive/2009/04/30/4139211.aspx

开拓进取的小乌龟------->CSDN点滴点点滴滴Blog

之前大概看了一下openMP,并且在初期像很多人一样犯了一些低级错误。

openMP是一种API,不是另一种编程语言,是像XML这样的一种业界标准,是一种基于内存共享的可扩展的可移植的多核并行编程模型,它适合于PC应用程序,同样也适合超级计算机的并行编程。可以去openMP的官网(www.openmp.org)它的介绍。

openMP的语句也很简洁,由#pragma omp这个directives加上一些如parallel、for、section、etc子语句组成,现在又很多人写了大量这方面的文章,如何感兴趣可以去baidu or google一下,当然最好是去官网或者www.wikipedia.org看看,毕竟它们更权威和专业。

支持openMP的编译器有很多,流行的Visual Studio .NET 2005/2008都支持(说明一下,只有professional Edition 和 Team Edition才支持)、当然还有Intel studio@parallel、gcc 4.3.2以上版本的编译器都支持openMP。在VS系列中,如果没有用到openMP的omp_get_thread_num()等等这些API,是不需要包含<omp.h>头文件的。只需要在项目属性中打开openMP支持就可以了。

 

 #include "stdafx.h" #include <omp.h> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int a[6][6] = {0}; int i, j; int sum = 0; omp_set_num_threads(3);//设置线程数为3 // omp_get_thread_num()得到当前线程ID #pragma omp parallel for private(i, j) reduction(+:sum) for(i = 1; i <= 6; i ++) { for(j = 1; j <= 6; j ++) { a[i][j] = i * j; printf("a[%d][%d] = %d ID = %d/n", i, j, a[i][j], omp_get_thread_num()); sum += a[i][j]; } } int max = 0; int min = 37; int m, n; #pragma omp parallel for private(m, n) //lastprivate(max, min) for(m = 6; m >= 0; m --) { for(n = 0; n <= 6; n ++) { #pragma omp parallel sections { #pragma omp section #pragma omp critical if(max < a[m][n]) max = a[m][n]; #pragma omp section #pragma omp critical if(min > a[m][n]) min = a[m][n]; } printf("max = %d min = %d ID = %d/n", max, min, omp_get_thread_num()); } } cout<<"/nsum = "<<sum<<endl; cout<<"max = "<<max<<endl; cout<<"min = "<<min<<endl; int loop; cin>>loop; return 0; }

上面是一个简单的利用了openMP的程序 ,执行结果如下:

a[3][1] = 3 ID = 1 a[1][1] = 1 ID = 0 a[5][1] = 5 ID = 2 a[3][2] = 6 ID = 1 a[1][2] = 2 ID = 0 a[5][2] = 10 ID = 2 a[3][3] = 9 ID = 1 a[1][3] = 3 ID = 0 a[5][3] = 15 ID = 2 a[3][4] = 12 ID = 1 a[1][4] = 4 ID = 0 a[5][4] = 20 ID = 2 a[3][5] = 15 ID = 1 a[1][5] = 5 ID = 0 a[5][5] = 25 ID = 2 a[3][6] = 18 ID = 1 a[1][6] = 6 ID = 0 a[5][6] = 30 ID = 2 a[4][1] = 4 ID = 1 a[2][1] = 2 ID = 0 a[6][1] = 6 ID = 2 a[4][2] = 8 ID = 1 a[2][2] = 4 ID = 0 a[6][2] = 12 ID = 2 a[4][3] = 12 ID = 1 a[2][3] = 6 ID = 0 a[6][3] = 18 ID = 2 a[4][4] = 16 ID = 1 a[2][4] = 8 ID = 0 a[6][4] = 24 ID = 2 a[4][5] = 20 ID = 1 a[2][5] = 10 ID = 0 a[6][5] = 30 ID = 2 a[4][6] = 24 ID = 1 a[2][6] = 12 ID = 0 a[6][6] = 36 ID = 2 max = 30 min = 30 ID = 0 max = 30 min = 0 ID = 2 max = 30 min = 0 ID = 1 max = 30 min = 0 ID = 0 max = 30 min = 0 ID = 2 max = 30 min = 0 ID = 1 max = 30 min = 0 ID = 0 max = 30 min = 0 ID = 2 max = 30 min = 0 ID = 1 max = 30 min = 0 ID = 0 max = 30 min = 0 ID = 2 max = 30 min = 0 ID = 1 max = 30 min = 0 ID = 0 max = 30 min = 0 ID = 2 max = 30 min = 0 ID = 1 max = 30 min = 0 ID = 0 max = 30 min = 0 ID = 2 max = 30 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 2 max = 36 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 2 max = 36 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 2 max = 36 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 2 max = 36 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 2 max = 36 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 2 max = 36 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 2 max = 36 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 2 max = 36 min = 0 ID = 1 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 0 max = 36 min = 0 ID = 0 sum = 441 max = 36 min = 0

 

注:在这里说明一下,不能使用C++的cout输出,貌似是不支持多线程输出,大家可以自己去测试。

你可能感兴趣的:(thread,多线程,编程,Blog,编译器,parallel)