section制导
OpenMP中的section
子句是用于在sections
子句内部将代码划分成几个不同的段。当与#pragma omp parallel sections
结合使用时,这些代码段会并行处理。每个section
由其中的一个线程执行一次,不同的section
可以由不同的线程执行。当然,如果一个线程运行得足够快,它可能会执行多个section
。
#include <iostream>
#include <omp.h>
using namespace std;
int main(int argc, char* argv[]){
int a[5];
#pragma omp parallel sections
{
#pragma omp section
{
a[0] = omp_get_thread_num();
}
#pragma omp section
{
a[1] = omp_get_thread_num();
}
#pragma omp section
{
a[2] = omp_get_thread_num();
}
#pragma omp section
{
a[3] = omp_get_thread_num();
}
#pragma omp section
{
a[4] = omp_get_thread_num();
}
}
for (int i = 0; i < 5; ++i) {
cout << "section " << i << " -> " << "thread " << a[i] << endl;
}
return 0;
}
section
section 0 -> thread 0
section 1 -> thread 0
section 2 -> thread 1
section 3 -> thread 2
section 4 -> thread 3
section 6 thread 4
section 0 -> thread 0
section 1 -> thread 0
section 2 -> thread 1
section 3 -> thread 1
section 4 -> thread 2
section 5 -> thread 3
section 2 thread 4
section 0 -> thread 0
section 1 -> thread 1
手动阀手动阀
标签:num,thread,get,section,编译,omp,pragma,OpenMP,制导 From: https://www.cnblogs.com/tao-gak567/p/18065919