系统 : 2 x Xeon 5130 2.00GHz 1333MHz FSB (4 cores) - Woodcrest B2, 64-bit, dual-core, 65nm, L2: 4MB
testc.c:
1 #include <omp.h>
2 #include <stdio.h>
3
4 int main()
5 {
6 int i = 0;
7 int j = 0;
8 fprintf(stderr,"omp max: %d\n",omp_get_max_threads());
9 omp_set_num_threads(4);
10
11 #pragma omp parallel for
12 for(i = 0; i< 10;i++)
13 {
14 fprintf(stderr,"omp get: (%d) %d\n",i,omp_get_num_threads());
15 for (j = 0;j<10000;j++)
16 ;
17 fprintf(stderr,"%d\n",i);
18 }
19 return 0;
20 }
testcpp.cpp
1 #include <omp.h>
2 #include <iostream>
3
4 using namespace std;
5
6 int main()
7 {
8 int i = 0;
9 int j = 0;
10 cerr << "omp max: " << omp_get_max_threads() << endl;
11 omp_set_num_threads(4);
12
13 #pragma omp parallel for
14 for(i = 0; i< 10;i++)
15 {
16 cerr << "omp get: (" << i << ") " << omp_get_num_threads() << endl;
17 for (j = 0;j<10000;j++)
18 ;
19 cerr << i << endl;
20 }
21 return 0;
22 }
Makefile:
1 all:
2 g++42 -o testcpp testcpp.cpp -lgomp -lpthread -fopenmp
3 gcc42 -o testc testc.c -lgomp -lpthread -fopenmp
两段程序几乎一样,除了对信息的输出对于结果的现实却相差很远:
[~workspace]$ time ./testc
omp max: 4
omp get: (3) 4
omp get: (0) 4
omp get: (6) 4
omp get: (9) 4
0
3
6
omp get: (1) 4
omp get: (4) 4
9
omp get: (7) 4
1
omp get: (2) 4
4
2
7
omp get: (5) 4
omp get: (8) 4
8
5
real 0m0.003s
user 0m0.003s
sys 0m0.001s
而:
[~workspace]$ time ./testcpp
omp max: 4
omp get: (omp get: (omp get: (omp get: (0693) ) ) ) 4444
06
omp get: (9omp get: (317
) ) 4omp get: (4
4
) 14
7
omp get: (42omp get: (
) 8omp get: (4) 5
4)
42
8
5
real 0m0.008s
user 0m0.004s
sys 0m0.007s
看来openmp对c的支持要更规矩一些。
testc.c:
1 #include <omp.h>
2 #include <stdio.h>
3
4 int main()
5 {
6 int i = 0;
7 int j = 0;
8 fprintf(stderr,"omp max: %d\n",omp_get_max_threads());
9 omp_set_num_threads(4);
10
11 #pragma omp parallel for
12 for(i = 0; i< 10;i++)
13 {
14 fprintf(stderr,"omp get: (%d) %d\n",i,omp_get_num_threads());
15 for (j = 0;j<10000;j++)
16 ;
17 fprintf(stderr,"%d\n",i);
18 }
19 return 0;
20 }
testcpp.cpp
1 #include <omp.h>
2 #include <iostream>
3
4 using namespace std;
5
6 int main()
7 {
8 int i = 0;
9 int j = 0;
10 cerr << "omp max: " << omp_get_max_threads() << endl;
11 omp_set_num_threads(4);
12
13 #pragma omp parallel for
14 for(i = 0; i< 10;i++)
15 {
16 cerr << "omp get: (" << i << ") " << omp_get_num_threads() << endl;
17 for (j = 0;j<10000;j++)
18 ;
19 cerr << i << endl;
20 }
21 return 0;
22 }
Makefile:
1 all:
2 g++42 -o testcpp testcpp.cpp -lgomp -lpthread -fopenmp
3 gcc42 -o testc testc.c -lgomp -lpthread -fopenmp
两段程序几乎一样,除了对信息的输出对于结果的现实却相差很远:
[~workspace]$ time ./testc
omp max: 4
omp get: (3) 4
omp get: (0) 4
omp get: (6) 4
omp get: (9) 4
0
3
6
omp get: (1) 4
omp get: (4) 4
9
omp get: (7) 4
1
omp get: (2) 4
4
2
7
omp get: (5) 4
omp get: (8) 4
8
5
real 0m0.003s
user 0m0.003s
sys 0m0.001s
而:
[~workspace]$ time ./testcpp
omp max: 4
omp get: (omp get: (omp get: (omp get: (0693) ) ) ) 4444
06
omp get: (9omp get: (317
) ) 4omp get: (4
4
) 14
7
omp get: (42omp get: (
) 8omp get: (4) 5
4)
42
8
5
real 0m0.008s
user 0m0.004s
sys 0m0.007s
看来openmp对c的支持要更规矩一些。
OpenMP并行库在多核机的大量计算中尤其是序列无关的循环计算中显示出极大的性能提升。看到05年xbox的说明中已经支持了OpenMP. OpenMP大量使用在图形的渲染和着色上。
MSDN 上有一篇文章介绍关于在OpenMP支持Xbox的文章 :
Coding For Multiple Cores on Xbox 360 and Microsoft Windows
这里有一片文章介绍OpenMP在游戏中渲染地形的使用:
Multi-Threaded Terrain Smoothing
也许当未来出现多核手机的时候OpenMP可以简化手机的线程处理, 看电池的好坏了...
Parallel computing is the wave of the future…and always will be.
AMD
Cray
Fujitsu/Lahey
HP
IBM
Intel
Microsoft
NEC
The Portland Group, Inc.
SGI
Sun Microsystems
Totalview Technologies
Vast from Crescent Bay Software
Cray
Fujitsu/Lahey
HP
IBM
Intel
Microsoft
NEC
The Portland Group, Inc.
SGI
Sun Microsystems
Totalview Technologies
Vast from Crescent Bay Software
gcc 4.2支持 openMP, 可以尝试在 multi processor 环境下编写简单的MP程序.
更新说明: http://gcc.gnu.org/gcc-4.2/changes.html
gomp: http://gcc.gnu.org/projects/gomp/
The GOMP project is developing an implementation of OpenMP for the C, C++, and Fortran 95 compilers in the GNU Compiler Collection. As part of the GNU Project, GOMP will simplify parallel programming for all GNU system variants. This effort operates in an open environment to attract developers and ensure applicability across multiple architectures and applications....
更新说明: http://gcc.gnu.org/gcc-4.2/changes.html
gomp: http://gcc.gnu.org/projects/gomp/
The GOMP project is developing an implementation of OpenMP for the C, C++, and Fortran 95 compilers in the GNU Compiler Collection. As part of the GNU Project, GOMP will simplify parallel programming for all GNU system variants. This effort operates in an open environment to attract developers and ensure applicability across multiple architectures and applications....
come from www.openmp.org
API documents
API documents