<i id='TWRFa'><tr id='TWRFa'><dt id='TWRFa'><q id='TWRFa'><span id='TWRFa'><b id='TWRFa'><form id='TWRFa'><ins id='TWRFa'></ins><ul id='TWRFa'></ul><sub id='TWRFa'></sub></form><legend id='TWRFa'></legend><bdo id='TWRFa'><pre id='TWRFa'><center id='TWRFa'></center></pre></bdo></b><th id='TWRFa'></th></span></q></dt></tr></i><div id='TWRFa'><tfoot id='TWRFa'></tfoot><dl id='TWRFa'><fieldset id='TWRFa'></fieldset></dl></div>

      <legend id='TWRFa'><style id='TWRFa'><dir id='TWRFa'><q id='TWRFa'></q></dir></style></legend>
    1. <tfoot id='TWRFa'></tfoot>
        <bdo id='TWRFa'></bdo><ul id='TWRFa'></ul>

    2. <small id='TWRFa'></small><noframes id='TWRFa'>

      1. 运行时不同的执行策略

        时间:2023-09-26
        • <small id='JFINc'></small><noframes id='JFINc'>

              <tfoot id='JFINc'></tfoot>
            • <i id='JFINc'><tr id='JFINc'><dt id='JFINc'><q id='JFINc'><span id='JFINc'><b id='JFINc'><form id='JFINc'><ins id='JFINc'></ins><ul id='JFINc'></ul><sub id='JFINc'></sub></form><legend id='JFINc'></legend><bdo id='JFINc'><pre id='JFINc'><center id='JFINc'></center></pre></bdo></b><th id='JFINc'></th></span></q></dt></tr></i><div id='JFINc'><tfoot id='JFINc'></tfoot><dl id='JFINc'><fieldset id='JFINc'></fieldset></dl></div>

                <tbody id='JFINc'></tbody>

              1. <legend id='JFINc'><style id='JFINc'><dir id='JFINc'><q id='JFINc'></q></dir></style></legend>

                  <bdo id='JFINc'></bdo><ul id='JFINc'></ul>
                  本文介绍了运行时不同的执行策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 C++17 中,algorithm 标头中的许多函数现在可以采用执行策略.例如,我可以定义和调用这样的函数:

                  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.

                  我尝试了各种变体,但最终问题总是出在不同类型的 std::execution::seqstd::execution::parstd::execution::par_unseq.

                  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.

                  推荐答案

                  这里的标准方法是将类型的选择与类型的使用分开:后者采用由前者的非模板函数(或模板参数较少的函数模板)多次实例化的函数模板的形式.

                  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).

                  为了避免在这两层之间复制普通参数,请使用通用 lambda 作为模板.为避免重复选择逻辑,请制作一个函数模板,使用适当的策略调用任何 lambda:

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

                  这篇关于运行时不同的执行策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何编辑和重新构建 GCC libstdc++ C++ 标准库源代码? 下一篇:Openmpi mpmd 获取通信大小

                  相关文章

                      <tfoot id='h9FRe'></tfoot><legend id='h9FRe'><style id='h9FRe'><dir id='h9FRe'><q id='h9FRe'></q></dir></style></legend>
                    1. <i id='h9FRe'><tr id='h9FRe'><dt id='h9FRe'><q id='h9FRe'><span id='h9FRe'><b id='h9FRe'><form id='h9FRe'><ins id='h9FRe'></ins><ul id='h9FRe'></ul><sub id='h9FRe'></sub></form><legend id='h9FRe'></legend><bdo id='h9FRe'><pre id='h9FRe'><center id='h9FRe'></center></pre></bdo></b><th id='h9FRe'></th></span></q></dt></tr></i><div id='h9FRe'><tfoot id='h9FRe'></tfoot><dl id='h9FRe'><fieldset id='h9FRe'></fieldset></dl></div>

                      <small id='h9FRe'></small><noframes id='h9FRe'>

                        <bdo id='h9FRe'></bdo><ul id='h9FRe'></ul>