OpenMP是一种支持多平台共享内存并行编程的API,它非常适合在多核处理器上并行执行循环和任务。通过在代码中插入指令(如#pragma omp parallel for),可以轻松地将顺序代码转换为并行代码。
1、#pragma omp parallel for 固定迭代次数
描述:在OpenMP并行for循环中,循环次数是固定的,循环结束时并行计算停止
# include <omp.h>
# include <stdio.h>
# define N 100
int main ( ) {
int i;
int a[ N] , b[ N] ;
for ( i = 0 ; i < N; i++ ) {
a[ i] = i;
}
# pragma omp parallel for
for ( i = 0 ; i < N; i++ ) {
b[ i] = a[ i] * a[ i] ;
}
for ( i = 0 ; i < 10 ; i++ ) {
printf ( "b[%d] = %d\n" , i, b[ i] ) ;
}
return 0 ;
}
编译方法:
gcc -fopenmp -o parallel_example parallel_example.c
./parallel_example
2、 #pragma omp parallel 并行区域
描述: #pragma omp parallel 用于创建一个并行区域,在这个区域内的代码将被多个线程并行执行。每个线程会执行并行区域内的所有代码
# include <omp.h>
# include <stdio.h>
int main ( ) {
# pragma omp parallel
{
int thread_id = omp_get_thread_num ( ) ;
printf ( "Hello from thread %d\n" , thread_id) ;
}
return 0 ;
}
3、#pragma omp sections 分段并行
描述: #pragma omp sections 用于并行化多个独立的代码段,每个代码段可以由不同的线程执行。
# include <omp.h>
# include <stdio.h>
int main ( ) {
# pragma omp parallel sections
{
# pragma omp section
{
printf ( "Section 1 executed by thread %d\n" , omp_get_thread_num ( ) ) ;
}
# pragma omp section
{
printf ( "Section 2 executed by thread %d\n" , omp_get_thread_num ( ) ) ;
}
# pragma omp section
{
printf ( "Section 3 executed by thread %d\n" , omp_get_thread_num ( ) ) ;
}
}
return 0 ;
}
4、#pragma omp single 单一执行
描述: #pragma omp single 指定的代码段只由一个线程执行,而不是由所有线程执行。其他线程将在这里等待,直到该代码段完成。
# include <omp.h>
# include <stdio.h>
int main ( ) {
# pragma omp parallel
{
# pragma omp single
{
printf ( "This is executed by thread %d\n" , omp_get_thread_num ( ) ) ;
}
}
return 0 ;
}
5、 #pragma omp master 主线程执行
描述: #pragma omp master 确保只有主线程(线程ID为0的线程)执行特定的代码块,其他线程不会执行该块,也不会等待。
# include <omp.h>
# include <stdio.h>
int main ( ) {
# pragma omp parallel
{
# pragma omp master
{
printf ( "This is executed by the master thread (thread 0)\n" ) ;
}
}
return 0 ;
}
6、 #pragma omp critical 临界区
描述: #pragma omp critical 用于定义一个临界区,在该区域内的代码一次只能被一个线程执行,以防止数据竞争。
# include <omp.h>
# include <stdio.h>
int main ( ) {
int sum = 0 ;
# pragma omp parallel
{
# pragma omp critical
{
sum += 1 ;
}
}
printf ( "Sum is %d\n" , sum) ;
return 0 ;
}
7、 #pragma omp barrier 屏障同步
描述: #pragma omp barrier 用于同步所有线程,确保在屏障之前的所有代码块都执行完毕后,所有线程才会继续执行。
# include <omp.h>
# include <stdio.h>
int main ( ) {
# pragma omp parallel
{
int thread_id = omp_get_thread_num ( ) ;
printf ( "Before barrier - thread %d\n" , thread_id) ;
# pragma omp barrier
printf ( "After barrier - thread %d\n" , thread_id) ;
}
return 0 ;
}
8、 #pragma omp task 任务并行
描述: #pragma omp task 创建独立的任务,这些任务可以由不同的线程执行,适用于动态并行计算。
# include <omp.h>
# include <stdio.h>
void task_function ( int n) {
printf ( "Task %d executed by thread %d\n" , n, omp_get_thread_num ( ) ) ;
}
int main ( ) {
# pragma omp parallel
{
# pragma omp single
{
for ( int i = 0 ; i < 5 ; i++ ) {
# pragma omp task
task_function ( i) ;
}
}
}
return 0 ;
}
9、#pragma omp atomic 原子操作
描述: #pragma omp atomic 保证对某个变量的特定操作(如加、减)是原子的,即不会被线程中断,防止数据竞争。
# include <omp.h>
# include <stdio.h>
int main ( ) {
int sum = 0 ;
# pragma omp parallel
{
# pragma omp atomic
sum += 1 ;
}
printf ( "Sum is %d\n" , sum) ;
return 0 ;
}