一个OpenMP的学习程序
贴一个学习OpenMP过程中的程序
1#include <stdio.h>
2#include <tchar.h>
3#include <omp.h>
4int _tmain(int argc, _TCHAR* argv[])
5{
6 Timer tm;
7// omp_set_num_threads(2),设定活动线程数为2
8 omp_set_num_threads(3);
9// omp_get_num_procs()
10 int procs=omp_get_num_procs();
11 printf("There are %d processor(s) available!\n",procs);
12// omp_get_thread_num(),返回当前线程号;
13 int Threadid=omp_get_thread_num();
14 printf("当前线程号为:%d\n",Threadid);
15// omp_get_num_threads(),返回当前线程总数;
16 int Threadnum = omp_get_num_threads();
17 printf("当前活动线程数为:%d\n",Threadnum);
18
19// 并行化编译指导语句
20 printf("下面开始测试并行化编译指导语句!!\n\n");
21// 1.parallel
22 printf("------------------华丽无比的分隔线--------------------\n");
23 printf("#pragma omp parallel\n\n");
24
25#pragma omp parallel
26
27 printf("This is Thread %d say hello to you!\n",omp_get_thread_num());
28
29// 2.parallel for
30 printf("------------------华丽无比的分隔线--------------------\n");
31 printf("#pragma omp parallel for\n\n");
32
33#pragma omp parallel for
34 for (int i=0;i<4;i++)
35 {
36 printf("Thread %d is active when i = %d\n",omp_get_thread_num(),i);
37 }
38 int i,j=0;
39// 3.parallel for private
40 printf("------------------华丽无比的分隔线--------------------\n");
41 printf("#pragma omp parallel for private(j)\n\n");
42
43 printf("使用private子句\n");
44#pragma omp parallel for private(j)
45 for (i=0;i<2;i++)
46 for(j=5;j<7;j++)
47 printf("Thread %d is active when i = %d,and j= %d\n",omp_get_thread_num(),i,j);
48 printf("不使用private子句\n");
49#pragma omp parallel for
50 for (i=0;i<2;i++)
51//#pragma omp parallel for
52 for(j=5;j<7;j++)
53 printf("Thread %d is active when i = %d,and j= %d\n",omp_get_thread_num(),i,j);
54// 4.parallel for firstprivate() and lastprivate()
55 printf("------------------华丽无比的分隔线--------------------\n");
56 printf("#pragma omp parallel for firstprivate and lastprivate\n\n");
57
58 int ai=10;
59 int bi=0;
60 printf("Before \"parallel for \", ai = 10 ,and bi = 0 !\n\n");
61#pragma omp parallel for firstprivate(ai) lastprivate(bi)
62 for (int i=1;i<4;i++)
63 {
64 bi=ai;
65 ai=0;
66 }
67 printf("After \"parallel for \" bi equal to %d !\n\n",bi);
68 printf("After \"parallel for \" ai still equal to %d !\n\n",ai);
69// 5.critical 临界区
70 printf("------------------华丽无比的分隔线--------------------\n");
71 printf("#pragma omp critical \n\n");
72
73 int n=100000;
74 int ver = 0;
75 tm.start();
76#pragma omp parallel for
77 for (int i=1;i<=n;i++)
78 {
79 //for (j=0;j<1000000;j+=2){;}
80#pragma omp critical
81 ver+=i;
82 //printf("Thread %d is active when i = %d\n",omp_get_thread_num(),i);
83 }
84 printf("ver = %d\n",ver);
85 tm.stop();
86 printf("%f\n",tm.elapsedSeconds());
87
88// 6.reduction(<op>:<variable>)
89 printf("------------------华丽无比的分隔线--------------------\n");
90 printf("#pragma omp reduction \n\n");
91
92 int ver2 = 0;
93 tm.start();
94#pragma omp parallel for reduction(+:ver2)
95 for(int i=0;i<=n;i++)
96 {
97 //for (j=0;j<1000000;j+=2){;}
98 ver2+=i;
99 }
100
101 printf("ver2 = %d\n",ver2);
102 tm.stop();
103 printf("%f\n",tm.elapsedSeconds());
104
105// 7.#pragma omp for
106 printf("------------------华丽无比的分隔线--------------------\n");
107 printf("#pragma omp for \n\n");
108//将内层for循环并行化
109#pragma omp parallel
110 for (int i=0;i<2;i++)
111 {
112#pragma omp for
113 for (int j=0;j<2;j++)
114 {
115 printf("Thread %d is active when i=%d,and j=%d\n",omp_get_thread_num(),i,j);
116 }
117 }
118// 8.threadprivate and copyin
119 printf("------------------华丽无比的分隔线--------------------\n");
120 printf("#pragma omp threadprivate and copyin \n\n");
121
122 int global=10;
123//#pragma omp threadprivate(global)
124//#pragma omp parallel
125 for (int i=0;i<100;i++)
126 {
127 global++;
128 }
129
130 printf("Global is equal to %d\n",global);
131
132
133// 9.功能并行#prgama omp parallel sections
134 printf("------------------华丽无比的分隔线--------------------\n");
135 printf("#pragma omp parallel sections \n\n");
136
137#pragma omp parallel sections
138 {
139#pragma omp section
140 printf("Section 1 's thread is NO.%d\n",omp_get_thread_num());
141#pragma omp section
142 printf("Section 2 's thread is NO.%d\n",omp_get_thread_num());
143#pragma omp section
144 printf("Section 3 's thread is NO.%d\n",omp_get_thread_num());
145 }
146
147// 10.同步编译指导语句
148// 1).同步屏障
149#pragma omp parallel
150 {
151 int initialization=0;
152#pragma omp barrier
153 int process=1;
154 }
155
156// 2.互斥锁机制(库函数)
157#ifdef LOCKLOCK
158#define LOCKLOCK
159 omp_lock_t lock;
160//普通锁
161 omp_init_lock(&lock);
162 omp_destroy_lock(&lock);
163 omp_set_lock(&lock);
164 omp_unset_lock(&lock);
165 omp_test_lock(&lock);
166//嵌套锁
167 omp_init_nest_lock(&lock);
168 omp_destroy_nest_lock(&lock);
169 omp_set_nest_lock(&lock);
170 omp_unset_nest_lock(&lock);
171 omp_test_nest_lock(&lock);
172#endif
173//其他的OpenMP编译指导语句请参考OpenMP手册;
174
175
176
177 return 0;
178}
2#include <tchar.h>
3#include <omp.h>
4int _tmain(int argc, _TCHAR* argv[])
5{
6 Timer tm;
7// omp_set_num_threads(2),设定活动线程数为2
8 omp_set_num_threads(3);
9// omp_get_num_procs()
10 int procs=omp_get_num_procs();
11 printf("There are %d processor(s) available!\n",procs);
12// omp_get_thread_num(),返回当前线程号;
13 int Threadid=omp_get_thread_num();
14 printf("当前线程号为:%d\n",Threadid);
15// omp_get_num_threads(),返回当前线程总数;
16 int Threadnum = omp_get_num_threads();
17 printf("当前活动线程数为:%d\n",Threadnum);
18
19// 并行化编译指导语句
20 printf("下面开始测试并行化编译指导语句!!\n\n");
21// 1.parallel
22 printf("------------------华丽无比的分隔线--------------------\n");
23 printf("#pragma omp parallel\n\n");
24
25#pragma omp parallel
26
27 printf("This is Thread %d say hello to you!\n",omp_get_thread_num());
28
29// 2.parallel for
30 printf("------------------华丽无比的分隔线--------------------\n");
31 printf("#pragma omp parallel for\n\n");
32
33#pragma omp parallel for
34 for (int i=0;i<4;i++)
35 {
36 printf("Thread %d is active when i = %d\n",omp_get_thread_num(),i);
37 }
38 int i,j=0;
39// 3.parallel for private
40 printf("------------------华丽无比的分隔线--------------------\n");
41 printf("#pragma omp parallel for private(j)\n\n");
42
43 printf("使用private子句\n");
44#pragma omp parallel for private(j)
45 for (i=0;i<2;i++)
46 for(j=5;j<7;j++)
47 printf("Thread %d is active when i = %d,and j= %d\n",omp_get_thread_num(),i,j);
48 printf("不使用private子句\n");
49#pragma omp parallel for
50 for (i=0;i<2;i++)
51//#pragma omp parallel for
52 for(j=5;j<7;j++)
53 printf("Thread %d is active when i = %d,and j= %d\n",omp_get_thread_num(),i,j);
54// 4.parallel for firstprivate() and lastprivate()
55 printf("------------------华丽无比的分隔线--------------------\n");
56 printf("#pragma omp parallel for firstprivate and lastprivate\n\n");
57
58 int ai=10;
59 int bi=0;
60 printf("Before \"parallel for \", ai = 10 ,and bi = 0 !\n\n");
61#pragma omp parallel for firstprivate(ai) lastprivate(bi)
62 for (int i=1;i<4;i++)
63 {
64 bi=ai;
65 ai=0;
66 }
67 printf("After \"parallel for \" bi equal to %d !\n\n",bi);
68 printf("After \"parallel for \" ai still equal to %d !\n\n",ai);
69// 5.critical 临界区
70 printf("------------------华丽无比的分隔线--------------------\n");
71 printf("#pragma omp critical \n\n");
72
73 int n=100000;
74 int ver = 0;
75 tm.start();
76#pragma omp parallel for
77 for (int i=1;i<=n;i++)
78 {
79 //for (j=0;j<1000000;j+=2){;}
80#pragma omp critical
81 ver+=i;
82 //printf("Thread %d is active when i = %d\n",omp_get_thread_num(),i);
83 }
84 printf("ver = %d\n",ver);
85 tm.stop();
86 printf("%f\n",tm.elapsedSeconds());
87
88// 6.reduction(<op>:<variable>)
89 printf("------------------华丽无比的分隔线--------------------\n");
90 printf("#pragma omp reduction \n\n");
91
92 int ver2 = 0;
93 tm.start();
94#pragma omp parallel for reduction(+:ver2)
95 for(int i=0;i<=n;i++)
96 {
97 //for (j=0;j<1000000;j+=2){;}
98 ver2+=i;
99 }
100
101 printf("ver2 = %d\n",ver2);
102 tm.stop();
103 printf("%f\n",tm.elapsedSeconds());
104
105// 7.#pragma omp for
106 printf("------------------华丽无比的分隔线--------------------\n");
107 printf("#pragma omp for \n\n");
108//将内层for循环并行化
109#pragma omp parallel
110 for (int i=0;i<2;i++)
111 {
112#pragma omp for
113 for (int j=0;j<2;j++)
114 {
115 printf("Thread %d is active when i=%d,and j=%d\n",omp_get_thread_num(),i,j);
116 }
117 }
118// 8.threadprivate and copyin
119 printf("------------------华丽无比的分隔线--------------------\n");
120 printf("#pragma omp threadprivate and copyin \n\n");
121
122 int global=10;
123//#pragma omp threadprivate(global)
124//#pragma omp parallel
125 for (int i=0;i<100;i++)
126 {
127 global++;
128 }
129
130 printf("Global is equal to %d\n",global);
131
132
133// 9.功能并行#prgama omp parallel sections
134 printf("------------------华丽无比的分隔线--------------------\n");
135 printf("#pragma omp parallel sections \n\n");
136
137#pragma omp parallel sections
138 {
139#pragma omp section
140 printf("Section 1 's thread is NO.%d\n",omp_get_thread_num());
141#pragma omp section
142 printf("Section 2 's thread is NO.%d\n",omp_get_thread_num());
143#pragma omp section
144 printf("Section 3 's thread is NO.%d\n",omp_get_thread_num());
145 }
146
147// 10.同步编译指导语句
148// 1).同步屏障
149#pragma omp parallel
150 {
151 int initialization=0;
152#pragma omp barrier
153 int process=1;
154 }
155
156// 2.互斥锁机制(库函数)
157#ifdef LOCKLOCK
158#define LOCKLOCK
159 omp_lock_t lock;
160//普通锁
161 omp_init_lock(&lock);
162 omp_destroy_lock(&lock);
163 omp_set_lock(&lock);
164 omp_unset_lock(&lock);
165 omp_test_lock(&lock);
166//嵌套锁
167 omp_init_nest_lock(&lock);
168 omp_destroy_nest_lock(&lock);
169 omp_set_nest_lock(&lock);
170 omp_unset_nest_lock(&lock);
171 omp_test_nest_lock(&lock);
172#endif
173//其他的OpenMP编译指导语句请参考OpenMP手册;
174
175
176
177 return 0;
178}