为函数创建OpenMP线程

creating openmp threads for functions

本文关键字:线程 OpenMP 创建 函数      更新时间:2023-10-16

我看到的所有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