为函数创建OpenMP线程
creating openmp threads for functions
我看到的所有OpenMP的教程示例都是为循环创建线程。但是我需要为普通语句组创建线程,这些线程可能会集中在函数中。例如,类似以下内容:
#include <stdio.h>
#include <omp.h>
int A() { printf("in A:%dn", omp_get_thread_num()); }
int B() { printf("in B:%dn", omp_get_thread_num()); }
int D() { printf("in D:%dn", omp_get_thread_num()); }
int E() { printf("in E:%dn", omp_get_thread_num()); }
int F() { printf("in F:%dn", omp_get_thread_num()); }
int G() { printf("in G:%dn", omp_get_thread_num()); }
int H() { printf("in H:%dn", omp_get_thread_num()); }
int C() {
printf("in C:%dn", omp_get_thread_num());
#pragma omp parallel num_threads(2)
{
D(); // want to execute D,E in separate threads
E();
}
F();
}
main() {
omp_set_nested(1);
printf("in main:%dn", omp_get_thread_num());
G();
#pragma omp parallel num_threads(3)
{
A(); // want to execute A,B,C in separate threads
B();
C();
}
H();
}
在上面的代码中,我希望每个函数都可以精确执行一次,但要在不同的线程中执行。(因此,我在上述代码中使用指令使用了错误,请根据需要进行更正。)
如何使用OpenMP编码这种函数的嵌套并行性?这些函数是否会共享所有可用的全局变量,还是有办法指定将通过哪些函数共享哪些变量?
编辑:阅读下面的豪尔赫·贝隆(Jorge Bellon)的答案后,我对以下进行了编码,并且在代码之后显示其输出。看起来TREED -0用于许多功能,这不是我的意图 - 我希望该功能并行执行。另外,我只想为G执行一个执行,因此看起来我必须删除" num_threads(3)"行。让我知道这个问题的解决方案是什么。
// compile this with: g++ -fopenmp
int A() { printf("in H:%dn", omp_get_thread_num()); sleep(1); }
// similarly for B, D, E, F, G, H
int C() {
printf("in C:%dn", omp_get_thread_num()); sleep(1);
#pragma omp task
D();
#pragma omp task
E();
#pragma omp taskwait
F(); sleep(1);
}
main() {
omp_set_nested(1);
printf("in main:%dn", omp_get_thread_num());
#pragma omp parallel num_threads(3)
G();
#pragma omp task
A();
#pragma omp task
B();
#pragma omp task
C();
#pragma omp taskwait
H();
}
// outputs:
in main:0
in G:1
in G:0
in G:2
in A:0
in B:0
in C:0
in D:0
in E:0
in F:0
in H:0
并行化这种代码的最佳方法是使用OpenMP task
构造。您的并行区域将创建一个线程池,主线程将创建外部任务,其余的线程将在这些任务可用后立即处理。
// [...]
int C() {
// You can create tasks within tasks
// In this example is better to place {D,E} and {E} in tasks
// and omit the task construct of C function call
#pragma omp task
{
D();
E();
}
// if F() needs D and E to finish, a taskwait is necessary
F();
}
main() {
// omp_set_nested no longer necessary
printf("in main:%dn", omp_get_thread_num());
G();
#pragma omp parallel num_threads(3)
#pragma omp single
{
// a single thread creates the tasks
// other threads in the team will be able to execute them
// want to execute A,B,C in separate threads
#pragma omp task
A();
#pragma omp task
B();
#pragma omp task
C();
// wait until all the tasks have been finished
#pragma omp taskwait
}
H();
}
是否在另一个线程中执行每个函数在运行时完全取决于程序的状态。这意味着如果所有其他线程都忙,则可能在同一线程中执行某些任务,这并不是一个特别的问题。
您可以使用任务依赖(从OpenMP 4开始)来控制是否允许任务在创建点执行。
使用C 11线程实现以下解决方案。仍将确定详细的OpenMP版本。
// compile this with: g++ -pthread -std=gnu++0x
#include <stdio.h>
#include <unistd.h> // for sleep
#include <thread>
#include <iostream>
#include <sstream>
using namespace std;
int A() { stringstream ss; ss << this_thread::get_id();
printf("in A:%sn", ss.str().c_str()); sleep(1); }
// similarly for B, D, E, F, G, H
int C() {
stringstream ss; ss << this_thread::get_id();
printf("in C:%sn", ss.str().c_str()); sleep(1);
std::thread thread_1(D);
std::thread thread_2(E);
thread_1.join();
thread_2.join();
F(); sleep(1);
}
main() {
printf("in mainn");
G();
std::thread thread_1(A);
std::thread thread_2(B);
std::thread thread_3(C);
thread_1.join();
thread_2.join();
thread_3.join();
H();
}
// outputs:
in main
in G:0x600000060
in A:0x60005aa10
in B:0x60005ab10
in C:0x60005ae40
in D:0x60005af40
in E:0x60005b040
in F:0x60005ae40
in H:0x600000060
相关文章:
- 在 openmp 中,omp_get_thread_num是否绑定到物理线程?
- 我使用 OpenMP 的线程越多,执行时间就越长,这是怎么回事?
- 并行块(线程清理器)之外的 OpenMP 中的争用条件;误报?
- 如何使用 OpenMP 使线程以相反的顺序写入其数字?
- 有人可以解释为什么使用 OpenMP 部分的运行速度比单个线程慢吗?
- 是否可以在并行区域中为共享 2D 数组创建选定元素的线程本地副本?(共享,私有,障碍:OPenMP)
- 如何让线程继续并启动其他功能 - C++使用 OpenMP?
- OpenMP:当我按线程 ID 访问共享变量时,我是否需要一个关键部分
- 多个 OpenMP 线程读取(而不是写入)共享变量的性能成本?
- 如何修复 OpenMP 程序的 gdb 运行中的线程数
- 充分私有化 OpenMP 线程中的数据
- openmp 锁由同一线程重新获取,而另一个线程正在等待它
- Openmp:所有线程由于一个线程中的延迟而停止
- OpenMP 线程创建
- OpenMP:共享同一算法的单线程和多线程实现
- C++ OpenMP 斐波那契:1 个线程的执行速度比 4 个线程快得多
- 在向量上声明缩减,在 1 个线程上运行给出的结果与没有 openmp 的结果不同
- for 循环内的多线程 - OpenMP
- 多线程 (openMP) - 多少个并行线程
- 顺序比多线程- OpenMp - c++更快