Title: OpenMP
1OpenMP
2???????? ????? ??????? ???????????
????????
?????
3????? ? ?????????????? ??????
var
??????????????
?????
var
var
4??????????? OpenMP
??????????
????????????
OpenMP ??????????
?????????? ?????
OpenMP ??????????
????? ??
5?????? ?????????? OpenMP ??????????
6?????? ? ?????????????? ????????????? ?????
??????
- ??????-???? ????? ????? 0
- ????? ??????, ??????????? ?????? ????????????
- - ?????????? ????????? OMP_NUM_THREADS
- - ????? ??????? omp_set_num_threads() (?????
?????????? ????? ???????????? ????????, ?? ??
?????? ????? ???????) - ??????????? ????? ??????????? ? ???????
omp_get_num_procs()
7?????? ? ?????????????? ?????????????
???????????? ??????????? ????? ??????
? ????????? ??????? ????????????? ?????????????
????? ?????? ??????????? ? ??????????? ??
???????? ????????? ???????????. ???????? ??????
????? ????? ? ??????? ?????????? ?????
OMP_DYNAMIC TRUE, FALSE ??? ? ??????? ???????
omp_set_dynamic(int flag) (????? ??????????
????? ???????????? ????????, ?? ?? ?????? ?????
???????) ???? flag ! 0 , ?? ???????? ??????????,
? ????????? ?????? ???????????.
8??????????? ????? ???????????, ?????? ? ?????
????????? ? ???????
int omp_get_num_procs() ?????????? ??????????
??????????? ? ??????? int omp_get_num_threads()
?????????? ?????????? ??????, ???????????
???????????? ??????? (???????? ?????? ??
???????????????? ????????) int
omp_get_thread_num() ?????????? ????? ???????????
?????.
9include ltstdio.hgtinclude ltstdlib.hgtinclude
ltomp.hgt main(int argc, char argv)
omp_set_num_threads(atoi(argv1))
printf("Total number of processors is d\n",
omp_get_num_procs()) pragma omp
parallel printf("Hello, World from thread d
of d\n",
omp_get_thread_num(), omp_get_num_threads())
10????? ????????? ???????? OpenMP
pragma omp directive_name clauseclause ...
newline ????????, ??????????????? ?????????
??????????? ??????????????? ? ????????????
?????, ?????????????? ?? ??????????. ???????????
?????? ????? ???? ????? ????????, ???????
???????????? ???? ? ???????????? ?????. ????
????????? ??????????? ?? ???????? ??????
?????????, ?? ??? ??????????? ?? ????? ?????.
11?????????? ? OpenMP-?????????
extern int A void f(int c) static double z int x main() double y pragma omp parallel int a f(5) ?? ????????? ? ????????????? ??????? ????????? ?????????? ???????? ?????? A, z, y ????????? ?????????? a, c, x ???????? ???????????????.
12????? ??? ??????
????? private
??????, ??????? ? ???????, ?????????? ????
????????????? ??????????, ???????? ??????
(shared). ??????????, ??????????? ?????? ?????
?.?. ????????? ??????????????? (private). ?????
private ?????? ?????? ??????????????
??????????. ?????? shared-?????????? ?
?????????? ???????????? ????? ????? ????
??????????? ????? private
13????? firstprivate
????? firstprivate ???????? ??? ?? ??????????,
??? ? ????? private. ??? ????, ??? ?????
?????????? ???????????????? ????????? ????????
?????????? ?? ????? ? ???? ?? ??????-?????.
?
14????? default
????? default ?????? ????? ??-?????????
??? ??????????. ?????? pragma omp parallel
default(private)
????? shared
????? shared ?????? ?????? ????? ??????????.
pragma omp parallel default(private) shared(x)
15include ltomp.hgt main () int nthreads,
tid pragma omp parallel private(nthreads,
tid) tid omp_get_thread_num()
printf("Hello World from thread d\n", tid)
if (tid 0) nthreads
omp_get_num_threads() printf("Number of
threads d\n", nthreads)
16?????????? ????? ??????
???????? ????? private ???????? ?????? ???
??????????-??????? ?????? ? ????????
????????????? ??????? static int a f()
printf(d\n, a) main() omp parallel
private (a) a omp_num_thread() f()
???????? a ????????????
17????????? threadprivate
omp threadprivate (?????? ??????????
??????????) ?????????? ?????????? ?????? ??? ????
?????? static int a f() printf(d\n,
a) main() omp threadprivate(a) omp
parallel a omp_num_thread() f()
18????? copyin
????? copyin ????????? parallel ??????????
??????? ????????????? threadprivate-??????????
??? ?????????? ???????????????? ????????? ??
master-????? ? ?????? ????????????? ???????.
19?????????? ?????????????? ??????????
- ??? ????????????? ?????????? ???????????
- ???????????
- for
- sections
- single
20????????? for
pragma omp for clause ... clause
schedule (type ,chunk)
ordered private
(list) firstprivate (list)
lastprivate (list)
reduction (operator list)
nowait
21 ????????? ???????????? ????? for ?????????????
???? for(init-expr, var logical_op b,
incr_expr) init_expr var
expr logical_op gt, lt, gt, lt
22incr_expr var var var -- --
var var incr var - incr var incr
var var var incr var var incr var
?????????? ?????? ???? incr, lb, b
?????????? ????? ?????? ????
23include ltomp.hgtinclude ltstdio.hgtinclude
ltstdlib.hgtinclude lttime.hgt main(int argc, char
argv) int n, iters, t, i, j double a,
b, alpha 0.1 n atoi(argv1) iters
atoi(argv2) a (double)malloc(n
sizeof(double)) b (double)malloc(n
sizeof(double)) t time(NULL) for(i 0 i
lt iters i ) pragma omp parallel for
private(j), firstprivate(n) for(j 0 j lt n
j ) aj aj alpha bj
t time(NULL) - t printf("parallel loop
d seconds\n", t)
???????? (? ??????????) ???????? ????????????
???????.
24?????????? ????????????
????????? 2 x 64-????????? ????????? Intel
Itanium-2 1.6 ???.
??????????? ????? ???????? 1 CPU 2 CPU
20000 200000 8 ??? 4 ???
25?????????????? ??????????? ? ?? ??????????
26????????? ??????????????
double func(double x) double rv int i
const double alpha 100 const int n 100
rv 0. for(i 0 i lt n i ) rv
cos(i x) sin(i x) log(i x 1)
return rv
main() double A, B, v int N A 0.
B 100. N 10000000 v integr(A, B, N,
func) printf("lf\n", v)
27????????? ??????????????
double integr(double a, double b, int n, double
(g)(double)) int i double s, h s
0. h (b - a) / n pragma omp parallel for
for(i 0 i lt n i ) s g(a i h
0.5 h) return s h
28????????? ??????????????
?????????? ?????? 2 x Intel Xeon 53xx
(Clovertown) 8 core ????????????????
??????? 41 ?. v
71747.34 ???????????? ??????? 5 ?.
v 56234.44 ?? ????????? !
29?????????????? ???????????
??????????? ?? ?????? 1 a 12 b a
1
2
??????????? ?? ?????????? 1 if(a) 2 x c
d3 y 14
1
2
3
30?????????????? ??????????? ? ?????
for(j 0 j lt n j ) aj aj 1
aj 2
31????? reduction
????? reduction ?????????? ??? ?? ?????? ??
????????????? ????? ?????????? ???????
??????????????? ????????. ?????? pragma omp
for reduction( x) ????????? ?????????
???????? , , -, , , , ,
32????? reduction
????? reduction ?????????? ???????? ??????????,
???????? ? ?????? ?? ??????????, ?? ???????
?????? ????? ?????????? ????????????? ??????? ???
????????? ?????????? ??????????? ????????. ??
?????? ?? ???????, ??????????? ????????????
???????, ?????????? ???????????????? ?????????,
??????????????? ??????????? ????????. ?????
???????????? ??????? ?????????? n ????????, ? ??
???? ?????????? ????? ???????? v. ???? ? ?????
?????????? ????????????? ??????? ????????? ?????
?????????? a ????? ???????? v1, , vn, ?? ?????
????????????? ??????? ?????????? a ?? ???????
?????? ??????? ????????, ?????? (v x v1 x v2 x
x vn).
33????? reduction
???????? ???????? ??? ?????????????
0
1
- 0
0
0
0
1
0
34????????? ??????????????(?????????? ???????)
double integr(double a, double b, int n, double
(g)(double)) int i double s, h s
0. h (b - a) / n pragma omp parallel for
reduction(s) for(i 0 i lt n i ) s
g(a i h 0.5 h) return s h
35????????? ??????????????
?????????? ?????? 2 x Intel Xeon 53xx
(Clovertown) 8 core ???????????????? ???????
41 ?. v 71747.34 ??????????
?? ??????? 5 ?. v
71747.34 ????????? !
36????? shedule ????????? for
????? shedule ????????? ?????????
????????? static - ????????????? ??????????????
?????????? dynamic - ?????????????
?????????????? ??????????? (????, ???????????
??????????, ???????? ????? ??????
????????) guided - ?????????? dynamic, ?? ??
?????? ????????? ???????? ?????? ???????????????
????? ???????? ????? ???????? ?????? ?????
?????????? ????????, ????????? ?? ?????
??????????? ??????, ???? ??? ????? ??????
????????? ???????? chunk, ??? ???????? chunk ?
????????? ?????? (??????? ?????? ??????
?????????????) runtime - ?????????????
?????????????? ?? ????? ?????????? ????????
????????? ??????? ?????????? (???????? chunk ??
????????) ?? ?????? ?????????? ?????
37??????????? ????? schedule ????????? for
- ???????? chunk ????????????????? ?????? ??????
? ?????? static, dynamic, guided - ?? ????????? chunk ????????? ?????? 1
- ????????????????? ? ??????? ????? runtime
?????????????? ????????? ???????? ??????????
OMP_SCHEDULE ??????. setenv OMP_SCHEDULE
guided,4
38include ltstdio.hgtinclude ltstdlib.hgtinclude
lttime.hgtmain(int argc, char argv) int n,
iters, t, i, j double a, b, c, alpha
0.1 n atoi(argv1) iters
atoi(argv2) a (double)malloc(n
sizeof(double)) b (double)malloc(n
sizeof(double)) t time(NULL) for(i 0 i
lt iters i ) for(j 0 j lt n j )
aj aj alpha bj t
time(NULL) - t printf("sequential loop d
seconds\n", t)
???????? (? ??????????) ????????
???????????????? ???????.
39include ltomp.hgtinclude ltstdio.hgtinclude
ltstdlib.hgtinclude lttime.hgt main(int argc, char
argv) int n, iters, t, i, j double a,
b, alpha 0.1 n atoi(argv1) iters
atoi(argv2) a (double)malloc(n
sizeof(double)) b (double)malloc(n
sizeof(double)) t time(NULL) for(i 0 i
lt iters i ) pragma omp parallel for
private(j), firstprivate(n) for(j 0 j lt n
j ) aj aj alpha bj
t time(NULL) - t printf("parallel loop
d seconds\n", t)
???????? (? ??????????) ???????? ????????????
???????.
40????????? sections
pragma omp sections clause ... structured_block
clause private (list)
firstprivate (list)
lastprivate (list)
reduction (operator list)
nowait pragma omp
section structured_block pragma omp
section structured_block
41????? lastprivate
????? lastprivate ???????? ??? ?? ??????????, ???
? ????? private. ??? ????, ???????? ??????????
????? ?????????? ????? ????????????? ??????????
???????????? ??? ?? ???????? ?? ?????????
???????? ????? ??? ? ????????? ?????? ???
work-sharing ??????????? (? ????? ??????
????????????????? ??????????).
?
42????????? single
pragma omp single clause ...
structured_block ????????? single ??????????
??? ??????????? ???? ????? ??????????? ??????
????? ??????
43????????? ?????????????
- master
- critical
- barrier
- atomic
- flush
- ordered
44pragma omp master ?????????? ?????? ????,
???????????? ?????? master-?????? pragma omp
critical (name) ?????????? ?????? ????,
???????????? ?????? ????? ?????? ? ?????? ??????
??????? pragma omp barrier ?????????? ??????
????, ???????????? ?????? ????? ?????? ? ??????
?????? ???????
45pragma omp atomic ltexpr-stmtgt ltexpr-stmtgt
x binop expr x x x -- -- x
46paragma omp flush var-list ltexpr-stmtgt
x binop expr x x x -- --
x ????????? ???????? ??????? flush barrier,
???? ? ????? ?? critical, ordered, ????? ??
parallel, for, sections, single