10

In C++17, a number of functions in the algorithm header now can take an execution policy. I can for example define and call a function like this:

template <class ExecutionPolicy>
void f1(const std::vector<std::string>& vec, const std::string& elem, ExecutionPolicy&& policy) {
    const auto it = std::find(
        std::forward<ExecutionPolicy>(policy),
        vec.cbegin(), vec.cend(), elem
    );
}

std::vector<std::string> vec;
f1(vec, "test", std::execution::seq);

However I haven't found a good way to use different policies at runtime. For example when I want to use a different policy depending on some input file.

I toyed around with variants, but in the end the problem was always the different types of std::execution::seq, std::execution::par and std::execution::par_unseq.

A working but cumbersome solution would look like this:

void f2(const std::vector<std::string>& vec, const std::string& elem, const int policy) {
    const auto it = [&]() {
        if (policy == 0) {
            return std::find(
                std::execution::seq,
                vec.cbegin(), vec.cend(), elem
            );
        }
        else if (policy == 1) {
            return std::find(
                std::execution::par,
                vec.cbegin(), vec.cend(), elem
            );
        }
        else{
            return std::find(
                std::execution::par_unseq,
                vec.cbegin(), vec.cend(), elem
            );
        }
    };
}

f2(vec, "test", 0);

Is there any more elegant solution I'm overlooking?

edit: maybe I should be more precise. Let's say the goal is to save the policy in a variable that can have either of the three policies. That variable should be a parameter to the function.

3
  • This depends entirely on where the choice comes from. Without constraining that policy variable to being an int, for example, you can just literally pass an ExecutionPolicy into f2. But presumably you have some constraint, and we don't know what that is. Oct 24, 2018 at 17:52
  • 1
    But the thing is, that the three policies do not have a common base class as far as I understood things. That's precisely what makes this so tricky
    – Basti
    Oct 24, 2018 at 17:56
  • Oh, right. Well, meh. The best solution still depends on where that "choice" will come from. You might be able to use like a constexpr vector or something to map integers to types. Though that still doesn't completely help you. Not sure; good luck! Oct 24, 2018 at 18:05

2 Answers 2

11

The standard approach here is to separate the selection of a type from the use of the type: the latter takes the form of a function template instantiated several times by the former non-template function (or function template with fewer template parameters).

To avoid duplicating the normal parameters between these two layers, use a generic lambda as the template. To avoid duplicating the selection logic, make a function template that calls whatever lambda with the appropriate policy:

enum Policy {seq,par,par_unseq};

template<class F>
auto maybe_parallel(F f,Policy p) {
  switch(p) {
  case seq: return f(std::execution::seq);
  case par: return f(std::execution::par);
  default: return f(std::execution::par_unseq);
  }
}

auto f2(const std::vector<std::string>& vec,
        const std::string& elem,Policy p) {
  return maybe_parallel
    ([&](auto &pol) {return std::find(pol,vec.begin(),vec.end(),elem);},p);
}
1
  • Hey wow that seems clever. I'm gonna try it later. Thanks a lot!
    – Basti
    Oct 26, 2018 at 7:23
1

An alternative approach is to use std::variant and std::visit for this purpose.

using parallel_policy = std::variant
                        <
                         std::execution::sequenced_policy,
                         std::execution::parallel_policy,
                         std::execution::parallel_unsequenced_policy,
                         std::execution::unsequenced_policy
                        >;

void f2(const std::vector<std::string>& vec,
        const std::string&              elem,
        const parallel_policy           policy
       )
{
    std::visit
    (
        [&](auto policy_real)
        {
            f1(vec, elem, policy_real);
        },
        policy
    );
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.