简体   繁体   English

C ++任务链

[英]C++ chain of tasks

I have to handle the following scenerio: I've 5 tasks("A","B","C","D","E"), I'd like to parallelize them but in respect to their dependiences. 我必须处理以下场景:我有5个任务(“A”,“B”,“C”,“D”,“E”),我想将它们并行化,但是就它们的依赖性而言。 They have to be executed in such an order: 他们必须按照这样的顺序执行:

A --> B --\
C ----------> E
D --------/

So "E" is executed when all the previous ones are finished and "B" has to be executed after A. And here is my question. 因此,当所有先前的完成并且必须在A之后执行“B”时执行“E”。这是我的问题。 Are there any ready to go solutions (STL, Boost)? 有没有准备好的解决方案(STL,Boost)? Or I will have to implement it basing on std::thread? 或者我将基于std :: thread实现它?

check out TBB's Flow Graph or the PPL . 查看TBB的Flow GraphPPL

The example in the TBB link shows approximately what you have sketched. TBB链接中的示例大致显示了您所绘制的内容。 You already formulated your problem abstracted as tasks. 您已经将您的问题抽象为任务。 No need to get down to the thread level at first. 首先无需深入到线程级别。

I think you can do this with the OpenMP section directive which you can do with ICC, GCC, and MSVC. 我认为您可以使用OpenMP section指令执行此操作,您可以使用ICC,GCC和MSVC执行此操作。 The OpenMP task directive is probably a better choice and can be done with ICC and GCC but not with any version of MSVC. OpenMP task指令可能是更好的选择,可以使用ICC和GCC,但不能使用任何版本的MSVC。

The code below uses OpenMP sections . 下面的代码使用OpenMP sections Since E is run after all other tasks are finished it's possible to parallelize it as well so in the following code E is run by all threads after A, B, C, D have finished. 由于E在所有其他任务完成后运行,因此可以将其并行化,因此在下面的代码中,在A, B, C, D完成后A, B, C, D所有线程都运行E If E is iterating over a loop then you can parallelize the loop this way. 如果E迭代循环,那么您可以通过这种方式并行化循环。 I'm not sure that's what you want but it's easy to make it run in one thread as if you wanted. 我不确定这是你想要的,但很容易让它在一个线程中运行,就像你想要的那样。

#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();
}   

You can use std::thread which I never used but seems pretty straightforward. 您可以使用我从未使用的std :: thread,但看起来非常简单。

Here's an example of a simple program found on cppreference.com : 以下是cppreference.com上的简单程序示例:

#include <iostream>
#include <thread>
#include <chrono>

void foo()
{
    // simulate expensive operation
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

void bar()
{
    // simulate expensive operation
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main()
{
    std::cout << "starting first helper...\n";
    std::thread helper1(foo);

    std::cout << "starting second helper...\n";
    std::thread helper2(bar);

    std::cout << "waiting for helpers to finish...\n";
    helper1.join();
    helper2.join();

    std::cout << "done!\n";
}

You can use the join() function to wait for a thread to finish. 您可以使用join()函数等待线程完成。 For example you create a new thread that will execute the task A. Then you wait for it to finish before creating a new thread that will execute task B. 例如,您创建一个将执行任务A的新线程。然后在创建将执行任务B的新线程之前等待它完成。

You should compile with g++ -std=c++0x -pthread main.cpp 你应该使用g ++ -std = c ++ 0x -pthread main.cpp进行编译

Alternatively you can search for MPI and OpenMP that can provide some possibilities std::thread can't. 或者你可以搜索MPI和OpenMP,它可以提供std :: thread所不具备的一些可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM