Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

parallel_pipeline

velvia
Beginner
530 Views

Hi,

I have some problems running a simple parallel_pipeline construct. The template errors is not really helpful to me. Can any of you spot what's wrong in my tbb::parallel_pipeline call?

#include <iostream>
#include <cstdint>
#include <tbb/tbb.h>
#include <tbb/mutex.h>

tbb::mutex mutex_console{};

class SubTask {
 private:
  int k_;
  char c_;

 public:
  SubTask(int k, char c) {
    k_ = k;
    c_ = c;
  }
  void operator()() const {
    {
      tbb::mutex::scoped_lock lock{mutex_console};
      std::cout << "Begin task: " << k_ << ", SubTask: " << c_ << std::endl;
    }
    int64_t sum{0};
    for (int64_t i = 0; i < 500000000; ++i) {
      if ((i % 2) == 0) {
        sum += i;
      } else {
        sum -= i;
      }
    }
    if (sum == 0) {
      std::cout << std::endl;
    }
  }
};

class Task {
 private:
  int k_;

 public:
  Task(int k) { k_ = k; }
  void operator()() const {
    tbb::parallel_invoke(
        [=] {
          SubTask task{k_, 'a'};
          task();
        },
        [=] {
          SubTask task{k_, 'b'};
          task();
        });
  }
};

void parallel_for() {
  tbb::task_scheduler_init init{1};
  int nb_task{4};

  std::vector<Task> task{};
  for (int i = 0; i < nb_task; i++) {
    task.emplace_back(i);
  }

  tbb::parallel_for(tbb::blocked_range<int>{0, nb_task},
                    [=](const tbb::blocked_range<int> &range) {
                      for (int i{range.begin()}; i < range.end(); ++i) {
                        task();
                      }
                    });
}

void producer_consumer() {
  int nb_token{1};
  int nb_task{4};

  tbb::parallel_pipeline(
      nb_token,
      tbb::make_filter<void, Task>(tbb::filter::serial,
                                   [&](tbb::flow_control &fc) -> Task {
                                     if (nb_task == 0) {
                                       fc.stop();
                                       return Task{0};
                                     } else {
                                       --nb_task;
                                       return Task{nb_task};
                                     }
                                   }) &
          tbb::make_filter<Task, void>(tbb::filter::parallel,
                                       [](Task task) { task(); }));
}

int main(int argc, const char *argv[]) {
  parallel_for();
  producer_consumer();

  return 0;
}

 

0 Kudos
1 Reply
Vladimir_P_1234567890
530 Views

Line 42: type 'Task' has a user-defined constructor or non-trivial default constructor

This constructor conflicts with one of internal pipeline structures.

--Vladimir

 

0 Kudos
Reply