OpenMP
Designed for a shared memory system.
Every process can access to all avaiable memory.
1 |
|
编译器会把OpenMP代码编译为pthread
有一个Master thread, 创建一系列slaves
Most basic parallel directive:
1 |
Clause
#pragma omp parallel后面的东西
比如#pragma omp parallel num_threads(3)
防止race condition
1 | global_result = 0.0; |
Scope of variable:
可以被所有线程操作的变量:share scope(默认情况)
只能被单个线程才操作的变量:private scope
Reduction operators:
定义:binary operation,+, *, -, &, |, ˆ, &&, ||
reduction(\
可以被加入到clause里面
1 | global_result = 0.0; |
Parallel For
1 | h = (b-a)/n; |
Data dependencies
斐波那契数列:
OpenMp不会去检查iteration之间的数据依赖!