section
これは、ICC、GCC、および MSVC で実行できるOpenMP ディレクティブで実行できると思います。OpenMPtask
ディレクティブはおそらくより良い選択であり、ICC と GCC で実行できますが、MSVC のどのバージョンでも実行できません。
以下のコードは OpenMP を使用していますsections
。E は他のすべてのタスクが終了した後に実行されるため、それを並列化することも可能であるため、次のコードE
では終了後にすべてのスレッドによって実行されますA, B, C, D
。がループを反復している場合E
、この方法でループを並列化できます。それがあなたの望むものかどうかはわかりませんが、1 つのスレッドで実行するのは簡単です。
#include <stdio.h>
#include <omp.h>
void A() { printf("A\n"); }
void B() { printf("B\n"); }
void C() { printf("C\n"); }
void D() { printf("D\n"); }
void E() {
printf("E: %d\n", omp_get_thread_num());
#pragma omp for
for(int i=0; i<10; i++) {
//do something as a function of i
}
}
void foo() {
#pragma omp parallel // starts a new team
{
#pragma omp sections // divides the team into sections
{
{ A(); B(); }
#pragma omp section
{ C(); }
#pragma omp section
{ D(); }
}
E();
}
}
int main() {
foo();
}