Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any reason we cannot implement a parallel_reduce for a rvalue range? #1299

Closed
sukeya opened this issue Jan 20, 2024 · 2 comments
Closed
Labels

Comments

@sukeya
Copy link
Contributor

sukeya commented Jan 20, 2024

Hello,

I'm writing a program merging many std::sets. I would like to parallelize it with joining two sets by std::set::merge, but oneTBB doesn't seem to provide any move join function like T join(T&&, T&&) as far as I can see Reduction, parallel_reduce.h and enumerable_thread_specific.h.
Is there any reason we cannot implement it?

@dnmokhov
Copy link
Contributor

You should be able to use the lvalue-reference version of std::set::merge, e.g.,

struct Body {
  std::vector<std::set<int>> &sets;
  std::set<int> result;

  Body(std::vector<std::set<int>> &data) : sets(data) {}
  Body(Body &left, tbb::split) : sets(left.sets) {}

  void operator()(tbb::blocked_range<int> range) {
    for (int i = range.begin(); i < range.end(); ++i) {
      result.merge(sets[i]);
    }
  }

  void join(Body &right) { result.merge(right.result); }
};

In the example above, right is an lvalue reference to the subtask andright.result is an lvalue reference to its result.

Specifying a parallel_reduce body is further documented at https://oneapi-src.github.io/oneTBB/main/tbb_userguide/parallel_reduce.html

@sukeya
Copy link
Contributor Author

sukeya commented Jan 23, 2024

I didn't know that an Body can have the join member function which argument is a lvalue-reference without const.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants